<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class contactOModel extends X {


  
        public function get_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."contact WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}
 
        public function get_top_menu_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."user_tag WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}


        public function get_contact_list($userid){
             $sql=" SELECT * FROM `oa_contact` WHERE user_id='$userid' AND is_del=0";
             $list = parent::$obj->getall($sql);
             return $list;
        }
        public function get_data_list($controller, $tag_id = null) {
		$user_id = $_SESSION['USER_AUTH_KEY'];
		$where = "tag.user_id='$user_id' and tag.controller='$controller'";
		if (!empty($tag_id)) {
			$where .= " and tag_id=$tag_id";
		}
		$join = 'join ' . DB_PREFIXOA . 'user_tag tag on tag_id=tag.id';
                $sql="SELECT `row_id`,`tag_id` FROM `oa_user_tag_data` ".$join." WHERE ".$where;
		return parent::$obj->getall($sql);
	}
         public function get_tag_list($controller) {
		$user_id = $_SESSION['USER_AUTH_KEY'];
		$where = "user_id='$user_id' and controller='$controller'";
                $sql="SELECT `id`,`name` FROM `oa_user_tag` WHERE ".$where;
		return parent::$obj->getall($sql);
	}

                public function doEdit($id,$arr){
                unset($arr['id']);
                unset($arr['opmode']);
                unset($arr['ajax']);        
                $result = parent::$obj->update(DB_PREFIXOA.'contact', $arr, 'id='.$id.'');
                return $result;
        }  
        public function doAdd($array) {
              unset($array['ajax']);
              unset($array['opmode']);
              $array['user_id']= $_SESSION['USER_AUTH_KEY'];
           return parent::$obj->insert(DB_PREFIXOA.'contact', $array); 
      }
      public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."contact WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
      
      	function del_data_by_row($row_id, $controller) {
		if (isset($row_id)) {
                       
			if (is_array($row_id)) {
                                foreach ($row_id as $v){
                                     $sql = "DELETE FROM ".DB_PREFIXOA."user_tag_data WHERE row_id='".intval($v)."' AND controller='$controller'";
                                     $result= parent::$obj->query($sql);
                                }
			} else {
				$sql = "DELETE FROM ".DB_PREFIXOA."user_tag_data WHERE row_id='".intval($row_id)."' AND controller='$controller'";
                                $result= parent::$obj->query($sql);
			}
		}
		return $result;
	}
        function set_tag($row_list, $tag_list, $controller) {
		if (empty($row_list)) {
			return true;
		}
		if (empty($tag_list)) {
			return true;
		}
		if (is_array($row_list)) {
			$row_list = array_filter($row_list);
		} else {
			$row_list = explode(",", $row_list);
			$row_list = array_filter($row_list);
		}
		$row_list = implode(",", $row_list);
		if (is_array($tag_list)) {
			$tag_list = array_filter($tag_list);
		} else {
			$tag_list = explode(",", $tag_list);
			$tag_list = array_filter($tag_list);
		}
		$tag_list = implode(",", $tag_list);
		$where = 'a.id in (' . $row_list . ') AND b.id in(' . $tag_list . ')';
		$sql = 'INSERT INTO ' . DB_PREFIXOA . 'user_tag_data (row_id,controller,tag_id) SELECT a.id,b.controller,b.id ';
		$sql .= ' FROM ' . DB_PREFIXOA . $controller . ' a, ' . DB_PREFIXOA . 'user_tag b WHERE ' . $where;

		$result= parent::$obj->query($sql);
		if ($result === false) {
			return false;
		} else {
			return true;
		}
	}
        
            public function add($arr){
                
                   return parent::$obj->insert(DB_PREFIXOA.'user_tag', $arr); 
            }
            
         public function dotagEdit($id,$arr){
                unset($arr['id']);
                unset($arr['opmode']);
                unset($arr['ajax']);        
                $result = parent::$obj->update(DB_PREFIXOA.'user_tag', $arr, 'id='.$id.'');
                return $result;
        }  
        public function dotagadd($array) {
              unset($array['opmode']);
              $array['user_id']= $_SESSION['USER_AUTH_KEY'];
           return parent::$obj->insert(DB_PREFIXOA.'user_tag', $array); 
      }
        
            public function del_tag($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."user_tag WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
}
?>
