<?php

ob_start();
//include("writeraccess.php");
//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
include("../public/include_dao.php");
//properities
require_once('../models/dao/PropertiesDAO.class.php');
require_once('../models/dto/Propertie.class.php');
require_once('../models/mysql/PropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/PropertiesMySqlExtDAO.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');
//Relusergroupproperties
require_once('../models/dao/RelusergrouppropertiesDAO.class.php');
require_once('../models/dto/Relusergrouppropertie.class.php');
require_once('../models/mysql/RelusergrouppropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/RelusergrouppropertiesMySqlExtDAO.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 :- propertiesCTRL

  OPERTATION in Controller

  1-display add form
  2- add in properity tbl
  3- select data from usergroup tbl
  4-add in relusergroupproperties tbl
  5-display sucess or error

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


//here the global templates
$smarty->display("header.html");
//$smarty->display("quciktask.html");
//here goes the instances and general variables
$myProperities = new Propertie();
$myProperitiesRecord = new PropertiesMySqlDAO();
$myProperitiesEx = new PropertiesMySqlExtDAO();


$myUserGroup = new Usergroup();
$myUserGroupRecord = new UsergroupMySqlDAO();

$myRelusergroup = new Relusergrouppropertie();
$myRelusergroupRecord = new RelusergrouppropertiesMySqlDAO();


//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    //include_once("../public/auth.php");
    // here the function that do the action
    $propertiesData = loadproperity();
    $smarty->assign("propertiesData", $propertiesData);
    //print("<pre>");
    //print_r($propertiesData);
    //print("</pre>");
    //here the smarty templates
    $smarty->display("propertiesview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    //	include_once("../public/authentication.php");

    try {
        add();
        $url = "propertiesController.php";
        $smarty->assign('urldirect', $url);
        $note = "تمت العملية بنجاح , سوف يتم تحويلك الى صفحة العرض خلال 3 ثوانى";

        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    } catch (Exception $e) {

        $url = "propertiesController.php";
        $smarty->assign('urldirect', $url);
        $note = "لقد حدث خطأ اثناء اجراء العملية , سوف يتم تحويلك الى صفحة العرض خلال 3 ثوانى";

        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    }
} elseif ($do == "show") {

    //here the permssion check
    //include_once("../public/auth.php");
    $getallprop = $myProperitiesRecord->queryAll();
    $smarty->assign("getallprop", $getallprop);

    $propertiesData = loadproperity();
    $smarty->assign("propertiesData", $propertiesData);

    //here the smarty templates
    $smarty->display("propertiesview/add.html");
} elseif ($do == "edit") {
    //here the permssion check
    //	include_once("../public/authentication.php");
    $theid = $_GET['id'];
    $getallpropwithid = $myProperitiesRecord->load($theid);
    $smarty->assign("getallpropwithid", $getallpropwithid);
    // here the function that do the action
    $propertiesData = loadproperity();
    $smarty->assign("propertiesData", $propertiesData);
    //here the smarty templates
    $smarty->display("propertiesview/edit.html");
} elseif ($do == "update") {
    //here the permssion check
    //	include_once("../public/authentication.php");

    $nameprp = $_POST['propertyname'];
    $idprp = $_POST['propertyid'];
    $urlprp = $_POST['propertyurl'];
    try {

        // here the function that do the action

        $newprop = new Propertie;
        $newprop->propertyid = $idprp;
        $newprop->propertyname = $nameprp;
        $newprop->propertyurl = $urlprp;

        $myProperitiesRecord->updates($newprop);

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






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

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

// select from properity tbl
function loadproperity() {

    //to use the variable out side the funcion
    global $myProperitiesRecord;

    // select all data from properity tbl
    $propertiesData = $myProperitiesRecord->queryAll();

    return $propertiesData;
}

// add in properity tbl
function add() {

    //to use the variable out side the funcion
    global $myProperities;
    global $myProperitiesRecord;

    global $myUserGroupRecord;

    global $myRelusergroup;
    global $myRelusergroupRecord;

    $propertyName = $_POST['propertyname'];
    $propertyParent = $_POST['propertyparent'];
    $propertyDefault = $_POST['propertydefault'];
    $propertyurl = $_POST['propertyurl'];
    $propertyname_en = $_POST['propertyname_en'];

    //add in properity tbl
    $myProperities->propertyname = $propertyName;
    $myProperities->propertyparent = $propertyParent;
    $myProperities->propertydefault = $propertyDefault;
    $myProperities->propertyurl = $propertyurl;
    $myProperities->propertyname_en = $propertyname_en;


    $propertyId = $myProperitiesRecord->insert($myProperities);

    //select all data from usergroup
    $usergroupData = $myUserGroupRecord->queryAll();

    foreach ($usergroupData as $usergroup) {
        $usergroupId = $usergroup->usergroupid;

        //add in relusergroupproperties tbl
        $myRelusergroup->propertyid = $propertyId;
        $myRelusergroup->usergroupid = $usergroupId;
        $myRelusergroup->propertyvalue = $propertyDefault;

        $myRelusergroupRecord->insert($myRelusergroup);
    }
}

?>