Index: install/upgrades.php =================================================================== --- install/upgrades.php (revision 14653) +++ install/upgrades.php (working copy) @@ -729,8 +729,8 @@ /** * Group list records by header, saves internal order in group * - * @param Array $records - * @param string $heading_field + * @param Array $variables + * @return Array */ function _groupRecords($variables) { @@ -754,6 +754,7 @@ * Returns module root category * * @param string $module_name + * @param string $module_prefix * @return int */ function _getRootCategory($module_name, $module_prefix) @@ -1814,4 +1815,74 @@ $this->Conn->Query($sql); } } + + /** + * Update to 5.2.0-B1; Transform list sortings storage + * + * @param string $mode when called mode {before, after) + */ + public function Upgrade_5_2_0_B1($mode) + { + if ($mode == 'after') { + $this->transformSortings(); + } + } + + /** + * Transforms a way, how list sortings are stored + * + * @return void + */ + function transformSortings() + { + $sql = 'SELECT VariableName, PortalUserId + FROM ' . TABLE_PREFIX . 'PersistantSessionData + WHERE VariableName LIKE "%_Sort1.%"'; + $sortings = $this->Conn->Query($sql); + + foreach ($sortings AS $sorting) { + if ( !preg_match('/^(.*)_Sort1.(.*)$/', $sorting['VariableName'], $regs) ) { + continue; + } + + $user_id = $sorting['PortalUserId']; + $prefix_special = $regs[1] . '_'; + $view_name = '.' . $regs[2]; + + $old_variable_names = Array ( + $prefix_special . 'Sort1' . $view_name, $prefix_special . 'Sort1_Dir' . $view_name, + $prefix_special . 'Sort2' . $view_name, $prefix_special . 'Sort2_Dir' . $view_name, + ); + $old_variable_names = array_map(Array (&$this->Conn, 'qstr'), $old_variable_names); + + $sql = 'SELECT VariableValue, VariableName + FROM ' . TABLE_PREFIX . 'PersistantSessionData + WHERE PortalUserId = ' . $user_id . ' AND VariableName IN (' . implode(',', $old_variable_names) . ')'; + $sorting_data = $this->Conn->GetCol($sql, 'VariableName'); + + // prepare & save new sortings + $new_sorting = Array ( + 'Sort1' => $sorting_data[$prefix_special . 'Sort1' . $view_name], + 'Sort1_Dir' => $sorting_data[$prefix_special . 'Sort1_Dir' . $view_name], + ); + + if ( isset($sorting_data[$prefix_special . 'Sort2' . $view_name]) ) { + $new_sorting['Sort2'] = $sorting_data[$prefix_special . 'Sort2' . $view_name]; + $new_sorting['Sort2_Dir'] = $sorting_data[$prefix_special . 'Sort2_Dir' . $view_name]; + } + + $fields_hash = Array ( + 'PortalUserId' => $user_id, + 'VariableName' => $prefix_special . 'Sortings' . $view_name, + 'VariableValue' => serialize($new_sorting), + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'PersistantSessionData'); + + // delete sortings, that were already processed + $sql = 'DELETE FROM ' . TABLE_PREFIX . 'PersistantSessionData + WHERE PortalUserId = ' . $user_id . ' AND VariableName IN (' . implode(',', $old_variable_names) . ')'; + $this->Conn->Query($sql); + } + } } \ No newline at end of file Index: kernel/db/db_event_handler.php =================================================================== --- kernel/db/db_event_handler.php (revision 14681) +++ kernel/db/db_event_handler.php (working copy) @@ -401,7 +401,7 @@ /** * Common builder part for Item & List * - * @param kDBBase $object + * @param kDBBase|kDBItem|kDBList $object * @param kEvent $event * @access private */ @@ -988,19 +988,21 @@ $object =& $event->getObject(); /* @var $object kDBList */ - if ($object->isMainList()) { + if ( $object->isMainList() ) { $sort_by = $this->Application->GetVar('sort_by'); $cur_sort1 = $cur_sort1_dir = $cur_sort2 = $cur_sort2_dir = false; - if ($sort_by) { + if ( $sort_by ) { list ($cur_sort1, $cur_sort1_dir) = explode(',', $sort_by); } } else { - $cur_sort1 = $this->getListSetting($event, 'Sort1'); - $cur_sort1_dir = $this->getListSetting($event, 'Sort1_Dir'); - $cur_sort2 = $this->getListSetting($event, 'Sort2'); - $cur_sort2_dir = $this->getListSetting($event, 'Sort2_Dir'); + $sorting_settings = $this->getListSetting($event, 'Sortings'); + + $cur_sort1 = getArrayValue($sorting_settings, 'Sort1'); + $cur_sort1_dir = getArrayValue($sorting_settings, 'Sort1_Dir'); + $cur_sort2 = getArrayValue($sorting_settings, 'Sort2'); + $cur_sort2_dir = getArrayValue($sorting_settings, 'Sort2_Dir'); } $tag_sort_by = $event->getEventParam('sort_by'); @@ -1036,15 +1038,15 @@ } // use default if not specified in session - if (!$cur_sort1 || !$cur_sort1_dir) { + if ( !$cur_sort1 || !$cur_sort1_dir ) { $sorting = getArrayValue($list_sortings, $sorting_prefix, 'Sorting'); - if ($sorting) { + if ( $sorting ) { reset($sorting); $cur_sort1 = key($sorting); $cur_sort1_dir = current($sorting); - if (next($sorting)) { + if ( next($sorting) ) { $cur_sort2 = key($sorting); $cur_sort2_dir = current($sorting); } @@ -1055,18 +1057,18 @@ $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting'); /* @var $forced_sorting Array */ - if ($forced_sorting) { + if ( $forced_sorting ) { foreach ($forced_sorting as $field => $dir) { $object->AddOrderField($field, $dir); } } // add user sorting fields - if ($cur_sort1 != '' && $cur_sort1_dir != '') { + if ( $cur_sort1 != '' && $cur_sort1_dir != '' ) { $object->AddOrderField($cur_sort1, $cur_sort1_dir); } - if ($cur_sort2 != '' && $cur_sort2_dir != '') { + if ( $cur_sort2 != '' && $cur_sort2_dir != '' ) { $object->AddOrderField($cur_sort2, $cur_sort2_dir); } } @@ -1076,14 +1078,15 @@ * * @param kEvent $event * @param string $variable_name - * @return string + * @return string|Array + * @access protected */ - function getListSetting(&$event, $variable_name) + protected function getListSetting(&$event, $variable_name) { $view_name = $this->Application->RecallVar($event->getPrefixSpecial() . '_current_view'); $storage_prefix = $event->getEventParam('same_special') ? $event->Prefix : $event->getPrefixSpecial(); - // get sorting from pesistent session + // get sorting from persistent session $variable_value = $this->Application->RecallPersistentVar($storage_prefix . '_' . $variable_name . '.' . $view_name, ALLOW_DEFAULT_SETTINGS); /*if (!$variable_value) { @@ -1091,6 +1094,10 @@ $variable_value = $this->Application->RecallVar($storage_prefix . '_' . $variable_name); }*/ + if ( kUtil::IsSerialized($variable_value) ) { + $variable_value = unserialize($variable_value); + } + return $variable_value; } @@ -1099,14 +1106,20 @@ * * @param kEvent $event * @param string $variable_name - * @param string $variable_value + * @param string|Array $variable_value + * @return void + * @access protected */ - function setListSetting(&$event, $variable_name, $variable_value = null) + protected function setListSetting(&$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 if ( isset($variable_value) ) { + if ( is_array($variable_value) ) { + $variable_value = serialize($variable_value); + } + $this->Application->StorePersistentVar($event->getPrefixSpecial() . '_' . $variable_name . '.' . $view_name, $variable_value, true); //true for optional } else { @@ -1201,36 +1214,39 @@ */ function OnSetSorting(&$event) { - $cur_sort1 = $this->getListSetting($event, 'Sort1'); - $cur_sort1_dir = $this->getListSetting($event, 'Sort1_Dir'); + $sorting_settings = $this->getListSetting($event, 'Sortings'); + $cur_sort1 = getArrayValue($sorting_settings, 'Sort1'); + $cur_sort1_dir = getArrayValue($sorting_settings, 'Sort1_Dir'); $use_double_sorting = $this->Application->ConfigValue('UseDoubleSorting'); - if ($use_double_sorting) { - $cur_sort2 = $this->getListSetting($event, 'Sort2'); - $cur_sort2_dir = $this->getListSetting($event, 'Sort2_Dir'); + if ( $use_double_sorting ) { + $cur_sort2 = getArrayValue($sorting_settings, 'Sort2'); + $cur_sort2_dir = getArrayValue($sorting_settings, 'Sort2_Dir'); } $passed_sort1 = $this->Application->GetVar($event->getPrefixSpecial(true) . '_Sort1'); - if ($cur_sort1 == $passed_sort1) { + if ( $cur_sort1 == $passed_sort1 ) { $cur_sort1_dir = $cur_sort1_dir == 'asc' ? 'desc' : 'asc'; } else { - if ($use_double_sorting) { + if ( $use_double_sorting ) { $cur_sort2 = $cur_sort1; $cur_sort2_dir = $cur_sort1_dir; } + $cur_sort1 = $passed_sort1; $cur_sort1_dir = 'asc'; } - $this->setListSetting($event, 'Sort1', $cur_sort1); - $this->setListSetting($event, 'Sort1_Dir', $cur_sort1_dir); + $sorting_settings = Array ('Sort1' => $cur_sort1, 'Sort1_Dir' => $cur_sort1_dir); - if ($use_double_sorting) { - $this->setListSetting($event, 'Sort2', $cur_sort2); - $this->setListSetting($event, 'Sort2_Dir', $cur_sort2_dir); + if ( $use_double_sorting ) { + $sorting_settings['Sort2'] = $cur_sort2; + $sorting_settings['Sort2_Dir'] = $cur_sort2_dir; } + + $this->setListSetting($event, 'Sortings', $sorting_settings); } /** @@ -1244,18 +1260,17 @@ $prefix_special = $event->getPrefixSpecial(); $combined = $this->Application->GetVar($event->getPrefixSpecial(true) . '_CombinedSorting'); - if ($combined) { + if ( $combined ) { list ($field, $dir) = explode('|', $combined); - if ($this->Application->isAdmin || !$this->Application->GetVar('main_list')) { - $this->setListSetting($event, 'Sort1', $field); - $this->setListSetting($event, 'Sort1_Dir', $dir); + if ( $this->Application->isAdmin || !$this->Application->GetVar('main_list') ) { + $this->setListSetting($event, 'Sortings', Array ('Sort1' => $field, 'Sort1_Dir' => $dir)); } else { $event->setPseudoClass('_List'); $this->Application->SetVar('sort_by', $field . ',' . $dir); - $object =& $event->getObject( Array ('main_list' => 1) ); + $object =& $event->getObject(Array ('main_list' => 1)); /* @var $object kDBList */ $list_helper =& $this->Application->recallObject('ListHelper'); @@ -1263,14 +1278,14 @@ $this->_passListParams($event, 'sort_by'); - if ($list_helper->hasUserSorting($object)) { + if ( $list_helper->hasUserSorting($object) ) { $event->SetRedirectParam('sort_by', $field . ',' . strtolower($dir)); } $event->SetRedirectParam('pass', 'm'); } - return ; + return; } // used in "View Menu -> Sort" menu in administrative console @@ -1286,10 +1301,7 @@ */ function OnResetSorting(&$event) { - $this->setListSetting($event, 'Sort1'); - $this->setListSetting($event, 'Sort1_Dir'); - $this->setListSetting($event, 'Sort2'); - $this->setListSetting($event, 'Sort2_Dir'); + $this->setListSetting($event, 'Sortings'); } /**