<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace ModStart\Field; use ModStart\Core\Dao\ModelUtil; use ModStart\Core\Util\ConvertUtil; use ModStart\Core\Util\SerializeUtil; use ModStart\Core\Util\TagUtil; use ModStart\Field\Concern\CanCascadeFields; class Checkbox extends AbstractField { use CanCascadeFields; const SERIALIZE_TYPE_DEFAULT = null; const SERIALIZE_TYPE_COLON_SEPARATED = 1; protected $value = array(); protected function setup() { $this->addVariables(array('options' => array(), 'serializeType' => null)); } public function options($Ho5Xg) { $this->addVariables(array('options' => $Ho5Xg)); return $this; } public function serializeType($cBDsM) { $this->addVariables(array('serializeType' => $cBDsM)); return $this; } public function optionModel($loNjC, $WoS8y = 'id', $tYSg7 = 'name') { return $this->options(ModelUtil::valueMap($loNjC, $WoS8y, $tYSg7)); } public function optionType($xc6uF) { return $this->options($xc6uF::getList()); } public function unserializeValue($cBDsM, AbstractField $Ldet7) { if (null === $cBDsM) { return $cBDsM; } switch ($this->getVariable('serializeType')) { case self::SERIALIZE_TYPE_COLON_SEPARATED: return TagUtil::string2Array($cBDsM); default: return ConvertUtil::toArray($cBDsM); } } public function serializeValue($cBDsM, $GMqtO) { switch ($this->getVariable('serializeType')) { case self::SERIALIZE_TYPE_COLON_SEPARATED: return TagUtil::array2String($cBDsM); default: if (is_array($cBDsM)) { foreach ($cBDsM as $lxIPA => $knQyJ) { if (is_numeric($knQyJ) && preg_match('/^\\d+$/', $knQyJ)) { $cBDsM[$lxIPA] = intval($knQyJ); } } } return SerializeUtil::jsonEncode($cBDsM); } } public function prepareInput($cBDsM, $GMqtO) { switch ($this->getVariable('serializeType')) { case self::SERIALIZE_TYPE_COLON_SEPARATED: return TagUtil::string2Array($cBDsM); default: return ConvertUtil::toArray($cBDsM, false); } } public function render() { $this->addCascadeScript(); return parent::render(); } }