<?php
if (!defined('R_P')) exit("access Die");
require_once(R_P . "/include/class.tree.php");
class category{
	var $channelid = 0;
	var $data = array();
	var $cate = array();
	function category($channelid){
		$this->channelid = $channelid;
		$this->_get();
		$this->_sort();
	}
	function update(){
		foreach($this->cate as $val){
			$param = array(
				'child'=>$val['child'],
				'arrchildid'=>empty($val['children']) ? implode(',', $val['childrens']) : implode(',', $val['children']),
				'arrchildids'=>implode(',', $val['childrens']),
				'pur_browse'=>$val['pur_browse'],
				'ciddir'=>substr($val['ciddir'], 1),
			);
			$sql = qbase::createSql("".TABLEPRE."acategory", $param, "update") . " where cid = '{$val['cid']}' ";
			qdb::query($sql);
		}
		$this->cache();
		foreach($this->cate as $val){
			$key = 'cid_' . $val['cid'];
			cache::delete($key);
		}
	}
	function cache(){
		$menus = array();
		foreach($this->cate as $key=>$val){
			if($val['vlayer'] == 1){
				$str = str_repeat("", $val['vlayer'] - 1);
			}else{
				$str = str_repeat("<img src='images/folder/line.gif' class='cate'/>", max(0, $val['vlayer'] - 2));
				$str .= "<img src='images/folder/join.gif' class='cate' style='margin-right:5px;'/>";
			}
			$temp = explode(',', $val['arrchildid']);
			foreach($temp as &$id){
				$id = intval($id);
			}
			unset($id);
			$menus[] = array(
				'cid'=>intval($val['cid']),
				'child'=>intval($val['child']),
				'pre'=>$str ,
				'title'=>$val['title'],
				'pid'=>intval($val['cupid']),
				'vlayer'=>$val['vlayer'] - 1,
				'c_post'=>intval($val['c_post']),
				'c_allow'=>intval($val['c_allow']),
				'arrchildid'=>$temp,
			);
		}
		cache::set('sys_category_' . $this->channelid, $menus, 86400);
		return $menus;
	}
	function get(){
		$menus = array();
		foreach($this->cate as $key=>$val){
			if($val['vlayer'] == 1){
				$str = str_repeat("", $val['vlayer'] - 1);
			}else{
				$str = str_repeat("<img src='images/folder/line.gif' class='cate'/>", max(0, $val['vlayer'] - 2));
				$str .= "<img src='images/folder/join.gif' class='cate' style='margin-right:5px;'/>";
			}
			$val['ciddir'] = substr($val['ciddir'], 1);
			$val['str'] = $str;
			$menus[] = $val;
		}
		return $menus;
	}	
	function _get(){
		$this->data = qdb::fetchAll("select cid,cupid,dir as path,pic,pur_browse,title,c_post,c_allow,c_check, num, cnum, cfilename, description,arrchildids, arrchildid, displayorder,category_html,html,showphp,pur_usergroup FROM `".TABLEPRE."acategory` where `channelid`='{$this->channelid}' order by cupid asc, displayorder asc");
	}

	function _sort(){
		$tp = $this->data[0];
		$tp['cupid'] = 0;
		$tp['cid'] = 0;
		$tp['pur_browse'] = 0;
		$tp['path'] = '';
		$tp['child'] = 1;
		$tp['title'] = '_root';
		array_unshift($this->data, $tp);
		$tree = new tree($this->data);
		$tree->_idkey = 'cid';
		$tree->_pidkey = 'cupid';
		$tree->_rootid = 0;
		$tree->_isordered = false;
		$this->cate = $tree->make_tree();
		$this->cate = $tree->make_truetree($this->cate);
		$this->cate = $tree->make_chilrens($this->cate);
		$this->cate = $tree->make_sort($this->cate);
		array_shift($this->cate);
	}
}
?>