<?php
/**
 * @package admin
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: products_previous_next.php 3806 2006-06-19 16:18:12Z ajeh $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

/////
// BOF PREVIOUS NEXT

  if ($prev_next_list=='') {
// calculate the previous and next

    $check_type = $db->Execute("select products_type from " . TABLE_PRODUCTS . " where products_id='" . $products_filter . "'");
    define('PRODUCT_INFO_PREVIOUS_NEXT_SORT', zen_get_configuration_key_value_layout('PRODUCT_INFO_PREVIOUS_NEXT_SORT', $check_type->fields['products_type']));

    // sort order
    switch(PRODUCT_INFO_PREVIOUS_NEXT_SORT) {
      case (0):
        $prev_next_order= ' order by LPAD(p.products_id,11,"0")';
        break;
      case (1):
        $prev_next_order= " order by pd.products_name";
        break;
      case (2):
        $prev_next_order= " order by p.products_model";
        break;
      case (3):
        $prev_next_order= " order by p.products_price, pd.products_name";
        break;
      case (4):
        $prev_next_order= " order by p.products_price, p.products_model";
        break;
      case (5):
        $prev_next_order= " order by pd.products_name, p.products_model";
        break;
      default:
        $prev_next_order= " order by pd.products_name";
        break;
      }


// set current category
    $current_category_id = (isset($_GET['current_category_id']) ? $_GET['current_category_id'] : $current_category_id);

    if (!$current_category_id) {
      $sql = "SELECT categories_id
              from   " . TABLE_PRODUCTS_TO_CATEGORIES . "
              where  products_id ='" .  $products_filter . "'";

      $cPath_row = $db->Execute($sql);
      $current_category_id = $cPath_row->fields['categories_id'];
    }

    $sql = "select p.products_id, pd.products_name
            from   " . TABLE_PRODUCTS . " p, "
                     . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                     . TABLE_PRODUCTS_TO_CATEGORIES . " ptc
            where  p.products_id = pd.products_id and pd.language_id= '" . $_SESSION['languages_id'] . "' and p.products_id = ptc.products_id and ptc.categories_id = '" . $current_category_id . "'" .
            $prev_next_order
            ;

    $products_ids = $db->Execute($sql);
  }

// reset if not already set for display
  ($_GET['products_filter'] == '' ? $_GET['products_filter'] = $products_filter : '');
  ($_GET['current_category_id'] == '' ? $_GET['current_category_id'] = $current_category_id : '');

  while (!$products_ids->EOF) {
    $id_array[] = $products_ids->fields['products_id'];
    $products_ids->MoveNext();
  }

// if invalid product id skip
  if (is_array($id_array)) {
    reset ($id_array);
    $counter = 0;
    while (list($key, $value) = each ($id_array)) {
      if ($value == $products_filter) {
        $position = $counter;
        if ($key == 0) {
          $previous = -1; // it was the first to be found
        } else {
          $previous = $id_array[$key - 1];
        }
        if ($id_array[$key + 1]) {
          $next_item = $id_array[$key + 1];
        } else {
          $next_item = $id_array[0];
        }
      }
      $last = $value;
      $counter++;
    }

    if ($previous == -1) $previous = $last;

    $sql = "select categories_name
            from   " . TABLE_CATEGORIES_DESCRIPTION . "
            where  categories_id = '" . (int)$current_category_id . "' AND language_id = '" . $_SESSION['languages_id'] . "'";

    $category_name_row = $db->Execute($sql);
  } // if is_array

/*
  if (strstr($PHP_SELF, FILENAME_PRODUCTS_PRICE_MANAGER)) {
    $curr_page = FILENAME_PRODUCTS_PRICE_MANAGER;
  } else {
    $curr_page = FILENAME_ATTRIBUTES_CONTROLLER;
  }
*/

  switch(true) {
  case (strstr($PHP_SELF, FILENAME_ATTRIBUTES_CONTROLLER)):
    $curr_page = FILENAME_ATTRIBUTES_CONTROLLER;
    break;
  case (strstr($PHP_SELF, FILENAME_PRODUCTS_TO_CATEGORIES)):
    $curr_page = FILENAME_PRODUCTS_TO_CATEGORIES;
    break;
  default:
    $curr_page = FILENAME_PRODUCTS_PRICE_MANAGER;
    break;
  }
// to display use products_previous_next_display.php
?>
