Index: kernel/db/cat_tag_processor.php =================================================================== --- kernel/db/cat_tag_processor.php (revision 14476) +++ kernel/db/cat_tag_processor.php (working copy) @@ -89,12 +89,12 @@ $params['pass'] = 'm,'.$this->Prefix; } - $item_id = isset($params[$id_prefix.'_id']) && $params[$id_prefix.'_id']; - if (!$item_id) { - $item_id = $this->Application->GetVar($this->getPrefixSpecial().'_id'); - if (!$item_id) { - $item_id = $this->Application->GetVar($this->Prefix.'_id'); - } + // set by PrintList2 tag + $item_id = $this->Application->GetVar($this->getPrefixSpecial() . '_id'); + + if ( !$item_id && ($this->Special != 'next' && $this->Special != 'previous') ) { + // set from page url + $item_id = $this->Application->GetVar($this->Prefix . '_id'); } $object =& $this->getObject($params); Index: kernel/db/db_event_handler.php =================================================================== --- kernel/db/db_event_handler.php (revision 14476) +++ kernel/db/db_event_handler.php (working copy) @@ -221,6 +221,16 @@ $event->setEventParam('raise_warnings', 1); } + if ( $event->Special == 'previous' || $event->Special == 'next' ) { + $object =& $this->Application->recallObject( $event->getEventParam('item') ); + /* @var $object kDBItem */ + + $list_helper =& $this->Application->recallObject('ListHelper'); + /* @var $list_helper ListHelper */ + + return $list_helper->getNavigationResource($object, $event->getEventParam('list'), $event->Special == 'next'); + } + if (preg_match('/^auto-(.*)/', $event->Special, $regs) && $this->Application->prefixRegistred($regs[1])) { // - returns field DateFormat value from language (LanguageId is extracted from current phrase object) $main_object =& $this->Application->recallObject($regs[1]); @@ -1011,6 +1021,9 @@ $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 Index: kernel/db/db_tag_processor.php =================================================================== --- kernel/db/db_tag_processor.php (revision 14476) +++ kernel/db/db_tag_processor.php (working copy) @@ -606,6 +606,42 @@ } /** + * Returns ID of previous record (related to current) in list. + * Use only on item detail pages. + * + * @param Array $params + * @return int + */ + function PreviousResource($params) + { + $object =& $this->getObject($params); + /* @var $object kDBItem */ + + $list_helper =& $this->Application->recallObject('ListHelper'); + /* @var $list_helper ListHelper */ + + return $list_helper->getNavigationResource($object, $params['list'], false); + } + + /** + * Returns ID of next record (related to current) in list. + * Use only on item detail pages. + * + * @param Array $params + * @return int + */ + function NextResource($params) + { + $object =& $this->getObject($params); + /* @var $object kDBItem */ + + $list_helper =& $this->Application->recallObject('ListHelper'); + /* @var $list_helper ListHelper */ + + return $list_helper->getNavigationResource($object, $params['list'], true); + } + + /** * Allows to modify block params & current list record before PrintList parses record * * @param kDBList $object Index: units/helpers/list_helper.php =================================================================== --- units/helpers/list_helper.php (revision 14476) +++ units/helpers/list_helper.php (working copy) @@ -122,4 +122,79 @@ } 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->SelectClause = str_replace(Array ('%1$s.*', '%2$s'), Array ($select_fields, ''), $list->SelectClause); + + $operators = Array ( + 'asc' => $next ? '>' : '<', + 'desc' => $next ? '<' : '>', + ); + + $where_clause = Array (); + $order_fields_backup = $list->OrderFields; + $lang = $this->Application->GetVar('m_lang'); + + foreach ($list->OrderFields as $index => $order) { + $where_clause[$index] = Array (); + + if (!$next) { + $list->OrderFields[$index][1] = $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 = array_key_exists($order_field, $list->VirtualFields) || $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->OrderFields = $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