Index: trunk/core/units/users/users_event_handler.php =================================================================== diff -u -N -r8729 -r8796 --- trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 8729) +++ trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 8796) @@ -1015,8 +1015,6 @@ } } - - /** * Delete users from groups if their membership is expired * @@ -1243,6 +1241,99 @@ $virtual_users = Array (-1, -2); // root, Guest return ($object->GetDBField('Status') == STATUS_ACTIVE) || in_array($object->GetID(), $virtual_users); } + + /** + * Sends approved/declined email event on user status change + * + * @param kEvent $event + */ + function OnAfterItemUpdate(&$event) + { + $object =& $event->getObject(); + /* @var $object UsersItem */ + + if (!$this->Application->IsAdmin() || $object->IsTempTable()) { + return ; + } + + $this->sendStatusChangeEvent($object->GetID(), $object->GetOriginalField('Status'), $object->GetDBField('Status')); + } + + /** + * Stores user's original Status before overwriting with data from temp table + * + * @param kEvent $event + */ + function OnBeforeDeleteFromLive(&$event) + { + $user_status = $this->Application->GetVar('user_status'); + if (!$user_status) { + $user_status = Array (); + } + + $user_id = $event->getEventParam('id'); + if ($user_id > 0) { + $user_status[$user_id] = $this->getUserStatus($user_id); + $this->Application->SetVar('user_status', $user_status); + } + } + + /** + * Sends approved/declined email event on user status change (in temp tables during editing) + * + * @param kEvent $event + */ + function OnAfterCopyToLive(&$event) + { + $temp_id = $event->getEventParam('temp_id'); + if ($temp_id == 0) { + // this is new user create, don't send email events + return ; + } + + $new_status = $this->getUserStatus($temp_id); + $user_status = $this->Application->GetVar('user_status'); + + $this->sendStatusChangeEvent($temp_id, $user_status[$temp_id], $new_status); + } + + /** + * Returns user status (active, pending, disabled) based on ID and temp mode setting + * + * @param int $user_id + * @return int + */ + function getUserStatus($user_id) + { + $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); + $table_name = $this->Application->getUnitOption($this->Prefix, 'TableName'); + + $sql = 'SELECT Status + FROM '.$table_name.' + WHERE '.$id_field.' = '.$user_id; + return $this->Conn->GetOne($sql); + } + + /** + * Sends approved/declined email event on user status change + * + * @param int $user_id + * @param int $prev_status + * @param int $new_status + */ + function sendStatusChangeEvent($user_id, $prev_status, $new_status) + { + $status_events = Array ( + STATUS_ACTIVE => 'USER.APPROVE', + STATUS_DISABLED => 'USER.DENY', + ); + $email_event = isset($status_events[$new_status]) ? $status_events[$new_status] : false; + + if (($prev_status != $new_status) && $email_event) { + $this->Application->EmailEventUser($email_event, $user_id); + $this->Application->EmailEventAdmin($email_event); + } + } } ?> \ No newline at end of file