<?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
// GOES HERE ....................
include("../public/include_dao.php");

//
require_once('../models/dao/SizecolorDAO.class.php');
require_once('../models/dto/Sizecolor.class.php');
require_once('../models/mysql/SizecolorMySqlDAO.class.php');
require_once('../models/mysql/ext/SizecolorMySqlExtDAO.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');

//Breadcrumb
require_once("../library/breadcrumb.php");


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

//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 :-sizecolorCTRL

  OPERTATION in Controller

  1-display add form
  2-display sucess or error
  3-display show and delete
  4-display edit
  ======================== */


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


//
$sizeColor = new Sizecolor();
$sizeColorDAO = new SizecolorMySqlDAO();
$sizeColorEX = new SizecolorMySqlExtDAO();

//Breadcrumb
$breadcrumbObject = new Breadcrumb();






//check and use the condetion that suite this action
if (empty($do)) {
    //here the smarty templates
    $smarty->display("sizecolorview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");

    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $allSizeColor = $sizeColorDAO->queryByConditions(0);
    $smarty->assign("allSizeColor", $allSizeColor);

    $smarty->display("sizecolorview/show.html");
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    // here the function that do the action

    $id = $_GET['id'];
    try {
        if (!empty($id)) {
            //$sizeColorDAO->delete($id);
            $sizeColor = $sizeColorDAO->load($id);
            $sizeColor->conditions = 1;
            $sizeColorDAO->update($sizeColor);
        }
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    //here the permssion check
    include_once("../public/authentication.php");

    $id = (int) $_GET['id'];
    $loadData = $sizeColorDAO->load($id);
    $smarty->assign("loadData", $loadData);

    $smarty->display("sizecolorview/edit.html");
}



//edit  update
elseif ($do == "update") {

    //here the permssion check
    include_once("../public/authentication.php");
    //
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}


$smarty->assign("customValidation", 1);

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

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

// add in Storerawmaterial tbl
function add() {

    //to use the variable out side the funcion
    global $sizeColor;
    global $sizeColorDAO;


    $name = $_POST['name'];
    $type = (int) $_POST['type'];


    //add in tbl
    $sizeColor->name = $name;
    $sizeColor->type = $type;
    $sizeColor->userid = $_SESSION['userid'];
    $sizeColor->conditions = 0;
    $sizeColor->sysdate = date("Y-m-d");
    $sizeColorDAO->insert($sizeColor);
}

//show
function show() {

    //to use the variable out side the funcion
    global $myStoreEx;

    $storeData = $myStoreEx->queryWithOrderWithQuantityAndPrice();

    return $storeData;
}

function update() {
    //to use the variable out side the funcion
    global $sizeColor;
    global $sizeColorDAO;


    $name = $_POST['name'];
    $type = (int) $_POST['type'];
    $id = (int) $_POST['id'];


    $sizeColor = $sizeColorDAO->load($id);
    $sizeColor->name = $name;
    $sizeColor->type = $type;
    $sizeColorDAO->update($sizeColor);
}

?>