* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute' , 'WBException_Call' ); /** * Populate form element's values from Dictionary * * Use dictionary lists to fill values * * @version 0.1.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_Dictionary extends WBDatasource_FormAttribute { /** * configuration parameter * * - dict: name of dictionary * - map: map data to option attributes: * map => array( * 'class' => array('format' => '%s (%s)', 'column' => array('foo', 'bar')) * ) * @var array */ protected $config = array( 'dict' => 'Country', 'map' => array() ); /** * dictionary * @var WBDictionary */ protected $dict; /** * get attribute's value * * Use dicktionary to fetch list of values * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { if (!is_array($current)) { $current = array(); } $this->dict = WBClass::create('WBDictionary_' . $this->config['dict']); try { $list = $this->dict->getExtendedList(); } catch(WBException_Call $e) { return $current; } if (empty($list)) { return $current; } // append list foreach ($list as $l) { $tmp = array( 'value' => $l['id'], 'label' => $l['word'], ); $this->applyMap($l, $tmp); $current[] = $tmp; } return $current; } private function applyMap($data, &$target) { if (empty($this->config['map'])) { return; } foreach ($this->config['map'] as $att => $config) { if (is_scalar($config)) { $config = array( 'format' => '%s', 'column' => array($config) ); } $col = array(); foreach ($config['column'] as $c) { if (empty($c)) { continue; } if (empty($data[$c])) { $col[] = ''; continue; } $col[] = $data[$c]; } if (empty($col)) { continue; } $target[$att] = vsprintf($config['format'], $col); } } }