<?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("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";

        $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 = '123';
        $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
    );
}

?>