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




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



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

  Controller Name :- transferdatabaseCTRL اضافة قاعدة بيانات ونقل الجداول

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

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

//check and use the condetion that suite this action

if ($do == "checkDatabaseName") {

    $databaseSearchName = $_GET['databaseSearchName'];

    $databaseSearchValid = checkDatabaseName($databaseSearchName);

    echo $databaseSearchValid;
} elseif ($do == "emptyTable") {
    $tableName = $_GET['tablename'];

    $newdatabaseName = $_GET['newdatabaseName'];
    $newUserName = $_GET['newUserName'];
    $newPassword = $_GET['newPassword'];


    emptyTable($tableName, $newdatabaseName, $newUserName, $newPassword);
}






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

function getDatabaseName() {

    // Read in entire file
    $databsesNames = file_get_contents("../views/default/archiveview/dbNames.txt");

    $databases = array_reverse(explode(",", $databsesNames));

    return $databases;
}

function checkDatabaseName($databaseSearchName) {
    $databasesName = getDatabaseName();

    if (in_array($databaseSearchName, $databasesName)) {
        return 1;
    } else {
        return 0;
    }
}

function emptyTable($tableName, $newdatabaseName, $newUserName, $newPassword) {

    //new Database
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());


    //if($tableName == "**agz_tbl")
//			{
//
//			}
//			elseif($tableName == "**backups_tbl")
//			{
//			}
//			elseif($tableName == "**backup_info_tbl")
//			{
//			}

    if ($tableName == "back_buy_bill_tbl") {

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnbuybill", $newDatabaseLink) or die(mysql_error());


        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnbuybilldetail", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "back_sell_bill_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnsellbill", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnsellbilldetail", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "bank_account_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".bank", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".bankaccount", $newDatabaseLink) or die(mysql_error());
    }


    //elseif($tableName == "bank_deposite_tbl")
//			{
//
//				$smarty->assign("tableArabicName", "ايداع شيكات");
//
//			}
//
//
//			elseif($tableName == "bank_sa7b_tbl")
//			{
//				$smarty->assign("tableArabicName", "سحب شيكات");
//			}
    elseif ($tableName == "buy_bill_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybill`", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybilldetail`", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "cat_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`productcat`", $newDatabaseLink) or die(mysql_error());
    }


    //elseif($tableName == "**changinstores_tbl")
//			{
//
//			}
//
//
//			elseif($tableName == "**client1_report_tbl")
//			{
//
//			}
//
    elseif ($tableName == "clients_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`client`", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`clientdebtchange`", $newDatabaseLink) or die(mysql_error());
    }



    //elseif($tableName == "client_report_tbl")
//			{
//				$smarty->assign("tableArabicName", "مديونية العملاء");
//			}
    //elseif($tableName == "**discount_product_tmp_tbl")
//			{
//
//			}
    //	elseif($tableName == "**expenses_report_tbl")
//			{
//
//
//			}
    elseif ($tableName == "expenses_tbl") {

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`expenses`", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "expenses_type") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`expensestype`", $newDatabaseLink) or die(mysql_error());
    }

    ////////
    elseif ($tableName == "kem_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`kempiala`", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "owners") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`partner`", $newDatabaseLink) or die(mysql_error());
    }


    //elseif($tableName == "**producttemp_tbl")
//			{
//
//			}
//
    elseif ($tableName == "product_setting_tbl") {

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`productsetting`", $newDatabaseLink) or die(mysql_error());
    }


    //elseif($tableName == "**product_supplier_tbl")
//			{
//
//			}
//
    elseif ($tableName == "product_tbl") {

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".product", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".productunit", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "prog_setting_tbl") {

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".programsettings", $newDatabaseLink) or die(mysql_error());
    }


    //elseif($tableName == "**save")
//			{
//
//			}
    elseif ($tableName == "saves_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".save", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "save_report_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".savedaily", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "sell_bill_tbl") {
        mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".sellbill", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".sellbilldetail", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "store_details") {
        mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".storereport", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "store_details_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".storedetail", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "store_tbl") {
        mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".store", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "store_transfer_tbl") {
        mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".storemovement", $newDatabaseLink) or die(mysql_error());
    }



    //elseif($tableName == "**store_transfer_tmp_tbl")
//			{
//
//
//			}
    //elseif($tableName == "supplier_report_tbl")
//			{
//				$smarty->assign("tableArabicName", "مديونية الموردين");
//			}
    elseif ($tableName == "supplier_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplier`", $newDatabaseLink) or die(mysql_error());

        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplierdebtchange`", $newDatabaseLink) or die(mysql_error());
    }


    //	elseif($tableName == "type_tbl")
//			{
//				$smarty->assign("tableArabicName", "وحدات القياس");
//			}
    elseif ($tableName == "users_tbl") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".user", $newDatabaseLink) or die(mysql_error());
    } elseif ($tableName == "withdrawals") {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`partnerwithdrawal`", $newDatabaseLink) or die(mysql_error());
    }
}

?>