* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource' , 'WBDatasource_Table' , 'WBDatasource_FormAttribute'); /** * Populate form element's values with user ids * * Select users with clause and or by user group * * @version 0.3.1 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_User extends WBDatasource_FormAttribute { /** * configuration parameter * @var array */ protected $config = array( 'dbconfig' => '', 'labelcolumns' => array('surname','forename'), 'labelformat' => '%s, %s', 'mandator' => 0, '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) { $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(); } $clause = $this->config['clause']; if (!is_array($clause)) { $clause = array(); } $primary = $this->table->getIdentifier(WBDatasource::TABLE_USER); $options = array( 'limit' => 0, 'groupby' => array( 'field' => $primary ) ); if (!empty($this->config['order'])) { $options['order'] = $this->config['order']; } $this->injectGroup($clause, $options); $this->injectMandator($clause, $options); $list = $this->table->get(WBDatasource::TABLE_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 mandator in clause and options * * Allow to select users by mandator * * @param array $clause * @param array $options */ private function injectMandator(&$clause, &$options) { if ("0" == $this->config['mandator'] && empty($this->config['mandator'])) { return; } /** @var WBMandator */ $man = WBClass::create('WBMandator', array('table' => $this->table)); if (!$man->isEnabled()) { return; } $primary = $this->table->getIdentifier(WBDatasource::TABLE_MANDATOR); if ('{' . strtoupper($primary) . '}' == $this->config['mandator']) { return; } if (!isset($options['join'])) { $options['join'] = array(); } if (0 != $this->config['mandator']) { $options['join'][] = array( 'table' => WBDatasource::TABLE_MANDATORUSER, 'using' => $this->table->getIdentifier(WBDatasource::TABLE_USER) ); $clause[] = array( 'table' => WBDatasource::TABLE_MANDATORUSER, 'field' => $primary, 'value' => $this->config['mandator'] ); } if (empty($this->config['group'])) { return; } $clause[] = array( 'table' => WBDatasource::TABLE_USERGROUP, 'field' => $primary, 'value' => $this->config['mandator'] ); } /** * 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])) { $this->config['group'] = array(); return; } if (!isset($options['join'])) { $options['join'] = array(); } // add join to options $options['join'][] = array( 'table' => WBDatasource::TABLE_USERGROUP, 'using' => $this->table->getIdentifier('user') ); $options['join'][] = array( 'lefttable' => WBDatasource::TABLE_USERGROUP, 'table' => WBDatasource::TABLE_GROUP, 'onleft' => $this->table->getIdentifier('group'), 'onright' => $this->table->getIdentifier('group') ); $clause[] = array( 'table' => WBDatasource::TABLE_GROUP, 'field' => 'groupname', 'relation' => 'in', 'value' => $this->config['group'] ); } }