<?php
class extend {
	static $_instance = null;
	var $name;
	var $ext;
	var $type;
	var $data;
	var $value;
	var $filter;
	var $filter_error;
	var $need = 0;
	var $upload = 0;
	function get_instance() {
		if (!self :: $_instance) self :: $_instance = new extend();
		return self :: $_instance;
	} 

	function build($val, $default = '', $ac = 'add') {
		$op = 'op_' . $val['type'];
		if (method_exists($this, $op)) {
			$this-> name = $val['name'];
			$this -> ext = $val['ext'];
			$this -> type = $val['type'];
			$this -> upload = $val['upload'];
			$this-> filter = $val['filter'];
			$this-> filter_error = $val['filter_error'];
			$this-> need = $val['need'];
			$this -> data = $val['data'] ? @unserialize($val['data']) : '';
			$this -> value = $ac == 'add' ? $val['word'] : $default;
			return $this -> $op();
		} else {
			return '出错了，错误的扩展类型';
		} 
	} 

	function op_input() {
		global $channelid;
		$s = "<input type='text' class='f-text' name='extend[{$this->ext}]' id='ext_{$this->ext}_0' value='{$this->value}' size='35' maxlength='255'/>";
		if($this->upload){
			$s .= "<input type=\"button\" class=\"f-button\" value=\"上传文件\" onclick=\"qmeCms.dialogView.iframe('上传','system.php?sys=upload&amp;id=ext_{$this->ext}&amp;channelid={$channelid}&file_type=all',{type:'abs'});\"/>";
			$s .= "<input type=\"button\" class=\"f-button\" value=\"浏览服务器\" onclick=\"SelectBrowse('".A_P."/images/browse.html?type=all&channelid={$channelid}','ext_{$this->ext}', 550,360);;\"/>";
		}
		$s .= "<script type='text/javascript'>qmeCms.extend.add(\"input\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $s;
	} 

	function op_map() {
		global $channelid;
		$s = "<input type='text' class='f-text' name='extend[{$this->ext}]' id='ext_{$this->ext}_0' value='{$this->value}' size='20' maxlength='30' readonly onclick=\"return getMapMark(this.id);\" />";
		$s .= "<script type='text/javascript'>qmeCms.extend.add(\"input\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $s;
	} 

	function op_date() {
		global $channelid;
		$s = "<input type='text' class='f-text' name='extend[{$this->ext}]' id='ext_{$this->ext}_0' value='{$this->value}' size='15' maxlength='10'/>";
		$s .= "<script type='text/javascript'>domReady(function(){new new qmeCms.Calendar('ext_{$this->ext}_0');});qmeCms.extend.add(\"date\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $s;
	} 

	function op_radio() {
		$html = '<div class="modify-cate">';
		foreach($this->data as $key=>$val){
			if($this->value){
				$checked = $val == $this->value ? " checked" : "";
			}else{
				$checked = $key == 0 ? " checked" : "";
			}
			$html .= "<input type='radio' name='extend[{$this->ext}]' id='ext_{$this->ext}_{$key}' value='{$val}'  {$checked} /> <label style='margin-right:10px;' for='ext_{$this->ext}_{$key}'>{$val}</label>";
		}
		$html .= '</div>';
		$html .= "<script type='text/javascript'>qmeCms.extend.add(\"radio\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $html;
	}

	function op_select() {
		$html = "<select name='extend[{$this->ext}]' id='ext_{$this->ext}_0'>";
		foreach($this->data as $key=>$val){
			if($this->value){
				$checked = $val == $this->value ? " selected" : "";
			}else{
				$checked = $key == 0 ? " selected" : "";
			}
			$html .= "<option value='{$val}'  {$checked}>{$val}</option>";
		}
		$html .= "</select>";
		$html .= "<script type='text/javascript'>qmeCms.extend.add(\"select\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $html;
	}

	function op_checkbox()
	{
		$html = '<div class="modify-cate">';
		$result = explode(",", $this->value);
		foreach($this->data as $key=>$val){
			if($this->value){
				$checked = in_array($val, $result) ? " checked" : "";
			}
			$html .= "<input type='checkbox' name='extend[{$this->ext}][]' id='ext_{$this->ext}_{$key}' value='{$val}'  {$checked} /> <label style='margin-right:10px;' for='ext_{$this->ext}_{$key}'>{$val}</label>";
		}
		$html .= '</div>';
		$html .= "<script type='text/javascript'>qmeCms.extend.add(\"checkbox\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $html;
	}

	function op_area() {
		$s = "<textarea class='f-text' name='extend[{$this->ext}]' id='ext_{$this->ext}_0' style='width:400px;height:200px;'>{$this->value}</textarea>";
		$s .= "<script type='text/javascript'>qmeCms.extend.add(\"area\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";
		return $s;
	} 

	function op_html() {
		$s = "<textarea class='f-text' name='extend[{$this->ext}]' id='ext_{$this->ext}_0'  style='display:none;width:650px;height:150px;'>{$this->value}</textarea>";
		$s .= "<iframe width='650' height='200' style='width:650px;height:150px;' frameborder='0' scrolling='no' src='../images/fck/editor/fckeditor.html?InstanceName=ext_{$this->ext}_0&Toolbar=Standard&width=600&height=150' id='FCKeditor1___Frame__{$this->ext}' name='FCKeditor1___Frame__{$this->ext}'></iframe>";
		$s .= "<script type='text/javascript'>qmeCms.extend.add(\"html\", \"".addslashes($this->ext)."\", \"{$this->need}\", \"". addslashes($this->name)."\", \"". addslashes($this->filter)."\", \"". addslashes($this->filter_error)."\");</script>";		
		return $s;
	} 
} 

?>