<?php
/*
	[Destoon B2B System] Copyright (c) 2008-2013 Destoon.COM
	This is NOT a freeware, use is subject to license.txt
*/
defined('IN_DESTOON') or exit('Access Denied');
/*if($action=='add' || $action=='edit'){
$CAT or msg('请指定分类ID');
}*/
$menus = array (
    array('添加属性', '?file='.$file.'&catid='.$catid.'&action=add'),
    array('属性参数', '?file='.$file.'&catid='.$catid),
	array('添加类型', '?file='.$file.'&catid='.$catid.'&action=typeadd'),
	array('类型管理', '?file='.$file.'&catid='.$catid.'&action=type'),
);
//$TYPE = array('单行文本(text)', '多行文本(textarea)', '列表选择(select)', '复选框(checkbox)');
$TYPE = array('文本(text)', '列表(select)');
$TYPES = array('普通模板', '账号模板');
if($action=='add' || $action=='edit'){
	$on = 0;
	}elseif($action=='typeadd' || $action=='typeedit'){
		$on = 2;
		}
	elseif($action=='type'){
		$on = 3;
		}else{
			$on = 1;
			}

$do = new property;
$do->catid = $catid;
function property_select($parentid, $catid) {
    global $db;
	$parentid = $parentid ? $parentid=$parentid : $parentid=0;
	$condition ="moduleid=16";
	$result = mysql_query("SELECT * FROM {$db->pre}category_type WHERE $condition ORDER BY listorder ASC");
	echo '<select name="post[parentid]"  id="catid_1">';
	echo "<option value='0'>请选择</option>";
	while($r = mysql_fetch_array($result)) {
		if($parentid==$r['id']){
		echo "<option value='".$r['id']."'   selected='selected'>".$r['title']."</option>";
		}else{
		echo "<option value='".$r['id']."'>".$r['title']."</option>";
			}
			/*$sql = mysql_query("SELECT * FROM {$db->pre}category_option WHERE parentid=".$r['oid']." and catid=$catid ORDER BY listorder ASC");
			while($row = mysql_fetch_array($sql)) {
		if($row['oid']==$parentid && $parentid!=0){
		echo "<option value='".$row['oid']."'   selected='selected'> -".$row['name']."</option>";
		}else{
		echo "<option value='".$row['oid']."'> -".$row['name']."</option>";
			}
			}*/
	}
    echo '</select>';
}

function property_pid($parentid, $catid) {
    global $db;
	$parentid = $parentid ? $parentid=$parentid : $parentid=0;
	$condition ="pid=$parentid";
	$result = mysql_query("SELECT * FROM {$db->pre}category_option WHERE $condition ORDER BY listorder ASC");
	echo '<select name="post[pid]"  id="catid_1">';
	echo "<option value='0'>请选择</option>";
	while($r = mysql_fetch_array($result)) {
		if($parentid==$r['id']){
		echo "<option value='".$r['oid']."'   selected='selected'>".$r['name']."</option>";
		}else{
		echo "<option value='".$r['oid']."'>".$r['name']."</option>";
			}
			/*$sql = mysql_query("SELECT * FROM {$db->pre}category_option WHERE parentid=".$r['oid']." and catid=$catid ORDER BY listorder ASC");
			while($row = mysql_fetch_array($sql)) {
		if($row['oid']==$parentid && $parentid!=0){
		echo "<option value='".$row['oid']."'   selected='selected'> -".$row['name']."</option>";
		}else{
		echo "<option value='".$row['oid']."'> -".$row['name']."</option>";
			}
			}*/
	}
    echo '</select>';
}

function tongji($oid){
	global $db;
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$db->pre}category_option WHERE pid=$oid");
	return $r['num'];
	
	}
