<?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 $controllname;
    public $shcontrollname;
    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 = 'index';
        $this->shcontrollname = 'الرئيسيه';
        $this->smarty->assign('controllname', $this->controllname);
        $this->smarty->assign('shcontrollname', $this->shcontrollname);

        //breadcrumbs
        require_once("../../library/plugins/Breadcrumbs/Breadcrumbs_ar.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] = 'index'; //controller name or tab name
        $this->active = $active;


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

        $statistics = R::findOne("statistics", "order by id desc");
        $this->smarty->assign("statistics", $statistics);

        $this->smarty->display('index.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function onesetup() {

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

        try {

            $listOfTables = R::inspect();
            foreach ($listOfTables as $key => $table) {
                if ($key >= 7) {
                    R::wipe($table);
                    print_r($table . '<br/>');
                }
            }

            print_r('trash all , install is good');
        } catch (Exception $ex) {
            print_r($ex);
            print_r('something go wrong  , try again');
        }
    }

    //ajax
    public function statistics() {//there is also social visits
        $colName = filter_input(INPUT_POST, "id");
        $val = (int) filter_input(INPUT_POST, "val");
        try {
            $statistics = R::findOne("statistics", "order by id desc");
            if (empty($statistics->id)) {
                $statistics_obj = R::dispense("statistics");
                //$statistics_obj->id = 1;
                $statistics_obj->visitorsno = 0;
                $statistics_obj->familiesno = 0;
                $statistics_obj->membersno = 0;
                $statistics_obj->visitorsnoshow = 0;
                $statistics_obj->familiesnoshow = 0;
                $statistics_obj->membersnoshow = 0;
                R::store($statistics_obj);
            }
            $statistics = R::findOne("statistics", "order by id desc");
            R::exec("update statistics set $colName = $val where id=$statistics->id");
//        $statistics->$colName = $val;//give redbean error
//        R::store($statistics);
            echo 1;
        } catch (Exception $e) {
            echo -1;
        }
    }

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

}
