<?php

session_start();
ob_start();
if (!ini_get('date.timezone')) {
    date_default_timezone_set('GMT'); //Asia/Riyadh
}
//date_default_timezone_set('America/Los_Angeles');

include("../public/include_dao.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');

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

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

//Notics
require_once('../models/dao/NoticsDAO.class.php');
require_once('../models/dto/Notic.class.php');
require_once('../models/mysql/NoticsMySqlDAO.class.php');
require_once('../models/mysql/ext/NoticsMySqlExtDAO.class.php');

//Menuurl
$Menuurl = new Menuurl();
$MenuurlDAO = new MenuurlMySqlDAO();
$MenuurlEX = new MenuurlMySqlExtDAO();


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

//Notics
$Notics = new Notic();
$NoticsDAO = new NoticsMySqlDAO();
$NoticsExt = new NoticsMySqlExtDAO();

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Relusergroupproperties
$myRelusergrouppropertiesRecord = new RelusergrouppropertiesMySqlDAO();
$myRelusergrouppropertiesEx = new RelusergrouppropertiesMySqlExtDAO();

if (isset($_SESSION['usergroupid']) && $_SESSION['usergroupid'] != "") {
    $myusergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
}



$_SESSION['hidecat'] = $myusergroupdata->hidecat;
$_SESSION['clientnegative'] = $myusergroupdata->clientnegative;
$_SESSION['suplliernegative'] = $myusergroupdata->suplliernegative;
$_SESSION['storenegative'] = $myusergroupdata->storenegative;


$sessionUserId = $_SESSION['userid'];
$userGroupIDs = $_SESSION['usergroupid'];


$sessionEmployeename = $_SESSION['employeename'];

$prog = $ProgramsettingDAO->load(1);
if (empty($prog->currancy)) {
    $prog->currancy = "ريال";
}
$smarty->assign("currency", $prog->currancy);
if (empty($sessionUserId) || empty($userGroupIDs)) {
    //send the user to the login page
    header("location:login.php");
} else {
    $smarty->assign("sessionEmployeename", $sessionEmployeename);
    $smarty->assign("sessionUserId", $sessionUserId);

    $userGroupIDs = $_SESSION['usergroupid'];

    $reluseData = $myRelusergrouppropertiesEx->queryAllwithUsergroupidAndValue($userGroupIDs);


    //if this group not created yet
    if (count($reluseData) <= 0) {
        session_destroy();
        header("location:login.php");
    } else {
        /* if ($myusergroupdata->hidemenu == 1) {//display
          $cashedheaderSize = filesize('../views/default/cashedheader.html');
          $cashedheaderSize = rtrim($cashedheaderSize);
          $smarty->assign("cashedheaderSize", $cashedheaderSize);
          if ($cashedheaderSize > 0) {

          $cashedheader = file_get_contents("../views/default/cashedheader.html");
          $smarty->assign("cashedheader", $cashedheader);
          } else {
          $h = 1;
          foreach ($reluseData as $reluse) {
          $propertyid = $reluse->propertyid;
          //	print_r("propertyid=".$propertyid."<br>");
          $subReluseData = $myRelusergrouppropertiesEx->queryAllwithUsergroupidAndPropertyparent($userGroupIDs, $propertyid);
          if (count($subReluseData) <= 0) {
          $subReluseNum = 0;
          } else {
          $subReluseNum = 1;
          }
          $smarty->assign("subReluseNum" . $h . "", $subReluseNum);

          $smarty->assign("subReluseData" . $h . "", $subReluseData);

          $k = 1;
          foreach ($subReluseData as $subReluse) {
          $subPropertyid = $subReluse->propertyid;

          $subSubReluseData = $myRelusergrouppropertiesEx->queryAllwithUsergroupidAndPropertyparent($userGroupIDs, $subPropertyid);

          if (count($subSubReluseData) <= 0) {
          $subSubReluseNum = 0;
          } else {
          $subSubReluseNum = 1;
          }

          $smarty->assign("subSubReluseNum" . $h . $k . "", $subSubReluseNum);
          $smarty->assign("subSubReluseData" . $h . $k . "", $subSubReluseData);

          $k++;
          }

          $h++;
          }

          $smarty->assign("reluseData", $reluseData);
          }
          } */
    }




    $theRoleAndpageName = $myRelusergrouppropertiesEx->queryAllwithUsergroupidAndURL($userGroupIDs, PageNamectrl());

    if (count($theRoleAndpageName) > 0 || PageNamectrl() == "index.php") {

    } else {

        header("location:login.php");
    }
}



$idarray = array();
$x = array();
$finalnamex = "";
$allmenudata = $MenuurlDAO->queryAll();

if (count($allmenudata) > 0) {
    foreach ($allmenudata as $myallmenudata) {


        $allusergroupids = $myallmenudata->usergroupid;
        //

        $idarray = explode(',', $allusergroupids);
        //print_r($idarray);
        $kay = in_array($_SESSION['usergroupid'], $idarray);
        if ($kay) {

            $finalnamex = $finalnamex . $myallmenudata->menuurlid . ",";
        }
    }

    $allusergroupids = substr($finalnamex, 0, -1);

    if ($allusergroupids != "") {
        $alllastdata = $MenuurlEX->queryAllinarray($allusergroupids);
    }

    $smarty->assign("alllastdata", $alllastdata);
}


#################################################################  notics   #################################################################
$sum = 0;
$smarty->assign("sum", $sum);
?>