<?php

// +----------------------------------------------------------------------

// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.

// +----------------------------------------------------------------------

// | Author: Tuolaji <479923197@qq.com>

// +----------------------------------------------------------------------

namespace Portal\Controller;

use Common\Controller\AdminbaseController;

class AdminTermController extends AdminbaseController {

	

	protected $terms_model;

	protected $taxonomys=array("article"=>"文章","picture"=>"图片");

	

	function _initialize() {
		parent::_initialize();
		$this->terms_model = D("Portal/Terms");
		$this->assign("taxonomys",$this->taxonomys);

	}

function index(){
		$map['webid'] = I('webid',0);
		$result = $this->terms_model->where($map)->order(array("listorder"=>"asc"))->select();
		$weblist = M('Website')->getField('id,host,title');
		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminTerm/add", array("parent" => $r['term_id'],'webid'=>$r['webid'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'],'webid'=>$r['webid'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$url=U('portal/list/index',array('id'=>$r['term_id']));
			$r['url'] = $url;
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id']=$r['term_id'];
			$r['webtitle']=$weblist[$r['webid']]['title']?$weblist[$r['webid']]['title']:'游戏主站';
			if($weblist[$r['webid']]['title'])
			$r['url'] = 'http://'.$weblist[$r['webid']]['host'].$url;
			$r['parentid']=$r['parent'];
			$array[] = $r;
		}
		
		$tree->init($array);
		$str = "<tr>
					<td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
					<td>\$id</td>
					<td>\$spacer <a href='\$url' target='_blank'>\$name</a></td>
	    			<td>\$taxonomys</td>
	    			<td>\$webtitle</td>
					<td align='center'><a href='\$url' target='_blank'>访问</a></td>
					<td>\$str_manage</td>
				</tr>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
		$this->assign("weblist", $weblist);
		$this->assign("webid", $map['webid']);
		$this->display();
	}
	
        private function _getTree($webid='0'){
		$term_id=empty($_REQUEST['term'])?0:intval($_REQUEST['term']);
		$result = $this->terms_model->where(array('webid'=>$webid))->order(array("listorder"=>"asc"))->select();
		$tree = new \Tree();

		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');

		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';

		foreach ($result as $r) {

			$r['str_manage'] = '<a href="' . U("AdminTerm/add", array("parent" => $r['term_id'],'webid'=>$r['webid'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'],'webid'=>$r['webid'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';

			$r['visit'] = "<a href='#'>访问</a>";

			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];

			$r['id']=$r['term_id'];

			$r['parentid']=$r['parent'];

			$r['selected']=$term_id==$r['term_id']?"selected":"";

			$array[] = $r;

		}	
		$tree->init($array);

		$str="<option value='\$id' \$selected>\$spacer\$name</option>";

		$taxonomysx = $tree->get_tree(0, $str);
		$this->assign("taxonomysx", $taxonomysx);
	}




	
	function add(){
	 	$parentid = intval(I("get.parent"));
	    $term_id = intval(I("get.term"));
	 	$tree = new \Tree();
	 	$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
	 	$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
	 	$terms = $this->terms_model->order(array("path"=>"asc"))->select();
	 	
	 	$new_terms=array();
	 	foreach ($terms as $r) {
	 		$r['id']=$r['term_id'];
	 		$r['parentid']=$r['parent'];
	 		$r['selected']= (!empty($parentid) && $r['term_id']==$parentid)? "selected":"";
	 		$new_terms[] = $r;
	 	}
	 	$tree->init($new_terms);
	 	$tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
	 	$tree=$tree->get_tree(0,$tree_tpl);
	 	$this->assign("taxonomysx",$tree);

	 	$this->assign("term_id",$parentid);
	 	$weblist = M('Website')->select();
	 	$this->assign("weblist",$weblist);
        $this->_getTree(I('webid'));      
        /*所属站点*/
		$web = M('website')->select();
		$this->assign("web",$web);
		$this->assign("webid",I('webid'));
            
         

	 	    $this->display();
	}
	
	function add_post(){
		if (IS_POST) {
			if ($this->terms_model->create()) {
				if ($this->terms_model->add()!==false) {
				    F('all_terms',null);
					$this->success("添加成功！",U("AdminTerm/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->terms_model->getError());
			}
		}
	}
	
	function edit(){
		$id = intval(I("get.id"));
		$data=$this->terms_model->where(array("term_id" => $id))->find();
		// $tree = new \Tree();
		// $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		// $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		// $terms = $this->terms_model->where(array("term_id" => array("NEQ",$id), "path"=>array("notlike","%-$id-%")))->order(array("path"=>"asc"))->select();
		
		// $new_terms=array();
		// foreach ($terms as $r) {
		// 	$r['id']=$r['term_id'];
		// 	$r['parentid']=$r['parent'];
		// 	$r['selected']=$data['parent']==$r['term_id']?"selected":"";
		// 	$new_terms[] = $r;
		// }	
		// $tree->init($new_terms);
		// $tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
		// $tree=$tree->get_tree(0,$tree_tpl);	
		// $this->assign("terms_tree",$tree);

                /*所属站点*/
			$web = M('website')->select();
			$this->assign("web",$web);
			$this->assign("webid",I('webid'));
            $this->_getTree(I('webid'));

		    $this->assign("data",$data);
		    $weblist = M('Website')->select();
	 	    $this->assign("weblist",$weblist);
		    $this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			if ($this->terms_model->create()) {
				if ($this->terms_model->save()!==false) {
				    F('all_terms',null);
					$this->success("修改成功！");
				} else {
					$this->error("修改失败！");
				}
			} else {
				$this->error($this->terms_model->getError());
			}
		}
	}
	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->terms_model);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	/**
	 *  删除
	 */
	public function delete() {
		$id = intval(I("get.id"));
		$count = $this->terms_model->where(array("parent" => $id))->count();
		$isposts = M('term_relationships')->where(array("term_id"=>$id))->find();
		if($isposts){
			$this->error("该分类下含有文章，无法删除");
		}
		if ($count > 0) {
			$this->error("该菜单下还有子类，无法删除！");
		}
		
		if ($this->terms_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

	

}