<?php

//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
// GOES HERE ....................
include("../public/include_dao.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');



//here the db files that include in the file
//get the do the action
$do = $_GET['do'];

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

  Controller Name :- absenceDaysController

  OPERTATION in Controller

  1-add in absenceDays tbl
  2- show FORM
  3-edit absenceDays data
  4-delete from absenceDays tbl

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

//here the global templates
$smarty->display("header.html");

//Newdbbackup
$myNewdbbackup = new Newdbbackup();
$myNewdbbackupRecord = new NewdbbackupMySqlDAO();


if (empty($do)) {
    include_once("../public/authentication.php");

    $smarty->display("newdbbackupview/add.html");
} elseif ($do == "add") {
    //here the permssion check
    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        add();

        $url = "backupController.php";

        $smarty->assign('urldirect', $url);
        $note = "تمت العملية بنجاح";

        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } catch (Exception $e) {
        $url = "backupController.php";

        $smarty->assign('urldirect', $url);
        $note = "حدث خطأ اثناء اجراء العملية";

        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    }
} elseif ($do == "backup") {
    //stage
    $databaseData = loadDatabase();
    $smarty->assign("databaseData", $databaseData);


    $smarty->display("newdbbackupview/backup.html");
} elseif ($do == "changedb") {
    changedb();

    //send the user to the main page
    header("location:index.php");
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

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


$smarty->assign('customValidation', 1);

//here the global templates
$smarty->display("footer.html");

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

function add() {
    global $myNewdbbackup;
    global $myNewdbbackupRecord;


    $backupName = $_POST["backupname"];


    $myNewdbbackup->backupname = $backupName;
    $myNewdbbackup->backupdate = date("Y-m-d H:i:s");

    $backupId = $myNewdbbackupRecord->insert($myNewdbbackup);

    duplicateTables(ConnectionProperty::getDatabase(), "erp" . $backupId);
}

function duplicateTables($sourceDB = NULL, $targetDB = NULL) {
    $link = mysql_connect(ConnectionProperty::getHost(), ConnectionProperty::getUser(), ConnectionProperty::getPassword()) or die(mysql_error()); // connect to database

    $sql1 = "CREATE DATABASE   " . $targetDB . " ";

    $result1 = mysql_query($sql1) or die(mysql_error());
    $result = mysql_query('SHOW TABLES FROM ' . $sourceDB) or die(mysql_error());

    while ($row = mysql_fetch_row($result)) {
        mysql_query('DROP TABLE IF EXISTS `' . $targetDB . '`.`' . $row[0] . '`') or die(mysql_error());

        mysql_query('CREATE TABLE `' . $targetDB . '`.`' . $row[0] . '` LIKE `' . $sourceDB . '`.`' . $row[0] . '`') or die(mysql_error());

        mysql_query('INSERT INTO `' . $targetDB . '`.`' . $row[0] . '` SELECT * FROM `' . $sourceDB . '`.`' . $row[0] . '`') or die(mysql_error());

        mysql_query('OPTIMIZE TABLE `' . $targetDB . '`.`' . $row[0] . '`') or die(mysql_error());
    }

    mysql_free_result($result);
    mysql_close($link);

    return 1;
}

function loadDatabase() {
    global $myNewdbbackupRecord;

    $databaseData = $myNewdbbackupRecord->queryAll();

    return $databaseData;
}

function changedb() {
    $backupId = $_POST['backupid'];

    if ($backupId == -2) {
        unset($_SESSION['dbname']);
    } else {
        $_SESSION['dbname'] = "storesdb" . $backupId;
    }
}

?>