<?php
/**
  @licence GPL 2005-2011  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  23/01/11, 20:54
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
*/

$nID=(isset($_GET['nID'])? $_GET['nID'] : '');

$error = false ;
switch ($action) {
  case 'setflag':
    if ( ($_GET['flag'] == '0') || ($_GET['flag'] == '1') )
      if (isset($nID))  oscss_set_nav_status($nID, $_GET['flag']);

    osCSS_Cache::clear('menu');
    tep_redirect(tep_href_link(FILENAME_NAVIGATION_LINKS));
  break;
  case 'delete':
    if (isset($nID)) {
      tep_db_query(" delete from ".TABLE_NAVIGATION_LINKS." where nav_link_id ='".(int)$nID."' ");
      tep_db_query(" delete from ".TABLE_NAVIGATION_LINKS_DESCRIPTION." where nav_link_id ='".(int)$nID."' ");
    }
    osCSS_Cache::clear('menu');
    tep_redirect(tep_href_link(FILENAME_NAVIGATION_LINKS));
  break;
  case 'sort':
    if(isset($_POST['sort_order']) && is_array($_POST['nav_link_id']))
      for ($i=0; $i< sizeof($_POST['nav_link_id']) ; $i++ )
	tep_db_query(" update ".TABLE_NAVIGATION_LINKS." set nav_sort_order = '".$_POST['sort_order'][$i]."'  where nav_link_id ='".(int)$_POST['nav_link_id'][$i]."' ");

    osCSS_Cache::clear('menu');
    tep_redirect(tep_href_link(FILENAME_NAVIGATION_LINKS));
  break;
  case 'insert_new':
  case 'update':

    $nav_link_type = tep_db_prepare_input($_POST['type']);
    $nav_file = tep_db_prepare_input($_POST['link']);
    $nav_css_id = tep_db_prepare_input($_POST['css_id']);
    $nav_sort_order = tep_db_prepare_input($_POST['sort']);
    $nav_customer_status = tep_db_prepare_input($_POST['condition']);
    $parent_id =(isset($_POST['parent_id'])) ? tep_db_prepare_input($_POST['parent_id']) : 0;

    $nID = (isset($nID) && is_numeric($nID)) ? (int)$nID : '';

// print_r($_POST);
// exit;

    if ($nav_link_type == '1' )
      if( oscss_check_url($nav_file) == false ) {
	$error = true;
	$messageStack->add_session(ERROR_BAD_URL, 'error');
      }

    if ($nav_link_type == '0' )
      if(!tep_not_null($nav_file) )  {
	$error = true;
	$messageStack->add_session(ERROR_NO_FILES, 'error');
      }


    if (!tep_not_null($nav_sort_order) || !is_numeric($nav_sort_order)) $nav_sort_order='0';
// var_dump($error);
    if( $error == false ) {

      if($action == 'insert_new') {
	$sql_data_array=array('nav_link_id'=>'',
			      'nav_sort_order'=>$nav_sort_order,
			      'nav_css_id'=>$nav_css_id,
			      'nav_file'=>$nav_file,
			      'nav_link_type'=>$nav_link_type,
			      'customers_status'=>$nav_customer_status,
			      'nav_link_status'=>tep_db_prepare_input($_POST['nav_link_status']),
			      'parent_id'=>$parent_id
			);
	$res=tep_db_perform(TABLE_NAVIGATION_LINKS, $sql_data_array);
	$nav_link_id =tep_db_insert_id($res);
// 	$query=tep_db_query("select  nav_link_id from " . TABLE_NAVIGATION_LINKS . " where nav_file='".$nav_file."' and nav_sort_order = '".$nav_sort_order."' and nav_css_id ='".$nav_css_id."' and nav_link_type ='".$nav_link_type."'");
// 	$res_q=tep_db_fetch_array($query);
// 	$nav_link_id = $res_q['nav_link_id'];

	$languages = tep_get_languages();
	for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
	  $sql_data_array=array('language_id'=>$languages[$i]['id'],
				'nav_name'=>tep_db_prepare_input($_POST['nav_link_name'][$languages[$i]['id']]),
				'nav_link_id'=>$nav_link_id  );
	  tep_db_perform(TABLE_NAVIGATION_LINKS_DESCRIPTION, $sql_data_array);
	}
      } elseif($action == 'update') {
	$sql_data_array=array('nav_sort_order'=>$nav_sort_order,
			      'nav_css_id'=>$nav_css_id,
			      'nav_file'=>$nav_file,
			      'nav_link_type'=>$nav_link_type,
			      'customers_status'=>$nav_customer_status,
			      'nav_link_status'=>tep_db_prepare_input($_POST['nav_link_status']),
			      'parent_id'=>$parent_id
			);
	tep_db_perform(TABLE_NAVIGATION_LINKS, $sql_data_array, 'update', "  nav_link_id ='".$nID."' ");
// print_r($sql_data_array);
// exit;
	$languages = tep_get_languages();
	for ($i=0, $n=sizeof($languages); $i<$n; $i++) {

	  $sql_data_array=array('nav_name'=>tep_db_prepare_input($_POST['nav_link_name'][$languages[$i]['id']]) );

	  tep_db_perform(TABLE_NAVIGATION_LINKS_DESCRIPTION, $sql_data_array,  'update'," language_id ='".$languages[$i]['id']."' and nav_link_id ='".$nID."' ");
	}
      }

	osCSS_Cache::clear('menu');

    }
    tep_redirect(tep_href_link(FILENAME_NAVIGATION_LINKS));

  break;
  case 'new';
    //! new item
    $action_post_form='action=insert_new';

    $nav_links=array('nav_sort_order'=>'',
			    'nav_css_id'=>'',
			    'nav_file'=>'',
			    'nav_link_type'=>'',
			    'customers_status'=>'',
			    'nav_link_status'=>0,
			    'parent_id'=>0
		      );
  break;
  case 'edit';
    //! edit d'un item
    $action_post_form='action=update&amp;nID='.$nID;

    $nav_links_query = tep_db_query("SELECT * from ".TABLE_NAVIGATION_LINKS ." where nav_link_id ='".(int)$nID."'" );
    $nav_links = tep_db_fetch_array($nav_links_query);

  break;
  default:
    //! Liste par defaut

    $arry_navlink=list_item_order_niv();
}


$languages = tep_get_languages();
$NAVLINK_CONDITION = array( '0'=> CONDITIONS_NONE ,
		      '1'=>  CONDITIONS_LOG_OFF ,
		      '2'=>  CONDITIONS_LOG_ON
                     );
$navlink_condition = array( array('id' => '0','text' => CONDITIONS_NONE ),
		       array('id' => '1', 'text' => CONDITIONS_LOG_OFF ),
		       array('id' => '2', 'text' => CONDITIONS_LOG_ON )
                     );

?>