<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends oabase {
    public function action_run() {
                $id= $_SESSION['USER_AUTH_KEY'];
                $model = parent::model('contact', 'oa');
                $list=$model->get_contact_list($id);
                $tag_data = $model-> get_data_list($this->gb_c);
                $tag_list = $model-> get_tag_list($this->gb_c);
                $new_tag_list=array();
		$new = array();
		foreach ($tag_data as $val) {
			$new[$val['row_id']] = $new[$val['row_id']] . $val['tag_id'] . ",";
		}
                foreach ($tag_list as $val) {
			$new_tag_list[$val['id']]=$val['name'];
		}
                    foreach($list as $k=>$v){
                        $list[$k]['tag']=$new[$v['id']];
                    }
                $var_array = array(
                    'list'=>$list,
                    'tag_data'=>$new,
                    'tag_list'=>$new_tag_list
                );            
               unset($model);
               TPL::assign($var_array);
	       TPL::display($this->cptpl.'contact/index.tpl');
    }
    
    public function action_add(){
            $id=$_REQUEST['id'];
            $opmode="add";
            if($id){
                $opmode="edit";
                $model = parent::model('contact', 'oa');
                $vo=$model->get_data($id);
                
            }
             $var_array = array(
                    'opmode'=>$opmode,
                    'vo'=>$vo
                );            
            TPL::assign($var_array);          
            TPL::display($this->cptpl.'contact/add.tpl');
    }
    
    public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $model = parent::model('contact', 'oa');
            $arr=$this->SafeFilter($arr);
            if($arr['opmode']=='add'){
                $result = $model->doadd($arr);
            }else{
                $result = $model->doEdit($id, $arr);
            }
            if (true === $result) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
        }
        public function action_del(){
                $ids=$_POST['id'];
                if(empty($ids)){
                     $data['info']='操作失败';
                     echo json_encode($data);
                die;
                }
                $model = parent::model('contact', 'oa');
                foreach($ids as $v){
                   $res=$model->del($v);
                }
	
                if($res){
                     $data['status']=1;
                     $data['info']='删除成功';
                } else {
                     $data['info']='操作失败';
                }
                 echo json_encode($data);
                die;
	}
        public function action_set_tag() {
		$id = $_POST['id'];
		$tag = $_POST['tag'];
		$new_tag = $_POST['new_tag'];
                $model = parent::model('contact', 'oa');
		if (!empty($id)) {
			$model -> del_data_by_row($id,$this->gb_c);
			if (!empty($_POST['tag'])) {
				$result = $model -> set_tag($id, $tag, $this->gb_c);
			}
		}
		if (!empty($new_tag)) {
                        $add['controller']=$this->gb_c;
                        $add['name']=$new_tag;
                        $add['user_id']=$_SESSION['USER_AUTH_KEY'];  
			$new_tag_id = $model -> add($add);
			if (!empty($id)) {
				$result = $model -> set_tag($id, $new_tag_id ,$this->gb_c);
			}
		}
		if ($result !== false) {//保存成功
			 XHandle::halt('操作成功', $this->cpfile.'?c=Contact', 0);
		} else {
                         XHandle::halt('操作失败！', '', 1);
		}
	}
        
        public function action_tag_manage(){
            $model = parent::model('contact', 'oa');
            $tag_list = $model-> get_tag_list($this->gb_c);
            $tree = $this->list_to_tree($tag_list);
            $menu= $this->sub_tree_menu($tree);
             $var_array = array(
                    'menu'=>$menu,
                );            
            TPL::assign($var_array); 
            TPL::display($this->cptpl.'contact/tag_manage.tpl');
        }
        
        public function action_tag_read(){
                $id=$_REQUEST['id'];
		$node = parent::model('contact', 'oa');
		$vo = $node -> get_top_menu_data($id);
		
			if (!empty($vo)) {// 读取成功
				$return['data'] = $vo;
				$return['status'] = 1;
				$return['info'] = "读取成功";
				echo json_encode($return);
			} else {
				$return['status'] = 0;
				$return['info'] = "读取错误";
				echo json_encode($return);
			}
		
        }
        
        public function action_add_tag(){
            $arr=$_POST;
            $arr['controller']= $this->gb_c;
            $model = parent::model('contact', 'oa');
            $result = $model->dotagadd($arr);     
            if (true === $result) {
                XHandle::halt('操作成功', $this->cpfile.'?c=Contact&a=tag_manage', 0);
            }
            else {
                 XHandle::halt('操作失败！', '', 1);
            }      
        }
            public function action_save_tag(){
            $arr=$_POST;
            $arr['controller']= $this->gb_c;
            $model = parent::model('contact', 'oa');
            $id=$arr['id'];
            $result = $model->dotagEdit($id,$arr);     
            if (true === $result) {
                XHandle::halt('操作成功', $this->cpfile.'?c=Contact&a=tag_manage', 0);
            }
            else {
                 XHandle::halt('操作失败！', '', 1);
            }     
        }
                public function action_del_tag(){
                $id=$_POST['id'];
                $node = parent::model('contact', 'oa');
		$res=$node->del_tag($id);
                if($res){
                    XHandle::halt('操作成功', $this->cpfile.'?c=Contact&a=tag_manage', 0);
                } else {
                    XHandle::halt('操作失败！', '', 1);
                }
	}
}