<?php

session_start();
ob_start();
//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
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');
//Usergroupadmin
require_once('../models/dao/UsergroupadminDAO.class.php');
require_once('../models/dto/Usergroupadmin.class.php');
require_once('../models/mysql/UsergroupadminMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupadminMySqlExtDAO.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 :- usergroupCTRL

  OPERTATION in Controller

  1-load property data from properities tble
  2-display add form
  2- add in usergroup and relusergroupproperties tble
  3-display sucess or error
  4-display show form
  5-delete from usergroup tble
  6-edit in usergroup data

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

//include_once("../public/auth.php");
//here the global templates
if ($do != "en") {
    $smarty->display("header.html");
}

//here goes the instances and general variables
$myProperitiesRecord = new PropertiesMySqlDAO();

$userGroupAdmin = new Usergroupadmin();
$userGroupAdminDAO = new UsergroupadminMySqlDAO();
$userGroupAdminEX = new UsergroupadminMySqlExtDAO();
//
$myRelusergroup = new Relusergrouppropertie();
$myRelusergroupRecord = new RelusergrouppropertiesMySqlDAO();
$myRelusergrouppropertieExt = new RelusergrouppropertiesMySqlExtDAO();
//check and use the condetion that suite this action
if (empty($do) || $do == "show") {
    $smarty->display("usergroupviewadmin/pass.html");
} elseif ($do == "en") {
    // Read in entire file
    $password = filter_input(INPUT_POST, "password");
    $save = file_get_contents("../models/sql/Save.txt");
    $save = trim($save);
    if ($password == $save) {
        $editData = edit();
        $smarty->assign("properityData", $editData[0]);
        $smarty->assign("properityNum", $editData[1]);

        $smarty->display("usergroupviewadmin/edit.html");
        //header("location:usergroupControllerAdmin.php?do=edit");
    } else {
        echo -1;
    }
} elseif ($do == "update") {
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    $smarty->display("succes.html");
} elseif ($do == "error") {
    $smarty->display("error.html");
}

if ($do != "en") {
    $smarty->display("footer.html");
}

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

// edit in table
function edit() {
    global $userGroupAdminDAO;
    global $userGroupAdminEX;

    //to add any new properity with its default value
    $userGroupAdminRowsNo = $userGroupAdminEX->getCount();
    $userGroupAdminEX->insertSpecial();
    if ($userGroupAdminRowsNo == 0) {
        $userGroupAdminEX->setIsAllowedLikeGroupAdmin();
    }
    //make sure programsetting, usergroup stay visible in menu of admin group
    $sqlRuleGroup = "UPDATE usergroupadmin SET isallowed = 0 WHERE propertyid in(1,4,85);";
    $sqlRuleGroup .= "UPDATE relusergroupproperties SET propertyvalue = 0 WHERE usergroupid = 1 and propertyid in(1,4,85);";
    $userGroupAdminEX->updateBulk($sqlRuleGroup);


    $properityData = $userGroupAdminEX->queryAllEX();
    //get count of selected Data
    $properityNum = count($properityData);
    return array($properityData, $properityNum);
}

// update table
function update() {
    global $userGroupAdmin;
    global $userGroupAdminDAO;
    global $userGroupAdminEX;
    global $myRelusergrouppropertieExt;

    $itr = $_POST['itr'];

    //update data in relusergroupproperties tbl
    $sql = '';
    $sqlRuleGroup = '';
    $h = 1;
    for ($h = 1; $h <= $itr; $h++) {
        if (isset($_POST['propertydefault' . $h . '']) && isset($_POST['propertyid' . $h . ''])) {
            $isallowed = (int) $_POST['propertydefault' . $h . ''];
            $propertyId = (int) $_POST['propertyid' . $h . ''];
            $id = (int) $_POST['id' . $h . ''];

            $sql .= "UPDATE usergroupadmin SET isallowed = $isallowed WHERE id = $id;"; //SET propertyid = $propertyId,
            if ($isallowed == 1) {
                $sqlRuleGroup .= "UPDATE relusergroupproperties SET propertyvalue = 1 WHERE propertyid = $propertyId;";
            }
        }
    }
    if ($sql != "") {
        $userGroupAdminEX->updateBulk($sql);
    }
    if ($sqlRuleGroup != "") {
        //make sure programsetting, usergroup stay visible in menu of admin group
        $sqlRuleGroup .= "UPDATE usergroupadmin SET isallowed = 0 WHERE propertyid in(1,4,85);";
        $sqlRuleGroup .= "UPDATE relusergroupproperties SET propertyvalue = 0 WHERE usergroupid = 1 and propertyid in(1,4,85);";
        $userGroupAdminEX->updateBulk($sqlRuleGroup);
    }
    //finally
    $myRelusergrouppropertieExt->setAllControllersAllowedForGroup(-1); //group -1 is gt4 admin hidden group
    $fh = fopen('../temp__cashedheader/cashedheader_' . $_SESSION['dbname'] . '_' . $_SESSION['userid'] . '.html', 'w');
    fclose($fh);
//    var_dump('../temp__cashedheader/cashedheader_' . $_SESSION['dbname'] . '_' . $_SESSION['userid'] . '.html');
//    die();
}

?>