Index: kernel/db/cat_event_handler.php =================================================================== --- kernel/db/cat_event_handler.php (revision 15417) +++ kernel/db/cat_event_handler.php (working copy) @@ -332,12 +332,12 @@ $recycle_bin = $this->Application->ConfigValue('RecycleBinFolder'); if ( $recycle_bin ) { - $rb = $this->Application->recallObject('c.recycle', null, array ('skip_autoload' => true)); + $rb = $this->Application->recallObject('c.recycle', NULL, array ('skip_autoload' => true)); /* @var $rb CategoriesItem */ $rb->Load($recycle_bin); - $object = $this->Application->recallObject($event->Prefix . '.recycleitem', null, Array ('skip_autoload' => true)); + $object = $this->Application->recallObject($event->Prefix . '.recycleitem', NULL, Array ('skip_autoload' => true)); /* @var $object kCatDBItem */ foreach ($ids as $id) { @@ -501,7 +501,7 @@ } } - $p_item = $this->Application->recallObject($related_prefix.'.current', null, Array('skip_autoload' => true)); + $p_item = $this->Application->recallObject($related_prefix.'.current', NULL, Array('skip_autoload' => true)); /* @var $p_item kCatDBItem */ $p_item->Load( (int)$id ); @@ -719,7 +719,7 @@ * @param kDBList $object * @param int $user_id */ - function applyPendingEditingFilter(&$object, $user_id = null) + function applyPendingEditingFilter(&$object, $user_id = NULL) { $sql = 'SELECT OrgId FROM '.$object->TableName.' @@ -781,7 +781,7 @@ // hot items (cache updated every hour) if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) { - $serial_name = $this->Application->incrementCacheSerial($event->Prefix, null, false); + $serial_name = $this->Application->incrementCacheSerial($event->Prefix, NULL, false); $hot_limit = $this->Application->getCache($property_map['HotLimit'] . '[%' . $serial_name . '%]'); } else { @@ -834,7 +834,7 @@ $hot_limit = (double)array_shift($res); if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { - $serial_name = $this->Application->incrementCacheSerial($event->Prefix, null, false); + $serial_name = $this->Application->incrementCacheSerial($event->Prefix, NULL, false); $this->Application->setCache($property_map['HotLimit'] . '[%' . $serial_name . '%]', $hot_limit); } else { @@ -2010,7 +2010,7 @@ $image_data = $object->getPrimaryImageData(); - $image = $this->Application->recallObject('img', null, Array ('skip_autoload' => true)); + $image = $this->Application->recallObject('img', NULL, Array ('skip_autoload' => true)); /* @var $image kDBItem */ if ( $image_data ) { @@ -2227,7 +2227,7 @@ $temp_handler = $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); /* @var $temp_handler kTempTablesHandler */ - $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array ($object->GetID()), null, null, null, true); + $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array ($object->GetID()), NULL, NULL, NULL, true); $ci_table = $this->Application->GetTempName(TABLE_PREFIX . 'CategoryItems'); // 2. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem @@ -2330,7 +2330,7 @@ $object->SetDBField('ResourceId', 0); // this will reset it if ( $this->Application->GetVar('ResetCatBeforeClone') ) { - $object->SetDBField('CategoryId', null); + $object->SetDBField('CategoryId', NULL); } } @@ -2508,7 +2508,7 @@ $object->Load($original_id, 'OrgId'); if (!$object->isLoaded()) { // 1. user has no pending copy of live item -> clone live item - $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($original_id), null, null, null, true); + $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($original_id), NULL, NULL, NULL, true); $object->Load($cloned_ids[0]); $object->SetFieldsFromHash($field_values, $this->getRequestProtectedFields($field_values)); @@ -2721,32 +2721,70 @@ /* @var $object kDBList */ // 1. no user sorting - sort by relevance + $default_sortings = parent::_getDefaultSorting($event); + $default_sorting = key($default_sortings['Sorting']) . ',' . current($default_sortings['Sorting']); + if ( $object->isMainList() ) { - if ( !$this->Application->GetVar('sort_by') ) { - $this->Application->SetVar('sort_by', 'Relevance,desc'); + $sort_by = $this->Application->GetVar('sort_by', ''); + + if ( !$sort_by ) { + $this->Application->SetVar('sort_by', 'Relevance,desc|' . $default_sorting); } + elseif ( strpos($sort_by, 'Relevance,') !== false ) { + $this->Application->SetVar('sort_by', $sort_by . '|' . $default_sorting); + } } else { $sorting_settings = $this->getListSetting($event, 'Sortings'); + $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); - if ( !trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ',') ) { - $event->setEventParam('sort_by', 'Relevance,desc'); + if ( !$sort_by ) { + $event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting); } + elseif ( strpos($sort_by, 'Relevance,') !== false ) { + $event->setEventParam('sort_by', $sort_by . '|' . $default_sorting); + } } - // 2. remove default sortings - $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); - unset($config_mapping['DefaultSorting1Field']); - $this->Application->setUnitOption($event->Prefix, 'ConfigMapping', $config_mapping); - - // 2. remove forced sortings - $this->Application->setUnitOption($event->Prefix, 'ListSortings', Array ()); + $this->_removeForcedSortings($event); } parent::SetSorting($event); } /** + * Removes forced sortings + * + * @param kEvent $event + */ + protected function _removeForcedSortings(kEvent $event) + { + $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); + /* @var $list_sortings Array */ + + foreach ($list_sortings as $special => $sortings) { + unset($list_sortings[$special]['ForcedSorting']); + } + + $this->Application->setUnitOption($event->Prefix, 'ListSortings', $list_sortings); + } + + /** + * Default sorting in search results only comes from relevance field + * + * @param kEvent $event + * @return Array + * @access protected + */ + protected function _getDefaultSorting(kEvent $event) + { + $types = $event->getEventParam('types'); + $types = $types ? explode(',', $types) : Array (); + + return in_array('search', $types) ? Array () : parent::_getDefaultSorting($event); + } + + /** * Returns current per-page setting for list * * @param kEvent $event Index: kernel/db/db_event_handler.php =================================================================== --- kernel/db/db_event_handler.php (revision 15425) +++ kernel/db/db_event_handler.php (working copy) @@ -36,7 +36,6 @@ * */ - /** * EventHandler that is used to process * any database related events @@ -139,7 +138,6 @@ 'OnExportBegin' => Array ('self' => 'view|advanced:export'), 'OnExportProgress' => Array ('self' => 'view|advanced:export'), - 'OnSetAutoRefreshInterval' => Array ('self' => true, 'subitem' => true), 'OnAutoRefreshToggle' => Array ('self' => true, 'subitem' => true), @@ -224,7 +222,7 @@ $list_helper = $this->Application->recallObject('ListHelper'); /* @var $list_helper ListHelper */ - $select_clause = $this->Application->getUnitOption($object->Prefix, 'NavigationSelectClause', null); + $select_clause = $this->Application->getUnitOption($object->Prefix, 'NavigationSelectClause', NULL); return $list_helper->getNavigationResource($object, $event->getEventParam('list'), $event->Special == 'next', $select_clause); } @@ -281,7 +279,7 @@ * @return Array * @access protected */ - protected function StoreSelectedIDs(kEvent $event, $direct_ids = null) + protected function StoreSelectedIDs(kEvent $event, $direct_ids = NULL) { $wid = $this->Application->GetTopmostWid($event->Prefix); $session_name = rtrim($event->getPrefixSpecial() . '_selected_ids_' . $wid, '_'); @@ -1059,7 +1057,12 @@ $cur_sort1 = $cur_sort1_dir = $cur_sort2 = $cur_sort2_dir = false; if ( $sort_by ) { - list ($cur_sort1, $cur_sort1_dir) = explode(',', $sort_by); + $sortings = explode('|', $sort_by); + list ($cur_sort1, $cur_sort1_dir) = explode(',', $sortings[0]); + + if ( isset($sortings[1]) ) { + list ($cur_sort2, $cur_sort2_dir) = explode(',', $sortings[1]); + } } } else { @@ -1088,24 +1091,11 @@ } } - $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); - $sorting_prefix = array_key_exists($event->Special, $list_sortings) ? $event->Special : ''; - $sorting_configs = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); + $list_sortings = $this->_getDefaultSorting($event); - if ( $sorting_configs && array_key_exists('DefaultSorting1Field', $sorting_configs) ) { - // sorting defined in configuration variables overrides one from unit config - $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']), - ); - - // TODO: lowercase configuration variable values in db, instead of here - $list_sortings[$sorting_prefix]['Sorting'] = array_map('strtolower', $list_sortings[$sorting_prefix]['Sorting']); - } - // use default if not specified in session if ( !$cur_sort1 || !$cur_sort1_dir ) { - $sorting = getArrayValue($list_sortings, $sorting_prefix, 'Sorting'); + $sorting = getArrayValue($list_sortings, 'Sorting'); if ( $sorting ) { reset($sorting); @@ -1120,7 +1110,7 @@ } // always add forced sorting before any user sorting fields - $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting'); + $forced_sorting = getArrayValue($list_sortings, 'ForcedSorting'); /* @var $forced_sorting Array */ if ( $forced_sorting ) { @@ -1140,6 +1130,33 @@ } /** + * Returns default list sortings + * + * @param kEvent $event + * @return Array + * @access protected + */ + protected function _getDefaultSorting(kEvent $event) + { + $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); + $sorting_prefix = array_key_exists($event->Special, $list_sortings) ? $event->Special : ''; + $sorting_configs = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); + + if ( $sorting_configs && array_key_exists('DefaultSorting1Field', $sorting_configs) ) { + // sorting defined in configuration variables overrides one from unit config + $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']), + ); + + // TODO: lowercase configuration variable values in db, instead of here + $list_sortings[$sorting_prefix]['Sorting'] = array_map('strtolower', $list_sortings[$sorting_prefix]['Sorting']); + } + + return isset($list_sortings[$sorting_prefix]) ? $list_sortings[$sorting_prefix] : Array (); + } + + /** * Gets list setting by name (persistent or real session) * * @param kEvent $event @@ -1177,7 +1194,7 @@ * @return void * @access protected */ - protected function setListSetting(kEvent $event, $variable_name, $variable_value = null) + protected function setListSetting(kEvent $event, $variable_name, $variable_value = NULL) { $view_name = $this->Application->RecallVar($event->getPrefixSpecial() . '_current_view'); // $this->Application->StoreVar($event->getPrefixSpecial() . '_' . $variable_name, $variable_value, true); //true for optional @@ -2330,7 +2347,6 @@ $event->SetRedirectParam('opener', 'u'); } - /* End of Edit events */ // III. Events that allow to put some code before and after Update,Load,Create and Delete methods of item @@ -2804,7 +2820,7 @@ if ( $resource_id ) { $t_prefixes = explode(',', $this->Application->GetVar('translator_prefixes')); - $cdata = $this->Application->recallObject($t_prefixes[1], null, Array ('skip_autoload' => true)); + $cdata = $this->Application->recallObject($t_prefixes[1], NULL, Array ('skip_autoload' => true)); /* @var $cdata kDBItem */ $cdata->Load($resource_id, 'ResourceId'); @@ -2899,7 +2915,6 @@ $event->SetRedirectParam('opener', 'u'); } - /** EXPORT RELATED **/ /** Index: units/categories/categories_event_handler.php =================================================================== --- units/categories/categories_event_handler.php (revision 15421) +++ units/categories/categories_event_handler.php (working copy) @@ -1606,31 +1606,69 @@ /* @var $object kDBList */ // 1. no user sorting - sort by relevance + $default_sortings = parent::_getDefaultSorting($event); + $default_sorting = key($default_sortings['Sorting']) . ',' . current($default_sortings['Sorting']); + if ( $object->isMainList() ) { - if ( !$this->Application->GetVar('sort_by') ) { - $this->Application->SetVar('sort_by', 'Relevance,desc'); + $sort_by = $this->Application->GetVar('sort_by', ''); + + if ( !$sort_by ) { + $this->Application->SetVar('sort_by', 'Relevance,desc|' . $default_sorting); } + elseif ( strpos($sort_by, 'Relevance,') !== false ) { + $this->Application->SetVar('sort_by', $sort_by . '|' . $default_sorting); + } } else { $sorting_settings = $this->getListSetting($event, 'Sortings'); + $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); - if ( !trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ',') ) { - $event->setEventParam('sort_by', 'Relevance,desc'); + if ( !$sort_by ) { + $event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting); } + elseif ( strpos($sort_by, 'Relevance,') !== false ) { + $event->setEventParam('sort_by', $sort_by . '|' . $default_sorting); + } } - // 2. remove default sortings - $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); - unset($config_mapping['DefaultSorting1Field']); - $this->Application->setUnitOption($event->Prefix, 'ConfigMapping', $config_mapping); - - // 2. remove forced sortings - $this->Application->setUnitOption($event->Prefix, 'ListSortings', Array ()); + $this->_removeForcedSortings($event); } parent::SetSorting($event); } + /** + * Removes forced sortings + * + * @param kEvent $event + */ + protected function _removeForcedSortings(kEvent $event) + { + $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); + /* @var $list_sortings Array */ + + foreach ($list_sortings as $special => $sortings) { + unset($list_sortings[$special]['ForcedSorting']); + } + + $this->Application->setUnitOption($event->Prefix, 'ListSortings', $list_sortings); + } + + /** + * Default sorting in search results only comes from relevance field + * + * @param kEvent $event + * @return Array + * @access protected + */ + protected function _getDefaultSorting(kEvent $event) + { + $types = $event->getEventParam('types'); + $types = $types ? explode(',', $types) : Array (); + + return in_array('search', $types) ? Array () : parent::_getDefaultSorting($event); + } + // ============= for cms page processing ======================= /**