ThinkPHPカスタムラベルテスト

3533 ワード

テスト機能:source(2 D配列またはテキスト[1,男性|0,女性])に基づいてラジオボタングループを生成し、text:表示値は2 D配列に対して
val:値を2 D配列に対してとる
テキスト:カンマの前は値、後は表示値
<?php

/**
 +-----------------------------
 * @desc ThinkPHP Fcb 
 +-----------------------------
 * @author 710
 +-----------------------------
 * @date 2011-12-14
 +-----------------------------
 */
import("TagLib");
class TagLibFcb extends TagLib{
	
	protected $tags = array(
	  'radiolist'=>array('attr'=>'name,source,type,text,val,checked,style,class','close'=>0,'level'=>3,'alias'=>'rt,rl'),
	);
	
	/**
	 +-------------------------------
	 * @desc  fcb:radiolist  
	 * @param string $type=> 
	 * list: ,text: ( :1, |2, )
	 +-------------------------------
	 */
	public function _radiolist($attr,$content,$type='list'){
		$tag = $this->parseXmlAttr($attr,'radiolist');
		$type = empty($tag['type']) ? $type : $tag['type'];
		$source = $tag['source'];
		$name = isset($tag['name']) ? 'radio'.date('is') : $tag['name'];
		$text = empty($tag['text']) ? 'text' : $tag['text'];
		$val = empty($tag['val']) ? 'id' : $tag['val'];
		$style = empty($tag['style']) ? '' : 'style="'. $tag['style'].'"';
		$class= empty($tag['class']) ? '' : 'class="'. $tag['class'].'"';
		$checked = isset($tag['checked']) ? $tag['checked'] : '';
		$result='';
		if($type=='list'){
			$result.='<?php foreach($'.$source.' as $key=>$val): ?>';
			$result.='<input type="radio" <?php if($val['.$val.']=="'.$checked.'"): echo "checked=\"checked\"";endif ?>   '.$class.' '.$style.' name="'.$name.'" value="<?php echo $val['.$val.'] ?>" /><?php echo $val['.$text.'] ?>';
			$result.='<?php endforeach ?>';
		}elseif($type=='text'){
			$list = explode("|",$source);
			foreach($list as $key=>$val){
				$item = explode(",",$val);
				$result .= '<input type="radio" '.($item[0]==$checked?"checked=\"checked\"":"").' value="'.$item[0].'" name="'.$name.'" '.$class.' '.$style.' />'.$item[1];
			}
		}
		return $result;
	}
	
	/**
	 +------------------------------
	 * radiolist   =>  
	 * <fcb:rt source="1, |2, " />
	 *  , 
	 +------------------------------
	 */
	public function _rt($attr,$content){
		return $this->_radiolist($attr, $content,'text');
	}
	
	/**
	 +-------------------------------
	 * radiolist  =>  
	 * <fcb:rl source="list" text="name" val="id" />
	 * text: ,val: 
	 +-------------------------------
	 */
	public function _rl($attr,$content){
		return $this->_radiolist($attr, $content,'list');
	}
	
}
?>