<?php

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

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

//Employeegroup
require_once('../models/dao/EmployeegroupDAO.class.php');
require_once('../models/dto/Employeegroup.class.php');
require_once('../models/mysql/EmployeegroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeegroupMySqlExtDAO.class.php');
//Employeesubgroup
require_once('../models/dao/EmployeesubgroupDAO.class.php');
require_once('../models/dto/Employeesubgroup.class.php');
require_once('../models/mysql/EmployeesubgroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeesubgroupMySqlExtDAO.class.php');
//Employeeattendancesystem
require_once('../models/dao/EmployeeattendancesystemDAO.class.php');
require_once('../models/dto/Employeeattendancesystem.class.php');
require_once('../models/mysql/EmployeeattendancesystemMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeattendancesystemMySqlExtDAO.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 :-storeCTRL
  ======================== */

//here the global templates
$smarty->display("header.html");
//Employeegroup
$employeeGroup = new Employeegroup();
$employeeGroupDAO = new EmployeegroupMySqlDAO();
$employeeGroupEX = new EmployeegroupMySqlExtDAO();
//Employeesubgroup
$employeeSubGroup = new Employeesubgroup();
$employeeSubGroupDAO = new EmployeesubgroupMySqlDAO();
$employeeSubGroupEX = new EmployeesubgroupMySqlExtDAO();
//Employeeattendancesystem
$employeeAttendanceSystem = new Employeeattendancesystem();
$employeeAttendanceSystemDAO = new EmployeeattendancesystemMySqlDAO();
$employeeAttendanceSystemEX = new EmployeeattendancesystemMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Store
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $employeeAttendanceSystem = $employeeAttendanceSystemDAO->queryByDel(0);
    $smarty->assign('employeeAttendanceSystem', $employeeAttendanceSystem);
    $employeeGroup = $employeeGroupDAO->queryByDel(0);
    $smarty->assign('employeeGroup', $employeeGroup);

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

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

    $showData = $employeeSubGroupDAO->queryByDel(0);
    $smarty->assign("showData", $showData);

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

    $smarty->display("employeesubgroupsview/show.html");

    $smarty->assign("customCheck", 1);
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    // here the function that do the action

    try {
        $id = (int) $_GET['id'];
        $employeeSubGroupDAO->delete($id);
        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 = $employeeSubGroupDAO->load($id);
    $smarty->assign("loadData", $loadData);
    $employeeGroup = $employeeGroupDAO->queryByDel(0);
    $smarty->assign('employeeGroup', $employeeGroup);

    $employeeAttendanceSystem = $employeeAttendanceSystemDAO->queryByDel(0);
    $smarty->assign('employeeAttendanceSystem', $employeeAttendanceSystem);

    $smarty->display("employeesubgroupsview/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 $employeeSubGroup;
    global $employeeSubGroupDAO;

    //add in tbl
    $employeeSubGroup->name = filter_input(INPUT_POST, 'name');
    $employeeSubGroup->employeegroupid = (float) filter_input(INPUT_POST, 'employeegroupid');
    $employeeSubGroup->employeeattendancesystemid = (float) filter_input(INPUT_POST, 'employeeattendancesystemid');

    $employeeSubGroup->userid = $_SESSION["userid"];
    $employeeSubGroup->del = 0;
    $employeeSubGroup->sysdate = date("Y-m-d H:i:s");

    $employeeSubGroupDAO->insert($employeeSubGroup);
}

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

    $id = (int) filter_input(INPUT_POST, 'id');
    $employeeSubGroup = $employeeSubGroupDAO->load($id);
    $employeeSubGroup->name = filter_input(INPUT_POST, 'name');
    $employeeSubGroup->employeegroupid = (float) filter_input(INPUT_POST, 'employeegroupid');
    $employeeSubGroup->employeeattendancesystemid = (float) filter_input(INPUT_POST, 'employeeattendancesystemid');

    $employeeSubGroup->userid = $_SESSION["userid"];
    $employeeSubGroup->del = 0;
    $employeeSubGroup->sysdate = date("Y-m-d H:i:s");

    $employeeSubGroupDAO->update($employeeSubGroup);
}

?>