<?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/04/11, 10:44
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
  @brief generic page for configuration in confuguration_table
	  This page is replaced by module type configuration if exists
	  For replace this page by module configurtaion type, name new module cfg_xx, and xx is gID (configuration_group_id)
*/


  // clef de groupe (configuration_group_id)
  $gID = (isset($_GET['gID'])) ? $_GET['gID'] : 1;
  // groupe parent
  $gparentID = (isset($_GET['gparentID'])) ? $_GET['gparentID'] : 0;


  $class='cfg_'.$gID;
  if(file_exists(DIR_WS_MODULES.'configuration/'.$class.'.php') && !class_exists($class)) {
    require(DIR_WS_MODULES.'configuration/'.$class.'.php');
    // module key precis
    $mID=$gID;

    $cfg =new $class();
  }

  if(isset($mID) && class_exists($class)) $cfg->draw_action();
  else
    switch ($action) {
      case 'save':
	$configuration_value = tep_db_prepare_input($_POST['configuration_value']);
	$cID = tep_db_prepare_input($_GET['cID']);

	if(!tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . tep_db_input($configuration_value) . "', last_modified = now() where configuration_id = '" . (int)$cID . "'"))
		$messageStack->add(SAVE_CONFIG_OK, 'success');
	else $messageStack->add(SAVE_CONFIG_NOT_OK, 'error');

	tep_redirect(tep_href_link(FILENAME_CONFIGURATION, 'gparentID=' . $gparentID.'&gID=' . $_GET['gID'] . '&cID=' . $cID));
      break;
      case 'save_mutli':
	if(tep_not_null($gID) ){
	  $configuration_query = tep_db_query("select configuration_key,configuration_id, configuration_title, configuration_description,configuration_value, use_function,set_function from " . TABLE_CONFIGURATION . " where configuration_group_id = '" . (int)$gID . "' order by sort_order");
	  while ($configuration = tep_db_fetch_array($configuration_query)) {
	    if(isset($_POST['configuration'][$configuration['configuration_key']])) {
	      $configuration_value = tep_db_prepare_input($_POST['configuration'][$configuration['configuration_key']]);
	      if( (tep_db_query($sql="update " . TABLE_CONFIGURATION . " set configuration_value = '" . tep_db_input($configuration_value) . "', last_modified = now() where configuration_key = '" . $configuration['configuration_key']. "'")) !=false) $erreur[]=true;
	      else $erreur[]=false;
	    }
	  }

	  if(!in_array(false,$erreur))  $messageStack->add_session(SAVE_CONFIG_OK, 'success');
	  else $messageStack->add_session(SAVE_CONFIG_NOT_OK, 'error');
	}
	tep_redirect(tep_href_link(FILENAME_CONFIGURATION, 'gparentID=' . $gparentID.'&gID=' . $_GET['gID'] . '&cID=' . $cID));
      break;
      case 'save_grp':
	$new_grp_id = tep_db_prepare_input($_POST['new_grp_id']);
	$cID = tep_db_prepare_input($_GET['cID']);

	if(tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_group_id = '" . tep_db_input($new_grp_id) . "', 	last_modified = now() where configuration_id = '" . (int)$cID . "'") !=false)
			$messageStack->add_session(SAVE_CONFIG_OK, 'success');
	else $messageStack->add_session(SAVE_CONFIG_NOT_OK, 'error');
	tep_redirect(tep_href_link(FILENAME_CONFIGURATION, 'gparentID=' . $gparentID.'&gID=' . $new_grp_id . '&cID=' . $cID));
	break;
      case 'delete_confirm':

	if(tep_db_query("DELETE FROM " . TABLE_CONFIGURATION . " where configuration_id = '" . (int)$cID . "'") !=false)
			$messageStack->add_session(SAVE_CONFIG_OK, 'success');
	else $messageStack->add_session(SAVE_CONFIG_NOT_OK, 'error');
	tep_redirect(tep_href_link(FILENAME_CONFIGURATION, 'gparentID=' . $gparentID.'&gID=' . $gparentID  ));
      break;
      default:

	$list_edit=array();
	if(isset($mID) ){
	  $cfg->load_db_values();
	}elseif(tep_not_null($gID) && is_numeric($gID) ){
	  $configuration_query = tep_db_query("select configuration_key,configuration_id, configuration_title, configuration_description,configuration_value, use_function,set_function from " . TABLE_CONFIGURATION . " where configuration_group_id = '" . (int)$gID . "' order by sort_order");
	  while ($configuration = tep_db_fetch_array($configuration_query)) {
	  $cfgValue =configUtility::CallExecConfigLine($configuration['use_function'], $configuration['configuration_key'],  $configuration['configuration_value']);
	  if ($configuration['set_function']) eval($s[]='$value_field = ' . $configuration['set_function'] . '"' . htmlspecialchars($configuration['configuration_value']) . '","'.$configuration['configuration_key'].'");');
	  else $value_field = tep_draw_input_field('configuration['.$configuration['configuration_key'].']', '',$configuration['configuration_value']);
	  $configuration['cfgValue']=$cfgValue;
	  $configuration['value_field']=$value_field;

	  $list_edit[]=$configuration;
	}
      }

    }



  $cfg_group_query = tep_db_query("select configuration_group_title from " . TABLE_CONFIGURATION_GROUP . " where configuration_group_id = '" . (int)$gID . "'");
  $cfg_group = tep_db_fetch_array($cfg_group_query);
?>