Index: branches/5.2.x/units/files/files_event_handler.php
===================================================================
diff -u -r14258 -r14594
--- branches/5.2.x/units/files/files_event_handler.php	(.../files_event_handler.php)	(revision 14258)
+++ branches/5.2.x/units/files/files_event_handler.php	(.../files_event_handler.php)	(revision 14594)
@@ -1,6 +1,6 @@
 <?php
 /**
-* @version	$Id: files_event_handler.php 14258 2011-03-16 21:43:52Z alex $
+* @version	$Id: files_event_handler.php 14594 2011-09-29 15:45:36Z alex $
 * @package	In-Commerce
 * @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
 * @license	Commercial License
@@ -30,8 +30,15 @@
 		return parent::getMainSpecial($event);
 	}
 
-
-	function SetCustomQuery(&$event)
+	/**
+	 * Apply any custom changes to list's sql query
+	 *
+	 * @param kEvent $event
+	 * @return void
+	 * @access protected
+	 * @see kDBEventHandler::OnListBuild()
+	 */
+	protected function SetCustomQuery(&$event)
 	{
 		$object =& $event->getObject();
 		switch ($event->Special)
@@ -42,56 +49,77 @@
 		}
 	}
 
-	function prepareObject(&$object, &$event)
+	/**
+	 * Occurs before updating item
+	 *
+	 * @param kEvent $event
+	 * @return void
+	 * @access protected
+	 */
+	protected function OnBeforeItemUpdate(&$event)
 	{
-		if($this->Application->GetVar('file_event') == 'OnNew' || $this->Application->GetVar('file_event') == 'OnCreate')
-		{
-			$options = $object->getFieldOptions('RealPath');
-			$options['required'] = 1;
-			$object->setFieldOptions('RealPath', $options);
+		parent::OnBeforeItemUpdate($event);
+
+		$this->itemChanged($event);
+	}
+
+	/**
+	 * Occurs before creating item
+	 *
+	 * @param kEvent $event
+	 * @return void
+	 * @access protected
+	 */
+	protected function OnBeforeItemCreate(&$event)
+	{
+		parent::OnBeforeItemCreate($event);
+
+		$this->itemChanged($event);
+
+		$object =& $event->getObject();
+		/* @var $object kDBItem */
+
+		$parent_info = $object->getLinkedInfo($event->Special);
+		
+		$sql = 'SELECT FileId
+				FROM ' . $object->TableName . '
+				WHERE IsPrimary = 1 AND ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
+		$file_id = $this->Conn->GetOne($sql);
+
+		if ( !$file_id ) {
+			$object->SetDBField('IsPrimary', 1);
+			$object->SetDBField('Status', 1);
 		}
+
+		$object->SetDBField('AddedById', $this->Application->RecallVar('user_id'));
 	}
 
 	/**
-	 * Enter description here...
+	 * Occurs before item is changed
 	 *
 	 * @param kEvent $event
 	 */
-	function OnBeforeItemUpdate(&$event)
+	function itemChanged(&$event)
 	{
 		$object =& $event->getObject();
+		/* @var $object kDBItem */
 
-		if($object->GetDBField('IsPrimary'))
-		{
+		if ( $object->GetDBField('IsPrimary') ) {
 			$parent_info = $object->getLinkedInfo($event->Special);
-			$sql = 'UPDATE '.$object->TableName.'
+
+			$sql = 'UPDATE ' . $object->TableName . '
 					SET IsPrimary = 0
-					WHERE '.$parent_info['ForeignKey'].' = '.$parent_info['ParentId'];
+					WHERE ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
 			$this->Conn->Query($sql);
+
 			$object->SetDBField('Status', 1);
 		}
 
-		if($object->GetDBField('Name') == '')
-		{
+		if ( $object->GetDBField('Name') == '' ) {
 			$object->SetDBField('Name', $object->GetDBField('FilePath'));
 		}
 	}
 
-	function OnBeforeItemCreate(&$event)
-	{
-		$this->OnBeforeItemUpdate($event);
-
-		$object =& $event->getObject();
-		$parent_info = $object->getLinkedInfo($event->Special);
-		$sql = 'SELECT FileId FROM '.$object->TableName.' WHERE IsPrimary = 1 AND '.$parent_info['ForeignKey'].' = '.$parent_info['ParentId'];
-		if( !$this->Conn->GetOne($sql) )
-		{
-			$object->SetDBField('IsPrimary', 1);
-			$object->SetDBField('Status', 1);
-		}
-		$object->SetDBField('AddedById', $this->Application->RecallVar('user_id'));
-	}
-
 	/**
 	 * Enter description here...
 	 *
@@ -110,44 +138,45 @@
 	}
 
 	/**
-	 * Deletes all selected items.
-	 * Automatically recurse into sub-items using temp handler, and deletes sub-items
-	 * by calling its Delete method if sub-item has AutoDelete set to true in its config file
+	 * Don't allow to delete primary product file, when there are no more files
 	 *
 	 * @param kEvent $event
+	 * @param string $type
+	 * @return void
+	 * @access protected
 	 */
-	function OnMassDelete(&$event)
+	protected function customProcessing(&$event, $type)
 	{
-		$event->status=kEvent::erSUCCESS;
+		if ( $event->Name == 'OnMassDelete' && $type == 'before' ) {
+			$ids = $event->getEventParam('ids');
 
-		$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+			$object =& $event->getObject();
+			/* @var $object kDBItem */
 
-		$ids = $this->StoreSelectedIDs($event);
+			$parent_info = $object->getLinkedInfo($event->Special);
 
-		$event->setEventParam('ids', $ids);
-		$this->customProcessing($event, 'before');
-		$ids = $event->getEventParam('ids');
+			$sql = 'SELECT FileId
+					FROM ' . $object->TableName . '
+					WHERE IsPrimary = 1 AND ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
+			$primary_file_id = $this->Conn->GetOne($sql);
 
-		$object =& $event->getObject();
-		$parent_info = $object->getLinkedInfo($event->Special);
+			if ( $primary_file_id ) {
+				$file_id_index = array_search($primary_file_id, $ids);
 
-		$sql = 'SELECT FileId FROM '.$object->TableName.' WHERE IsPrimary = 1 AND '.$parent_info['ForeignKey'].' = '.$parent_info['ParentId'];
-		$primary = $this->Conn->GetOne($sql);
-		if( $primary && ($key = array_search($primary, $ids)) )
-		{
-			$sql = 'SELECT FileId FROM '.$object->TableName.' WHERE IsPrimary = 0 AND '.$parent_info['ForeignKey'].' = '.$parent_info['ParentId'];
-			$res = $this->Conn->Query($sql);
-			if($res)
-			{
-				unset($ids[$key]);
+				if ( $file_id_index ) {
+					// allow deleting of primary product file, when there is another file to make primary
+					$sql = 'SELECT COUNT(*)
+							FROM ' . $object->TableName . '
+							WHERE IsPrimary = 0 AND ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
+					$non_primary_file_count = $this->Conn->GetOne($sql);
+
+					if ( $non_primary_file_count ) {
+						unset($ids[$file_id_index]);
+					}
+				}
 			}
-		}
 
-		if($ids)
-		{
-			$temp->DeleteItems($event->Prefix, $event->Special, $ids);
+			$event->setEventParam('ids', $ids);
 		}
-		$this->clearSelectedIDs($event);
 	}
-
 }
\ No newline at end of file