<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.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");

//Menuurl
require_once('../models/dao/MenuurlDAO.class.php');
require_once('../models/dto/Menuurl.class.php');
require_once('../models/mysql/MenuurlMySqlDAO.class.php');
require_once('../models/mysql/ext/MenuurlMySqlExtDAO.class.php');

//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');

//Breadcrumb
//require_once("../library/breadcrumb.php");
//get the do the action
$do = $_GET['do'];



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

  Controller Name :- saveCTRL اضافة وتعديل وعرض الخزن

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

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


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


//here goes the instances and general variables
$Menuurl = new Menuurl();
$MenuurlDAO = new MenuurlMySqlDAO();
$MenuurlEX = new MenuurlMySqlExtDAO();

$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();

//Breadcrumb
//$breadcrumbObject = new Breadcrumb();
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");


    //breadcrumb
    /* $breadcrumbObject->add('الادارة><a href="menuurController..php?do=show"> عرض الروابط </a> > اضافة رابط', 'menuurController..php', 0);
      //Now output the navigation.
      $breadCrumb = $breadcrumbObject->output();
      $smarty->assign("breadCrumb",$breadCrumb); */


    $allusergroup = $UsergroupDAO->queryByConditions(0);
    $smarty->assign("allusergroup", $allusergroup);

    //here the smarty templates
    //$smarty->display("menuurlview/add.html");

    $smarty->display("menuurlview/add.html");
} elseif ($do == "add") {


    print_r(44444);


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


    // here the function that do the action
    $alldata = show();
    $smarty->assign("alldata", $alldata);


    //breadcrumb
    /* $breadcrumbObject->add('الادارة><a href="menuurController.php?do=show"> عرض الروابط </a> > عرض الروباط', 'menuurController.php', 0);
      //Now output the navigation.
      $breadCrumb = $breadcrumbObject->output();
      $smarty->assign("breadCrumb",$breadCrumb); */





    //here the smarty templates
    $smarty->display("menuurlview/show.html");

    $smarty->assign("customBank", 1);
} elseif ($do == "delete") {
    //here the permssion check
    include_once("../public/authentication.php");

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

    $loadData = edit();
    $smarty->assign("loadData", $loadData);


    //breadcrumb
    /* $breadcrumbObject->add('الادارة><a href="menuurController.php?do=show"> عرض الروابط </a> > تعديل رابط', 'menuurController.php', 0);
      //Now output the navigation.
      $breadCrumb = $breadcrumbObject->output();
      $smarty->assign("breadCrumb",$breadCrumb); */

    $usersgroup = $loadData->usergroupid;

    $allusergroup = $UsergroupEX->queryAllnotin($usersgroup);
    $smarty->assign("allusergroup", $allusergroup);


    $alloldusergroup = $UsergroupEX->queryAllarein($usersgroup);
    $smarty->assign("alloldusergroup", $alloldusergroup);


    //here the smarty templates
    $smarty->display("menuurlview/edit.html");
} 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 Save tbl
function add() {

    //to use the variable out side the funcion
    global $Menuurl;
    global $MenuurlDAO;
    global $MenuurlEX;

    //post variable
    $urltitle = $_POST['urltitle'];
    $menuurl = $_POST['menuurl'];
    $usergroupname = $_POST['usergroupname'];

    $finalname = "";
    foreach ($usergroupname as $myname) {
        $finalname = $finalname . $myname . ",";

        //print_r($finalname."<br>");
    }

    //print_r($finalname);
    $finalname = substr($finalname, 0, -1);
    $handle = new upload($_FILES['urlimage']);

    $urlimage = uploadImages($handle, "../views/default/images/upload/images150_150", 336, 280);



    $Menuurl->menuurl = $menuurl;
    $Menuurl->urlimage = $urlimage;
    $Menuurl->urltitle = $urltitle;
    $Menuurl->usergroupid = $finalname;

    print_r($Menuurl);
    $MenuurlDAO->insert($Menuurl);
}

// show the form
function show() {
    //to use the variable out side the funcion
    global $Menuurl;
    global $MenuurlDAO;
    global $MenuurlEX;


    //select all data from Save tbl
    $alldata = $MenuurlDAO->queryAll();

    return $alldata;
}

//delete
function delete() {
    //to use the variable out side the funcion
    global $Menuurl;
    global $MenuurlDAO;
    global $MenuurlEX;


    $id = $_GET["id"];
    $MenuurlDAO->delete($id);
}

// edit in table
function edit() {
    //to use the variable out side the funcion
    global $Menuurl;
    global $MenuurlDAO;
    global $MenuurlEX;

    //get variable
    $id = $_GET['id'];

    //load data by saveid
    $loadData = $MenuurlDAO->load($id);


    return $loadData;
}

// update table
function update() {

    //to use the variable out side the funcion
    global $Menuurl;
    global $MenuurlDAO;
    global $MenuurlEX;

    //post variable
    $menuurlid = $_POST['menuurlid'];
    $urltitle = $_POST['urltitle'];
    $menuurl = $_POST['menuurl'];
    $usergroupname = $_POST['usergroupname'];

    $finalname = "";
    foreach ($usergroupname as $myname) {
        $finalname = $finalname . $myname . ",";

        //print_r($finalname."<br>");
    }

    //print_r($finalname);
    $finalname = substr($finalname, 0, -1);
    $handle = new upload($_FILES['urlimage']);

    $urlimage = updateImages($handle, 'oldimage', "../views/default/images/upload/images150_150", 336, 280);



    $Menuurl->menuurl = $menuurl;
    $Menuurl->urlimage = $urlimage;
    $Menuurl->urltitle = $urltitle;
    $Menuurl->usergroupid = $finalname;
    $Menuurl->menuurlid = $menuurlid;

    $MenuurlDAO->update($Menuurl);
}

?>