<?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/KnownwaysDAO.class.php');
require_once('../models/dto/Knownway.class.php');
require_once('../models/mysql/KnownwaysMySqlDAO.class.php');
require_once('../models/mysql/ext/KnownwaysMySqlExtDAO.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');


require_once('../models/mysql/ext/SellbillMySqlExtDAO.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 :- knownWaysController - طرق المعرفة

  OPERTATION in Controller

  1-display add form
  2- add in user tble
  3-display sucess or error
  4-display show form
  5-delete from user tbl
  6-edit in user data

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


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



//here goes the instances and general variables

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

//KnownWays
$knownWays = new Knownway();
$knownWaysDAO = new KnownwaysMySqlDAO();

//sellbill
$sellBillExt  = new SellbillMySqlExtDAO();

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

    //here the smarty templates
    $smarty->display("knownwaysview/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);

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

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

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

    $id = $_GET['id'];

    try {
        // here the function that do the action
        $policyValid = delete($id);
        if ($policyValid == 1) {
            $url = "knownWaysController.php?do=show";

            $smarty->assign('urldirect', $url);
            $note = "لا يمكن حذف هذا العنصر لوجود فواتير بيع مرتبطة به";

            $smarty->assign('msgnote', $note);
            $smarty->display("notes2.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
##
elseif ($do == "edit") {
    //here the permssion check
    include_once("../public/authentication.php");

    $id = $_GET['id'];

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

    //here the smarty templates
    $smarty->display("knownwaysview/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 user tbl
function add() {

    //to use the variable out side the funcion
    global $knownWays;
    global $knownWaysDAO;

    $name = $_POST['name'];

    $knownWays->name = $name;
    $knownWaysDAO->insert($knownWays);
}

function delete($id) {

    //to use the variable out side the funcion
    global $knownWaysDAO;
    global $sellBillExt;

    // checksellbills
    $allBills = $sellBillExt->queryAllByKnownWay($id);
    if (count($allBills) > 0) {
        return 1;
    } else {
        $knownWaysDAO->delete($id);
    } return 0;
}

// show the form
function show() {
    //to use the variable out side the funcion
    global $knownWays;
    global $knownWaysDAO;

    $knownWaysData = $knownWaysDAO->queryAll();

    return $knownWaysData;
}

// edit in table
function edit($id) {
    //to use the variable out side the funcion
    global $knownWaysDAO;
    $loadData = $knownWaysDAO->load($id);
    return ($loadData);
}

// update table
function update() {
    //to use the variable out side the funcion
    global $knownWays;
    global $knownWaysDAO;

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

    $knownWays->name = $name;
    $knownWays->id = $id;
    $knownWaysDAO->update($knownWays);
}

?>