* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute' , 'WBUser'); /** * Populate form element's attribute conditional * * * @version 2.0.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_SwitchByGroup extends WBDatasource_FormAttribute { /** * configuration parameter * * - group List of groups, negate with ! in front. Only set user id of user in group or not in groups * - switch named attribute like "disabled". used for value lists * - value switch target value * @var array */ protected $config = array( 'group' => array(), 'switch' => 'disabled', 'value' => 'no', 'table' => '', 'clause' => array(), 'option' => array(), ); /** * get attribute's value * * * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { $user = WBUser::getCurrent(); if (!$this->checkTable($user)) { return $current; } if (!$this->checkTable($user)) { return $current; } // just a single value if (!is_array($current)) { return $this->config['value']; } // value list to change foreach ($current as &$c) { $c[$this->config['switch']] = $this->config['value']; } return $current; } /** * Check User in Group * * @param WBUser * @return bool */ private function checkGroup($user) { if (empty($this->config['group'])) { return false; } foreach ($this->config['group'] as $g) { $rel = true; if (0 == strncmp('!', $g, 1)) { $rel = false; $g = substr($g, 1); } if ($user->isInGroup($g) !== $rel) { return false; } } return true; } /** * Check Table * * @param WBUser * @return bool */ private function checkTable($user) { if (empty($this->config['table'])) { return true; } $table = WBClass::create('WBDatasource_Table'); $clause = $this->config['clause']; if (!is_array($clause)) { $clause = array(); } $clause[] = array( 'field' => $table->getIdentifier('user'), 'value' => $user->getId() ); $cnt = $table->count($this->config['table'], null, null, $clause, $this->config['option']); if (0 < $cnt) { return true; } return false; } }