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

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

//Newdbbackup
require_once('../models/dao/NewdbbackupDAO.class.php');
require_once('../models/dto/Newdbbackup.class.php');
require_once('../models/mysql/NewdbbackupMySqlDAO.class.php');
require_once('../models/mysql/ext/NewdbbackupMySqlExtDAO.class.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');

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


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

  Controller Name :- assetsCTRL

  OPERTATION in Controller

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

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


//here goes the instances and general variables
//Usergroup
$myUsergroupRecord = new UsergroupMySqlDAO();
//Newdbbackup
$myNewdbbackupRecord = new NewdbbackupMySqlDAO();
//User
$userDAO = new UserMySqlDAO();

//Checkcontroller
//check and use the condetion that suite this action
if (empty($do)) {

    $flag = '0';
    $name = $_GET['name'];
    $tablename = $_GET['tablename'];
    $catid = $_GET['catid'];
    if (!isset($catid) && $catid == '-1') {
        $catid = 0;
    }
    checkName($name, $tablename, $catid);
} elseif ($do == "checkNameWithId") {
    $flag = '0';
    $name = $_GET['name'];
    $id = $_GET['id'];
    $tablename = $_GET['tablename'];
    $catid = 0;

    if (isset($name) && $name != "" && isset($tablename) && $tablename != "" && isset($id) && $id != "") {
        if ($tablename == "usergroup") {
            $data = $myUsergroupRecord->load($id);
            $oldName = $data->usergroupname;
        } elseif ($tablename == "newdbbackup") {
            $data = $myNewdbbackupRecord->load($id);
            $oldName = $data->backupname;
        } elseif ($tablename == "user") {
            $data = $userDAO->load($id);
            $oldName = $data->username;
        }
        if (count($data) > 0) {
            if ($name != $oldName) {
                checkName($name, $tablename, 0);
            } else {
                echo $flag;
            }
        }
    }
}
/* ===============================
  function in this CONTROLLER
  ================================ */

function checkName($name, $tablename, $catid) {

    global $myUsergroupRecord;
    global $myNewdbbackupRecord;
    global $userDAO;

    //print("name: " . $name . "  table: " . $tablename);

    $flag = '0';
    if (isset($name) && $name != "" && isset($tablename) && $tablename != "") {
        //print("name: ".$name."  table: ".$tablename);
        if (($tablename == "usergroup" && (count($myUsergroupRecord->queryByUsergroupname($name)) > 0)) || ($tablename == "newdbbackup" && (count($myNewdbbackupRecord->queryByBackupname($name)) > 0) || ($tablename == "user" && (count($userDAO->queryByUsername($name)) > 0)) )
        ) {
            $flag = '1';
        } else {
            $flag = '0';
        }
    }

    echo $flag;
}

?>