<?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;
    //menu itr for input
    public $menuitr;

    // 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 = 'siteinfo';
        $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, $this->controllname, $this->shcontrollname, $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] = 'siteinfo';
        $active[1] = 'siteinfo'; //controller name or tab 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);

        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        #select table
        $setting = R::getAll('SELECT * FROM siteinfo order by id desc');
        if (count($setting) > 0) {

            //send info to html
            $this->smarty->assign('setting', $setting[0]);
//            print_r($setting[0]);
        }
//
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('siteinfo/edit.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    //the add and update in siteinfo table
    public function edit() {

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

        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //get all vars
        $project_name = filter_input(INPUT_POST, 'project_name');
        $project_name_en = filter_input(INPUT_POST, 'project_name_en');

        $email = filter_input(INPUT_POST, 'email');
        $address = filter_input(INPUT_POST, 'address');
        $address_en = filter_input(INPUT_POST, 'address_en');

        $fb = filter_input(INPUT_POST, 'fb');
        $tw = filter_input(INPUT_POST, 'tw');
        $linked = filter_input(INPUT_POST, 'linked');
        $st = filter_input(INPUT_POST, 'st');
        $gplus = filter_input(INPUT_POST, 'gplus');
        $phone = filter_input(INPUT_POST, 'phone');
        $mobile = filter_input(INPUT_POST, 'mobile');
        $fax = filter_input(INPUT_POST, 'fax');
        $aboutsite = filter_input(INPUT_POST, 'aboutsite');
        $aboutsite_en = filter_input(INPUT_POST, 'aboutsite_en');
        $map = filter_input(INPUT_POST, 'map_en');

        $hours = filter_input(INPUT_POST, 'work_hours');
        $hours_en = filter_input(INPUT_POST, 'work_hours_en');

        $android = filter_input(INPUT_POST, 'android');
        $ios = filter_input(INPUT_POST, 'ios');
        #select table
        $setting = R::findAll('siteinfo', 'order by id desc');
        if (count($setting) > 0) {
            //update current
            //get id vars in update statution
            $id = filter_input(INPUT_POST, 'id');
            $logo = uploadupdate('filee', 'imageurl', False, 0, 0, 'siteinfo');
            $imgabout = uploadupdate('imgabout', 'imgabouturl', False, 0, 0, 'siteinfo');
            $plan = uploadupdate('plan', 'planurl', False, 0, 0, 'siteinfo', FALSE);
//
            // echo $id ;

            $setting = R::load('siteinfo', $id);
//            print_r($setting);
//
//            //fill object with data
            $setting->project_name = $project_name;
            $setting->project_name_en = $project_name_en;

            $setting->email = $email;

            $setting->address = $address;
            $setting->address_en = $address_en;

            $setting->hours = $hours;
            $setting->hours_en = $hours_en;

            $setting->fb = $fb;

            $setting->tw = $tw;

            $setting->linked = $linked;
//           echo $setting->linked ;

            $setting->st = $st;
//           echo $setting->st ;

            $setting->gplus = $gplus;
//           echo $setting->gplus ;
            $setting->logo = $logo;
            $setting->imgabout = $imgabout;
//           echo $setting->logo ;
            $setting->phone = $phone;
//           echo $setting->phone ;
            $setting->mobile = $mobile;
//           echo $setting->mobile ;
            $setting->fax = $fax;
            $setting->map = $map;
            $setting->aboutsite = $aboutsite;
            $setting->aboutsite_en = $aboutsite_en;

            $setting->android = $android;
            $setting->ios = $ios;
            $setting->plan = $plan;

            R::store($setting);

            try {
                R::store($setting);
                header('Location:' . $this->hosturl . '/' . $this->controllname . '/index');
            } catch (Exception $e) {
//                header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
            }
        } else {
            //insert new

            $logo = uploadnew('filee', False, 0, 0, 'siteinfo');
            $imgabout = uploadnew('imgabout', False, 0, 0, 'siteinfo');
            $plan = uploadnew('plan', False, 0, 0, 'siteinfo', FALSE);

            $setting = R::dispense('siteinfo');

            //fill object with data
            $setting->project_name = $project_name;
            $setting->project_name_en = $project_name_en;

            $setting->email = $email;
            $setting->address = $address;
            $setting->address_en = $address_en;

            $setting->fb = $fb;
            $setting->tw = $tw;
            $setting->linked = $linked;
            $setting->st = $st;
            $setting->gplus = $gplus;
            $setting->logo = $logo;
            $setting->imgabout = $imgabout;
            $setting->phone = $phone;
            $setting->mobile = $mobile;
            $setting->fax = $fax;
            $setting->map = $map;

            $setting->aboutsite = $aboutsite;
            $setting->aboutsite_en = $aboutsite_en;

            $setting->android = $android;
            $setting->ios = $ios;
            $setting->plan = $plan;
            try {
                $id = R::store($setting);
                header('Location:' . $this->hosturl . '/' . $this->controllname . '/index');
            } catch (Exception $e) {
//                header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
            }
        }
    }

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

}
