<?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;

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


        $controlname = 'index';
        $this->smarty->assign('index', 1);

        require_once("header.php");
        require_once("footer.php");
    }

    //here the frist page and
    //the frist action in this class
    public function index() {
        if (isset($_SESSION['msg']) && !empty($_SESSION['msg'])) {
            $this->smarty->assign('msg', $_SESSION['msg']);
            unset($_SESSION['msg']);
        } else {
            $this->smarty->assign('msg', "");
        }


        //slider
        $allslider = R::findAll('slider', 'hide=0');
        $this->smarty->assign('allslider', $allslider);
        //statistics
        $statistics = R::findOne("statistics", "order by id desc");
        $this->smarty->assign("statistics", $statistics);
        //$packages for pricing html
        $packages = R::findAll("packages", 'hide=0 limit 5');
        $this->smarty->assign("packages", $packages);

//        //services
//        $allservices = R::findAll('services', 'del=0 order by rand() limit 3');
//        $this->smarty->assign('allservices', $allservices);
//        //productscategory
//        $productscategory = R::findAll('productscategory', 'del = 0');
//        $this->smarty->assign('productscategory', $productscategory);
//        $this->smarty->assign('productscategoryCount', count($productscategory));
//        //productscategory
//        $allproducts = R::getAll("select products.*,productsgallery.name,
//            productscategory.id as catId,productscategory.productname_ar as catName,productscategory.productname_en as catName_en
//                from products
//                join productsgallery on productsgallery.product_id = products.id
//                join productscategory on productscategory.id = products.category_id
//                where products.del=0 and productscategory.del=0 order by rand() limit 6");
//        $this->smarty->assign('allproducts', $allproducts);
//        $categories = array();
//        foreach ($allproducts as $pro) {
//            $cat = new stdClass();
//            $cat->id = $pro["catId"];
//            $cat->productname_ar = $pro["catName"];
//            $cat->productname_en = $pro["catName_en"];
//            $categories[$cat->id] = $cat;
//        }
//        $this->smarty->assign('productscategoryCount', count($categories));
//
//        $this->smarty->assign('productscategory', $categories);
//
//        $allPage = R::findAll('page', 'limit 3');
//        $this->smarty->assign('allPage', $allPage);
//        //News
//        $allNews = R::findAll('news', 'order by rand() limit 3');
//        $this->smarty->assign('allNews', $allNews);
        //clients
        $allClient = R::findAll('clients', 'del = 0');
        $this->smarty->assign('allClient', $allClient);
//        $feature = R::findOne('features', 'id = 1 order by id desc');
//        $this->smarty->assign('feature', $feature);
//
//        $questionnaire = R::findOne('features', 'id = 2 order by id desc');
//        $this->smarty->assign('questionnaire', $questionnaire);
//
//        //Category
//        $allCategory = R::getAll('select distinct productscategory.* from productscategory, products where productscategory.id = products.category_id and productscategory.del = 0 and products.del = 0 order by productscategory.id desc');
//        $this->smarty->assign('allCategory', $allCategory);
//
//        //Products
//        $allProduct = R::findAll('products', 'del = 0 order by id desc limit 6');
//        foreach ($allProduct as $product) {
//            $product->image = R::findOne('productsgallery', 'product_id = ' . $product->id)->name;
//        }
//        $this->smarty->assign('allProduct', $allProduct);
        $aboutus = R::load('page', 1);
        $this->smarty->assign('aboutus', $aboutus);

        header::headershow($this->smarty, $this->hosturl);

        $this->smarty->display('templates/' . default_lang_interface . '/slider.html');
        $this->smarty->display('templates/' . default_lang_interface . '/about.html');
        $this->smarty->display('templates/' . default_lang_interface . '/facts.html');
        $this->smarty->display('templates/' . default_lang_interface . '/pricing.html');
        $this->smarty->display('templates/' . default_lang_interface . '/testemonials.html');
        //$this->smarty->display('templates/' . default_lang_interface . '/client.html');

        footer::footershow($this->smarty, $this->hosturl);
    }

    public function addVisit() {
        $site = filter_input(INPUT_POST, 'site');

        $visit = R::load('socialvisit', 1);
        if ($visit) {
            if ($visit->$site)
                $count = $visit->$site + 1;
            else
                $count = 1;

            $visit->$site = $count;
        } else {
            $visit = R::dispense('socialvisit');
            $visit->$site = 1;
        }
        R::store($visit);
    }

    public function increaseVisitorsno() {
        $statistics = R::findOne("statistics", "order by id desc");
        if ($statistics) {
            $statistics->visitorsno = $statistics->visitorsno + 1;
        } else {
            $statistics = R::dispense("statistics");
            $statistics->visitorsno = 1;
            $statistics->familiesno = 0;
            $statistics->membersno = 0;
            $statistics->visitorsnoshow = 0;
            $statistics->familiesnoshow = 0;
            $statistics->membersnoshow = 0;
        }
        R::store($statistics);
    }

    public function changelang() {


        $newlang = filter_input(INPUT_POST, 'newlang');
        $_SESSION['new_lang'] = $newlang;
    }

    public function subscribe() {
        $email = filter_input(INPUT_POST, 'email_sub');
        if ($email) {
            $newsletter = R::dispense('newsletter');

            $newsletter->email = $email;
            $newsletter->mdate = date('Y-m-d H:i:s');
            $newsletter->conditions = 0;

            try {
                R::debug(TRUE);
                R::store($newsletter);
                $_SESSION['msgSub'] = 1;
            } catch (Exception $e) {
                $_SESSION['msgSub'] = -1;
            }
        } else {
            $_SESSION['msgSub'] = -1;
        }
        header('location:' . $this->hosturl . '/' . $this->controlname);
    }

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

}
