* @license PHP License * @package WB * @subpackage db */ /** * Populate form element's attribute * * Generic class to populate element's attribute. The template method is * implemented here, the child class is required to actually receive proper * attribute values. * * @version 0.2.1 * @package WB * @subpackage db */ class WBDatasource_FormAttribute extends WBStdClass { /** * configuration values * @var array */ protected $config = array( 'ajaxupdate' => '', ); /** * form element to set values to * @var patForms_Element */ protected $element; /** * session storage container * @var patSession_Storage */ protected $sess; /** * set form element * * Tell which element to manipulate * * @param patForms_Element $element */ private function setElement($element) { $this->element = $element; } /** * set configuration * * @param array $config */ private function setConfig($config) { // usually we are going to populate the values attriubte if (!isset($this->config['attribute'])) { $this->config['attribute'] = 'values'; } $this->config = array_merge($this->config, $config); // generic config if (!isset($this->config['ajaxupdate'])) { $this->config['ajaxupdate'] = ''; } } /** * populate elements attribute(s) * * Template method that does the whole thing. * * @param patForms_Element $element * @param array $config */ final public function populate($element, $config) { $this->setElement($element); $this->setConfig($config); // load JavaScript classes if (!empty($this->config['ajaxupdate'])) { WBHtml_JS::add('WB/Ajax'); WBHtml_JS::add('WB/Ajax/Form'); WBHtml_JS::add('WB/Ajax/Form/Element'); } $value = $this->element->getAttribute($this->config['attribute']); $value = $this->getAttributeValue($value); $this->element->setAttribute($this->config['attribute'], $value); $name = 'form:element:update:config:' . $this->element->getAttribute('name'); $this->sess = WBClass::create('patSession'); if (empty($this->config['ajaxupdate'])) { $this->sess->clear($name); return; } $this->sess->set($name, $this->config); } /** * get attribute's value * * Implement this method in actual child classes * * @param mixed $current actual attribute value * @return mixed */ protected function getAttributeValue($current) { return ''; } /** * custom compare function * * Comparision function for value lists with label * * @see usort() * @param array $a * @param array $b * @return int -1, 0 or 1 */ protected function compareValuesByLabel($a, $b) { if ($a['label'] == $b['label']) { return 0; } if ($a['label'] > $b['label']) { return 1; } return -1; } }