switch($action) {
	case 'add':
		if($submit) {
			if($do->pass($post)) {
				$do->add($post);
				dmsg('添加成功', '?file='.$file.'&catid='.$catid);
			} else {
				msg($do->errmsg);
			}
		} else {
			$type = 1;
			$required = $search = 1;
			$name = $value = $extend = '';
			include tpl('property_edit');
		}
	break;
	case 'edit':
		//$oid or msg();
		$do->oid = $oid;
		if($submit) {
			if($do->pass($post)) {
				$do->edit($post);
				dmsg('修改成功', $forward);
			} else {
				msg($do->errmsg);
			}
		} else {
			extract($do->get_one($oid));
			include tpl('property_edit');
		}
	break;
	case 'order':
		$do->order($listorder, $pid);
		dmsg('排序成功', $forward);
	break;
	case 'delete':
		$oid or msg();
		$do->oid = $oid;
		$do->delete($pid);
		dmsg('删除成功', '?file='.$file.'&catid='.$catid);
	break;
	case 'orders':
		$do->order($listorder, $pid);
		dmsg('排序成功', $forward);
	break;
	case 'deletes':
		$id or msg();
		$do->id = $id;
		$do->deletes($pid);
		dmsg('删除成功', '?file='.$file.'&catid='.$catid.'&action=type');
	break;
	case 'typeadd':
		if($submit) {
			if($do->passtype($post)) {
				$do->typeadd($post);
				dmsg('添加成功', '?file='.$file.'&catid='.$catid.'&action=type');
			} else {
				msg($do->errmsg);
			}
		} else {
			$type = 1;
			$required = $search = 1;
			$name = $value = $extend = '';
			include tpl('property_edit_type');
		}
	break;
	case 'typeedit':
		//$id or msg();
		$do->id = $id;
		if($submit) {
			if($do->passtype($post)) {
				$do->typeedit($post);
				dmsg('修改成功', $forward);
			} else {
				msg($do->errmsg);
			}
			
			
		} else {
			extract($do->get_ones($id));
			include tpl('property_edit_type');
		}
		echo "55555";
	break;
   case 'type':
		$lists = $do->get_lists();
		include tpl('property_type');
	break;
		case 'small':
		$lists = $do->get_lista($oid);
		include tpl('property');
	break;
	default:
		$lists = $do->get_list();
		include tpl('property');
	break;
}
class property {
	var $db;
	var $oid;
	var $catid;
	var $table;
	var $errmsg = errmsg;
	var $id;

	function property() {
		global $db, $DT_PRE;
		$this->table = $DT_PRE.'category_option';
		$this->db = &$db;
	}

	function pass($post) {
		if(!is_array($post)) return false;
		//if(!$post['pid']) return $this->_(lang('message->pass_property_op_pid'));
		if(!$post['name']) return $this->_('请填写属性名称');
		if($post['type'] == 3) {
			if(!$post['value']) return $this->_('请填写备选值');
			if(strpos($post['value'], '|') === false) return $this->_('最少需要设定2个备选值');
		}
		return true;
	}

	function set($post) {
		$post['value'] = $post['type'] ? trim($post['value']) : '';
		if($post['type'] < 2) $post['search'] = 0;
		return $post;
	}

	function add($post) {
		$post = $this->set($post);
		$sqlk = $sqlv = '';
		foreach($post as $k=>$v) {
			$sqlk .= ','.$k; $sqlv .= ",'$v'";
		}
        $sqlk = substr($sqlk, 1);
        $sqlv = substr($sqlv, 1);
		$this->db->query("INSERT INTO {$this->table} ($sqlk) VALUES ($sqlv)");
		return true;
	}

	function edit($post) {
		$post = $this->set($post);
		$sql = '';
		foreach($post as $k=>$v) {
			$sql .= ",$k='$v'";
		}
        $sql = substr($sql, 1);
	    $this->db->query("UPDATE {$this->table} SET $sql WHERE oid=$this->oid");
		return true;
	}

	function get_one() {
        return $this->db->get_one("SELECT * FROM {$this->table} WHERE oid=$this->oid");
	}

	function delete($pid) {
		$this->db->query("DELETE FROM {$this->table} WHERE oid=$this->oid");
	}

	function order($listorder) {
		if(!is_array($listorder)) return false;
		foreach($listorder as $k=>$v) {
			$k = intval($k);
			$v = intval($v);
			$this->db->query("UPDATE {$this->table} SET listorder=$v WHERE oid=$k");
		}
		return true;
	}

