<?php

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

//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.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');

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

//Relusergrouppropertie
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'];


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

  Controller Name :- loginfunctionCTRL

  OPERTATION in Controller

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

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



//here goes the instances and general variables
//User
$myUser = new User();
$myUserRecord = new UserMySqlDAO();

//Usergroup
$myUsergroup = new Usergroup();
$myUsergroupRecord = new UsergroupMySqlDAO();
$myUsergroupEx = new UsergroupMySqlExtDAO();

//Properties
$myPropertiesRecord = new PropertiesMySqlDAO();

//Relusergrouppropertie
$myRelusergrouppropertie = new Relusergrouppropertie();
$myRelusergrouppropertieRecord = new RelusergrouppropertiesMySqlDAO();



//check and use the condetion that suite this action
if (empty($do)) {
    try {
        run_sql_file("initialValues/pro.sql");


        addUserGroup();

        header("location:login.php");
    } catch (Exception $e) {

    }
} 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
  ================================ */

// add data in save tbl
// add data in usergroup tbl
function addUserGroup() {

    //to use the variable out side the funcion
    global $myUsergroup;
    global $myUsergroupRecord;
    global $myUsergroupEx;



    global $myPropertiesRecord;

    global $myRelusergrouppropertie;
    global $myRelusergrouppropertieRecord;

    $usergroupValidation = $myUsergroupEx->queryAllWithName("admin");

    if (count($usergroupValidation) <= 0) {

        $myUsergroup->currentdate = date("Y-m-d");
        $myUsergroup->startpage = "articleController.php";
        $myUsergroup->tempdelete = 0;
        $myUsergroup->usergroupname = "admin";
        //
        $myUserGroup->savedailydate = date("Y-m-d");
        $myUserGroup->hidemenu = 1; //1 to show menu
        $myUserGroup->hidecat = 0;
        $myUserGroup->clientnegative = 1;
        $myUserGroup->suplliernegative = 1;
        $myUserGroup->storenegative = 1;
        $myUserGroup->directDiscount = 0;
        $myUserGroup->percentageDiscount = 0;
        $myUserGroup->priceupdate = 0;
        $myUserGroup->allowEditDelInBills = 0;
        $myUserGroup->allowEditDelInBuyBills = 0;
        $myUserGroup->showsStorerQuantity = 0;
        $myUserGroup->showSellAllPrice = 0;
        $myUserGroup->showSellHalfPrice = 0;
        $myUserGroup->showSellUnitPrice = 0;
        $myUserGroup->showBuyPrice = 0;
        $myUserGroup->productno = 0;
        $myUserGroup->kempilano = 0;
        $myUserGroup->productnotsellno = 0;
        $myUserGroup->clientcheckno = 0;
        $myUserGroup->suppliercheckno = 0;
        $myUserGroup->productRequist = 0;
        $myUserGroup->otherRequist = 0;
        $myUserGroup->toWorkShopOrder = 0;
        $myUserGroup->insuranceBill = 0;
        $myUserGroup->sellreportpricetype = 0;
        $myUserGroup->netprofit = 0;
        $myUserGroup->hideproductcatid = 0;
        $myUserGroup->directSaveBills = 0;
        $myUserGroup->seeAllHalls = 0;
        $myUserGroup->homePageIcon = 0;
        $myUserGroup->sendKitchen = 1;
        $myUserGroup->printKitchen = $printKitchen;
        $myUserGroup->endBill = 1;
        $myUserGroup->endPrintBill = 1;

        $usergroupId = $myUsergroupRecord->insert($myUsergroup, "admin");
        addReluesUsergroup($usergroupId);
    } else {
        $usergroupId = $usergroupValidation->usergroupid;
    }

    addUser($usergroupId);
}

// add data in relues tbl
function addReluesUsergroup($usergroupId) {

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

    global $myRelusergrouppropertie;
    global $myRelusergrouppropertieRecord;

    $propertiesData = $myPropertiesRecord->queryAll();
    if ($usergroupId == 1) {
        foreach ($propertiesData as $properties) {
            $propertyId = $properties->propertyid;

            $myRelusergrouppropertie->propertyid = $propertyId;
            $myRelusergrouppropertie->propertyvalue = 0;
            $myRelusergrouppropertie->usergroupid = $usergroupId;

            $myRelusergrouppropertieRecord->insert($myRelusergrouppropertie);
        }
    }
}

// add data in user tbl
function addUser($usergroupId) {

    //to use the variable out side the funcion
    global $myUser;
    global $myUserRecord;

    $userValidation = $myUserRecord->queryByUserlevel($usergroupId);

    if (count($userValidation) <= 0) {


        $myUser->userlevel = $usergroupId;
        $myUser->password = 'manager';
        $myUser->username = 'admin';
        $myUserRecord->insert($myUser);
    }
}

function run_sql_file($location) {
    $con = mysql_connect("localhost", "root", "123456");
    mysql_select_db("arabcity", $con);
    $charset = mysql_client_encoding($con);
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER_SET utf8');

    mysql_query("INSERT INTO `properties` VALUES (1, 'Ø®ØµØ§Ø¦Øµ Ø¹Ø§Ù…Ù‡', 0, 1, '');");

    //load file
    $commands = file_get_contents($location);

    //delete comments
    $lines = explode("\n", $commands);
    $commands = '';
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line && !strpos($line, '--')) {
            $commands .= $line . "\n";
        }
    }

    //convert to array
    $commands = explode(";", $commands);
    /* 	print_r("<pre>");
      print_r($commands);
      print_r("<pre>"); */
    //run commands
    $total = $success = 0;
    foreach ($commands as $command) {

        if (trim($command)) {
            print_r($command . "<br>");
            if (mysql_query($command)) {
                $success += 1;
                $total += 1;
            }
            // $success += (@mysql_query($command)==false ? 0 : 1);
            // $total += 1;
        }
    }

    //return number of successful queries and total number of queries found
    return array(
        "success" => $success,
        "total" => $total
    );
}

?>