Index: branches/5.2.x/core/kernel/db/db_event_handler.php =================================================================== diff -u -N -r15252 -r15268 --- branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 15252) +++ branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 15268) @@ -1,6 +1,6 @@ getObject(); /* @var $object kDBItem */ + // 1. delete direct subscriptions to item, that was deleted + $this->_deleteSubscriptions($event->Prefix, 'ItemId', $object->GetID()); + + $sub_items = $this->Application->getUnitOption($event->Prefix, 'SubItems', Array ()); + /* @var $sub_items Array */ + + // 2. delete this item sub-items subscriptions, that reference item, that was deleted + foreach ($sub_items as $sub_prefix) { + $this->_deleteSubscriptions($sub_prefix, 'ParentItemId', $object->GetID()); + } + } + + /** + * Deletes all subscriptions, associated with given item + * + * @param string $prefix + * @param string $field + * @param int $value + * @return void + * @access protected + */ + protected function _deleteSubscriptions($prefix, $field, $value) + { $sql = 'SELECT EventId FROM ' . TABLE_PREFIX . 'EmailEvents - WHERE BindToSystemEvent REGEXP "' . $this->Conn->escape($event->Prefix) . '[.]{0,1}([^:]*):(.*)"'; + WHERE BindToSystemEvent REGEXP "' . $this->Conn->escape($prefix) . '(\\\\.[^:]*:.*|:.*)"'; $email_event_ids = $this->Conn->GetCol($sql); - if ( $email_event_ids ) { - // e-mail events, connected to that unit prefix are found - $sql = 'DELETE FROM ' . TABLE_PREFIX . 'EmailEventSubscribers - WHERE ItemId = ' . $object->GetID() . ' AND EmailEventId IN (' . implode(',', $email_event_ids) . ')'; - $this->Conn->Query($sql); + if ( !$email_event_ids ) { + return; } + + // e-mail events, connected to that unit prefix are found + $sql = 'SELECT SubscriptionId + FROM ' . TABLE_PREFIX . 'SystemEventSubscriptions + WHERE ' . $field . ' = ' . $value . ' AND EmailEventId IN (' . implode(',', $email_event_ids) . ')'; + $ids = $this->Conn->GetCol($sql); + + if ( !$ids ) { + return; + } + + $temp_handler = $this->Application->recallObject('system-event-subscription_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + + $temp_handler->DeleteItems('system-event-subscription', '', $ids); } /**