<?php

//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");
//Productsetting
require_once('../models/dao/ProductsettingDAO.class.php');
require_once('../models/dto/Productsetting.class.php');
require_once('../models/mysql/ProductsettingMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductsettingMySqlExtDAO.class.php');
//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

//get the do the action
$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

/* ======================

  Controller Name :- productSettingController

  OPERTATION in Controller

  1- edit productSetting
  ======================== */

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables
//Productsetting
$productSetting = new Productsetting();
$productSettingDAO = new ProductsettingMySqlDAO();
$productSettingExt = new ProductsettingMySqlExtDAO();
//Store
$store = new Store();
$storeDAO = new StoreMySqlDAO();
$storeExt = new StoreMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$today = date("Y-m-d");



//check and use the condition that suite this action
if (empty($do) || $do == "edit") {
    //here the permission check
    include_once("../public/authentication.php");
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $storeData = getStores();
    $smarty->assign("storeData", $storeData);
    $smarty->display("productSettingview/edit.html");
} elseif ($do == "editprint") {//edit
    include_once("../public/authentication.php");

    $storeData = getStores();
    $smarty->assign("storeData", $storeData);
    $smarty->display("productSettingview/editprint.html");
} elseif ($do == "update") { //edit update
    include_once("../public/authentication.php");

    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("productSettingview/succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("productSettingview/error.html");
}


$smarty->assign('productSetting', 1);

//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */

function getStores() {
    global $storeDAO;

    $storeData = $storeDAO->queryByConditions(0);
    return $storeData;
}

function update() {
    global $productSettingDAO;
    global $productSetting;

    $productsettingid = (int)$_POST['productsettingid'];
    $storeid = $_POST['storeid'];
    $leastValueAtStore = $_POST['leastValueAtStore'];

    if ($productsettingid == 0) { //Insert
        $productSetting->leastValueAtStore = $leastValueAtStore;
        $productSetting->storeid = $storeid;
        $productSettingDAO->insert($productSetting);
    } else { //Update
        $productSetting->leastValueAtStore = $leastValueAtStore;
        $productSetting->storeid = $storeid;
        $productSetting->productsettingid = $productsettingid;
        $productSettingDAO->update($productSetting);
    }
}

?>