	function get_list() {
		global $pages, $page, $pagesize, $offset, $pagesize, $CAT, $sum;
		$condition = "catid=$this->catid and pid=0";
		if($page > 1 && $sum) {
			$items = $sum;
		} else {			
			$r = $this->db->get_one("SELECT COUNT(*) AS num FROM {$this->table} WHERE $condition");
			$items = $r['num'];
		}
		if($items != $CAT['property']) $this->db->query("UPDATE {$this->db->pre}category SET property=$r[num] WHERE catid=$this->catid");
		$pages = pages($items, $page, $pagesize);
		$lists = array();
		$result = $this->db->query("SELECT * FROM {$this->table} WHERE $condition ORDER BY listorder ASC,oid ASC LIMIT $offset,$pagesize");
		while($r = $this->db->fetch_array($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

	function _($e) {
		$this->errmsg = $e;
		return false;
	}
	
	/*********类 型管理*///
		function typeadd($post) {
			global $db, $DT_PRE;
		//$post = $this->typeset($post);
		$sqlk = $sqlv = '';
		foreach($post as $k=>$v) {
			$sqlk .= ','.$k; $sqlv .= ",'$v'";
		}
        $sqlk = substr($sqlk, 1);
        $sqlv = substr($sqlv, 1);
		$this->db->query("INSERT INTO {$db->pre}category_type ($sqlk) VALUES ($sqlv)");
		return true;
	}

	function typeedit($post) {
		global $db;
		$sql = '';
		foreach($post as $k=>$v) {
			$sql .= ",$k='$v'";
		}
        $sql = substr($sql, 1);
	    $this->db->query("UPDATE {$db->pre}category_type SET $sql WHERE id=$this->id");
		return true;
	}
	function passtype($post) {
		if(!is_array($post)) return false;
		//if(!$post['pid']) return $this->_(lang('message->pass_property_op_pid'));
		if(!$post['title']) return $this->_('请填写属性名称');
		if(!$post['unit']) return $this->_('请填写属性单位');
		return true;
	}
	
	function deletes($pid) {
		global $db;
		$this->db->query("DELETE FROM {$db->pre}category_type WHERE id=$this->id");
	}

	function orders($listorder) {
		global $db;
		if(!is_array($listorder)) return false;
		foreach($listorder as $k=>$v) {
			$k = intval($k);
			$v = intval($v);
			$this->db->query("UPDATE {$db->pre}category_type SET listorder=$v WHERE id=$k");
		}
		return true;
	}
	
	function get_ones() {
		global $db;
        return $this->db->get_one("SELECT * FROM {$db->pre}category_type WHERE id=$this->id");
	}
	
    function get_lists() {
		global $db, $pages, $page, $pagesize, $offset, $pagesize, $CAT, $sum;
		$condition = "moduleid=16";
		/*if($page > 1 && $sum) {
			$items = $sum;
		} else {			
			$r = $this->db->get_one("SELECT COUNT(*) AS num FROM {$db->pre}category_type WHERE $condition");
			$items = $r['num'];
		}*/
		//if($items != $CAT['property']) $this->db->query("UPDATE {$this->db->pre}category SET property=$r[num] WHERE catid=$this->catid");
		//$pages = pages($items, $page, $pagesize);
		$lists = array();
		$result = $this->db->query("SELECT * FROM {$db->pre}category_type WHERE $condition ORDER BY listorder ASC");
		while($r = $this->db->fetch_array($result)) {
			$lists[] = $r;
		}
		return $lists;
	}
	
		function get_lista($oid) {
		global $pages, $page, $pagesize, $offset, $pagesize, $CAT, $sum;
		$condition = "catid=$this->catid and pid=$oid";
		if($page > 1 && $sum) {
			$items = $sum;
		} else {			
			$r = $this->db->get_one("SELECT COUNT(*) AS num FROM {$this->table} WHERE $condition");
			$items = $r['num'];
		}
		if($items != $CAT['property']) $this->db->query("UPDATE {$this->db->pre}category SET property=$r[num] WHERE catid=$this->catid");
		$pages = pages($items, $page, $pagesize);
		$lists = array();
		$result = $this->db->query("SELECT * FROM {$this->table} WHERE $condition ORDER BY listorder ASC,oid ASC LIMIT $offset,$pagesize");
		while($r = $this->db->fetch_array($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

}
?>