<?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 categoryIModel extends X {
    
    public function rootMenu() {
        $args = array(
            'rootid'=>0,
            'ismenu'=>1,
        );
        return $this->getChildCategory($args);
    }
    
    
    public function sedMenu() {
        $root_args = array(
            'rootid'=>0,
            'ismenu'=>1,
        );
        $data = $this->getChildCategory($root_args);
        foreach($data as $key=>$value) {
            $child_args = array(
                'rootid'=>$value['catid'],
                'ismenu'=>1,
            );
            $data[$key]['childmenu'] = $this->getChildCategory($child_args,$lang);
        }
  //   print_r($data);die;
        return $data;
    }
    
    
    public function subMenu($args=NULL){
        
        if (is_array($args)) {
            $args = array_merge($args, array('isaccessory'=>1));
        }
        else {
            $args = array('isaccessory'=>1);
        }
        return $this->getChildCategory($args);
    }
    
    
    public function rootCategory($args) {
        return $this->getChildCategory($args);
    }
    
    
    public function treeCategory($args) {
        $data = $this->getChildCategory($args);
      //  print_r($data)
        foreach($data as $key=>$value) {
            $child_args = array(
                'rootid'=>$value['catid'],
            );
            $data[$key]['childcatgory'] = $this->getChildCategory($child_args);
        }
        return $data;
    }
    
    
    public function getChildCategory($args) {
        $lang= parent::$lang;
        $query = "SELECT catid,modalias,catname,asname,dirname,rootid,relid,dirpath,linktype,outurl".
                    " FROM ".DB_PREFIX."category WHERE flag='1' AND catid!='126' AND lang='$lang'";
   
        
        if (true == XValid::isSpChar($args['module'])) {
            $query .= " AND modalias='{$args['module']}'";
        }
        if (true === XValid::isNumber($args['treeid'])) {
            $query .= " AND treeid='{$args['treeid']}'";
        }
        if (true === XValid::isNumber($args['rootid'])) {
            $query .= " AND rootid='{$args['rootid']}'";
        }
        if (true === XValid::isNumber($args['ismenu'])){
            $query .= " AND ismenu='{$args['ismenu']}'";
        }
        if (true === XValid::isNumber($args['isaccessory'])){
            $query .= " AND isaccessory='{$args['isaccessory']}'";
        }
        $query .= " ORDER BY orders ASC";
		if (true === XValid::isNumber($args['num'])) {
			$query .= " LIMIT ".$args['num']."";
		}
           // echo $query;
        $data = parent::$obj->getall($query);
        $i = 1;
    
        foreach($data as $key=>$value) {
            $data[$key]['i'] = $i;
            #URL路由
            if ($value['linktype'] == 2) {
               
                $m = parent::model('label', 'im');
                $data[$key]['url'] = $m->repLabel($value['outurl']);
                $data[$key]['wapurl'] = $m->repLabel($value['outurl']);
                unset($m);
            }
            else {
                #单页栏目
                if ($value['modalias'] == 'about') {
                    if ($value['rootid'] == 0) {
                          $lang= parent::$lang;
                        if($lang=='en'){
                            $data[$key]['url'] = XUrl::getContentUrl('about', 'about', 4);
                        }else{
                              $data[$key]['url'] = XUrl::getContentUrl('about', 'about', 3);
                        }
                       
                    }
                    else {
                        #URL路由
                        $abid = $this->_getAboutID($value['catid']);
                        $data[$key]['url'] = XUrl::getContentUrl('about', 'about', $abid);
                        $data[$key]['wapurl'] = XUrl::getContentWapUrl('about', $abid);
                    }
                }
                else {
                    $data[$key]['url'] = XUrl::getCategoryUrl($value['modalias'], $value['catid'], $value['dirname']);
                    $data[$key]['wapurl'] = XUrl::getCategoryWapUrl($value['modalias'], $value['catid']);
                }
            }
        
        }
        return $data;
    }
    
    
    public function getModName($val, $type=1) {
        $modname = '';
        $query = "SELECT modalias FROM ".DB_PREFIX."category";
        if ($type == 1) {
            $val = intval($val);
             $query .= " WHERE catid='{$val}'";
             $rows = parent::$obj->fetch_first($query);
             if (!empty($rows)) {
                $modname = $rows['modalias'];
             }
             unset($rows);
        }
        else {
            if (true == XValid::isSpChar($val)) {
                $query .= " WHERE dirname='{$val}'";
                $rows = parent::$obj->fetch_first($query);
                if (!empty($rows)) {
                    $modname = $rows['modalias'];
                }
                unset($rows);
            }
        }
        return $modname;
    }
    
    
    public function getOneData($id) {
        $sql = "SELECT v.*,m.tplindex AS mod_tplindex,m.tpllist AS mod_tpllist".
                    ",m.tpldetail AS mod_tpldetail".
                    " FROM ".DB_PREFIX."category AS v".
                    " LEFT JOIN ".DB_PREFIX."module AS m ON m.alias=v.modalias".
                    " WHERE v.catid='{$id}'";
        $data = parent::$obj->fetch_first($sql);
        if (!empty($data)) {
            if ($data['linktype'] == 2) {
                $m = parent::model('label', 'im');
                $data['url'] = $m->repLabel($data['outlink']);
                $data['wapurl'] = $m->repWapLabel($data['outlink']);
                unset($m);
            }
            else {
                $data['url'] = XUrl::getCategoryUrl($data['modalias'], $data['catid'], $data['dirname']);
                $data['wapurl'] = XUrl::getCategoryWapUrl($data['modalias'], $data['catid']);
            }
        }
        unset($sql);
        return $data;
    }
    
    
    public function getID($name) {
        $id = 0;
        if (true === XValid::isSpChar($name)) {
            $query = "SELECT catid FROM ".DB_PREFIX."category WHERE dirpath='{$name}'";
            $rows = parent::$obj->fetch_first($query);
            if (!empty($rows)) {
                $id = intval($rows['catid']);
            }
            unset($rows);
        }
        return $id;
    }
    
    
    public function getCategoryUrl($val, $valtype, $title='', $classname='', $target='_self') {
        $url = "";
        $query = "SELECT catid,modalias,rootid,catname,linktype,outurl,dirname FROM ".DB_PREFIX."category";
        if ($valtype == 1) {
            $query .= " WHERE catid='{$val}'";            
        }
        else {
            $query .= " WHERE dirname='{$val}'";
        }
        $rows = parent::$obj->fetch_first($query);
        if (!empty($rows)) {
            $title = empty($title) ? trim($rows['catname']) : $title;
            #URL路由
            if ($rows['linktype'] == 2) {
                $m_label = parent::model('label', 'im');
                $caturl = $m_label->repLabel($rows['outurl']);
                unset($m_label);
            }
            else {
                if ($rows['modalias'] == 'about') {
                    $abid = $this->_getAboutID($rows['catid']);
                    $caturl = XUrl::getContentUrl('about', 'about', $abid);
                }
                else {
                    $caturl = XUrl::getCategoryUrl($rows['modalias'], $rows['catid'], $rows['dirname']);
                }
            }
        }
        else {
            $caturl = '';
        }
        unset($rows);
        if (!empty($caturl)) {
            return "<a href='{$caturl}' class='{$classname}' target='{$target}'>{$title}</a>";
        }
        else {
            return '';
        }
    }
    
    
    public function getCatID($module, $name) {
        $query = "SELECT catid FROM ".DB_PREFIX."category WHERE modalias='{$module}' AND dirname='{$name}'";
        $rows = parent::$obj->fetch_first($query);
        if (!empty($rows)) {
            return $rows['catid'];
        }
        else {
            return 0;
        }
    }
    
    
    private function _getAboutID($cid) {
        $abid = 0;
        $sql = "SELECT `abid` FROM ".DB_PREFIX."about".
                " WHERE `catid`='{$cid}'";
        $rows = parent::$obj->fetch_first($sql);
        if (!empty($rows)) {
            $abid = $rows['abid'];
        }
        unset($sql, $rows);
        return $abid;
    }
    
    
    public function volistAll($where='', $orderby='', $num=0, $limit='') {
        $sql = "SELECT v.*".
                " FROM ".DB_PREFIX."category AS v".
                " WHERE v.flag='1'";
        if (!empty($where)) {
            $sql .= ' AND '.$where;
        }  
        if (!empty($orderby)) {
            $sql .= ' ORDER BY '.$orderby;
        }
        else {
            $sql .= ' ORDER BY v.orders ASC';
        }
        if (!empty($limit)) {     
            $sql .= " LIMIT {$limit}";
        }
        else {
            $num = intval($num)<1 ? 10 : intval($num);
            $sql .= " LIMIT {$num}";
        }
        $data = parent::$obj->getall($sql);
        $i = 1;
        foreach($data as $key=>$value) {
            $data[$key]['i'] = $i;
            #URL路由
            if ($value['linktype'] == 2) {
                $m = parent::model('label', 'im');
                $data[$key]['url'] = $m->repLabel($value['outurl']);
                $data[$key]['wapurl'] = $m->repLabel($value['outurl']);
                unset($m);
            }
            else {
                #单页栏目
                if ($value['modalias'] == 'about') {
                    if ($value['rootid'] == 0) {
                        $data[$key]['url'] = '';
                    }
                    else {
                        #URL路由
                        $abid = $this->_getAboutID($value['catid']);
                        $data[$key]['url'] = XUrl::getContentUrl('about', 'about', $abid);
                        $data[$key]['wapurl'] = XUrl::getContentWapUrl('about', $abid);
                    }
                }
                else {
                    $data[$key]['url'] = XUrl::getCategoryUrl($value['modalias'], $value['catid'], $value['dirname']);
                    $data[$key]['wapurl'] = XUrl::getCategoryWapUrl($value['modalias'], $value['catid']);
                }
            }
        }
        return $data;
    }

    
    public function getCatName($id) {
        $sql = "SELECT catname FROM ".DB_PREFIX."category".
                " WHERE catid='{$id}'";
        $rows = parent::$obj->fetch_first($sql);
        return trim($rows['catname']);
        unset($sql, $rows);
    }
    public function getCID($name) {
        $id = 0;
        if (true === XValid::isSpChar($name)) {
            $query = "SELECT catid FROM ".DB_PREFIX."category WHERE modalias='{$name}'";
            $rows = parent::$obj->fetch_first($query);
            if (!empty($rows)) {
                $id = intval($rows['catid']);
            }
            unset($rows);
        }
        return $id;
    }
    public function gerimgurl($cid){
        $data=array();
        $query = "SELECT `target`,`tagname`,`url`,`normbody`".
    					  " FROM ".DB_PREFIX."myads WHERE `catid`='$cid' AND `flag`='1'";
    	$rows = parent::$obj->fetch_first($query);
            if (!empty($rows)) {
                $info = XHandle::dounSerialize($rows['normbody']);
    		        $data['type'] = $info['tpye'];
                    $data['tagname'] = $rows['tagname'];
                    $data['title'] = $info['title'];
                    if (substr($info['uploadfiles'], 0, 15) == 'data/attachment') {
                        $data['uploadfiles'] = parent::$urlpath.$info['uploadfiles'];
                    }
                    else {
                        $data['uploadfiles'] = $info['uploadfiles'];
                    }
                    $data['width'] = $info['width'];
                    $data['height'] = $info['height'];
                    $data['url'] = $rows['url'];
                    if ($data['target'] == 1) {
                        $data['target'] = 'self';
                    }
                    else {
                        $data['target'] = '_blank';
                    }
            }
            return $data;      
    			
    }
    public function geradimgurl($cid){
        $data=array();
        $query = "SELECT `target`,`url`,`normbody`".
                          " FROM ".DB_PREFIX."myads WHERE `catid`='$cid' AND `flag`='1'";
        $rows = parent::$obj->getall($query);
        foreach ($rows as $key => $value) {
                $info = XHandle::dounSerialize($value['normbody']);
                $data[$key]['type'] = $info['tpye'];
                    $data[$key]['title'] = $info['title'];
                    if (substr($info['uploadfiles'], 0, 15) == 'data/attachment') {
                        $data[$key]['uploadfiles'] = parent::$urlpath.$info['uploadfiles'];
                    }
                    else {
                        $data[$key]['uploadfiles'] = $info['uploadfiles'];
                    }
                    $data[$key]['width'] = $info['width'];
                    $data[$key]['height'] = $info['height'];
                    $data[$key]['url'] = $rows['url'];
                    if ($data[$key]['target'] == 1) {
                        $data[$key]['target'] = 'self';
                    }
                    else {
                        $data[$key]['target'] = '_blank';
                    }
        }
            return $data;      
                
    }
        public function getlang() {
                $lang= parent::$lang;
                $query = "SELECT name,value".
                            " FROM ".DB_PREFIX."language WHERE lang='$lang'";

                $data = parent::$obj->getall($query);
                return $data;
        }
    public function add_lang($arr) {
        $result = parent::$obj->insert(DB_PREFIX.'language', $arr);
        return $result;
    }
    public function checklang() {
        $query = "SELECT * FROM ".DB_PREFIX."lang WHERE lang='en'";

         $data = parent::$obj->fetch_first($query);
    
        return $data;
    }
    
}
?>
