Index: branches/5.2.x/core/units/helpers/list_helper.php =================================================================== diff -u -N -r14628 -r14646 --- branches/5.2.x/core/units/helpers/list_helper.php (.../list_helper.php) (revision 14628) +++ branches/5.2.x/core/units/helpers/list_helper.php (.../list_helper.php) (revision 14646) @@ -33,7 +33,7 @@ $sort_fields = is_array($sorting) ? array_keys($sorting) : Array (); for ($order_number = 0; $order_number < 2; $order_number++) { - // currect sorting in list + // current sorting in list $sorting_pos = $user_sorting_start + $order_number; $current_order_field = $list->GetOrderField($sorting_pos, true); $current_order_direction = $list->GetOrderDirection($sorting_pos, true); @@ -120,6 +120,82 @@ if ( $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting') ) { $user_sorting_start = count($forced_sorting); } + return $user_sorting_start; } + + /** + * Returns ID of previous/next record related to current record + * + * @param kDBItem $object + * @param string $list_prefix + * @param bool $next + * @return int + */ + function getNavigationResource(&$object, $list_prefix, $next = true, $select_fields = null) + { + $list =& $this->Application->recallObject($list_prefix); + /* @var $list kDBList */ + + if ( !isset($select_fields) ) { + $select_fields = '%1$s.' . $object->IDField; + } + + if ( is_array($select_fields) ) { + $select_fields = implode(', ', $select_fields); + } + + $list->SetSelectSQL( str_replace(Array ('%1$s.*', '%2$s'), Array ($select_fields, ''), $list->GetPlainSelectSQL()) ); + + $operators = Array ( + 'asc' => $next ? '>' : '<', + 'desc' => $next ? '<' : '>', + ); + + $where_clause = Array (); + $lang = $this->Application->GetVar('m_lang'); + $order_fields = $order_fields_backup = $list->getOrderFields(); + + foreach ($order_fields as $index => $order) { + $where_clause[$index] = Array (); + + if ( !$next ) { + $list->changeOrderDirection($index, $order_fields_backup[$index][1] == 'asc' ? 'desc' : 'asc'); + } + + for ($i = 0; $i <= $index; $i++) { + $order_field = $order_fields_backup[$i][0]; + $is_expression = $order_fields_backup[$i][2]; + + if ( preg_match('/^IF\(COALESCE\(.*?\.(l' . $lang . '_.*?), ""\),/', $order_field, $regs) ) { + // undo result of kDBList::getMLSortField method + $order_field = $regs[1]; + $is_expression = false; + } + + $order_direction = $order_fields_backup[$i][1]; + + $field_prefix = $list->isVirtualField($order_field) || $is_expression ? '' : '%1$s.'; + + $actual_operator = $i == $index ? $operators[$order_direction] : '='; + $where_clause[$index][] = $field_prefix . $order_field . ' ' . $actual_operator . ' ' . $this->Conn->qstr($object->GetDBField($order_field)); + } + + $where_clause[$index] = '(' . implode(') AND (', $where_clause[$index]) . ')'; + } + + $where_clause = '(%1$s.' . $object->IDField . ' != ' . $object->GetID() . ') AND ((' . implode(') OR (', $where_clause) . '))'; + $list->addFilter('navigation_filter', $where_clause); + + $sql = $list->extractCalculatedFields($list->GetSelectSQL()); + + $list->removeFilter('navigation_filter'); + $list->setOrderFields($order_fields_backup); + + if ( $this->Application->isDebugMode() ) { + $this->Application->Debugger->appendHTML('Quering ' . ($next ? 'next' : 'previous') . ' item for "' . $list_prefix . '" list:'); + } + + return $this->Conn->GetOne($sql); + } } \ No newline at end of file