<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

require_once("imp/_imp.php");

class Controllers
{

    //public vars
    public $smarty;
    public $hosturl;
    public $active;

    // it will run in everytime with
    // default requried
    public function __construct()
    {
        require_once("../public/aw_config.php");
        require_once("imp/_autho.php");//general functions
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        //here name of controll to known
        $this->controllname = 'newdb';
        $this->shcontrollname = 'newdb';
        $this->smarty->assign('controllname', $this->controllname);
        $this->smarty->assign('shcontrollname', $this->shcontrollname);


        //breadcrumbs
        require_once("../../_library/plugins/Breadcrumbs/Breadcrumbs.php");
        $acty = filter_input(INPUT_GET, 'ac');
        $bc = new breadcrumbs($hosturl, '', '', $acty);
        $bc->crumbs();
        $breadcrumbs = $bc->breadcrumbs;
        $smarty->assign('breadcrumbs', $breadcrumbs);

        # here show the active page in menu
        // array called active[] with level of active element
        $active[0] = 'newdb'; //controller name or tab name
        $active[1] = 'newdb'; //controller name

        $this->active = $active;

        #include upload function
        require_once("../../_library/plugins/classupload/uploadfunctions.php");

        //here will call all php pages
        require_once("_header.php");
        require_once("_sidebar.php");
        require_once("_leftsidebar.php");
        require_once("_footer.php");

    }

    //here the frist page and
    //the frist action in this class
    public function index()
    {

        //check login
        autho::checkautho($this->hosturl);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->assign('status', '');
        $this->smarty->display('newdb/add.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);

    }


    public function addit()
    {

        //check login
        autho::checkautho($this->hosturl);

        //here will upload file

        $getname = uploadnew('newdb', False, 0, 0, 'dbs', false);

        $host = DB_HOST;
        $uname = DB_USER;
        $pass = DB_PASS;
        $database = DB_NAME; //Change Your Database Name
        $conn = new mysqli($host, $uname, $pass, $database);
        $getname = str_replace('sql', "txt", $getname);
        $filename = "../../upload/dbs/".$getname; //How to Create SQL File Step : url:http://localhost/phpmyadmin->detabase select->table select->Export(In Upper Toolbar)->Go:DOWNLOAD .SQL FILE
        $op_data = '';
        $lines = file($filename);
        foreach ($lines as $line)
        {
            if (substr($line, 0, 2) == '--' || $line == '')//This IF Remove Comment Inside SQL FILE
            {
                continue;
            }
            $op_data .= $line;
            if (substr(trim($line), -1, 1) == ';')//Breack Line Upto ';' NEW QUERY
            {
                $conn->query($op_data);
                $op_data = '';
            }
        }
        $status = "Table Created Inside " . $database . " Database";


        $this->smarty->assign('status', $status);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('newdb/add.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);

    }

    // at the end of class call
    public function __destruct()
    {
        //close connection
        R::close();
    }


}