Index: branches/RC/core/units/categories/categories_event_handler.php =================================================================== diff -u -r10538 -r10671 --- branches/RC/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 10538) +++ branches/RC/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 10671) @@ -134,10 +134,6 @@ { parent::SetCustomQuery($event); - $types=$event->getEventParam('types'); - $except_types=$event->getEventParam('except'); - $type_clauses = Array(); - $object =& $event->getObject(); /* @var $object kDBList */ @@ -160,7 +156,7 @@ if ($event->getEventParam('parent_cat_id') !== false) { $parent_cat_id = $event->getEventParam('parent_cat_id'); - if ($parent_cat_id == 'Root') { + if ("$parent_cat_id" == 'Root') { $module_name = $event->getEventParam('module') ? $event->getEventParam('module') : 'In-Commerce'; $parent_cat_id = $this->Application->findModule('Name', $module_name, 'RootCat'); } @@ -206,15 +202,98 @@ $object->addFilter('status_filter', $object->TableName.'.Status = 1'); } - /* TO DO - if(strpos($types, 'related') !== false) - { - $type_clauses = $this->getTypeClauses($event); + // process "types" and "except" parameters + $type_clauses = Array(); + + $types = $event->getEventParam('types'); + $types = $types ? explode(',', $types) : Array (); + + $except_types = $event->getEventParam('except'); + $except_types = $except_types ? explode(',', $except_types) : Array (); + + if (in_array('related', $types) || in_array('related', $except_types)) { + $related_to = $event->getEventParam('related_to'); + if (!$related_to) { + $related_prefix = $event->Prefix; + } + else { + $sql = 'SELECT Prefix + FROM '.TABLE_PREFIX.'ItemTypes + WHERE ItemName = '.$this->Conn->qstr($related_to); + $related_prefix = $this->Conn->GetOne($sql); + } + + $rel_table = $this->Application->getUnitOption('rel', 'TableName'); + $item_type = $this->Application->getUnitOption($event->Prefix, 'ItemType'); + + // process case, then this list is called inside another list + $prefix_special = $event->getEventParam('PrefixSpecial'); + if (!$prefix_special) { + $prefix_special = $this->Application->Parser->GetParam('PrefixSpecial'); + } + + $id = false; + if ($prefix_special !== false) { + $processed_prefix = $this->Application->processPrefix($prefix_special); + if ($processed_prefix['prefix'] == $related_prefix) { + // printing related categories within list of items (not on details page) + $list =& $this->Application->recallObject($prefix_special); + /* @var $list kDBList */ + + $id = $list->GetID(); + } + } + + if ($id === false) { + // printing related categories for single item (possibly on details page) + if ($related_prefix == 'c') { + $id = $this->Application->GetVar('m_cat_id'); + } + else { + $id = $this->Application->GetVar($related_prefix . '_id'); + } + } + + $p_item =& $this->Application->recallObject($related_prefix . '.current', null, Array('skip_autoload' => true)); + $p_item->Load( (int)$id ); + + $p_resource_id = $p_item->GetDBField('ResourceId'); + + $sql = 'SELECT SourceId, TargetId FROM '.$rel_table.' + WHERE + (Enabled = 1) + AND ( + (Type = 0 AND SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.') + OR + (Type = 1 + AND ( + (SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.') + OR + (TargetId = '.$p_resource_id.' AND SourceType = '.$item_type.') + ) + ) + )'; + + $related_ids_array = $this->Conn->Query($sql); + $related_ids = Array(); + + foreach ($related_ids_array as $key => $record) { + $related_ids[] = $record[ $record['SourceId'] == $p_resource_id ? 'TargetId' : 'SourceId' ]; + } + + if (count($related_ids) > 0) { + $type_clauses['related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_ids).')'; + $type_clauses['related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_ids).')'; + } + else { + $type_clauses['related']['include'] = '0'; + $type_clauses['related']['except'] = '1'; + } + + $type_clauses['related']['having_filter'] = false; } - */ - if(strpos($types, 'category_related') !== false) - { + if (in_array('category_related', $type_clauses)) { $object->removeFilter('parent_filter'); $resource_id = $this->Conn->GetOne(' SELECT ResourceId FROM '.$this->Application->getUnitOption($event->Prefix, 'TableName').' @@ -225,14 +304,14 @@ WHERE SourceId = '.$resource_id.' AND SourceType = 1'; $related_cats = $this->Conn->GetCol($sql); $related_cats = is_array($related_cats) ? $related_cats : Array(); + $sql = 'SELECT DISTINCT(SourceId) FROM '.TABLE_PREFIX.'Relationship WHERE TargetId = '.$resource_id.' AND TargetType = 1 AND Type = 1'; $related_cats2 = $this->Conn->GetCol($sql); $related_cats2 = is_array($related_cats2) ? $related_cats2 : Array(); $related_cats = array_unique( array_merge( $related_cats2, $related_cats ) ); - if($related_cats) - { + if ($related_cats) { $type_clauses['category_related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_cats).')'; $type_clauses['category_related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_cats).')'; } @@ -244,8 +323,7 @@ $type_clauses['category_related']['having_filter'] = false; } - if(strpos($types, 'product_related') !== false) - { + if (in_array('product_related', $types)) { $object->removeFilter('parent_filter'); $product_id = $event->getEventParam('product_id') ? $event->getEventParam('product_id') : $this->Application->GetVar('p_id'); @@ -264,74 +342,22 @@ $related_cats2 = is_array($related_cats2) ? $related_cats2 : Array(); $related_cats = array_unique( array_merge( $related_cats2, $related_cats ) ); - if($related_cats) - { + if ($related_cats) { $type_clauses['product_related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_cats).')'; $type_clauses['product_related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_cats).')'; } - else - { + else { $type_clauses['product_related']['include'] = '0'; $type_clauses['product_related']['except'] = '1'; } + $type_clauses['product_related']['having_filter'] = false; } - /********************************************/ + $search_helper =& $this->Application->recallObject('SearchHelper'); + /* @var $search_helper kSearchHelper */ - - - $includes_or_filter =& $this->Application->makeClass('kMultipleFilter'); - $includes_or_filter->setType(FLT_TYPE_OR); - - $excepts_and_filter =& $this->Application->makeClass('kMultipleFilter'); - $excepts_and_filter->setType(FLT_TYPE_AND); - - $includes_or_filter_h =& $this->Application->makeClass('kMultipleFilter'); - $includes_or_filter_h->setType(FLT_TYPE_OR); - - $excepts_and_filter_h =& $this->Application->makeClass('kMultipleFilter'); - $excepts_and_filter_h->setType(FLT_TYPE_AND); - - $except_types_array=explode(',', $types); - - if ($types){ - $types_array=explode(',', $types); - for ($i=0; $iremoveFilter('filter_'.$type); - $includes_or_filter_h->addFilter('filter_'.$type, $type_clauses[$type]['include']); - }else{ - $includes_or_filter->removeFilter('filter_'.$type); - $includes_or_filter->addFilter('filter_'.$type, $type_clauses[$type]['include']); - } - } - } - } - - if ($except_types){ - $except_types_array=explode(',', $except_types); - for ($i=0; $iremoveFilter('filter_'.$type); - $excepts_and_filter_h->addFilter('filter_'.$type, $type_clauses[$type]['except']); - }else{ - $excepts_and_filter->removeFilter('filter_'.$type); - $excepts_and_filter->addFilter('filter_'.$type, $type_clauses[$type]['except']); - } - } - } - } - - $object->addFilter('includes_filter', $includes_or_filter); - $object->addFilter('excepts_filter', $excepts_and_filter); - - $object->addFilter('includes_filter_h', $includes_or_filter_h, HAVING_FILTER); - $object->addFilter('excepts_filter_h', $excepts_and_filter_h, HAVING_FILTER); + $search_helper->SetComplexFilter($event, $type_clauses, implode(',', $types), implode(',', $except_types)); } /** @@ -813,89 +839,6 @@ } /** - * Return type clauses for list bulding on front - * - * @param kEvent $event - * @return Array - */ - function getTypeClauses(&$event) - { - $types = $event->getEventParam('types'); - $types = $types ? explode(',', $types) : Array (); - - $except_types = $event->getEventParam('except'); - $except_types = $except_types ? explode(',', $except_types) : Array (); - - $type_clauses = Array(); - - if (in_array('related', $types) || in_array('related', $except_types)) { - - - $related_to = $event->getEventParam('related_to'); - if (!$related_to) { - $related_prefix = $event->Prefix; - } - else { - $sql = 'SELECT Prefix - FROM '.TABLE_PREFIX.'ItemTypes - WHERE ItemName = '.$this->Conn->qstr($related_to); - $related_prefix = $this->Conn->GetOne($sql); - } - - $rel_table = $this->Application->getUnitOption('rel', 'TableName'); - $item_type = $this->Application->getUnitOption($event->Prefix, 'ItemType'); - - $p_item =& $this->Application->recallObject($related_prefix.'.current', null, Array('skip_autoload' => true)); - - $cat_id = $this->Application->GetVar('c_id'); - if (!$cat_id) { - $cat_id = $this->Application->GetVar('m_cat_id'); - } - if (!$cat_id) { - $cat_id = 0; - } - - $p_item->Load($cat_id); - - $p_resource_id = $p_item->GetDBField('ResourceId'); - - $sql = 'SELECT SourceId, TargetId FROM '.$rel_table.' - WHERE - (Enabled = 1) - AND ( - (Type = 0 AND SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.') - OR - (Type = 1 - AND ( - (SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.') - OR - (TargetId = '.$p_resource_id.' AND SourceType = '.$item_type.') - ) - ) - )'; - - $related_ids_array = $this->Conn->Query($sql); - $related_ids = Array(); - - foreach ($related_ids_array as $key => $record) { - $related_ids[] = $record[ $record['SourceId'] == $p_resource_id ? 'TargetId' : 'SourceId' ]; - } - - if (count($related_ids) > 0) { - $type_clauses['related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_ids).')'; - $type_clauses['related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_ids).')'; - } - else { - $type_clauses['related']['include'] = '0'; - $type_clauses['related']['except'] = '1'; - } - $type_clauses['related']['having_filter'] = false; - } - - return $type_clauses; - } - - /** * Set's correct page for list * based on data provided with event *