<?php
/**
 * 简介模型管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class IntroController extends BaseController{
	public function _initialize(){
		parent::_initialize();
        $this->mod=M('Intro');
        $this->introall=$this->mod->order($order)->select();
    }
    public function index(){
		$pid=I('pid')?I('pid'):1;
		$map['device']=0;
		$data = I('param.');$order = '';
	        	if(!empty($data['_order'])&&!empty($data['_field'])){$order = $data['_field']." ".$data['_order'].',';}
	        	$order .= 'sort asc';
		$Intro=M('Intro')->where($map)->order($order)->select();
		$Tree = new \Common\Vendor\Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($Intro as &$values) {
		   $url=$values['dirname']?geturl('Home/Intro/'.$values['dirname']):geturl('Home/Intro/index',array('id'=>$values['id']));
			$values['str_manage'] = '<a href="'.U('add',array('pid'=>$values['id'])).'" class="operate">添加子类</a>&nbsp;&nbsp;<a class="operate" href="'.$url.'" target="_blank">访问</a>&nbsp;&nbsp;<a class="operate" href="'.U('edit',array('id'=>$values['id'])).'">编辑</a>&nbsp;&nbsp;';
			$values['str_manage'] .= '<a href="javascript:;" onclick="$.showConfirm(\''. L('SURE_DELETE').'\',function(){window.location.href=\''.U('delete',array('id'=>$values['id'])).'\'},\'hc\')">删除</a>';
			$values['status'] = '<img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['status'] . '.gif" />';
		}
		$Tree->init($Intro);
		$str = "<tr>
		<td><input value='\$id' name='id[]' type='checkbox' ></td>
	    <td>\$id</td>
		<td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
        <td>\$dirname</td>
        <td>\$status</td>
		<td>\$str_manage</td>
		</tr>";
		$cateString = $Tree->get_tree(0, $str);
        $this->assign('cateString', $cateString);
		$this->display();
	}
	public function mobileindex(){
		$pid=I('pid')?I('pid'):1;
		$map['device']=1;
		$data = I('param.');$order = '';
		if(!empty($data['_order'])&&!empty($data['_field'])){$order = $data['_field']." ".$data['_order'].',';}
		$order .= 'sort asc';
		$Intro=M('Intro')->where($map)->order($order)->select();
		$Tree = new \Common\Vendor\Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($Intro as &$values) {
		   $url=$values['dirname']?geturl('Home/Intro/'.$values['dirname']):geturl('Home/Intro/index',array('id'=>$values['id']));
			$values['str_manage'] = '<a href="'.U('add',array('pid'=>$values['id'],'device'=>1)).'" class="operate">添加子类</a>&nbsp;&nbsp;<a class="operate" href="'.$url.'" target="_blank">访问</a>&nbsp;&nbsp;<a class="operate" href="'.U('edit',array('id'=>$values['id'],'device'=>1)).'">编辑</a>&nbsp;&nbsp;';
			$values['str_manage'] .= '<a href="javascript:;" onclick="$.showConfirm(\''. L('SURE_DELETE').'\',function(){window.location.href=\''.U('delete',array('id'=>$values['id'])).'\'},\'hc\')">删除</a>';
			$values['status'] = '<img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['status'] . '.gif" />';
		}
		$Tree->init($Intro);
		$str = "<tr>
		<td><input value='\$id' name='id[]' type='checkbox' ></td>
	    <td>\$id</td>
		<td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
        <td>\$dirname</td>
        <td>\$status</td>
		<td>\$str_manage</td>
		</tr>";
		$cateString = $Tree->get_tree(0, $str);
        $this->assign('cateString', $cateString);
		$this->display();
	}
    public function _before_add(){
		$this->getNodeTree();
		if(I('device')){
			$this->assign('device', 1);
		}	
	}
	public function edit(){
		$id  = I('get.id');$info='';
		if(!$id)$this->error('参数错误!');
        $info=idtoname($id,'Intro','*','id',false);
		$this->getNodeTree($info['pid']);
		$this->assign($info);
		$this->assign('info', $info);
		$this->display();
	}
	public function _before_delete(){
		$id=I('id');
		if(isset($id)&&is_array($id)){
			foreach($id as $k=>$v){
				$result=check_delete($v,7,CONTROLLER_NAME);
				if(!$result['status'])unset($id[$k]);
			}
			if(empty($id)){$this->error($result['info']);exit;}
		}else{
			$result=check_delete($id,7,CONTROLLER_NAME);
			if(!$result['status']){$this->error($result['info']);exit;}
		}
		$this->id=$id;
	}
    /* 得到节点树 */
	private function getNodeTree($pid = '') {
	   $select_introall='';
       $Intro = $this->introall;			
       if(empty($pid))$pid = I('pid');
	   $Tree = new \Common\Vendor\Tree();
       $treemini=$Tree->treemini($Intro);
       $select_introall.= $Tree->treeminioption($treemini,$pid);
	   $this->assign('select_introall',$select_introall);
    }
}