<?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 $tableName;
    public $active;
    private $cards_arr;

    // 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;
        $this->cards_arr = array();

        //here name of controll to known
        $this->controllname = 'slider';
        $this->shcontrollname = 'سليدر الباصات';
        $this->tableName = 'slider';
        $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] = 'slider'; //controller name or tab name
        $active[1] = 'slider'; //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);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //load all parents here
        $bus = R::findAll('bus');
        $this->smarty->assign('bus', $bus);


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

    public function addit() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        //vars here
        $title = filter_input(INPUT_POST, 'title');
        $bus_id = filter_input(INPUT_POST, 'bus_id');

        $content = filter_input(INPUT_POST, 'content');
        $link = filter_input(INPUT_POST, 'link');
        $title_en = filter_input(INPUT_POST, 'title_en');
        $content_en = filter_input(INPUT_POST, 'content_en');
        $image = uploadnew('slider', False, 0, 0, 'm_Slider');

        //here the add
        $slider = R::dispense('slider');
        $slider->title = $title;
        $slider->bus_id = $bus_id;

        $slider->content = $content;
        $slider->link = $link;
        $slider->title_en = $title_en;
        $slider->content_en = $content_en;
        $slider->image = $image;
        try {
            $id = R::store($slider);
            header('Location:' . $this->hosturl . '/' . $this->controllname . '/show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
        }
    }

    public function show() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        //here will load the menu show
        $sliders = R::findAll('slider', ' where default_bus = 0');

        foreach ($sliders as $value) {

            $bus_id = $value['bus_id'];
            $busName = 'لا يوجد';
            if ($bus_id != '') {
                $loadBusName = R::load('bus', $bus_id);
                $busName = $loadBusName['name'];
            }
            $value['bus_id'] = $busName;
        }


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

    public function edit() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        $sliders = R::findAll('slider', 'order by id desc');
        $this->smarty->assign('sliders', $sliders);

        //load all parents here
        $bus = R::findAll('bus');
        $this->smarty->assign('bus', $bus);

        //get id for item
        $itemid = filter_input(INPUT_GET, 'id');
        //load all here
        $slider = R::load('slider', $itemid);
        $this->smarty->assign('returnedData', $slider);


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

    public function updateit() {

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

        //vars here
        $title = filter_input(INPUT_POST, 'title');
        $bus_id = filter_input(INPUT_POST, 'bus_id');
        $content = filter_input(INPUT_POST, 'content');
        $link = filter_input(INPUT_POST, 'link');
        $title_en = filter_input(INPUT_POST, 'title_en');
        $content_en = filter_input(INPUT_POST, 'content_en');
        $image = uploadupdate('slider', 'imageurl', False, 0, 0, 'm_Slider');


        $id = filter_input(INPUT_POST, 'id');


        //R::debug( TRUE );
        //load all parents here
        $slider = R::load('slider', $id);

        //here the upadte
        $slider->title = $title;
        $slider->bus_id = $bus_id;
        $slider->content = $content;
        $slider->link = $link;
        $slider->title_en = $title_en;
        $slider->content_en = $content_en;
        $slider->image = $image;

        try {
            R::store($slider);
            header('Location:' . $this->hosturl . '/' . $this->controllname . '/show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
        }
    }

    public function hide() {

        //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_POST, 'itemid');
        $ishide = filter_input(INPUT_POST, 'ishide');
        //load all item here
        $slider = R::load('slider', $itemid);
        $slider->hide = $ishide;
        try {
            R::store($slider);
            //change the button
            echo '1';
        } catch (Exception $e) {
            echo '0';
        }
    }

    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
        $slider = R::load('slider', $itemid);
        if (file_exists('../../upload/' . $this->controllname . '/' . $slider->image)) {
            //echo $imageNewName;
            chmod('../../upload/' . $this->controllname . '/' . $slider->image, 0777);
            unlink('../../upload/' . $this->controllname . '/' . $slider->image);
        }

        try {

            R::trash($slider);
            header('Location:' . $this->hosturl . '/' . $this->controllname . '/show');
        } 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) {
            $slider = R::load('slider', $id);
            if (file_exists('../../upload/' . $this->controllname . '/' . $slider->image)) {
                //echo $imageNewName;
                chmod('../../upload/' . $this->controllname . '/' . $slider->image, 0777);
                unlink('../../upload/' . $this->controllname . '/' . $slider->image);
            }
            R::trash($slider);
        }
        echo 1;
    }

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

}
