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

        $this->smarty = $smarty;
        $this->hosturl = $hosturl;
    }

    public function uploadit() {

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

        //upload the files
        $img = uploadnew('file', False, 0, 0, 'firmfiles');
        $img_mob = uploadnew('file', TRUE, 300, 0, 'firmfiles');

        $firmfiles = R::dispense('firmfiles');
        $firmfiles->firmid = filter_input(INPUT_POST, 'firmid');
        $firmfiles->img = $img;
        $firmfiles->img_mob = $img_mob;
        $id = R::store($firmfiles);

        echo $id;
    }

    public function delfile() {

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

        //get id for item
        $itemid = filter_input(INPUT_POST, 'itemid');

        //load all item here
        $firmfiles = R::load('firmfiles', $itemid);

        if (file_exists('../../upload/firmfiles/' . $firmfiles->img)) {
            //echo $imageNewName;
            chmod('../../upload/firmfiles/' . $firmfiles->img, 0777);
            unlink('../../upload/firmfiles/' . $firmfiles->img);
        }
        if (file_exists('../../upload/firmfiles/' . $firmfiles->img_mob)) {
            //echo $imageNewName;
            chmod('../../upload/firmfiles/' . $firmfiles->img_mob, 0777);
            unlink('../../upload/firmfiles/' . $firmfiles->img_mob);
        }

        try {

            R::trash($firmfiles);
        } catch (Exception $e) {

        }
    }

    public function sendmsg() {

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

        $sendto = filter_input(INPUT_POST, 'emailto');
        $subjectto = filter_input(INPUT_POST, 'subject');
        $message = filter_input(INPUT_POST, 'msgit');

        $info = R::findOne('siteinfo', 'order by id desc');

        $to = $sendto;
        $subject = $subjectto;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= "From: " . $info->email;

        $retval = mail($to, $subject, $message, $headers);

        if ($retval == true) {
            echo "1";
        } else {
            echo "0";
        }
    }

    public function sendmsgnewsletter() {

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

        $sendToArr = filter_input(INPUT_POST, 'sendToArr', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
        $subjectto = filter_input(INPUT_POST, 'subject');
        $message = filter_input(INPUT_POST, 'msgit');

        $info = R::findOne('siteinfo', 'order by id desc');





        $subject = $subjectto;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= "From: " . $info->email;

        $fail_arr = array();
        foreach ($sendToArr as $sendto) {
            $to = $sendto;
            $retval = mail($to, $subject, $message, $headers);
            if ($retval == true) {
                //sucess
                //echo "1";
            } else {
                //fail
                //echo "0";
                array_push($fail_arr, $to);
            }
        }


        if (count($fail_arr) == 0) {
            echo 1; //all sucess
        } else {
            echo json_encode($fail_arr);
        }
    }

    public function checkusername() {

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

        $username = filter_input(INPUT_POST, 'username');
        $valis = filter_input(INPUT_POST, 'valis');

        $user_count = R::count('clients', 'user_name = ?', [$username]);
        $user_info = R::findOne('clients', 'user_name = ?', [$username]);

        if ($user_count > 0) {
            if ($valis) {
                //edit
                if ($user_info->user_name == $valis) {
                    echo "<span class='status-available'>اسم الدخول الخاص بك</span>";
                } else {
                    echo "<span class='status-not-available'> الاسم غير متوفر , جرب مره اخرى</span>";
                }
            } else {
                echo "<span class='status-not-available'> الاسم غير متوفر , جرب مره اخرى</span>";
            }
        } else {
            echo "<span class='status-available'> الاسم متوفر</span>";
        }
    }

    public function checkemail() {

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

        $email = filter_input(INPUT_POST, 'email');
        $valis = filter_input(INPUT_POST, 'valis');

        $user_count = R::count('clients', 'email = ?', [$email]);
        $user_info = R::findOne('clients', 'email = ?', [$email]);

        if ($user_count > 0) {
            if ($valis) {
                //edit
                if ($user_info->email == $valis) {
                    echo "<span class='status-available'>بريدك الخاص بك</span>";
                } else {
                    echo "<span class='status-not-available'> البريد غير متوفر , جرب مره اخرى</span>";
                }
            } else {
                echo "<span class='status-not-available'> البريد غير متوفر , جرب مره اخرى</span>";
            }
        } else {
            echo "<span class='status-available'> البريد متوفر</span>";
        }
    }

    public function checkfusername() {

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

        $username = filter_input(INPUT_POST, 'username');
        $valis = filter_input(INPUT_POST, 'valis');

        $user_count = R::count('firms', 'user_name = ?', [$username]);
        $user_info = R::findOne('firms', 'user_name = ?', [$username]);

        if ($user_count > 0) {
            if ($valis) {
                //edit
                if ($user_info->user_name == $valis) {
                    echo "<span class='status-available'>اسم الدخول الخاص بك</span>";
                } else {
                    echo "<span class='status-not-available'> الاسم غير متوفر , جرب مره اخرى</span>";
                }
            } else {
                echo "<span class='status-not-available'> الاسم غير متوفر , جرب مره اخرى</span>";
            }
        } else {
            echo "<span class='status-available'> الاسم متوفر</span>";
        }
    }

    public function checkfemail() {

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

        $email = filter_input(INPUT_POST, 'email');
        $valis = filter_input(INPUT_POST, 'valis');

        $user_count = R::count('firms', 'email = ?', [$email]);
        $user_info = R::findOne('firms', 'email = ?', [$email]);

        if ($user_count > 0) {
            if ($valis) {
                //edit
                if ($user_info->email == $valis) {
                    echo "<span class='status-available'>بريدك الخاص بك</span>";
                } else {
                    echo "<span class='status-not-available'> البريد غير متوفر , جرب مره اخرى</span>";
                }
            } else {
                echo "<span class='status-not-available'> البريد غير متوفر , جرب مره اخرى</span>";
            }
        } else {
            echo "<span class='status-available'> البريد متوفر</span>";
        }
    }

    public function checknat() {

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

        $natid = filter_input(INPUT_POST, 'natid');
        $valis = filter_input(INPUT_POST, 'valis');



        $user_count = R::count('clients', 'natid = ?', [$natid]);
        $user_info = R::findOne('clients', 'natid = ?', [$natid]);

        if ($user_count > 0) {
            if ($valis) {
                //edit
                if ($user_info->natid == $valis) {
                    echo "<span class='status-available'>رقم الخاص بك</span>";
                } else {
                    echo "<span class='status-not-available'>  رقم الهويه غير متوفر , جرب مره اخرى</span>";
                }
            } else {
                echo "<span class='status-not-available'>  رقم الهويه غير متوفر , جرب مره اخرى</span>";
            }
        } else {
            echo "<span class='status-available'> رقم الهويه متوفر</span>";
        }
    }

    public function uploadproit() {

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

        //upload the files
        $name = uploadnew('file', False, 0, 0, 'gallery', FALSE);

        $gallery = R::dispense('gallery');
        $gallery->cat_id = filter_input(INPUT_POST, 'cat_id');

        $gallery->name = $name;
        $gallery->name_from_user = $_FILES["file"]["name"];
        $id = R::store($gallery);

        echo $id;
    }

    public function delprod() {
        //check login
        autho::checkautho($this->hosturl);
        //get id for item
        $itemid = filter_input(INPUT_POST, 'itemid');
        //load all item here
        $gallery = R::load('gallery', $itemid);

        if (file_exists('../../upload/gallery/' . $gallery->image)) {
            //echo $imageNewName;
            chmod('../../upload/gallery/' . $gallery->name, 0777);
            unlink('../../upload/gallery/' . $gallery->name);
        }
        try {
            R::trash($gallery);
        } catch (Exception $e) {

        }
    }

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

}
