* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_FormAttribute' , 'WBUser'); /** * Populate form element's value in conditional to user group * * * @version 1.0.0 * @package WB * @subpackage db */ class WBDatasource_FormAttribute_ValueByGroup extends WBDatasource_FormAttribute { /** * configuration parameter * * - require: "all" (default) or "any" * - group List of groups, negate with ! in front. Only set user id of user in group or not in groups * - value array or string * @var array */ protected $config = array( 'require' => 'all', 'group' => array(), 'value' => null ); /** * get attribute's value * * * * @param mixed $current actual attribute value * @return array */ protected function getAttributeValue($current) { $user = WBUser::getCurrent(); if (empty($this->config['group'])) { return $this->config['value']; } foreach ($this->config['group'] as $g) { $rel = true; if (0 == strncmp('!', $g, 1)) { $rel = false; $g = substr($g, 1); } $ret = $this->config['value']; $res = ($user->isInGroup($g) !== $rel); switch ($this->config['require']) { case 'any': if ($rel) { return $this->config['value']; } $ret = $current; break; case 'all': default: if (!$res) { return $current; } break; } } return $ret; } }