Add "groupUpdateOperators" event

This commit is contained in:
Dmitriy Simushev 2014-10-31 09:59:44 +00:00
parent 07fc4a3666
commit f10079497c
3 changed files with 60 additions and 0 deletions

View File

@ -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.
*

View File

@ -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);
}
}
/**

View File

@ -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);
}
}
/**