* @package WB * @subpackage formAttribute */ WBClass::load('WBDatasource_FormAttribute'); /** * Populate form element's values from any table * * Use table data to populate values. Support for attributes: * - value * - default * - values * Also support for scalar labels etc * * @version 0.5.0 * @package WB * @subpackage formAttribute */ class WBDatasource_FormAttribute_Table extends WBDatasource_FormAttribute { /** * configuration parameter * @var array */ protected $config = array( 'dbconfig' => '', 'field' => '__primary', 'table' => 'group', 'labelcolumns' => array('groupname'), 'labelformat' => '%s', 'clause' => array(), 'order' => array(), 'options' => array(), 'scalar' => 0, 'id' => 0 ); /** * table * @var WBDatasource_Table */ protected $table; /** * get attribute's value * * Use dicktionary to fetch list of values * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { $parameter = array(); if (!empty($this->config['dbconfig'])) { $parameter['dbconfig'] = $this->config['dbconfig']; } $this->table = WBClass::create('WBDatasource_Table', $parameter); if (!is_array($current)) { $current = array(); } if (!is_array($this->config['clause'])) { $this->config['clause'] = array(); } $options = array_merge( array('limit' => 0), $this->config['options'] ); if (!empty($this->config['order'])) { $options['order'] = $this->config['order']; } $id = null; if (!empty($this->config['id'])) { $id = $this->config['id']; } $list = $this->table->get($this->config['table'], $id, null, $this->config['clause'], $options); if (!empty($this->config['scalar'])) { return $this->getLabel($list[0]); } if (empty($list)) { return $current; } // append list $field = $this->config['field']; if ('__PRIMARY' == strtoupper($field)) { $field = $this->table->getIdentifier($this->config['table']); } foreach ($list as $v => $l) { switch ($this->config['attribute']) { // simple list of ids case 'default': case 'value': $current[] = $l[$field]; break; // value / label list default: case 'values': $current[] = array( 'value' => $l[$field], 'label' => $this->getLabel($l) ); break; } } return $current; } private function getLabel($data) { $cols = array(); foreach ($this->config['labelcolumns'] as $lc) { $cols[] = $data[$lc]; } return vsprintf($this->config['labelformat'], $cols); } }