Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Thu, Jul 10, 9:12 PM

in-portal

Index: trunk/kernel/units/general/cat_dbitem.php
===================================================================
--- trunk/kernel/units/general/cat_dbitem.php (revision 2040)
+++ trunk/kernel/units/general/cat_dbitem.php (revision 2041)
@@ -1,113 +1,144 @@
<?php
class kCatDBItem extends kDBItem {
function Create()
{
if (!$this->Validate()) return false;
$this->SetDBField('ResourceId', $this->Application->NextResourceId());
$this->SetDBField('Modified', mktime());
$ret = parent::Create();
if($ret)
{
if ( kTempTablesHandler::IsTempTable($this->TableName) ) {
$table = kTempTablesHandler::GetTempName(TABLE_PREFIX.'CategoryItems');
}
else {
$table = TABLE_PREFIX.'CategoryItems';
}
$cat_id = $this->Application->GetVar('m_cat_id');
$query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat)
VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1)';
$this->Conn->Query($query);
}
return $ret;
}
function Update($id=null)
{
$this->VirtualFields['ResourceId'] = true;
$this->SetDBField('Modified', mktime());
return parent::Update($id);
}
function Copy($cat_id=null)
{
if (!isset($cat_id)) $cat_id = $this->Application->GetVar('m_cat_id');
$this->NameCopy($cat_id);
return $this->Create($cat_id);
}
function NameCopy($master=null, $foreign_key=null)
{
$title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField');
if (!$title_field) return;
$new_name = $this->GetDBField($title_field);
$cat_id = $this->Application->GetVar('m_cat_id');
$original_checked = false;
do {
if ( preg_match("/Copy ([0-9]*)[ ]*of(.*)/", $new_name, $regs) ) {
$new_name = 'Copy '.($regs[1]+1).' of '.$regs[2];
}
elseif ($original_checked) {
$new_name = 'Copy of '.$new_name;
}
$query = 'SELECT '.$title_field.' FROM '.$this->TableName.'
LEFT JOIN '.TABLE_PREFIX.'CategoryItems ON
('.TABLE_PREFIX.'CategoryItems.ItemResourceId = '.$this->TableName.'.ResourceId)
WHERE ('.TABLE_PREFIX.'CategoryItems.CategoryId = '.$cat_id.') AND '.
$title_field.' = '.$this->Conn->qstr($new_name);
$res = $this->Conn->GetOne($query);
$original_checked = true;
} while ($res !== false);
$this->SetDBField($title_field, $new_name);
}
function MoveToCat($cat_id=null)
{
// $this->NameCopy();
$cat_id = $this->Application->GetVar('m_cat_id');
// check if the product already exists in destination cat
$query = 'SELECT PrimaryCat FROM '.TABLE_PREFIX.'CategoryItems
WHERE CategoryId = '.$cat_id.' AND ItemResourceId = '.$this->GetDBField('ResourceId');
// if it's not found is_primary will be FALSE, if it's found but not primary it will be int 0
$is_primary = $this->Conn->GetOne($query);
$exists = $is_primary !== false;
if ($exists) { // if the Product already exists in destination category
if ($is_primary) return; // do nothing when we paste to primary
// if it's not primary - delete it from destination category,
// as we will move it from current primary below
$query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems
WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND CategoryId = '.$cat_id;
$this->Conn->Query($query);
}
$query = 'UPDATE '.TABLE_PREFIX.'CategoryItems SET CategoryId = '.$cat_id.
' WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND PrimaryCat = 1';
$this->Conn->Query($query);
$this->Update();
}
// We need to delete CategoryItems record when deleting product
function Delete($id=null)
{
if( isset($id) ) {
$this->setID($id);
}
$this->Load($this->GetID());
$ret = parent::Delete();
if ($ret) {
$query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$this->GetDBField('ResourceId');
$this->Conn->Query($query);
}
return $ret;
}
+
+ function DeleteFromCategories($delete_category_ids)
+ {
+ $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); // because item was loaded before by ResourceId
+
+ $ci_table = $this->Application->getUnitOption('ci', 'TableName');
+ $resource_id = $this->GetDBField('ResourceId');
+
+ $item_cats_sql = 'SELECT CategoryId FROM %s WHERE ItemResourceId = %s';
+ $delete_category_items_sql = 'DELETE FROM %s WHERE ItemResourceId = %s';
+
+ $category_ids = $this->Conn->GetCol( sprintf($item_cats_sql, $ci_table, $resource_id) );
+ $cats_left = array_diff($category_ids, $delete_category_ids);
+ if(!$cats_left)
+ {
+ $sql = 'SELECT %s FROM %s WHERE ResourceId = %s';
+ $ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) );
+
+ $temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ $temp->DeleteItems($this->Prefix, $this->Special, $ids);
+ }
+ else
+ {
+ $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id) );
+ if( count($cats_left) == 1 )
+ {
+ $sql = 'REPLACE INTO %s (CategoryId,ItemResourceId,PrimaryCat) VALUES (%s,%s,1)';
+ $this->Conn->Query( sprintf($sql, $ci_table, end($cats_left), $resource_id ) );
+ }
+ }
+ }
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/general/cat_dbitem.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property
Index: trunk/kernel/units/general/cat_event_handler.php
===================================================================
--- trunk/kernel/units/general/cat_event_handler.php (revision 2040)
+++ trunk/kernel/units/general/cat_event_handler.php (revision 2041)
@@ -1,186 +1,186 @@
<?php
$application =& kApplication::Instance();
$application->Factory->includeClassFile('kDBEventHandler');
class kCatDBEventHandler extends InpDBEventHandler {
function OnCopy(&$event)
{
$object = $event->getObject();
$this->StoreSelectedIDs($event);
$ids = $this->getSelectedIDs($event);
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids));
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'copy');
- $this->Application->StoreVar('clipboard', 'COPY-0.'.$object->TableName.'.ResourceId=0');
+ $this->Application->StoreVar('ClipBoard', 'COPY-0.'.$object->TableName.'.ResourceId=0');
$event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY
}
function OnCut(&$event)
{
$object = $event->getObject();
$this->StoreSelectedIDs($event);
$ids = $this->getSelectedIDs($event);
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids));
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'cut');
- $this->Application->StoreVar('clipboard', 'CUT-0.'.$object->TableName.'.ResourceId=0');
+ $this->Application->StoreVar('ClipBoard', 'CUT-0.'.$object->TableName.'.ResourceId=0');
$event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY
}
function OnPaste(&$event)
{
$ids = $this->Application->RecallVar($event->getPrefixSpecial().'_clipboard');
if ($ids == '') {
$event->redirect = false;
return;
}
//recalling by different name, because we may get kDBList, if we recall just by prefix
$object =& $this->Application->recallObject($event->getPrefixSpecial().'.item', $event->Prefix);
- $this->prepareObject($object,$event);
+ $this->prepareObject($object, $event);
if ($this->Application->RecallVar($event->getPrefixSpecial().'_clipboard_mode') == 'copy') {
$ids_arr = explode(',', $ids);
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
if($ids_arr)
{
$temp->CloneItems($event->Prefix, $event->Special, $ids_arr);
}
}
else { // mode == cut
$ids_arr = explode(',', $ids);
foreach ($ids_arr as $id) {
$object->Load($id);
$object->MoveToCat();
}
}
$event->status = erSUCCESS;
}
/**
* Occurs when pasting category
*
* @param kEvent $event
*/
function OnCatPaste(&$event)
{
- $inp_clipboard = $this->Application->RecallVar('clipboard');
+ $inp_clipboard = $this->Application->RecallVar('ClipBoard');
$inp_clipboard = explode('-', $inp_clipboard, 2);
if($inp_clipboard[0] == 'COPY')
{
$saved_cat_id = $this->Application->GetVar('m_cat_id');
$cat_ids = $event->getEventParam('cat_ids');
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$ids_sql = 'SELECT '.$id_field.' FROM '.$table.' WHERE ResourceId IN (%s)';
- $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s';
+ $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s AND PrimaryCat = 1';
$this->Application->setUnitOption($event->Prefix,'AutoLoad', false);
$object =& $this->Application->recallObject($event->Prefix.'.item', $event->Prefix);
foreach($cat_ids as $source_cat => $dest_cat)
{
$item_resource_ids = $this->Conn->GetCol( sprintf($resource_ids_sql, $source_cat) );
if(!$item_resource_ids) continue;
$this->Application->SetVar('m_cat_id', $dest_cat);
$item_ids = $this->Conn->GetCol( sprintf($ids_sql, implode(',', $item_resource_ids) ) );
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
if($item_ids) $temp->CloneItems($event->Prefix, $event->Special, $item_ids);
}
$this->Application->setUnitOption($event->Prefix,'AutoLoad', true);
$this->Application->SetVar('m_cat_id', $saved_cat_id);
}
}
/**
* Apply scope clause
*
* @param kEvent $event
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
if ($event->Special != 'showall') {
if ( $event->getEventParam('parent_cat_id') ) {
$parent_cat_id = $event->getEventParam('parent_cat_id');
}
else {
$parent_cat_id = $this->Application->GetVar('c_id');
if (!$parent_cat_id) {
$parent_cat_id = $this->Application->GetVar('m_cat_id');
}
if (!$parent_cat_id) {
$parent_cat_id = 0;
}
}
if ((string) $parent_cat_id != 'any') {
if ($event->getEventParam('recursive')) {
$current_path = $this->Conn->GetOne('SELECT ParentPath FROM '.TABLE_PREFIX.'Category WHERE CategoryId='.$parent_cat_id);
$subcats = $this->Conn->GetCol('SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE ParentPath LIKE "'.$current_path.'%" ');
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.CategoryId IN ('.implode(', ', $subcats).')');
}
else {
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.CategoryId = '.$parent_cat_id );
}
}
}
else {
$object->addFilter('primary_filter', 'PrimaryCat = 1');
}
$view_perm = 1;
$object->addFilter('perm_filter', 'perm.PermId = '.$view_perm);
if (!defined('ADMIN')) {
$groups = explode(',',$this->Application->RecallVar('UserGroups'));
foreach ($groups as $group) {
$view_filters[] = 'FIND_IN_SET('.$group.', perm.acl) || ((NOT FIND_IN_SET('.$group.',perm.dacl)) AND perm.acl=\'\')';
}
$view_filter = implode(' OR ', $view_filters);
$object->addFilter('perm_filter2', $view_filter);
}
if (!defined('ADMIN')) {
$object->addFilter('status_filter', $object->TableName.'.Status = 1');
}
/*$list_type = $event->getEventParam('ListType');
switch($list_type)
{
case 'favorites':
$fav_table = $this->Application->getUnitOption('fav','TableName');
$user_id =& $this->Application->GetVar('u_id');
$sql = 'SELECT DISTINCT f.ResourceId
FROM '.$fav_table.' f
LEFT JOIN '.$object->TableName.' p ON p.ResourceId = f.ResourceId
WHERE f.PortalUserId = '.$user_id;
$ids = $this->Conn->GetCol($sql);
if(!$ids) $ids = Array(-1);
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.PrimaryCat = 1');
$object->addFilter('favorites_filter', '%1$s.`ResourceId` IN ('.implode(',',$ids).')');
break;
case 'search':
$search_results_table = TABLE_PREFIX.'ses_'.$this->Application->GetVar('sid').'_'.TABLE_PREFIX.'Search';
$sql = ' SELECT DISTINCT ResourceId
FROM '.$search_results_table.'
WHERE ItemType=11';
$ids = $this->Conn->GetCol($sql);
if(!$ids) $ids = Array(-1);
$object->addFilter('search_filter', '%1$s.`ResourceId` IN ('.implode(',',$ids).')');
break;
} */
}
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/general/cat_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.6
\ No newline at end of property
+1.7
\ No newline at end of property
Index: trunk/kernel/units/category_items/category_items_event_handler.php
===================================================================
--- trunk/kernel/units/category_items/category_items_event_handler.php (revision 2040)
+++ trunk/kernel/units/category_items/category_items_event_handler.php (revision 2041)
@@ -1,101 +1,150 @@
<?php
class CategoryItemsEventHander extends InpDBEventHandler
{
/**
* Add's New Category To Product
*
* @param kEvent $event
*/
function OnAddCategory(&$event)
{
$cat_list = (int)rtrim($this->Application->GetVar('cat_list'), ',');
//if(!$cat_list) return true;
$cat_id = (int)$this->Conn->GetOne('SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE ResourceId='.$cat_list);
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
// don't allow duplicate records
$already_added = $this->Conn->GetOne('SELECT * FROM '.$object->TableName.' WHERE CategoryId='.$cat_id.' AND '.$table_info['ForeignKey'].'='.$table_info['ParentId']);
if($already_added === false)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$items_info[0]['CategoryId'] = $cat_id;
$items_info[0][ $table_info['ForeignKey'] ] = $table_info['ParentId'];
$this->Application->SetVar( $event->getPrefixSpecial(true), $items_info );
$rp_backup = $event->redirect_params; // save opener state
$event->setEventParam('ForceCreateId', true);
$this->OnCreate($event);
$event->redirect_params = $rp_backup; // restore opener state
}
}
/**
* Set's new category as primary for product
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$this->StoreSelectedIDs($event);
$ids=$this->getSelectedIDs($event);
if($ids)
{
$id = array_shift($ids);
$table_info = $object->getLinkedInfo();
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$id.')');
}
$event->redirect_params = Array('opener' => 's', 'pass_events' => true); //stay!
}
/**
* Apply custom processing to item
*
* @param kEvent $event
*/
function customProcessing(&$event, $type)
{
if($event->Name == 'OnMassDelete')
{
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
switch ($type)
{
case 'before':
$ids = $event->getEventParam('ids');
if($ids)
{
$ids = $this->Conn->GetCol('SELECT CategoryId FROM '.$object->TableName.' WHERE (PrimaryCat=0) AND ('.$table_info['ForeignKey'].'='.$table_info['ParentId'].') AND CategoryId IN ('.implode(',',$ids).')');
$event->setEventParam('ids',$ids);
}
break;
// not needed because 'before' does not allow to delete primary cat!
/*case 'after':
// set 1st not deleted category as primary
$has_primary = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName.' WHERE (PrimaryCat=1) AND ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].')');
if(!$has_primary)
{
$cat_id = $this->Conn->GetOne('SELECT CategoryId FROM '.$object->TableName.' WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$cat_id.')');
}
break;*/
}
}
}
+ /**
+ * Removes primary mark from cloned category items record
+ *
+ * @param kEvent $event
+ */
+ function OnAfterClone(&$event)
+ {
+ $id = $event->getEventParam('id');
+ $table = $this->Application->getUnitOption($event->Prefix, 'TableName');
+ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
+ $sql = 'UPDATE %s SET PrimaryCat = 0 WHERE %s = %s';
+
+ $this->Conn->Query( sprintf($sql, $table, $id_field, $id) );
+ }
+
+ /**
+ * Deletes items of requested type from requested categories.
+ * In case if item is deleted from it's last category, then delete item too.
+ *
+ * @param kEvent $event
+ */
+ function OnDeleteFromCategory(&$event)
+ {
+ $category_ids = $event->getEventParam('category_ids');
+ if(!$category_ids) return false;
+
+ $item_prefix = $event->getEventParam('item_prefix');
+ $this->Application->setUnitOption($item_prefix, 'AutoLoad', false);
+ $item =& $this->Application->recallObject($item_prefix.'.-item');
+
+ $ci_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
+ $item_table = $this->Application->getUnitOption($item_prefix, 'TableName');
+
+ $sql = 'SELECT ItemResourceId, CategoryId FROM %1$s INNER JOIN %2$s ON (%1$s.ResourceId = %2$s.ItemResourceId) WHERE CategoryId IN (%3$s)';
+ $category_items = $this->Conn->Query( sprintf($sql, $item_table, $ci_table, implode(',', $category_ids) ) );
+
+ $item_hash = Array();
+ foreach($category_items as $ci_row)
+ {
+ $item_hash[ $ci_row['ItemResourceId'] ][] = $ci_row['CategoryId'];
+ }
+
+ foreach($item_hash as $item_resource_id => $delete_category_ids)
+ {
+ $item->Load($item_resource_id, 'ResourceId');
+ $item->DeleteFromCategories($delete_category_ids);
+ }
+ }
+
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/category_items/category_items_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property
Index: trunk/kernel/units/category_items/category_items_config.php
===================================================================
--- trunk/kernel/units/category_items/category_items_config.php (revision 2040)
+++ trunk/kernel/units/category_items/category_items_config.php (revision 2041)
@@ -1,63 +1,63 @@
<?php
$config = Array(
'Prefix' => 'ci',
'ItemClass' => Array('class'=>'CategoryItems_DBItem','file'=>'category_items_dbitem.php','build_event'=>'OnItemBuild'),
'ListClass' => Array('class'=>'kDBList','file'=>'','build_event'=>'OnListBuild'),
'EventHandlerClass' => Array('class'=>'CategoryItemsEventHander','file'=>'category_items_event_handler.php','build_event'=>'OnBuild'),
'TagProcessorClass' => Array('class'=>'CategoryItemsTagProcessor','file'=>'category_items_tag_processor.php','build_event'=>'OnBuild'),
'AutoLoad' => true,
'hooks' => Array(),
'QueryString' => Array(
1 => 'id',
2 => 'page',
3 => 'event',
),
'IDField' => 'CategoryId', // in this case idfield doesn't exit in destination table
'TableName' => TABLE_PREFIX.'CategoryItems',
'ParentTableKey'=> 'ResourceId',
'ForeignKey' => 'ItemResourceId',
- 'ParentPrefix' => 'p',
+ 'ParentPrefix' => 'p',
'AutoDelete' => true,
- 'AutoClone' => false, // Critical !!!
+ 'AutoClone' => false,
'CalculatedFields' => Array(
'' => Array (
'CategoryName' => 'c.CachedNavbar',
'DummyId' => 'IF(ISNULL(c.CategoryId),0,c.CategoryId)',
)
),
'ListSQLs' => Array( ''=>' SELECT %1$s.* %2$s
FROM %1$s
LEFT JOIN '.TABLE_PREFIX.'Category AS c ON c.CategoryId = %1$s.CategoryId',
), // key - special, value - list select sql
'ItemSQLs' => Array( ''=>'SELECT * FROM %s',
),
'ListSortings' => Array(
'' => Array(
'Sorting' => Array('CategoryName' => 'asc'),
)
),
'Fields' => Array(
'DummyId' => Array(),
'CategoryId' => Array('type'=>'int','not_null'=>1,'default'=>0),
'ItemResourceId' => Array('not_null' => 1, 'default' => 0),
'PrimaryCat' => Array('type' => 'int', 'not_null' => 1, 'default' => 0),
),
'VirtualFields' => Array(
'CategoryName' => Array(),
'DummyId' => Array(),
),
'Grids' => Array(
'Default' => Array(
'Icons' => Array('default'=>'icon16_custom.gif'), // icons for each StatusField values, if no matches or no statusfield selected, then "default" icon is used
'Fields' => Array(
'CategoryName' => Array( 'title'=>'la_col_Category', 'data_block' => 'grid_checkbox_category_td'),
),
),
),
);
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/category_items/category_items_config.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property
Index: trunk/core/kernel/event_handler.php
===================================================================
--- trunk/core/kernel/event_handler.php (revision 2040)
+++ trunk/core/kernel/event_handler.php (revision 2041)
@@ -1,136 +1,136 @@
<?php
/**
* Note:
* 1. When adressing variables from submit containing
* Prefix_Special as part of their name use
* $event->getPrefixSpecial(true) instead of
* $event->Prefix_Special as usual. This is due PHP
* is converting "." symbols in variable names during
* submit info "_". $event->getPrefixSpecial optional
* 1st parameter returns correct corrent Prefix_Special
* for variables beeing submitted such way (e.g. variable
* name that will be converted by PHP: "users.read_only_id"
* will be submitted as "users_read_only_id".
*
* 2. When using $this->Application-LinkVar on variables submitted
* from form which contain $Prefix_Special then note 1st item. Example:
* LinkVar($event->getPrefixSpecial(true).'_varname',$event->Prefix_Special.'_varname')
*
*/
/**
* Default event handler. Mostly abstract class
*
*/
class kEventHandler extends kBase {
/**
* In case if event should be handled with mehod,
* which name differs from event name, then it
* should be specified here.
* key - event name, value - event method
*
* @var Array
* @access protected
*/
var $eventMethods=Array();
/**
* Define alternative event processing method names
*
* @see $eventMethods
* @access protected
*/
function mapEvents()
{
}
function getPrefixSpecial()
{
trigger_error('Usage of getPrefixSpecial() this method is forbidden in kEventHandler class children. Use $event->getPrefixSpecial(true); instead', E_USER_ERROR);
}
/**
* Set's prefix and special
*
* @param string $prefix
* @param string $special
* @access public
*/
function Init($prefix,$special)
{
parent::Init($prefix,$special);
$this->mapEvents();
}
/**
* Process Event
*
* @param kEvent $event
* @access public
*/
function processEvent(&$event)
{
$event_name=$event->Name;
if( isset($this->eventMethods[$event_name]) ) $event_name=$this->eventMethods[$event_name];
if( method_exists($this,$event_name) )
{
$this->$event_name($event);
}
else
{
trigger_error('event <b>'.$event->Name.'</b> not implemented in class <b>'.get_class($this).'</b>', E_USER_ERROR);
}
}
/**
* Sample dummy event
*
* @param kEvent $event
* @access protected
*/
function OnBuild(&$event)
{
/*echo 'building: <br>';
print_pre($event);*/
}
/**
* Apply some special processing to
* object beeing recalled before using
* it in other events that call prepareObject
*
* @param Object $object
* @param kEvent $event
* @access protected
*/
- function prepareObject(&$object,&$event)
+ function prepareObject(&$object, &$event)
{
// processing here
}
/**
* Creates new event as child of
* event passed as $event param
*
* @param kEvent $event
* @access protected
*/
function &inheritEvent(&$event, $name=null)
{
$child_event = new kEvent();
$child_event->MasterEvent =& $event;
$child_event->Prefix=$event->Prefix;
$child_event->Special=$event->Special;
$child_event->Prefix_Special=$event->Prefix_Special;
$child_event->Name = $name;
return $child_event;
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/kernel/event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.5
\ No newline at end of property
+1.6
\ No newline at end of property
Index: trunk/core/kernel/db/db_event_handler.php
===================================================================
--- trunk/core/kernel/db/db_event_handler.php (revision 2040)
+++ trunk/core/kernel/db/db_event_handler.php (revision 2041)
@@ -1,1460 +1,1449 @@
<?php
define('EH_CUSTOM_PROCESSING_BEFORE',1);
define('EH_CUSTOM_PROCESSING_AFTER',2);
/**
* Note:
* 1. When adressing variables from submit containing
* Prefix_Special as part of their name use
* $event->getPrefixSpecial(true) instead of
* $event->Prefix_Special as usual. This is due PHP
* is converting "." symbols in variable names during
* submit info "_". $event->getPrefixSpecial optional
* 1st parameter returns correct corrent Prefix_Special
* for variables beeing submitted such way (e.g. variable
* name that will be converted by PHP: "users.read_only_id"
* will be submitted as "users_read_only_id".
*
* 2. When using $this->Application-LinkVar on variables submitted
* from form which contain $Prefix_Special then note 1st item. Example:
* LinkVar($event->getPrefixSpecial(true).'_varname',$event->Prefix_Special.'_varname')
*
*/
/**
* EventHandler that is used to process
* any database related events
*
*/
class kDBEventHandler extends kEventHandler {
/**
* Description
*
* @var DBConnection
* @access public
*/
var $Conn;
/**
* Adds ability to address db connection
*
* @return kDBEventHandler
* @access public
*/
function kDBEventHandler()
{
parent::kBase();
$this->Conn =& $this->Application->GetADODBConnection();
}
function mapEvents()
{
$events_map = Array('OnRemoveFilters' => 'FilterAction',
'OnApplyFilters' => 'FilterAction');
$this->eventMethods = array_merge($this->eventMethods, $events_map);
}
/**
* Returns ID of current item to be edited
* by checking ID passed in get/post as prefix_id
* or by looking at first from selected ids, stored.
* Returned id is also stored in Session in case
* it was explicitly passed as get/post
*
* @param kEvent $event
* @return int
*/
function getPassedID(&$event)
{
//$ret = $this->Application->GetLinkedVar($event->getPrefixSpecial(true).'_id', $event->getPrefixSpecial().'_id');
// ?? We don't need to store selected id in session, as long as we have pass=all by default, which
// means that main item id will be passed to all sub-item screens by default
// another prove of that is that sub-items relay on main item '_mode' = 't' for switching to temp tables
// Also having id in session raised problems with the id of deleted item stuck in session
// 1. get id from post (used in admin)
$ret = $this->Application->GetVar($event->getPrefixSpecial(true).'_id');
if($ret) return $ret;
// 2. get id from env (used in front)
$ret = $this->Application->GetVar($event->getPrefixSpecial().'_id');
if($ret) return $ret;
// recall selected ids array and use the first one
$ids=$this->Application->GetVar($event->getPrefixSpecial().'_selected_ids');
if ($ids != '') {
$ids=explode(',',$ids);
if($ids) $ret=array_shift($ids);
}
else { // if selected ids are not yet stored
$this->StoreSelectedIDs($event);
return $this->Application->GetVar($event->getPrefixSpecial(true).'_id'); // StoreSelectedIDs sets this variable
}
return $ret;
}
/**
* Prepares and stores selected_ids string
* in Session and Application Variables
* by getting all checked ids from grid plus
* id passed in get/post as prefix_id
*
* @param kEvent $event
*/
function StoreSelectedIDs(&$event)
{
$ret = Array();
// May be we don't need this part: ?
$passed = $this->Application->GetVar($event->getPrefixSpecial(true).'_id');
if($passed !== false && $passed != '')
{
array_push($ret, $passed);
}
$ids = Array();
// get selected ids from post & save them to session
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
$id_field = $this->Application->getUnitOption($event->Prefix,'IDField');
foreach($items_info as $id => $field_values)
{
if( getArrayValue($field_values,$id_field) ) array_push($ids,$id);
}
//$ids=array_keys($items_info);
}
$ret = array_unique(array_merge($ret, $ids));
$this->Application->SetVar($event->getPrefixSpecial().'_selected_ids',implode(',',$ret));
$this->Application->LinkVar($event->getPrefixSpecial().'_selected_ids');
// This is critical - otherwise getPassedID will return last ID stored in session! (not exactly true)
// this smells... needs to be refactored
$first_id = getArrayValue($ret,0);
if($first_id === false) trigger_error('Requested ID for prefix <b>'.$event->getPrefixSpecial().'</b> <span class="debug_error">not passed</span>',E_USER_NOTICE);
$this->Application->SetVar($event->getPrefixSpecial(true).'_id', $first_id);
}
/**
* Returns stored selected ids as an array
*
* @param kEvent $event
* @return array
*/
function getSelectedIDs(&$event)
{
return explode(',', $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids'));
}
/**
* Returs associative array of submitted fields for current item
* Could be used while creating/editing single item -
* meaning on any edit form, except grid edit
*
* @param kEvent $event
*/
function getSubmittedFields(&$event)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$field_values = $items_info ? array_shift($items_info) : Array();
return $field_values;
}
/**
* Removes any information about current/selected ids
* from Application variables and Session
*
* @param kEvent $event
*/
function clearSelectedIDs(&$event)
{
$prefix_special = $event->getPrefixSpecial();
$ids = $this->Application->RecallVar($prefix_special.'_selected_ids');
$event->setEventParam('ids', $ids);
$this->Application->RemoveVar($prefix_special.'_selected_ids');
$this->Application->SetVar($prefix_special.'_selected_ids', '');
$this->Application->SetVar($prefix_special.'_id', ''); // $event->getPrefixSpecial(true).'_id' too may be
}
/*function SetSaveEvent(&$event)
{
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnUpdate');
$this->Application->LinkVar($event->Prefix_Special.'_SaveEvent');
}*/
/**
* Common builder part for Item & List
*
* @param kDBBase $object
* @param kEvent $event
* @access private
*/
function dbBuild(&$object,&$event)
{
$object->Configure();
- $this->PrepareObject($event);
+ $this->PrepareObject($object, $event);
$live_table = $event->getEventParam('live_table');
if( $this->UseTempTables($event) && !$live_table )
{
$object->SwitchToTemp();
}
// This strange constuction creates hidden field for storing event name in form submit
// It pass SaveEvent to next screen, otherwise after unsuccsefull create it will try to update rather than create
$current_event = $this->Application->GetVar($event->Prefix_Special.'_event');
// $this->Application->setEvent($event->Prefix_Special, $current_event);
$this->Application->setEvent($event->Prefix_Special, '');
$save_event = $this->UseTempTables($event) && $this->Application->GetTopmostPrefix($event->Prefix) == $event->Prefix ? 'OnSave' : 'OnUpdate';
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent',$save_event);
}
/**
- * Apply custom object modifications
- * after it's initialization
- *
- * @param kEvent $event
- */
- function PrepareObject(&$event)
- {
-
- }
-
- /**
* Builds item (loads if needed)
*
* @param kEvent $event
* @access protected
*/
function OnItemBuild(&$event)
{
$object =& $event->getObject();
$this->dbBuild($object,$event);
$sql = $this->ItemPrepareQuery($event);
$sql = $this->Application->ReplaceLanguageTags($sql);
$object->setSelectSQL($sql);
// 2. loads if allowed
$auto_load = $this->Application->getUnitOption($event->Prefix,'AutoLoad');
if($auto_load) $this->LoadItem($event);
$actions =& $this->Application->recallObject('kActions');
$actions->Set($event->Prefix_Special.'_GoTab', '');
$actions->Set($event->Prefix_Special.'_GoId', '');
}
/**
* Build subtables array from configs
*
* @param kEvent $event
*/
function OnTempHandlerBuild(&$event)
{
$object =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$object->BuildTables( $event->Prefix, $this->getSelectedIDs($event) );
}
/**
* Enter description here...
*
* @param kEvent $event
* @return unknown
*/
function UseTempTables(&$event)
{
$object = &$event->getObject();
$top_prefix = $this->Application->GetTopmostPrefix($event->Prefix);
return $this->Application->GetVar($top_prefix.'_mode') == 't';
}
/**
* Returns table prefix from event (temp or live)
*
* @param kEvent $event
* @return string
* @todo Needed? Should be refactored (by Alex)
*/
function TablePrefix(&$event)
{
return $this->UseTempTables($event) ? kTempTablesHandler::GetTempTablePrefix().TABLE_PREFIX : TABLE_PREFIX;
}
function LoadItem(&$event)
{
$object =& $event->getObject();
if ( $event->getEventParam('ByParent') ) {
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$parent_table_key = $this->Application->getUnitOption($event->Prefix, 'ParentTableKey');
$parent_object =& $this->Application->recallObject($parent_prefix);
$id = $parent_object->GetDBField($parent_table_key);
$id_field = $this->Application->getUnitOption($event->Prefix, 'ForeignKey');
}
else {
$id = $this->getPassedID($event);
$id_field = null;
}
if ($object->Load($id, $id_field)) {
$actions =& $this->Application->recallObject('kActions');
$actions->Set($event->Prefix_Special.'_id', $object->GetId());
}
}
/**
* Builds list
*
* @param kEvent $event
* @access protected
*/
function OnListBuild(&$event)
{
//$event->setPseudoClass('_List');
$object =& $event->getObject();
$this->dbBuild($object,$event);
$sql = $this->ListPrepareQuery($event);
$sql = $this->Application->ReplaceLanguageTags($sql);
$object->setSelectSQL($sql);
$object->linkToParent( $this->getMainSpecial($event) );
// $object->linkToParent($special);
$this->AddFilters($event);
$this->SetCustomQuery($event); // new!, use this for dynamic queries based on specials for ex.
$this->SetPagination($event);
$this->SetSorting($event);
$actions =& $this->Application->recallObject('kActions');
$actions->Set('remove_specials['.$event->Prefix_Special.']', '0');
$actions->Set($event->Prefix_Special.'_GoTab', '');
}
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
$special = $event->getEventParam('main_special');
if($special === false || $special == '$main_special')
{
$special = $event->Special;
}
return $special;
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
function SetCustomQuery(&$event)
{
}
/**
* Set's new perpage for grid
*
* @param kEvent $event
*/
function OnSetPerPage(&$event)
{
$per_page = $this->Application->GetVar($event->getPrefixSpecial(true).'_PerPage');
$this->Application->StoreVar( $event->getPrefixSpecial().'_PerPage', $per_page );
}
/**
* Set's correct page for list
* based on data provided with event
*
* @param kEvent $event
* @access private
* @see OnListBuild
*/
function SetPagination(&$event)
{
// get PerPage (forced -> session -> config -> 10)
$per_page = $this->getPerPage($event);
$object =& $event->getObject();
$object->SetPerPage($per_page);
$this->Application->StoreVarDefault($event->getPrefixSpecial().'_Page', 1);
$page = $this->Application->GetVar($event->getPrefixSpecial().'_Page');
if (!$page) {
$page = $this->Application->GetVar($event->getPrefixSpecial(true).'_Page');
}
if (!$page) {
$page = $this->Application->RecallVar($event->getPrefixSpecial().'_Page');
}
else {
$this->Application->StoreVar($event->getPrefixSpecial().'_Page', $page);
}
// $page = $this->Application->GetLinkedVar($event->getPrefixSpecial(true).'_Page', $event->getPrefixSpecial().'_Page');
if( !$event->getEventParam('skip_counting') )
{
$pages = $object->GetTotalPages();
if($page > $pages)
{
$this->Application->StoreVar($event->getPrefixSpecial().'_Page', 1);
$page = 1;
}
}
$object->SetPage($page);
}
function getPerPage($event)
{
$per_page = $event->getEventParam('per_page');
$config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
if ( $config_mapping ) {
switch ( $per_page ){
case 'short_list' :
$per_page = $this->Application->ConfigValue($config_mapping['ShortListPerPage']);
break;
case 'default' :
$per_page = $this->Application->ConfigValue($config_mapping['PerPage']);
break;
}
}
if(!$per_page)
{
$per_page_var = $event->getPrefixSpecial().'_PerPage';
$per_page = $this->Application->RecallVar($per_page_var);
if(!$per_page)
{
if ( $config_mapping ) {
$per_page = $this->Application->ConfigValue($config_mapping['PerPage']);
}
if(!$per_page) $per_page = 10;
}
}
return $per_page;
}
/**
* Set's correct sorting for list
* based on data provided with event
*
* @param kEvent $event
* @access private
* @see OnListBuild
*/
function SetSorting(&$event)
{
$event->setPseudoClass('_List');
$object =& $event->getObject();
$cur_sort1 = $this->Application->RecallVar($event->Prefix_Special.'_Sort1');
$cur_sort1_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort1_Dir');
$cur_sort2 = $this->Application->RecallVar($event->Prefix_Special.'_Sort2');
$cur_sort2_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort2_Dir');
$sorting_configs = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
$list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings');
$sorting_prefix = getArrayValue($list_sortings, $event->Special) ? $event->Special : '';
$tag_sort_by = $event->getEventParam('sort_by');
if ($tag_sort_by) {
list($by, $dir) = explode(',', $tag_sort_by);
if ($by == 'random') $by = 'RAND()';
$object->AddOrderField($by, $dir);
}
if ($sorting_configs && isset ($sorting_configs['DefaultSorting1Field'])){
$list_sortings[$sorting_prefix]['Sorting'] = Array(
$this->Application->ConfigValue($sorting_configs['DefaultSorting1Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting1Dir']),
$this->Application->ConfigValue($sorting_configs['DefaultSorting2Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting2Dir']),
);
}
// Use default if not specified
if ( !$cur_sort1 || !$cur_sort1_dir)
{
if ( $sorting = getArrayValue($list_sortings, $sorting_prefix, 'Sorting') ) {
reset($sorting);
$cur_sort1 = key($sorting);
$cur_sort1_dir = current($sorting);
if (next($sorting)) {
$cur_sort2 = key($sorting);
$cur_sort2_dir = current($sorting);
}
}
}
if ( $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting') ) {
foreach ($forced_sorting as $field => $dir) {
$object->AddOrderField($field, $dir);
}
}
if($cur_sort1 != '' && $cur_sort1_dir != '')
{
$object->AddOrderField($cur_sort1, $cur_sort1_dir);
}
if($cur_sort2 != '' && $cur_sort2_dir != '')
{
$object->AddOrderField($cur_sort2, $cur_sort2_dir);
}
}
/**
* Add filters found in session
*
* @param kEvent $event
*/
function AddFilters(&$event)
{
$object =& $event->getObject();
$search_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_search_filter');
if($search_filter)
{
$search_filter = unserialize($search_filter);
foreach($search_filter as $search_field => $filter_params)
{
$filter_type = ($filter_params['type'] == 'having') ? HAVING_FILTER : WHERE_FILTER;
$object->addFilter($search_field, $filter_params['value'], $filter_type, FLT_SEARCH);
}
}
$view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter');
if($view_filter)
{
$view_filter = unserialize($view_filter);
$temp_filter =& $this->Application->makeClass('kMultipleFilter');
$filter_menu = $this->Application->getUnitOption($event->Prefix,'FilterMenu');
$group_key = 0; $group_count = count($filter_menu['Groups']);
while($group_key < $group_count)
{
$group_info = $filter_menu['Groups'][$group_key];
$temp_filter->setType( constant('FLT_TYPE_'.$group_info['mode']) );
$temp_filter->clearFilters();
foreach ($group_info['filters'] as $flt_id)
{
$sql_key = getArrayValue($view_filter,$flt_id) ? 'on_sql' : 'off_sql';
if ($filter_menu['Filters'][$flt_id][$sql_key] != '')
{
$temp_filter->addFilter('view_filter_'.$flt_id, $filter_menu['Filters'][$flt_id][$sql_key]);
}
}
$object->addFilter('view_group_'.$group_key, $temp_filter, $group_info['type'] , FLT_VIEW);
$group_key++;
}
}
}
/**
* Set's new sorting for list
*
* @param kEvent $event
* @access protected
*/
function OnSetSorting(&$event)
{
$cur_sort1 = $this->Application->RecallVar($event->Prefix_Special.'_Sort1');
$cur_sort1_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort1_Dir');
$cur_sort2 = $this->Application->RecallVar($event->Prefix_Special.'_Sort2');
$cur_sort2_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort2_Dir');
$passed_sort1 = $this->Application->GetVar($event->getPrefixSpecial(true).'_Sort1');
if ($cur_sort1 == $passed_sort1) {
$cur_sort1_dir = $cur_sort1_dir == 'asc' ? 'desc' : 'asc';
}
else {
$cur_sort2 = $cur_sort1;
$cur_sort2_dir = $cur_sort1_dir;
$cur_sort1 = $passed_sort1;
$cur_sort1_dir = 'asc';
}
$this->Application->StoreVar($event->Prefix_Special.'_Sort1', $cur_sort1);
$this->Application->StoreVar($event->Prefix_Special.'_Sort1_Dir', $cur_sort1_dir);
$this->Application->StoreVar($event->Prefix_Special.'_Sort2', $cur_sort2);
$this->Application->StoreVar($event->Prefix_Special.'_Sort2_Dir', $cur_sort2_dir);
}
/**
* Set sorting directly to session
*
* @param kEvent $event
*/
function OnSetSortingDirect(&$event)
{
$combined = $this->Application->GetVar($event->getPrefixSpecial(true).'_CombinedSorting');
if ($combined) {
list($field,$dir) = explode('|',$combined);
$this->Application->StoreVar($event->Prefix_Special.'_Sort1', $field);
$this->Application->StoreVar($event->Prefix_Special.'_Sort1_Dir', $dir);
return;
}
$field_pos = $this->Application->GetVar($event->getPrefixSpecial(true).'_SortPos');
$this->Application->LinkVar( $event->getPrefixSpecial(true).'_Sort'.$field_pos, $event->Prefix_Special.'_Sort'.$field_pos);
$this->Application->LinkVar( $event->getPrefixSpecial(true).'_Sort'.$field_pos.'_Dir', $event->Prefix_Special.'_Sort'.$field_pos.'_Dir');
}
/**
* Reset grid sorting to default (from config)
*
* @param kEvent $event
*/
function OnResetSorting(&$event)
{
$this->Application->RemoveVar($event->Prefix_Special.'_Sort1');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort1_Dir');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort2');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort2_Dir');
}
/**
* Creates needed sql query to load item,
* if no query is defined in config for
* special requested, then use default
* query
*
* @param kEvent $event
* @access protected
*/
function ItemPrepareQuery(&$event)
{
$sqls = $this->Application->getUnitOption($event->Prefix,'ItemSQLs');
return isset($sqls[$event->Special]) ? $sqls[$event->Special] : $sqls[''];
}
/**
* Creates needed sql query to load list,
* if no query is defined in config for
* special requested, then use default
* query
*
* @param kEvent $event
* @access protected
*/
function ListPrepareQuery(&$event)
{
$sqls = $this->Application->getUnitOption($event->Prefix,'ListSQLs');
return isset( $sqls[$event->Special] ) ? $sqls[$event->Special] : $sqls[''];
}
/**
* Apply custom processing to item
*
* @param kEvent $event
*/
function customProcessing(&$event, $type)
{
}
/* Edit Events mostly used in Admin */
/**
* Creates new kDBItem
*
* @param kEvent $event
* @access protected
*/
function OnCreate(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
list($id,$field_values) = each($items_info);
$object->SetFieldsFromHash($field_values);
}
$this->customProcessing($event,'before');
//look at kDBItem' Create for ForceCreateId description, it's rarely used and is NOT set by default
if( $object->Create($event->getEventParam('ForceCreateId')) )
{
if( $object->IsTempTable() ) $object->setTempID();
$this->customProcessing($event,'after');
$event->status=erSUCCESS;
$event->redirect_params = Array('opener'=>'u');
}
else
{
$event->status=erFAIL;
$event->redirect=false;
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnCreate');
$object->setID($id);
}
}
/**
* Updates kDBItem
*
* @param kEvent $event
* @access protected
*/
function OnUpdate(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
foreach($items_info as $id => $field_values)
{
$object->Load($id);
$object->SetFieldsFromHash($field_values);
$this->customProcessing($event, 'before');
if( $object->Update($id) )
{
$this->customProcessing($event, 'after');
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
}
}
$event->redirect_params = Array('opener'=>'u');
}
/**
* Delete's kDBItem object
*
* @param kEvent $event
* @access protected
*/
function OnDelete(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$object->ID = $this->getPassedID($event);
if( $object->Delete() )
{
$event->status = erSUCCESS;
}
else
{
$event->status = erFAIL;
$event->redirect = false;
}
}
/**
* Prepares new kDBItem object
*
* @param kEvent $event
* @access protected
*/
function OnNew(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$object->setID(0);
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnCreate');
$table_info = $object->getLinkedInfo();
$object->SetDBField($table_info['ForeignKey'], $table_info['ParentId']);
$this->Application->setUnitOption($event->Prefix,'AutoLoad',true);
$event->redirect = false;
}
/**
* Cancel's kDBItem Editing/Creation
*
* @param kEvent $event
* @access protected
*/
function OnCancel(&$event)
{
$event->redirect_params = Array('opener'=>'u');
}
/**
* 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
*
* @param kEvent $event
*/
function OnMassDelete(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 0)) {
return;
}
$event->status=erSUCCESS;
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$this->StoreSelectedIDs($event);
$event->setEventParam('ids', $this->getSelectedIDs($event) );
$this->customProcessing($event, 'before');
$ids = $event->getEventParam('ids');
if($ids)
{
$temp->DeleteItems($event->Prefix, $event->Special, $ids);
}
$this->clearSelectedIDs($event);
}
/**
* Prepare temp tables and populate it
* with items selected in the grid
*
* @param kEvent $event
*/
function OnEdit(&$event)
{
$this->StoreSelectedIDs($event);
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp->PrepareEdit();
$event->redirect=false;
}
/**
* Saves content of temp table into live and
* redirects to event' default redirect (normally grid template)
*
* @param kEvent $event
*/
function OnSave(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status==erSUCCESS) {
$skip_master=false;
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
// newly created item
/*if($this->getPassedID($event) == 0)
{
$master_id = $temp->CopyMasterToOriginal();
$temp->UpdateForeignKeys($master_id); // save linked field values
$skip_master = true; //we've already copied master table to get the id
}*/
if (!$this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 0)) {
$temp->SaveEdit($skip_master);
}
$this->clearSelectedIDs($event);
$event->redirect_params = Array('opener'=>'u');
$this->Application->RemoveVar($event->getPrefixSpecial().'_modified');
}
}
/**
* Cancels edit
* Removes all temp tables and clears selected ids
*
* @param kEvent $event
*/
function OnCancelEdit(&$event)
{
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp->CancelEdit();
$this->clearSelectedIDs($event);
$event->redirect_params = Array('opener'=>'u');
$this->Application->RemoveVar($event->getPrefixSpecial().'_modified');
}
/**
* Saves edited item into temp table
* If there is no id, new item is created in temp table
*
* @param kEvent $event
*/
function OnPreSave(&$event)
{
//$event->redirect = false;
// if there is no id - it means we need to create an item
if (is_object($event->MasterEvent)) {
$event->MasterEvent->setEventParam('IsNew',false);
}
$item_id = $this->getPassedID($event);
if($item_id == '')
{
$event->CallSubEvent('OnPreSaveCreated');
if (is_object($event->MasterEvent)) {
$event->MasterEvent->setEventParam('IsNew',true);
}
return;
}
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
foreach($items_info as $id => $field_values)
{
$object->SetDefaultValues();
$object->Load($id);
$object->SetFieldsFromHash($field_values);
if( $object->Update($id) )
{
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
}
}
}
/**
* Saves edited item in temp table and loads
* item with passed id in current template
* Used in Prev/Next buttons
*
* @param kEvent $event
*/
function OnPreSaveAndGo(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status==erSUCCESS) {
$event->redirect_params[$event->getPrefixSpecial(true).'_id'] = $this->Application->GetVar($event->Prefix_Special.'_GoId');
}
}
/**
* Saves edited item in temp table and goes
* to passed tabs, by redirecting to it with OnPreSave event
*
* @param kEvent $event
*/
function OnPreSaveAndGoToTab(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status==erSUCCESS) {
$event->redirect=$this->Application->GetVar($event->getPrefixSpecial(true).'_GoTab');
}
}
/**
* Saves editable list and goes to passed tab,
* by redirecting to it with empty event
*
* @param kEvent $event
*/
function OnUpdateAndGoToTab(&$event)
{
$event->setPseudoClass('_List');
$event->CallSubEvent('OnUpdate');
if ($event->status==erSUCCESS) {
$event->redirect=$this->Application->GetVar($event->getPrefixSpecial(true).'_GoTab');
}
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
*/
function OnPreCreate(&$event)
{
$this->clearSelectedIDs($event);
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$temp =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler');
$temp->PrepareEdit();
$object->setID(0);
$event->redirect=false;
}
/**
* Creates a new item in temp table and
* stores item id in App vars and Session on succsess
*
* @param kEvent $event
*/
function OnPreSaveCreated(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info) $field_values = array_shift($items_info);
$object =& $event->getObject();
$object->SetFieldsFromHash($field_values);
$this->customProcessing($event, 'before');
if( $object->Create() )
{
$this->customProcessing($event, 'after');
$event->redirect_params[$event->getPrefixSpecial(true).'_id'] = $object->GetId();
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
$object->setID(0);
}
}
/* End of Edit events */
// III. Events that allow to put some code before and after Update,Load,Create and Delete methods of item
/**
* Occurse before loading item, 'id' parameter
* allows to get id of item beeing loaded
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemLoad(&$event)
{
}
/**
* Occurse after loading item, 'id' parameter
* allows to get id of item that was loaded
*
* @param kEvent $event
* @access public
*/
function OnAfterItemLoad(&$event)
{
}
/**
* Occurse before creating item
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemCreate(&$event)
{
}
/**
* Occurse after creating item
*
* @param kEvent $event
* @access public
*/
function OnAfterItemCreate(&$event)
{
}
/**
* Occurse before updating item
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemUpdate(&$event)
{
}
/**
* Occurse after updating item
*
* @param kEvent $event
* @access public
*/
function OnAfterItemUpdate(&$event)
{
}
/**
* Occurse before deleting item, id of item beeing
* deleted is stored as 'id' event param
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemDelete(&$event)
{
}
/**
* Occurse after deleting item, id of deleted item
* is stored as 'id' param of event
*
* @param kEvent $event
* @access public
*/
function OnAfterItemDelete(&$event)
{
}
/**
* Occurs after successful item validation
*
* @param kEvent $event
*/
function OnAfterItemValidate(&$event)
{
}
/**
* Occures after an item has been copied to temp
* Id of copied item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterCopyToTemp(&$event)
{
}
/**
* Occures before an item is deleted from live table when copying from temp
* (temp handler deleted all items from live and then copy over all items from temp)
* Id of item being deleted is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnBeforeDeleteFromLive(&$event)
{
}
/**
* Occures before an item is copied to live table (after all foreign keys have been updated)
* Id of item being copied is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnBeforeCopyToLive(&$event)
{
}
/**
* !!! NOT FULLY IMPLEMENTED - SEE TEMP HANDLER COMMENTS (search by event name)!!!
* Occures after an item has been copied to live table
* Id of copied item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterCopyToLive(&$event)
{
}
/**
* Occures before an item is cloneded
* Id of ORIGINAL item is passed as event' 'id' param
* Do not call object' Update method in this event, just set needed fields!
*
* @param kEvent $event
*/
function OnBeforeClone(&$event)
{
}
/**
* Occures after an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterClone(&$event)
{
}
/**
* Ensures that popup will be closed automatically
* and parent window will be refreshed with template
* passed
*
* @param kEvent $event
* @access public
*/
function finalizePopup(&$event, $main_prefix, $t)
{
$event->redirect = 'incs/close_popup';
// 2. substitute opener
$opener_stack = $this->Application->RecallVar('opener_stack');
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array();
//array_pop($opener_stack);
$new_level = 'index4.php|'.ltrim($this->Application->BuildEnv($t, Array('m_opener' => 'u'), 'all'), ENV_VAR_NAME.'=');
array_push($opener_stack,$new_level);
$this->Application->StoreVar('opener_stack',serialize($opener_stack));
}
/**
* Create search filters based on search query
*
* @param kEvent $event
* @access protected
*/
function OnSearch(&$event)
{
$event->setPseudoClass('_List');
$object =& $event->getObject();
$keyword = $this->Application->GetVar( $event->getPrefixSpecial(true).'_search_keyword');
$this->Application->StoreVar( $event->getPrefixSpecial().'_search_keyword', $keyword);
if(!$keyword)
{
$this->OnSearchReset($event);
return true;
}
$grid_name = $this->Application->GetVar('grid_name');
$grids = $this->Application->getUnitOption($event->Prefix,'Grids');
$search_fields = array_keys($grids[$grid_name]['Fields']);
$search_filter = Array();
foreach($search_fields as $search_field)
{
$filter_type = isset($object->VirtualFields[$search_field]) ? 'having' : 'where';
$field_type = getArrayValue($object->Fields[$search_field],'type');
if(!$field_type) $field_type = 'string'; // default LIKE filter for all fields without type
$keyword = trim($keyword);
$keyword = str_replace(Array('"',"'"),'',$keyword);
$filter_value = '';
$table_name = ($filter_type == 'where') ? '`'.$object->TableName.'`.' : '';
// get field clause by formatter name and/or parameters
$formatter = getArrayValue($object->Fields[$search_field],'formatter');
switch ($formatter)
{
case 'kOptionsFormatter':
$search_keys = Array();
$use_phrases = getArrayValue($object->Fields[$search_field], 'use_phrases');
foreach($object->Fields[$search_field]['options'] as $key => $val)
{
$pattern = '#'.$keyword.'#i';
if ( preg_match($pattern, $use_phrases ? $this->Application->Phrase($val) : $val) ) {
array_push($search_keys, $key);
}
}
if (count($search_keys) > 0) {
$filter_value = $table_name.'`'.$search_field.'` IN ('.implode(',', $search_keys).')';
}
$field_processed = true;
break;
default:
$field_processed = false;
break;
}
// if not already processed by formatter, then get clause by field type
if(!$field_processed)
{
switch($field_type)
{
case 'int':
case 'integer':
case 'numeric':
if( !is_numeric($keyword) ) break;
$filter_value = $table_name.'`'.$search_field.'` = \''.$keyword.'\'';
break;
case 'double':
case 'float':
case 'real':
if( !is_numeric($keyword) ) break;
$filter_value = 'ABS('.$table_name.'`'.$search_field.'` - \''.str_replace(',','.',$keyword).'\') <= 0.0001';
break;
case 'string':
$like_keyword = preg_replace( '/\'(.*)\'/U', '\\1', $this->Conn->qstr( str_replace('\\','\\\\', $keyword) ) );
$keywords = explode(' ', $like_keyword);
foreach($keywords as $keyword_pos => $keyword_value)
{
$keyword_value = trim($keyword_value);
if($keyword_value)
{
$keywords[$keyword_pos] = $table_name.'`'.$search_field.'` LIKE \'%'.$keyword_value.'%\'';
}
else
{
unset($keywords[$keyword_pos]);
}
}
$filter_value = '('.implode(') OR (',$keywords).')';
break;
}
}
if($filter_value) $search_filter[$search_field] = Array('type' => $filter_type, 'value' => $filter_value);
}
$this->Application->StoreVar($event->getPrefixSpecial().'_search_filter', serialize($search_filter) );
}
/**
* Clear search keywords
*
* @param kEvent $event
* @access protected
*/
function OnSearchReset(&$event)
{
$this->Application->RemoveVar($event->getPrefixSpecial().'_search_filter');
$this->Application->RemoveVar($event->getPrefixSpecial().'_search_keyword');
}
/**
* Set's new filter value (filter_id meaning from config)
*
* @param kEvent $event
*/
function OnSetFilter(&$event)
{
$filter_id = $this->Application->GetVar('filter_id');
$filter_value = $this->Application->GetVar('filter_value');
$view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter');
$view_filter = $view_filter ? unserialize($view_filter) : Array();
$view_filter[$filter_id] = $filter_value;
$this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) );
}
/**
* Add/Remove all filters applied to list from "View" menu
*
* @param kEvent $event
*/
function FilterAction(&$event)
{
$view_filter = Array();
$filter_menu = $this->Application->getUnitOption($event->Prefix,'FilterMenu');
switch ($event->Name)
{
case 'OnRemoveFilters':
$filter_value = 1;
break;
case 'OnApplyFilters':
$filter_value = 0;
break;
}
foreach($filter_menu['Filters'] as $filter_key => $filter_params)
{
if(!$filter_params) continue;
$view_filter[$filter_key] = $filter_value;
}
$this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) );
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnPreSaveAndOpenTranslator(&$event)
{
$this->Application->SetVar('allow_translation', true);
$object =& $event->getObject();
$this->RemoveRequiredFields($object);
$event->CallSubEvent('OnPreSave');
if ($event->status == erSUCCESS) {
// $url = $this->Application->HREF($t, '', Array('pass'=>'all', $event->getPrefixSpecial(true).'_id' => $object->GetId()));
// $field = $this->Application->GetVar('translator_field');
$event->redirect = $this->Application->GetVar('translator_t');
$event->redirect_params = Array('pass'=>'all,trans,'.$this->Application->GetVar('translator_prefixes'),
$event->getPrefixSpecial(true).'_id' => $object->GetId(),
'trans_event'=>'OnLoad',
'trans_prefix'=> $this->Application->GetVar('translator_prefixes'),
'trans_field'=>$this->Application->GetVar('translator_field'),
);
//$after_script = "openTranslator('".$event->getPrefixSpecial()."', '".$field."', '".$url."', '".$wnd_name."')";
}
// $this->Application->SetVar('after_script', $after_script);
// $event->redirect = false;
}
function RemoveRequiredFields(&$object)
{
// making all field non-required to achieve successful presave
foreach($object->Fields as $field => $options)
{
if(isset($options['required']))
{
unset($object->Fields[$field]['required']);
}
}
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/kernel/db/db_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.14
\ No newline at end of property
+1.15
\ No newline at end of property
Index: trunk/core/units/general/cat_dbitem.php
===================================================================
--- trunk/core/units/general/cat_dbitem.php (revision 2040)
+++ trunk/core/units/general/cat_dbitem.php (revision 2041)
@@ -1,113 +1,144 @@
<?php
class kCatDBItem extends kDBItem {
function Create()
{
if (!$this->Validate()) return false;
$this->SetDBField('ResourceId', $this->Application->NextResourceId());
$this->SetDBField('Modified', mktime());
$ret = parent::Create();
if($ret)
{
if ( kTempTablesHandler::IsTempTable($this->TableName) ) {
$table = kTempTablesHandler::GetTempName(TABLE_PREFIX.'CategoryItems');
}
else {
$table = TABLE_PREFIX.'CategoryItems';
}
$cat_id = $this->Application->GetVar('m_cat_id');
$query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat)
VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1)';
$this->Conn->Query($query);
}
return $ret;
}
function Update($id=null)
{
$this->VirtualFields['ResourceId'] = true;
$this->SetDBField('Modified', mktime());
return parent::Update($id);
}
function Copy($cat_id=null)
{
if (!isset($cat_id)) $cat_id = $this->Application->GetVar('m_cat_id');
$this->NameCopy($cat_id);
return $this->Create($cat_id);
}
function NameCopy($master=null, $foreign_key=null)
{
$title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField');
if (!$title_field) return;
$new_name = $this->GetDBField($title_field);
$cat_id = $this->Application->GetVar('m_cat_id');
$original_checked = false;
do {
if ( preg_match("/Copy ([0-9]*)[ ]*of(.*)/", $new_name, $regs) ) {
$new_name = 'Copy '.($regs[1]+1).' of '.$regs[2];
}
elseif ($original_checked) {
$new_name = 'Copy of '.$new_name;
}
$query = 'SELECT '.$title_field.' FROM '.$this->TableName.'
LEFT JOIN '.TABLE_PREFIX.'CategoryItems ON
('.TABLE_PREFIX.'CategoryItems.ItemResourceId = '.$this->TableName.'.ResourceId)
WHERE ('.TABLE_PREFIX.'CategoryItems.CategoryId = '.$cat_id.') AND '.
$title_field.' = '.$this->Conn->qstr($new_name);
$res = $this->Conn->GetOne($query);
$original_checked = true;
} while ($res !== false);
$this->SetDBField($title_field, $new_name);
}
function MoveToCat($cat_id=null)
{
// $this->NameCopy();
$cat_id = $this->Application->GetVar('m_cat_id');
// check if the product already exists in destination cat
$query = 'SELECT PrimaryCat FROM '.TABLE_PREFIX.'CategoryItems
WHERE CategoryId = '.$cat_id.' AND ItemResourceId = '.$this->GetDBField('ResourceId');
// if it's not found is_primary will be FALSE, if it's found but not primary it will be int 0
$is_primary = $this->Conn->GetOne($query);
$exists = $is_primary !== false;
if ($exists) { // if the Product already exists in destination category
if ($is_primary) return; // do nothing when we paste to primary
// if it's not primary - delete it from destination category,
// as we will move it from current primary below
$query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems
WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND CategoryId = '.$cat_id;
$this->Conn->Query($query);
}
$query = 'UPDATE '.TABLE_PREFIX.'CategoryItems SET CategoryId = '.$cat_id.
' WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND PrimaryCat = 1';
$this->Conn->Query($query);
$this->Update();
}
// We need to delete CategoryItems record when deleting product
function Delete($id=null)
{
if( isset($id) ) {
$this->setID($id);
}
$this->Load($this->GetID());
$ret = parent::Delete();
if ($ret) {
$query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$this->GetDBField('ResourceId');
$this->Conn->Query($query);
}
return $ret;
}
+
+ function DeleteFromCategories($delete_category_ids)
+ {
+ $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); // because item was loaded before by ResourceId
+
+ $ci_table = $this->Application->getUnitOption('ci', 'TableName');
+ $resource_id = $this->GetDBField('ResourceId');
+
+ $item_cats_sql = 'SELECT CategoryId FROM %s WHERE ItemResourceId = %s';
+ $delete_category_items_sql = 'DELETE FROM %s WHERE ItemResourceId = %s';
+
+ $category_ids = $this->Conn->GetCol( sprintf($item_cats_sql, $ci_table, $resource_id) );
+ $cats_left = array_diff($category_ids, $delete_category_ids);
+ if(!$cats_left)
+ {
+ $sql = 'SELECT %s FROM %s WHERE ResourceId = %s';
+ $ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) );
+
+ $temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ $temp->DeleteItems($this->Prefix, $this->Special, $ids);
+ }
+ else
+ {
+ $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id) );
+ if( count($cats_left) == 1 )
+ {
+ $sql = 'REPLACE INTO %s (CategoryId,ItemResourceId,PrimaryCat) VALUES (%s,%s,1)';
+ $this->Conn->Query( sprintf($sql, $ci_table, end($cats_left), $resource_id ) );
+ }
+ }
+ }
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/general/cat_dbitem.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property
Index: trunk/core/units/general/cat_event_handler.php
===================================================================
--- trunk/core/units/general/cat_event_handler.php (revision 2040)
+++ trunk/core/units/general/cat_event_handler.php (revision 2041)
@@ -1,186 +1,186 @@
<?php
$application =& kApplication::Instance();
$application->Factory->includeClassFile('kDBEventHandler');
class kCatDBEventHandler extends InpDBEventHandler {
function OnCopy(&$event)
{
$object = $event->getObject();
$this->StoreSelectedIDs($event);
$ids = $this->getSelectedIDs($event);
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids));
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'copy');
- $this->Application->StoreVar('clipboard', 'COPY-0.'.$object->TableName.'.ResourceId=0');
+ $this->Application->StoreVar('ClipBoard', 'COPY-0.'.$object->TableName.'.ResourceId=0');
$event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY
}
function OnCut(&$event)
{
$object = $event->getObject();
$this->StoreSelectedIDs($event);
$ids = $this->getSelectedIDs($event);
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids));
$this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'cut');
- $this->Application->StoreVar('clipboard', 'CUT-0.'.$object->TableName.'.ResourceId=0');
+ $this->Application->StoreVar('ClipBoard', 'CUT-0.'.$object->TableName.'.ResourceId=0');
$event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY
}
function OnPaste(&$event)
{
$ids = $this->Application->RecallVar($event->getPrefixSpecial().'_clipboard');
if ($ids == '') {
$event->redirect = false;
return;
}
//recalling by different name, because we may get kDBList, if we recall just by prefix
$object =& $this->Application->recallObject($event->getPrefixSpecial().'.item', $event->Prefix);
- $this->prepareObject($object,$event);
+ $this->prepareObject($object, $event);
if ($this->Application->RecallVar($event->getPrefixSpecial().'_clipboard_mode') == 'copy') {
$ids_arr = explode(',', $ids);
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
if($ids_arr)
{
$temp->CloneItems($event->Prefix, $event->Special, $ids_arr);
}
}
else { // mode == cut
$ids_arr = explode(',', $ids);
foreach ($ids_arr as $id) {
$object->Load($id);
$object->MoveToCat();
}
}
$event->status = erSUCCESS;
}
/**
* Occurs when pasting category
*
* @param kEvent $event
*/
function OnCatPaste(&$event)
{
- $inp_clipboard = $this->Application->RecallVar('clipboard');
+ $inp_clipboard = $this->Application->RecallVar('ClipBoard');
$inp_clipboard = explode('-', $inp_clipboard, 2);
if($inp_clipboard[0] == 'COPY')
{
$saved_cat_id = $this->Application->GetVar('m_cat_id');
$cat_ids = $event->getEventParam('cat_ids');
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$ids_sql = 'SELECT '.$id_field.' FROM '.$table.' WHERE ResourceId IN (%s)';
- $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s';
+ $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s AND PrimaryCat = 1';
$this->Application->setUnitOption($event->Prefix,'AutoLoad', false);
$object =& $this->Application->recallObject($event->Prefix.'.item', $event->Prefix);
foreach($cat_ids as $source_cat => $dest_cat)
{
$item_resource_ids = $this->Conn->GetCol( sprintf($resource_ids_sql, $source_cat) );
if(!$item_resource_ids) continue;
$this->Application->SetVar('m_cat_id', $dest_cat);
$item_ids = $this->Conn->GetCol( sprintf($ids_sql, implode(',', $item_resource_ids) ) );
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
if($item_ids) $temp->CloneItems($event->Prefix, $event->Special, $item_ids);
}
$this->Application->setUnitOption($event->Prefix,'AutoLoad', true);
$this->Application->SetVar('m_cat_id', $saved_cat_id);
}
}
/**
* Apply scope clause
*
* @param kEvent $event
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
if ($event->Special != 'showall') {
if ( $event->getEventParam('parent_cat_id') ) {
$parent_cat_id = $event->getEventParam('parent_cat_id');
}
else {
$parent_cat_id = $this->Application->GetVar('c_id');
if (!$parent_cat_id) {
$parent_cat_id = $this->Application->GetVar('m_cat_id');
}
if (!$parent_cat_id) {
$parent_cat_id = 0;
}
}
if ((string) $parent_cat_id != 'any') {
if ($event->getEventParam('recursive')) {
$current_path = $this->Conn->GetOne('SELECT ParentPath FROM '.TABLE_PREFIX.'Category WHERE CategoryId='.$parent_cat_id);
$subcats = $this->Conn->GetCol('SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE ParentPath LIKE "'.$current_path.'%" ');
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.CategoryId IN ('.implode(', ', $subcats).')');
}
else {
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.CategoryId = '.$parent_cat_id );
}
}
}
else {
$object->addFilter('primary_filter', 'PrimaryCat = 1');
}
$view_perm = 1;
$object->addFilter('perm_filter', 'perm.PermId = '.$view_perm);
if (!defined('ADMIN')) {
$groups = explode(',',$this->Application->RecallVar('UserGroups'));
foreach ($groups as $group) {
$view_filters[] = 'FIND_IN_SET('.$group.', perm.acl) || ((NOT FIND_IN_SET('.$group.',perm.dacl)) AND perm.acl=\'\')';
}
$view_filter = implode(' OR ', $view_filters);
$object->addFilter('perm_filter2', $view_filter);
}
if (!defined('ADMIN')) {
$object->addFilter('status_filter', $object->TableName.'.Status = 1');
}
/*$list_type = $event->getEventParam('ListType');
switch($list_type)
{
case 'favorites':
$fav_table = $this->Application->getUnitOption('fav','TableName');
$user_id =& $this->Application->GetVar('u_id');
$sql = 'SELECT DISTINCT f.ResourceId
FROM '.$fav_table.' f
LEFT JOIN '.$object->TableName.' p ON p.ResourceId = f.ResourceId
WHERE f.PortalUserId = '.$user_id;
$ids = $this->Conn->GetCol($sql);
if(!$ids) $ids = Array(-1);
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.PrimaryCat = 1');
$object->addFilter('favorites_filter', '%1$s.`ResourceId` IN ('.implode(',',$ids).')');
break;
case 'search':
$search_results_table = TABLE_PREFIX.'ses_'.$this->Application->GetVar('sid').'_'.TABLE_PREFIX.'Search';
$sql = ' SELECT DISTINCT ResourceId
FROM '.$search_results_table.'
WHERE ItemType=11';
$ids = $this->Conn->GetCol($sql);
if(!$ids) $ids = Array(-1);
$object->addFilter('search_filter', '%1$s.`ResourceId` IN ('.implode(',',$ids).')');
break;
} */
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/general/cat_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.6
\ No newline at end of property
+1.7
\ No newline at end of property
Index: trunk/core/units/category_items/category_items_event_handler.php
===================================================================
--- trunk/core/units/category_items/category_items_event_handler.php (revision 2040)
+++ trunk/core/units/category_items/category_items_event_handler.php (revision 2041)
@@ -1,101 +1,150 @@
<?php
class CategoryItemsEventHander extends InpDBEventHandler
{
/**
* Add's New Category To Product
*
* @param kEvent $event
*/
function OnAddCategory(&$event)
{
$cat_list = (int)rtrim($this->Application->GetVar('cat_list'), ',');
//if(!$cat_list) return true;
$cat_id = (int)$this->Conn->GetOne('SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE ResourceId='.$cat_list);
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
// don't allow duplicate records
$already_added = $this->Conn->GetOne('SELECT * FROM '.$object->TableName.' WHERE CategoryId='.$cat_id.' AND '.$table_info['ForeignKey'].'='.$table_info['ParentId']);
if($already_added === false)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$items_info[0]['CategoryId'] = $cat_id;
$items_info[0][ $table_info['ForeignKey'] ] = $table_info['ParentId'];
$this->Application->SetVar( $event->getPrefixSpecial(true), $items_info );
$rp_backup = $event->redirect_params; // save opener state
$event->setEventParam('ForceCreateId', true);
$this->OnCreate($event);
$event->redirect_params = $rp_backup; // restore opener state
}
}
/**
* Set's new category as primary for product
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
$this->Application->setUnitOption($event->Prefix,'AutoLoad',false);
$object =& $event->getObject();
$this->StoreSelectedIDs($event);
$ids=$this->getSelectedIDs($event);
if($ids)
{
$id = array_shift($ids);
$table_info = $object->getLinkedInfo();
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$id.')');
}
$event->redirect_params = Array('opener' => 's', 'pass_events' => true); //stay!
}
/**
* Apply custom processing to item
*
* @param kEvent $event
*/
function customProcessing(&$event, $type)
{
if($event->Name == 'OnMassDelete')
{
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
switch ($type)
{
case 'before':
$ids = $event->getEventParam('ids');
if($ids)
{
$ids = $this->Conn->GetCol('SELECT CategoryId FROM '.$object->TableName.' WHERE (PrimaryCat=0) AND ('.$table_info['ForeignKey'].'='.$table_info['ParentId'].') AND CategoryId IN ('.implode(',',$ids).')');
$event->setEventParam('ids',$ids);
}
break;
// not needed because 'before' does not allow to delete primary cat!
/*case 'after':
// set 1st not deleted category as primary
$has_primary = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName.' WHERE (PrimaryCat=1) AND ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].')');
if(!$has_primary)
{
$cat_id = $this->Conn->GetOne('SELECT CategoryId FROM '.$object->TableName.' WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$cat_id.')');
}
break;*/
}
}
}
+ /**
+ * Removes primary mark from cloned category items record
+ *
+ * @param kEvent $event
+ */
+ function OnAfterClone(&$event)
+ {
+ $id = $event->getEventParam('id');
+ $table = $this->Application->getUnitOption($event->Prefix, 'TableName');
+ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
+ $sql = 'UPDATE %s SET PrimaryCat = 0 WHERE %s = %s';
+
+ $this->Conn->Query( sprintf($sql, $table, $id_field, $id) );
+ }
+
+ /**
+ * Deletes items of requested type from requested categories.
+ * In case if item is deleted from it's last category, then delete item too.
+ *
+ * @param kEvent $event
+ */
+ function OnDeleteFromCategory(&$event)
+ {
+ $category_ids = $event->getEventParam('category_ids');
+ if(!$category_ids) return false;
+
+ $item_prefix = $event->getEventParam('item_prefix');
+ $this->Application->setUnitOption($item_prefix, 'AutoLoad', false);
+ $item =& $this->Application->recallObject($item_prefix.'.-item');
+
+ $ci_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
+ $item_table = $this->Application->getUnitOption($item_prefix, 'TableName');
+
+ $sql = 'SELECT ItemResourceId, CategoryId FROM %1$s INNER JOIN %2$s ON (%1$s.ResourceId = %2$s.ItemResourceId) WHERE CategoryId IN (%3$s)';
+ $category_items = $this->Conn->Query( sprintf($sql, $item_table, $ci_table, implode(',', $category_ids) ) );
+
+ $item_hash = Array();
+ foreach($category_items as $ci_row)
+ {
+ $item_hash[ $ci_row['ItemResourceId'] ][] = $ci_row['CategoryId'];
+ }
+
+ foreach($item_hash as $item_resource_id => $delete_category_ids)
+ {
+ $item->Load($item_resource_id, 'ResourceId');
+ $item->DeleteFromCategories($delete_category_ids);
+ }
+ }
+
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/category_items/category_items_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property
Index: trunk/core/units/category_items/category_items_config.php
===================================================================
--- trunk/core/units/category_items/category_items_config.php (revision 2040)
+++ trunk/core/units/category_items/category_items_config.php (revision 2041)
@@ -1,63 +1,63 @@
<?php
$config = Array(
'Prefix' => 'ci',
'ItemClass' => Array('class'=>'CategoryItems_DBItem','file'=>'category_items_dbitem.php','build_event'=>'OnItemBuild'),
'ListClass' => Array('class'=>'kDBList','file'=>'','build_event'=>'OnListBuild'),
'EventHandlerClass' => Array('class'=>'CategoryItemsEventHander','file'=>'category_items_event_handler.php','build_event'=>'OnBuild'),
'TagProcessorClass' => Array('class'=>'CategoryItemsTagProcessor','file'=>'category_items_tag_processor.php','build_event'=>'OnBuild'),
'AutoLoad' => true,
'hooks' => Array(),
'QueryString' => Array(
1 => 'id',
2 => 'page',
3 => 'event',
),
'IDField' => 'CategoryId', // in this case idfield doesn't exit in destination table
'TableName' => TABLE_PREFIX.'CategoryItems',
'ParentTableKey'=> 'ResourceId',
'ForeignKey' => 'ItemResourceId',
- 'ParentPrefix' => 'p',
+ 'ParentPrefix' => 'p',
'AutoDelete' => true,
- 'AutoClone' => false, // Critical !!!
+ 'AutoClone' => false,
'CalculatedFields' => Array(
'' => Array (
'CategoryName' => 'c.CachedNavbar',
'DummyId' => 'IF(ISNULL(c.CategoryId),0,c.CategoryId)',
)
),
'ListSQLs' => Array( ''=>' SELECT %1$s.* %2$s
FROM %1$s
LEFT JOIN '.TABLE_PREFIX.'Category AS c ON c.CategoryId = %1$s.CategoryId',
), // key - special, value - list select sql
'ItemSQLs' => Array( ''=>'SELECT * FROM %s',
),
'ListSortings' => Array(
'' => Array(
'Sorting' => Array('CategoryName' => 'asc'),
)
),
'Fields' => Array(
'DummyId' => Array(),
'CategoryId' => Array('type'=>'int','not_null'=>1,'default'=>0),
'ItemResourceId' => Array('not_null' => 1, 'default' => 0),
'PrimaryCat' => Array('type' => 'int', 'not_null' => 1, 'default' => 0),
),
'VirtualFields' => Array(
'CategoryName' => Array(),
'DummyId' => Array(),
),
'Grids' => Array(
'Default' => Array(
'Icons' => Array('default'=>'icon16_custom.gif'), // icons for each StatusField values, if no matches or no statusfield selected, then "default" icon is used
'Fields' => Array(
'CategoryName' => Array( 'title'=>'la_col_Category', 'data_block' => 'grid_checkbox_category_td'),
),
),
),
);
?>
\ No newline at end of file
Property changes on: trunk/core/units/category_items/category_items_config.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property

Event Timeline