From f10079497c880daaf9875f8fb224a690fbfa56e2 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 31 Oct 2014 09:59:44 +0000 Subject: [PATCH] Add "groupUpdateOperators" event --- .../classes/Mibew/EventDispatcher/Events.php | 12 +++++++ src/mibew/libs/groups.php | 15 +++++++++ src/mibew/libs/operator.php | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php index 93b618d8..fd1853e6 100644 --- a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php +++ b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php @@ -90,6 +90,18 @@ final class Events */ const GROUP_DELETE = 'groupDelete'; + /** + * Group's operators set is updated. + * + * This event is triggered after a set of operators related with a group has + * been changed. An associative array with the following items is passed to + * the event handlers: + * - "group": group's array. + * - "original_operators": array, list of operators IDs before the update. + * - "operators": array, list of operators IDs after the update. + */ + const GROUP_UPDATE_OPERATORS = 'groupUpdateOperators'; + /** * An invitation is created. * diff --git a/src/mibew/libs/groups.php b/src/mibew/libs/groups.php index 49512356..dd1ac088 100644 --- a/src/mibew/libs/groups.php +++ b/src/mibew/libs/groups.php @@ -448,11 +448,17 @@ function get_group_members($group_id) /** * Update operators of specific group * + * Triggers {@link \Mibew\EventDispatcher\Events::GROUP_UPDATE_OPERATORS} event. + * * @param int $group_id ID of the group. * @param array $new_value list of all operators of specified group. */ function update_group_members($group_id, $new_value) { + // Get the unchanged set of operators related with the group to trigger + // "update" event later + $original_operators = get_group_members($group_id); + $db = Database::getInstance(); $db->query( "DELETE FROM {operatortoopgroup} WHERE groupid = ?", @@ -465,6 +471,15 @@ function update_group_members($group_id, $new_value) array($group_id, $operator_id) ); } + if ($original_operators != $new_value) { + // Trigger the "update" event only if operators set is changed. + $args = array( + 'group' => group_by_id($group_id), + 'original_operators' => $original_operators, + 'operators' => $new_value, + ); + EventDispatcher::getInstance()->triggerEvent(Events::GROUP_UPDATE_OPERATORS, $args); + } } /** diff --git a/src/mibew/libs/operator.php b/src/mibew/libs/operator.php index 2d006c03..7a8b61ee 100644 --- a/src/mibew/libs/operator.php +++ b/src/mibew/libs/operator.php @@ -762,8 +762,31 @@ function check_password_hash($login, $password, $hash) } } +/** + * 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 = ?", @@ -776,6 +799,16 @@ function update_operator_groups($operator_id, $new_value) 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); + } } /**