/** * Builds a page with form for edit operator's groups. * * @param Request $request Incoming request. * @return string Rendered page content. * @throws NotFoundException If the operator with specified ID is not found * in the system. */ public function showFormAction(Request $request) { $operator = $this->getOperator(); $operator_in_isolation = in_isolation($operator); $op_id = $request->attributes->getInt('operator_id'); // Check if the target user exists $op = operator_by_id($op_id); if (!$op) { throw new NotFoundException('The operator is not found.'); } $page = array('opid' => $op_id, 'errors' => array()); $groups = $operator_in_isolation ? get_groups_for_operator($operator) : get_all_groups(); $can_modify = is_capable(CAN_ADMINISTRATE, $operator); $page['currentop'] = $op ? get_operator_name($op) . ' (' . $op['vclogin'] . ')' : getlocal('-not found-'); $page['canmodify'] = $can_modify ? '1' : ''; // Get IDs of groups the operator belongs to. $checked_groups = array(); if ($op) { $checked_groups = get_operator_group_ids($op_id); } // Get all available groups $page['groups'] = array(); foreach ($groups as $group) { $group['vclocalname'] = $group['vclocalname']; $group['vclocaldescription'] = $group['vclocaldescription']; $group['checked'] = in_array($group['groupid'], $checked_groups); $page['groups'][] = $group; } $page['stored'] = $request->query->has('stored'); $page['title'] = getlocal('Operator groups'); $page['menuid'] = $operator['operatorid'] == $op_id ? 'profile' : 'operators'; $page = array_merge($page, prepare_menu($operator)); $page['tabs'] = $this->buildTabs($request); return $this->render('operator_groups', $page); }
/** * Updates set of groups the operator belongs to. * * Triggers {@link \Mibew\EventDispatcher\Events::GROUP_UPDATE_OPERATORS} event. * * @param int $operator_id ID of the operator. * @param array $new_value List of operator's groups IDs. */ function update_operator_groups($operator_id, $new_value) { // Get difference of groups the operator belongs to before and after the // update. $original_groups = get_operator_group_ids($operator_id); $groups_union = array_unique(array_merge($original_groups, $new_value)); $groups_intersect = array_intersect($original_groups, $new_value); $updated_groups = array_diff($groups_union, $groups_intersect); // Get members of all updated groups. It will be used to trigger the // "update" event later. $original_relations = array(); foreach ($updated_groups as $group_id) { $original_relations[$group_id] = get_group_members($group_id); } // Update group members $db = Database::getInstance(); $db->query( "DELETE FROM {operatortoopgroup} WHERE operatorid = ?", array($operator_id) ); foreach ($new_value as $group_id) { $db->query( "INSERT INTO {operatortoopgroup} (groupid, operatorid) VALUES (?,?)", array($group_id, $operator_id) ); } // Trigger the "update" event foreach ($original_relations as $group_id => $operators) { $args = array( 'group' => group_by_id($group_id), 'original_operators' => $operators, 'operators' => get_group_members($group_id), ); EventDispatcher::getInstance()->triggerEvent(Events::GROUP_UPDATE_OPERATORS, $args); } }