Index: trunk/core/units/general/cat_event_handler.php =================================================================== diff -u -r3629 -r3635 --- trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3629) +++ trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3635) @@ -151,14 +151,151 @@ } /** - * Apply scope clause + * Return type clauses for list bulding on front * * @param kEvent $event + * @return Array */ + function getTypeClauses(&$event) + { + $types = $event->getEventParam('types'); + $except_types = $event->getEventParam('except'); + $type_clauses = Array(); + + $type_clauses['pick']['include'] = '%1$s.EditorsPick = 1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; + $type_clauses['pick']['except'] = '%1$s.EditorsPick! = 1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; + $type_clauses['pick']['having_filter'] = false; + + $type_clauses['hot']['include'] = '`IsHot` = 1 AND PrimaryCat = 1'; + $type_clauses['hot']['except'] = '`IsHot`! = 1 AND PrimaryCat = 1'; + $type_clauses['hot']['having_filter'] = true; + + $type_clauses['pop']['include'] = '`IsPop` = 1 AND PrimaryCat = 1'; + $type_clauses['pop']['except'] = '`IsPop`! = 1 AND PrimaryCat = 1'; + $type_clauses['pop']['having_filter'] = true; + + $type_clauses['new']['include'] = '`IsNew` = 1 AND PrimaryCat = 1'; + $type_clauses['new']['except'] = '`IsNew`! = 1 AND PrimaryCat = 1'; + $type_clauses['new']['having_filter'] = true; + + $type_clauses['displayed']['include'] = ''; + $displayed = $this->Application->GetVar($event->Prefix.'_displayed_ids'); + if ($displayed) { + $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); + $type_clauses['displayed']['except'] = '%1$s.'.$id_field.' NOT IN ('.$displayed.')'; + } + else { + $type_clauses['displayed']['except'] = ''; + } + $type_clauses['displayed']['having_filter'] = false; + + if (strpos($types, 'search') !== false || strpos($except_types, 'search') !== false) { + $event_mapping = Array( + 'simple' => 'OnSimpleSearch', + 'subsearch' => 'OnSubSearch', + 'advanced' => 'OnAdvancedSearch'); + if($this->Application->GetVar('INPORTAL_ON') && $this->Application->GetVar('Action') == 'm_simple_subsearch') + { + $type = 'subsearch'; + } + else + { + $type = $this->Application->GetVar('search_type') ? $this->Application->GetVar('search_type') : 'simple'; + } + + if($keywords = $event->getEventParam('keyword_string')) // processing keyword_string param of ListProducts tag + { + $this->Application->SetVar('keywords', $keywords); + $type = 'simple'; + } + $search_event = $event_mapping[$type]; + $this->$search_event($event); + $search_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search'; + $sql = 'SHOW TABLES LIKE "'.$search_table.'"'; + if ( $this->Conn->Query($sql) ) { + $search_res_ids = $this->Conn->GetCol('SELECT ResourceId FROM '.$search_table); + } + + if ($search_res_ids) { + $type_clauses['search']['include'] = '%1$s.ResourceId IN ('.implode(',', $search_res_ids).') AND PrimaryCat = 1'; + $type_clauses['search']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $search_res_ids).') AND PrimaryCat = 1'; + } + else { + $type_clauses['search']['include'] = '0'; + $type_clauses['search']['except'] = '1'; + } + $type_clauses['search']['having_filter'] = false; + } + + if (strpos($types, 'related') !== false || strpos($except_types, 'related') !== false) { + + $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)); + $p_item->Load( $this->Application->GetVar($related_prefix.'_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).') AND PrimaryCat = 1'; + $type_clauses['related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_ids).') AND PrimaryCat = 1'; + } + else { + $type_clauses['related']['include'] = '0'; + $type_clauses['related']['except'] = '1'; + } + $type_clauses['related']['having_filter'] = false; + } + + return $type_clauses; + } + + /** + * Apply filters to list + * + * @param kEvent $event + */ function SetCustomQuery(&$event) { + parent::SetCustomQuery($event); + $object =& $event->getObject(); + // add category filter if needed if ($event->Special != 'showall') { if ( $event->getEventParam('parent_cat_id') ) { $parent_cat_id = $event->getEventParam('parent_cat_id'); @@ -188,6 +325,7 @@ $object->addFilter('primary_filter', 'PrimaryCat = 1'); } + // add permission filter $view_perm = 1; $object->addFilter('perm_filter', 'perm.PermId = '.$view_perm); @@ -203,6 +341,61 @@ $object->addFilter('status_filter', $object->TableName.'.Status = 1'); } + $types = $event->getEventParam('types'); + $except_types = $event->getEventParam('except'); + $type_clauses = $this->getTypeClauses($event); + + // convert prepared type clauses into list filters + $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; $i < sizeof($types_array); $i++) { + $type = trim($types_array[$i]); + if (isset($type_clauses[$type])) { + if ($type_clauses[$type]['having_filter']) { + $includes_or_filter_h->removeFilter('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; $i < sizeof($except_types_array); $i++) { + $type = trim($except_types_array[$i]); + if (isset($type_clauses[$type])) { + if ($type_clauses[$type]['having_filter']) { + $excepts_and_filter_h->removeFilter('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']); + } + } + } + } + + /*if ( !$this->Application->IsAdmin() ) { + $object->addFilter('expire_filter', '%1$s.Expire IS NULL OR %1$s.Expire > UNIX_TIMESTAMP()'); + }*/ + /*$list_type = $event->getEventParam('ListType'); switch($list_type) { @@ -229,6 +422,12 @@ $object->addFilter('search_filter', '%1$s.`ResourceId` IN ('.implode(',',$ids).')'); break; } */ + + $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); } /** @@ -1416,6 +1615,32 @@ /* === RELATED TO IMPORT/EXPORT: END === */ + function BuildListSpecial($params) + { + if ($this->Special != '') return $this->Special; + if ( isset($params['parent_cat_id']) ) { + $parent_cat_id = $params['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'); + } + } + + $recursive = isset($params['recursive']); + + $types = $this->SelectParam($params, 'types'); + $except = $this->SelectParam($params, 'except'); + + if ($types.$except.$recursive == '') { + return parent::BuildListSpecial($params); + } + + $special = crc32($parent_cat_id.$types.$except.$recursive.$manufacturer); + return $special; + } + } ?> \ No newline at end of file