<?php

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

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.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");

//here the global templates
$smarty->display("header.html");
//$smarty->display("maintennanceHeader.html");
//here goes the instances and general variables
//Restauranthall
require_once('../models/dao/RestauranthallDAO.class.php');
require_once('../models/dto/Restauranthall.class.php');
require_once('../models/mysql/RestauranthallMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranthallMySqlExtDAO.class.php');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.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');
//check and use the condetion that suite this action
//Restauranthall
$restaurantHall = new Restauranthall();
$restaurantHallDAO = new RestauranthallMySqlDAO();
$restaurantHallEX = new RestauranthallMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//User
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

if (empty($do)) {
    include_once("../public/authentication.php");

    $allUsers = $userDAO->queryAll();
    $smarty->assign("allUsers", $allUsers);

    $smarty->display("restauranthallview/add.html");
    //date
} elseif ($do == "add") {
    // include_once("../public/authentication.php");
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        //print_r($e);
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $allHalls = show();
    $smarty->assign("allHalls", $allHalls);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $smarty->display("restauranthallview/show.html");
}

//edit
elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $alldata = edit();
    $smarty->assign("alldata", $alldata);

    $allUsers = $userDAO->queryAll();
    $smarty->assign("allUsers", $allUsers);

    //print_r($alldata);
    $smarty->display("restauranthallview/edit.html");
} elseif ($do == "update") {
    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 == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        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->display("footer.html");

////****************************  Functions  ********************///

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


function add() {
    global $restaurantHall;
    global $restaurantHallDAO;

    $name = $_POST['name'];
    $hallPrice = $_POST['hallPrice'];

    //like in expensetypecontroller
    $hallUsers = filter_input(INPUT_POST, 'hallUsers', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

    $hallUsers_str = '-1,';
    foreach ($hallUsers as $value) {
        $hallUsers_str .= $value . ",";
    }
    $hallUsers_str = rtrim($hallUsers_str, ',');

    $restaurantHall->name = $name;
    $restaurantHall->hallPrice = $hallPrice;
    $restaurantHall->hallUsers = $hallUsers_str;
    $restaurantHall->userId = $_SESSION['userid'];
    $restaurantHall->sysdate = date('Y-m-d');
    $restaurantHall->del = 0;

    $restaurantHallDAO->insert($restaurantHall);
}

function show() {
    global $restaurantHallDAO;

    $allRestaurantHall = $restaurantHallDAO->queryAll();

    return $allRestaurantHall;
}

function edit() {
    global $restaurantHallDAO;

    $id = $_GET['id'];
    $alldata = $restaurantHallDAO->load($id);
    $alldata->hallUsers = explode(',', $alldata->hallUsers);

    return $alldata;
}

function update() {
    global $restaurantHall;
    global $restaurantHallDAO;

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

    //like in expensetypecontroller
    $hallUsers = filter_input(INPUT_POST, 'hallUsers', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

    $hallUsers_str = '-1,';
    foreach ($hallUsers as $value) {
        $hallUsers_str .= $value . ",";
    }
    $hallUsers_str = rtrim($hallUsers_str, ',');

    $restaurantHall = $restaurantHallDAO->load($id);

    $restaurantHall->name = $name;
    $restaurantHall->hallPrice = $hallPrice;
    $restaurantHall->hallUsers = $hallUsers_str;
    $restaurantHall->userId = $_SESSION['userid'];
    $restaurantHall->sysdate = date('Y-m-d');

    $restaurantHallDAO->update($restaurantHall);
}

function delete() {
    global $restaurantHallDAO;

    $id = $_GET['id'];
    $restaurantHallDAO->delete($id);
}

?>