<?php

session_start();
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');




//get the do the action
$do = $_GET['do'];


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

  Controller Name :- userAjaxCTRL

  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();
$myUserEx = new UserMySqlExtDAO();

//usergroup
$myUserGroup = new Usergroup();
$myUserGroupRecord = new UsergroupMySqlDAO();
$myUserGroupEx = new UsergroupMySqlExtDAO();




if ($do == "checkusername") {
    $userName = $_GET['username'];

    // here the function that do the action
    $chkUserValid = queryUserName($userName);
    if ($chkUserValid == "0") {
        echo 'true';
    } else {
        echo 'false';
    }
}



if ($do == "checkusernameAnduserid") {


    $userName = $_GET['username'];
    $userIdsearch = $_GET['useridsearch'];

    // here the function that do the action
    $chkUserValid = queryUserNameAndId($userName, $userIdsearch);
    if ($chkUserValid == "0") {
        echo 'true';
    } else {
        echo 'false';
    }
} elseif ($do == "show") {


    // here the function that do the action
    $UserGroupData = loadusergroup();
    $smarty->assign("UserGroupData", $UserGroupData);

    $usergroupId = $_REQUEST['usergroupid'];

    if (isset($usergroupId) && $usergroupId != "-1") {
        showByUsergroupId($usergroupId);
    }

    //here the smarty templates
    $smarty->display("userview/user.html");
}


if ($do == "checkpassword") {
    $id = (int) $_POST['id'];
    $pass = $_POST['pass'];
    $flag = -1;
    if (is_int($id)) {
        $user = $myUserRecord->load($id);
        if ($user->password == $pass) {
            $flag = 1;
        }
    }
    echo $flag;
}




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

// load usergroup
function queryUserName($userName) {
    //to use the variable out side the funcion
    global $myUserRecord;


    $chkUserValid = $myUserRecord->queryByUsername($userName);
    if (count($chkUserValid) <= 0) {
        $chkUserValid = "0";
    } else {
        $chkUserValid = "1";
    }

    return $chkUserValid;
}

function queryUserNameAndId($userName, $userid) {
    //to use the variable out side the funcion
    global $myUserEx;


    $chkUserValid = $myUserEx->queryAllByUserNameAndUserId($userName, $userid);
    if (count($chkUserValid) <= 0) {
        $chkUserValid = "0";
    } else {
        $chkUserValid = "1";
    }

    return $chkUserValid;
}

// select from usergroup tbl
function loadusergroup() {

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

    // select all data from usergroup tbl
    $UserGroupData = $myUserGroupRecord->queryByConditions(0);

    return $UserGroupData;
}

// show the form
function showByUsergroupId($usergroupId) {
    //to use the variable out side the funcion

    global $myUserRecord;
    global $myUserEx;

    global $smarty;

    // required connect
    SmartyPaginate::connect();
    // set items per page
    SmartyPaginate::setLimit(25);


    // get the all colums number
    $allColums = $myUserEx->queryWithGroupIdAndUserId($_SESSION['userid'], $usergroupId);
    SmartyPaginate::setTotal(count($allColums));
    //get the selected data with limitation
    $userData = $myUserEx->queryWithGroupIdAndUserIdLimited(SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit(), $_SESSION['userid'], $usergroupId);

    // assign your db results to the template
    $smarty->assign('userData', $userData);
    // assign {$paginate} var
    SmartyPaginate::assign($smarty);
    //text that show in navgation
    SmartyPaginate::setNextText('التالى');
    SmartyPaginate::setPrevText('السابق');
    //add any varaible in url
    SmartyPaginate::setUrl('userController.php?do=show&usergroupid=' . $usergroupId . '&');
}

?>