<?php
  require("CommonAction.class.php");
  class friendshipAction extends CommonAction{
     
	 //首页
	 public function index()
	 {
	   $category = M("friendship");
	   $map["del"] = array("eq","0");
	   import("@.ORG.Page");       //导入分页类
		$count = $category->where($map)->count();    //计算总数
		$p = new Page ( $count, c("0.num") );
	   $num = $category->where($map)->count();
	   $this->assign("num",$num+1);
	   $data = $category->limit($p->firstRow.','.$p->listRows)->where($map)->order("id desc")->findall();
	    $p->setConfig('header','条链接');
		$page = $p->show ();            //分页的导航条的输出变量
		$this->assign( "page", $page);
		$this->assign("data",$data);
	   $category_type = M("friendship_type");
	   $categoryData = $category_type->where($map)->field("id,name,sort")->order("id desc")->findall();
	   $this->assign("categoryData",$categoryData);
	   $this->assign("table","friendship");
	   $this->display();
	 }
    //友情链接管理
	public function manage()
	{
	   $category = M("friendship_type");
	   $map["del"] = array("eq","0");
	   $categoryData = $category->where($map)->field("id,name,sort")->order("id desc")->findall();
	   $this->assign("categoryData",$categoryData);
	   $this->assign("table","friendship");
	   $this->display();
	}
	public function friendshipTypeAdd()
	{
	   $category = M("friendship_type");
	   $map["del"] = array("eq","0");
	   $num = $category->where($map)->count();
	   $this->assign("num",$num+1);
	   $this->display();
	}
	//添加友情链接
	public function add_friend()
	{
	  $friendship = D("admin.friendship");
	  if ($friendship->create()) {
            if (false !== $friendship->add()) {
			    $this->assign("jumpUrl",__URL__."/index");
                $this->success('类型添加成功！');
            } else {
			    $this->assign("jumpUrl",__URL__."/index");
                $this->error('类型添加错误');
            }
        } else {
            // 字段验证错误
            $this->error($friendship->getError());
        }
	}
	public function add()
	{
	   $category_type = M("friendship_type");
	   $map["del"] = array("eq","0");
	   $data = $category_type->where($map)->field("id,name")->select();
	   $this->assign("data",$data);
	   $category = M("friendship");
	   $map["del"] = array("eq","0");
	   $num = $category->where($map)->count();
	   $this->assign("num",$num+1);
	   $this->display();
	}
	public function  edit()
	{
	   $category = M("friendship");
	   $map["del"] = array("eq","0");
	   $map["id"] = array("eq",$_GET["id"]);
	   $num = $category->where($map)->findall();
	   $this->assign("num",$num);
	   $category_type = M("friendship_type");
	   $maps["del"] = array("eq","0");
	   $data = $category_type->where($maps)->field("id,name")->select();
	   $this->assign("data",$data);
	   $this->display();
	}
	public function friendshipTypeEdit()
	{
	   $category = M("friendship_type");
	   $map["del"] = array("eq","0");
	   $map["id"] = array("eq",$_GET["id"]);
	   $data = $category->where($map)->field("id,name,sort,show")->select();
	   $this->assign("data",$data);

	   $this->display();
	}
	
	//添加友情链接
	public function add_category()
	{
	   $category = D("admin.friendship_type");
	   if ($category->create()) {
            if (false !== $category->add()) {
			    $this->assign("jumpUrl",__URL__."/manage");
                $this->success('类型添加成功！');
            } else {
			    $this->assign("jumpUrl",__URL__."/manage");
                $this->error('类型添加错误');
            }
        } else {
            // 字段验证错误
            $this->error($category->getError());
        }  
	}
	//更新友情链接
	public function updateFriendshipType()
	{
	    $friendship = M("friendship_type");
		$map["id"] = array("eq",$_POST["id"]);
		if($_POST["show"] == "on")
		{
		   $_POST["show"] = 1;
		}else
		{
		  $_POST["show"] = 0;
		}
		$result = $friendship->where($map)->save($_POST);
	    if($result)
		{
		    $this->assign("jumpUrl",__URL__."/manage");
		   $this->success("链接更新成功");
		}else
		{
		   $this->assign("jumpUrl",__URL__."/manage");
		   $this->error("链接更新失败");
		}
	}
	
		//更新友情链接
	public function updateFriendship()
	{
	    $friendship = M("friendship");
		$map["id"] = array("eq",$_POST["id"]);
		if($_POST["show"] == "on")
		{
		   $_POST["show"] = 1;
		}else
		{
		  $_POST["show"] = 0;
		}
		$result = $friendship->where($map)->save($_POST);
	    if($result)
		{
		    $this->assign("jumpUrl",__URL__."/index");
		   $this->success("链接更新成功");
		}else
		{
		   $this->assign("jumpUrl",__URL__."/index");
		   $this->error("链接更新失败");
		}
	}
}
?>