* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute'); /** * Populate form element's values from any table * * Use table data to populate values * * @version 0.3.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_Tree extends WBDatasource_FormAttribute { /** * configuration parameter * @var array */ protected $config = array( 'table' => 'vfsdir', 'labelcolumns' => array('name'), 'labelformat' => ' %s', 'levelclass' => 'level level%d', 'branch' => '', 'maxdepth' => -1, 'clause' => array() ); /** * table * @var WBDatasource_Tree */ protected $tree; /** * current item * @var array */ protected $list = array(); /** * get attribute's value * * Use dicktionary to fetch list of values * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { $params = array( 'treetable' => $this->config['table'] ); if (!is_array($this->config['clause'])) { $this->config['clause'] = array(); } $this->tree = WBClass::create('WBDatasource_Tree', $params); if (!is_array($current)) { $current = array(); } if (!empty($this->config['clause']) && is_array($this->config['clause'])) { $this->tree->setClause($this->config['clause']); } $this->list = $current; $this->dig($this->config['branch'], 0); return $this->list; } /** * Dig up children * * @param string $branch id * @param int current level */ private function dig($branch, $level) { if (0 < $this->config['maxdepth'] && $level >= $this->config['maxdepth']) { return; } $list = $this->tree->getChildren($branch); $primary = $this->tree->getIdentifier(); foreach ($list as $v => $l) { $cols = array(); foreach ($this->config['labelcolumns'] as $lc) { $cols[] = $l[$lc]; } $this->list[] = array( 'value' => $l[$primary], 'label' => vsprintf($this->config['labelformat'], $cols), 'class' => sprintf($this->config['levelclass'], $level) ); // dig deeper $this->dig($l[$primary], ($level + 1)); } } }