Index: branches/5.2.x/units/links/links_event_handler.php
===================================================================
diff -u -r14850 -r14897
--- branches/5.2.x/units/links/links_event_handler.php	(.../links_event_handler.php)	(revision 14850)
+++ branches/5.2.x/units/links/links_event_handler.php	(.../links_event_handler.php)	(revision 14897)
@@ -1,6 +1,6 @@
 <?php
 /**
-* @version	$Id: links_event_handler.php 14850 2011-12-08 12:29:35Z alex $
+* @version	$Id: links_event_handler.php 14897 2011-12-21 15:21:13Z alex $
 * @package	In-Link
 * @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
 * @license      GNU/GPL
@@ -48,35 +48,39 @@
 			$object =& $event->getObject();
 			/* @var $object kDBList */
 
-			if (!$this->Application->isAdminUser) {
-				$object->addFilter('expire_filter', '(Expire > '.adodb_mktime().' OR Expire IS NULL)');
+			if ( !$this->Application->isAdminUser ) {
+				$object->addFilter('expire_filter', '(Expire > ' . adodb_mktime() . ' OR Expire IS NULL)');
 			}
 
-			if (substr($event->Special, 0, 10) == 'duplicates') {
+			if ( substr($event->Special, 0, 10) == 'duplicates' ) {
 				$object->removeFilter('category_filter');
 
 				$link_helper =& $this->Application->recallObject('LinkHelper');
 				/* @var $link_helper LinkHelper */
 
-				$grouping = $link_helper->getGrouping( $event->getPrefixSpecial() );
+				$grouping = $link_helper->getGrouping($event->getPrefixSpecial());
+
 				switch ($event->Special) {
 					case 'duplicates':
 						foreach ($grouping as $group_field) {
-							$object->AddGroupByField($object->TableName.'.'.$group_field);
+							$object->AddGroupByField($object->TableName . '.' . $group_field);
 						}
+
 						$object->addFilter('has_dupes_filter', 'DupeCount > 1', kDBList::AGGREGATE_FILTER);
 						break;
 
 					case 'duplicates-sub':
-						$main_object =& $this->Application->recallObject($event->Prefix.'.duplicates');
+						$main_object =& $this->Application->recallObject($event->Prefix . '.duplicates');
+						/* @var $main_object kDBItem */
+
 						foreach ($grouping as $field_index => $group_field) {
-							$object->addFilter('dupe_filter_'.$field_index, '%1$s.`'.$group_field.'` = '.$this->Conn->qstr($main_object->GetDBField($group_field)) );
+							$object->addFilter('dupe_filter_' . $field_index, '%1$s.`' . $group_field . '` = ' . $this->Conn->qstr($main_object->GetDBField($group_field)));
 						}
 						break;
 				}
-				$object->addFilter('primary_filter', TABLE_PREFIX.'CategoryItems.PrimaryCat = 1');
-			}
 
+				$object->addFilter('primary_filter', TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1');
+			}
 		}
 
 		/**
@@ -99,32 +103,33 @@
 			$link_helper =& $this->Application->recallObject('LinkHelper');
 			/* @var $link_helper LinkHelper */
 
-			$grouping = $link_helper->getGrouping( $event->getPrefixSpecial() );
+			$grouping = $link_helper->getGrouping($event->getPrefixSpecial());
 
 			$ids = $this->StoreSelectedIDs($event);
-			if (!$ids) {
-				return ;
+			if ( !$ids ) {
+				return;
 			}
 
 			// check, that user has not selected multiple links from same group
-			$primary_links = Array();
+			$primary_links = Array ();
 
 			$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
 			$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
 			$sql = 'SELECT *
-					FROM '.$table_name.'
-					WHERE '.$id_field.' IN ('.implode(',', $ids).')';
+					FROM ' . $table_name . '
+					WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ')';
 			$links = $this->Conn->Query($sql, $id_field);
 
 			$groping_error = false;
-			foreach ($links as $link_id => $link_data) {
+
+			foreach ($links as $link_data) {
 				$group_key = '';
 				foreach ($grouping as $grouping_field) {
-					$group_key .= 'main_table.`'.$grouping_field.'` = '.$this->Conn->qstr($link_data[$grouping_field]).' AND ';
+					$group_key .= 'main_table.`' . $grouping_field . '` = ' . $this->Conn->qstr($link_data[$grouping_field]) . ' AND ';
 				}
 				$group_key = substr($group_key, 0, -5);
 
-				if (isset($primary_links[$group_key])) {
+				if ( isset($primary_links[$group_key]) ) {
 					$groping_error = true;
 					break;
 				}
@@ -133,48 +138,48 @@
 				}
 			}
 
-			if (!$groping_error) {
-				$temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+			if ( !$groping_error ) {
+				$temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler');
 				/* @var $temp_handler kTempTablesHandler */
 
-				$categories_sql = 'SELECT main_table.ResourceId, ci.CategoryId, main_table.'.$id_field.'
-						FROM '.$table_name.' main_table
-						LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON main_table.ResourceId = ci.ItemResourceId
-						WHERE %s';
+				$categories_sql = '	SELECT main_table.ResourceId, ci.CategoryId, main_table.' . $id_field . '
+									FROM ' . $table_name . ' main_table
+									LEFT JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON main_table.ResourceId = ci.ItemResourceId
+									WHERE %s';
 
 				foreach ($primary_links as $group_key => $primary_resource_id) {
-					$categories = Array();
-					$group_links = Array();
+					$categories = Array ();
+					$group_links = Array ();
 					$group_categories = $this->Conn->Query(sprintf($categories_sql, $group_key));
+
 					foreach ($group_categories as $category_data) {
-						$group_links[ $category_data['ResourceId'] ] = $category_data[$id_field];
+						$group_links[$category_data['ResourceId']] = $category_data[$id_field];
 						$categories[$category_data['ResourceId'] == $primary_resource_id ? 'remove' : 'add'][] = $category_data['CategoryId'];
 					}
+
 					unset($group_links[$primary_resource_id]);
-					$categories = array_unique( array_diff($categories['add'], $categories['remove']) );
-					if ($categories) {
+					$categories = array_unique(array_diff($categories['add'], $categories['remove']));
+
+					if ( $categories ) {
 						// add link to other link categories
 						$values_sql = '';
 						foreach ($categories as $category_id) {
-							$values_sql .= '('.$category_id.','.$primary_resource_id.',0),';
+							$values_sql .= '(' . $category_id . ',' . $primary_resource_id . ',0),';
 						}
 						$values_sql = substr($values_sql, 0, -1);
-						$insert_sql = 'INSERT INTO '.TABLE_PREFIX.'CategoryItems (CategoryId,ItemResourceId,PrimaryCat) VALUES '.$values_sql;
+						$insert_sql = 'INSERT INTO ' . TABLE_PREFIX . 'CategoryItems (CategoryId,ItemResourceId,PrimaryCat) VALUES ' . $values_sql;
 						$this->Conn->Query($insert_sql);
 					}
 
 					// delete all links from group except primary
 					$temp_handler->DeleteItems($event->Prefix, $event->Special, array_values($group_links));
 				}
-
-
 			}
 			else {
 				$event->status = kEvent::erFAIL;
 				$event->redirect = false;
 				$this->Application->SetVar($event->getPrefixSpecial().'_error', 1);
 			}
-
 		}
 
 		/**
@@ -525,4 +530,68 @@
 				$this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones);
 			}
 		}
+
+		/**
+		 * Occurs before original item of item in pending editing got deleted (for hooking only)
+		 *
+		 * @param kEvent $event
+		 * @return void
+		 * @access protected
+		 */
+		protected function OnBeforeDeleteOriginal(kEvent &$event)
+		{
+			parent::OnBeforeDeleteOriginal($event);
+
+			$object =& $event->getObject();
+			/* @var $object kDBItem */
+
+			$link_id = $event->getEventParam('original_id');
+			$new_resource_id = $object->GetDBField('ResourceId');
+
+			$sql = 'SELECT ResourceId
+					FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+					WHERE ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . '=' . $link_id;
+			$old_resource_id = $this->Conn->GetOne($sql);
+
+			$this->Application->SetVar('original_resource_id', $old_resource_id);
+			$this->changeResourceId('rel', 'TargetId', $old_resource_id, $new_resource_id);
+		}
+
+		/**
+		 * Occurs after original item of item in pending editing got deleted
+		 *
+		 * @param kEvent $event
+		 * @return void
+		 * @access protected
+		 */
+		protected function OnAfterDeleteOriginal(kEvent &$event)
+		{
+			parent::OnAfterDeleteOriginal($event);
+
+			$object =& $event->getObject();
+			/* @var $object kDBItem */
+
+			$old_resource_id = $this->Application->GetVar('original_resource_id');
+			$new_resource_id = $object->GetDBField('ResourceId');
+
+			$this->changeResourceId('ls', 'ItemResourceId', $old_resource_id, $new_resource_id);
+		}
+
+		/**
+		 * Changes item resource id in one field
+		 *
+		 * @param string $prefix
+		 * @param string $field
+		 * @param string $old_resource_id
+		 * @param string $new_resource_id
+		 * @return void
+		 * @access protected
+		 */
+		protected function changeResourceId($prefix, $field, $old_resource_id, $new_resource_id)
+		{
+			$fields_hash = Array ($field => $new_resource_id);
+			$table_name = $this->Application->getUnitOption($prefix, 'TableName');
+
+			$this->Conn->doUpdate($fields_hash, $table_name, $field . ' = ' . $old_resource_id);
+		}
 	}
\ No newline at end of file