<?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");


//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');
//Currency
require_once('../models/dao/CurrencyDAO.class.php');
require_once('../models/dto/Currency.class.php');
require_once('../models/mysql/CurrencyMySqlDAO.class.php');
require_once('../models/mysql/ext/CurrencyMySqlExtDAO.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 :- currency

  OPERTATION in Controller
  ======================== */

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

//here goes the instances and general variables
//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();
//Currency
$currency = new Currency();
$currencyDAO = new CurrencyMySqlDAO();
$currencyEX = new CurrencyMySqlExtDAO();


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

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");
    //here the smarty templates
    $smarty->display("currencyview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    show();

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

    $smarty->display("currencyview/show.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $loadData = edit();
    $smarty->assign("loadData", $loadData);
    $smarty->display("currencyview/edit.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 == "delete") {
    include_once("../public/authentication.php");

    try {
        delete();
        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("customUnit", 1);

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

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

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

// add in unit tbl
function add() {
    global $currencyDAO;

    $name = filter_input(INPUT_POST, 'name');
    $symbol = filter_input(INPUT_POST, 'symbol');
    $conversionFactor = (float) filter_input(INPUT_POST, 'conversionFactor');
    $otherconversionFactor = (float) filter_input(INPUT_POST, 'othercFactor');


    $currency->name = $name;
    $currency->symbol = $symbol;
    $currency->conversionFactor = $conversionFactor;
    $currency->otherconversionFactor = $otherconversionFactor;
    $currency->conditions = 0;
    $currency->userid = $_SESSION['userid'];
    $currency->sysDate = date('Y-m-d H:i:s');

    $currencyDAO->insert($currency);
}

//show
function show() {
    global $currencyDAO;
    global $smarty;

    $allCurrency = $currencyDAO->queryByConditions(0);
    $smarty->assign('allCurrency', $allCurrency);
}

function edit() {
    global $currencyDAO;

    $id = (int) $_GET['id'];
    $loadData = $currencyDAO->load($id);
    return $loadData;
}

function update() {
    global $currencyDAO;
    global $ProgramsettingEX;

    $id = (int) filter_input(INPUT_POST, 'id');
    $name = filter_input(INPUT_POST, 'name');
    $symbol = filter_input(INPUT_POST, 'symbol');
    $conversionFactor = (float) filter_input(INPUT_POST, 'conversionFactor');
    $otherconversionFactor = (float) filter_input(INPUT_POST, 'othercFactor');

    $currency = $currencyDAO->load($id);
    $currency->name = $name;
    $currency->symbol = $symbol;
    $currency->conversionFactor = $conversionFactor;
    $currency->otherconversionFactor = $otherconversionFactor;
    $currency->userid = $_SESSION['userid'];
    $currency->sysDate = date('Y-m-d H:i:s');
    $currencyDAO->update($currency);

    if ($id == 1) {//it is main currency, so update its name in program settings
        $ProgramsettingEX->runSqlQuery(" update programsettings set currancy = '" . $currency->name . "' where programsettingsid=1");
    }
}

function delete() {
    global $currencyDAO;

    $id = (int) filter_input(INPUT_GET, 'id');
    $currency = $currencyDAO->load($id);
    $currency->conditions = 1;
    $currencyDAO->update($currency);
}

?>