<?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 contacturied
    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 = 'contactmessage';
        $this->shcontrollname = 'اتصل بنا';
        $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] = 'contactmessage'; //controller name or tab name
        $active[1] = 'contactmessage'; //controller name
        $this->active = $active;

        #include upload function
        require_once("../../library/plugins/classupload/uploadfunctions.php");
        require "../../library/plugins/PHPMailer/PHPMailer.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

        $allComplain = R::findAll("contactmsg", "type = 1 and del = 0 order by mdate desc");
        $allRequest = R::findAll("contactmsg", "type = 2 and del = 0 order by mdate desc");
        $allSuggestion = R::findAll("contactmsg", "type = 3 and del = 0 order by mdate desc");

        $this->smarty->assign('allComplain', $allComplain);
        $this->smarty->assign('allRequest', $allRequest);
        $this->smarty->assign('allSuggestion', $allSuggestion);

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

    public function show() {

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

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

        #select table

        $allComplain = R::findAll("contactmsg", "type = 1 and del = 0 order by mdate desc");
        $allRequest = R::findAll("contactmsg", "type = 2 and del = 0 order by mdate desc");
        $allSuggestion = R::findAll("contactmsg", "type = 3 and del = 0 order by mdate desc");

        $this->smarty->assign('allComplain', $allComplain);
        $this->smarty->assign('allRequest', $allRequest);
        $this->smarty->assign('allSuggestion', $allSuggestion);

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

    //the add and update in contacts table
    public function details() {

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

        //get id for item
        $itemid = filter_input(INPUT_GET, 'id');
        //update
        $contactmessage = R::load('contactmsg', $itemid);
        $contactmessage->read = 1;
        R::store($contactmessage);
        $editelement = R::load("contactmsg", $itemid);
        $country = R::load('country', $editelement->country_id);
        //load all here
//        $editelement = R::load('contact', $itemid);
        $this->smarty->assign('editelement', $editelement);
        $this->smarty->assign('country', $country->nameen);

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

    public function del() {

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

        //get id for item
        $itemid = filter_input(INPUT_GET, 'id');
        //load all item here
        $contacts = R::load('contactmsg', $itemid);


        try {
            $contacts->del = 1;
            R::store($contacts);
            header('Location:' . $this->hosturl . '/' . $this->controllname);
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notdel/' . $this->controllname);
        }
    }

    public function deleteselected() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        //get id for item
        $deleteidsString = $_POST['deleteids'];
        $deleteidsArray = explode(',', $deleteidsString);
        foreach ($deleteidsArray as $id) {
            $contacts = R::load('contactmsg', $id);
            $contacts->del = 1;
            R::store($contacts);
        }
        echo 1;
    }

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

}
