<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: header_php.php,v 1.1.1.1 2004/09/01 04:48:21 shidairiki Exp $
//
  $zc_install->error = false;

  require('../includes/configure.php');
  require('../includes/classes/db/' . DB_TYPE . '/query_factory.php');
  $db = new queryFactory;
  $db->Connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE) or die("Unable to connect to database");

    if (!isset($_POST['store_name'])) $_POST['store_name'] = '';
    if (!isset($_POST['store_owner'])) $_POST['store_owner'] = '';
    if (!isset($_POST['store_owner_email'])) $_POST['store_owner_email'] = '';
    if (!isset($_POST['store_country'])) $_POST['store_country'] = '223';
    if (!isset($_POST['store_zone'])) $_POST['store_zone'] = '';
    if (!isset($_POST['store_address'])) $_POST['store_address'] = STORE_ADDRESS_DEFAULT_VALUE;
    if (!isset($_POST['store_default_language'])) $_POST['store_default_language'] = '';
    if (!isset($_POST['store_default_currency'])) $_POST['store_default_currency'] = '';

  if (isset($_POST['submit'])) {
//    require('../includes/functions/functions_general.php');
//    require('../includes/functions/validations.php');

    $store_name = zen_db_prepare_input($_POST['store_name']);
    $store_owner = zen_db_prepare_input($_POST['store_owner']);
    $store_owner_email = zen_db_prepare_input($_POST['store_owner_email']);
    $store_country = zen_db_prepare_input($_POST['store_country']);
    $store_zone = zen_db_prepare_input($_POST['store_zone']);
    $store_address = zen_db_prepare_input($_POST['store_address']);
    $store_default_language = zen_db_prepare_input($_POST['store_default_language']);
    $store_default_currency = zen_db_prepare_input($_POST['store_default_currency']);

    $zc_install->isEmpty($store_name, ERROR_TEXT_STORE_NAME_ISEMPTY, ERROR_CODE_STORE_NAME_ISEMPTY);
    $zc_install->isEmpty($store_owner, ERROR_TEXT_STORE_OWNER_ISEMPTY, ERROR_CODE_STORE_OWNER_ISEMPTY);
    $zc_install->isEmpty($store_owner_email, ERROR_TEXT_STORE_OWNER_EMAIL_ISEMPTY, ERROR_CODE_STORE_OWNER_EMAIL_ISEMPTY);
    $zc_install->isEmail($store_owner_email, ERROR_TEXT_STORE_OWNER_EMAIL_NOTEMAIL, ERROR_CODE_STORE_OWNER_EMAIL_NOTEMAIL);
    $zc_install->isEmpty($store_address, ERROR_TEXT_STORE_ADDRESS_ISEMPTY, ERROR_CODE_STORE_ADDRESS_ISEMPTY);
    if ($_POST['demo_install'] == 'true') {
      $zc_install->fileExists('demo/' . DB_TYPE . '_demo.sql', ERROR_TEXT_DEMO_SQL_NOTEXIST, ERROR_CODE_DEMO_SQL_NOTEXIST);
    }

    if ($zc_install->error == false) {
      if ($_POST['demo_install'] == 'true') {
//        include('lib/db.func.php');
        executeSql('demo/' . DB_TYPE . '_demo.sql', DB_DATABASE, DB_PREFIX);
      }

      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_name) . "' where configuration_key = 'STORE_NAME'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_owner) . "' where configuration_key = 'STORE_OWNER'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_owner_email) . "' where configuration_key = 'STORE_OWNER_EMAIL_ADDRESS'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_owner_email) . "' where configuration_key = 'EMAIL_FROM'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_country) . "' where configuration_key = 'STORE_COUNTRY'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_zone) . "' where configuration_key = 'STORE_ZONE'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_address) . "' where configuration_key = 'STORE_NAME_ADDRESS'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_default_language) . "' where configuration_key = 'DEFAULT_LANGUAGE'";
      $db->Execute($sql);
      $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input($store_default_currency) . "' where configuration_key = 'DEFAULT_CURRENCY'";
      $db->Execute($sql);
      $db->Close();
      header('location: index.php?main_page=admin_setup&language=' . $language);
      exit;
    }
  }


  //if not submit, set some defaults
  $sql = "select countries_id, countries_name from " . DB_PREFIX . "countries";
  $country = $db->Execute($sql);
  $country_string = '';
  while (!$country->EOF) {
    $country_string .= '<option value="' . $country->fields['countries_id'] . '"' . setSelected($country->fields['countries_id'], $_POST['store_country']) . '>' . $country->fields['countries_name'] . '</option>';
    $country->MoveNext();
  }
  $sql = "select zone_id, zone_name from " . DB_PREFIX . "zones";
  $zone = $db->Execute($sql);
  $zone_string = '';
  while (!$zone->EOF) {
    $zone_string .= '<option value="' . $zone->fields['zone_id'] . '"' . setSelected($zone->fields['zone_id'], $_POST['store_zone']) . '>' . $zone->fields['zone_name'] . '</option>';
    $zone->MoveNext();
  }
  $sql = "select code, name from " . DB_PREFIX . "languages";
  $store_language = $db->Execute($sql);
  $language_string = '';
  while (!$store_language->EOF) {
    $language_string .= '<option value="' . $store_language->fields['code'] . '"' . setSelected($store_language->fields['code'], $_POST['store_default_language']) . '>' . $store_language->fields['name'] . '</option>';
    $store_language->MoveNext();
  }
  $sql = "select title, code from " . DB_PREFIX . "currencies";
  $currency = $db->Execute($sql) or die("error in $sql" . $db->ErrorMsg());;
  $currency_string = '';
  while (!$currency->EOF) {
    $currency_string .= '<option value="' . $currency->fields['code'] . '"' . setSelected($currency->fields['code'], $_POST['store_default_currency']) . '>' . $currency->fields['title'] . '</option>';
    $currency->MoveNext();
  }

  $db->Close();

  if (!isset($_POST['demo_install'])) $_POST['demo_install']=false;

  setInputValue($_POST['store_name'], 'STORE_NAME_VALUE', '');
  setInputValue($_POST['store_owner'], 'STORE_OWNER_VALUE', '');
  setInputValue($_POST['store_owner_email'], 'STORE_OWNER_EMAIL_VALUE', '');
  setInputValue($_POST['store_address'], 'STORE_ADDRESS_VALUE', STORE_ADDRESS_DEFAULT_VALUE);
  setRadioChecked($_POST['demo_install'], 'DEMO_INSTALL', 'false');

?>