* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute'); /** * Populate form element's values with user ids * * Select users with clause and or by user group * * @version 0.2.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_User extends WBDatasource_FormAttribute { /** * configuration parameter * @var array */ protected $config = array( 'labelcolumns' => array('surname','forename'), 'labelformat' => '%s, %s', 'group' => array(), 'clause' => array( array( 'field' => 'approved', 'value' => 1 ) ), 'order' => array( array( 'field' => 'surname', 'asc' => 1 ), array( 'field' => 'forename', 'asc' => 1 ) ) ); /** * 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) { $this->table = WBClass::create('WBDatasource_Table'); if (!is_array($current)) { $current = array(); } $clause = $this->config['clause']; if (!is_array($clause)) { $clause = array(); } $primary = $this->table->getIdentifier('user'); $options = array( 'limit' => 0, 'groupby' => array( 'field' => $primary ) ); if (!empty($this->config['order'])) { $options['order'] = $this->config['order']; } $this->injectGroup($clause, $options); $list = $this->table->get('user', null, null, $clause, $options); if (empty($list)) { return $current; } // append list foreach ($list as $v => $l) { $cols = array(); foreach ($this->config['labelcolumns'] as $lc) { $cols[] = $l[$lc]; } $label = vsprintf($this->config['labelformat'], $cols); $tmp = trim(implode('', $cols)); if (empty($tmp)) { $label = sprintf('%s@%s', $l['emaillocal'], $l['emaildomain']); } $current[] = array( 'value' => $l[$primary], 'label' => $label ); } return $current; } /** * Inject group attributes in clause and options * * Allow to select users by group * * @param array $clause * @param array $options */ private function injectGroup(&$clause, &$options) { if (!is_array($this->config['group'])) { $this->config['group'] = trim($this->config['group']); } if (empty($this->config['group']) || empty($this->config['group'][0])) { return; } // add join to options $options['join'] = array(); $options['join'][] = array( 'table' => 'usergroup', 'using' => $this->table->getIdentifier('user') ); $options['join'][] = array( 'lefttable' => 'usergroup', 'table' => 'group', 'onleft' => $this->table->getIdentifier('group'), 'onright' => $this->table->getIdentifier('group') ); $clause[] = array( 'table' => 'group', 'field' => 'groupname', 'relation' => 'in', 'value' => $this->config['group'] ); } }