<?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
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        //here name of controll to known
        $this->controllname = '_member';
        $this->shcontrollname = 'Member Panel';
        $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, $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] = 'managment'; //controller name or tab name
        $active[1] = 'member'; //controller name
        // do name  // switch($do)
        switch ($acty) {
            case '':$active[2] = 'memberadd';
                break;
            case 'show':$active[2] = 'membershow';
                break;
            default :$active[2] = 'membershow';
                break;
        }
        $this->active = $active;


        //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
        $roles = R::findAll('awrole');
        $this->smarty->assign('roles', $roles);

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

    //check useraname
    public function checkusername() {

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

        $username = filter_input(INPUT_POST, 'username');
        $checkedit = filter_input(INPUT_POST, 'editusername');


        if (!$checkedit) {
            //new
            $checkusername = R::getRow('SELECT * FROM awusers WHERE user_name = ? LIMIT 1', [$username]);
            if (count($checkusername) > 0) {
                echo "yes";
            } else {
                echo "no";
            }
        } else {

            //edit
            $checkusername = R::getRow('SELECT * FROM awusers WHERE user_name = ? LIMIT 1', [$username]);
            if (count($checkusername) > 0) {
                if ($checkedit == $checkusername['user_name']) {
                    echo "no";
                } else {
                    echo "yes";
                }
            } else {
                echo "no";
            }
        }
    }

    //check email
    public function checkemail() {

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

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

        if (!$checkedit) {
            //new
            $checkemail = R::getRow('SELECT * FROM awusers WHERE user_email = ? LIMIT 1', [$email]);

            if (count($checkemail) > 0) {
                echo "yes";
            } else {
                echo "no";
            }
        } else {

            //edit
            $checkemail = R::getRow('SELECT * FROM awusers WHERE user_email = ? LIMIT 1', [$email]);
            if (count($checkemail) > 0) {
                if ($checkedit == $checkemail['user_email']) {
                    echo "no";
                } else {
                    echo "yes";
                }
            } else {
                echo "no";
            }
        }
    }

    public function addit() {

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

        //vars here
        $name = filter_input(INPUT_POST, 'name');
        $email = filter_input(INPUT_POST, 'email');
        $role = filter_input(INPUT_POST, 'role');
        $special = filter_input(INPUT_POST, 'special');
        $position = filter_input(INPUT_POST, 'position');
        $username = filter_input(INPUT_POST, 'username');
        $password = filter_input(INPUT_POST, 'password');


        $user_name = trim($username);
        $hash_cost_factor = (defined('HASH_COST_FACTOR') ? HASH_COST_FACTOR : null);
        $user_password_hash = password_hash($password, PASSWORD_DEFAULT, array('cost' => $hash_cost_factor));
        $user_activation_hash = sha1(uniqid(mt_rand(), true));
        $user_email = trim($email);
        $user_registration_ip = $_SERVER['REMOTE_ADDR'];


        try {
            //here the add
            $newuser = R::exec('INSERT INTO awusers (user_name, user_password_hash, user_email,user_active, user_activation_hash, user_registration_ip, user_registration_datetime,role_id,name , specialid ,positionid ) VALUES(?, ?, ?, ?,?, ?, now(),?,?,? ,?)', [$user_name, $user_password_hash, $user_email, 1, $user_activation_hash, $user_registration_ip, $role, $name,$special , $position]);
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/controllers/error.php?ac=notadd&id=' . $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
        $users = R::getAll('SELECT * FROM awusers');
        $this->smarty->assign('users', $users);
        
        $roles = R::findAll('awrole');
        $this->smarty->assign('roles', $roles);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('_member/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']);


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

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


        //load all here
        $user = R::getRow('SELECT * FROM awusers WHERE user_id = ? LIMIT 1', [$itemid]);
        $this->smarty->assign('user', $user);
        
        $specials = R::findAll('awrolespecialize', 'roleid = ?' , [$user['role_id']]);
        $positions = R::findAll('awroleposition', 'roleid = ?' , [$user['role_id']]);

        $this->smarty->assign('specials', $specials);
        $this->smarty->assign('positions', $positions);
        
        
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('_member/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
        $name = filter_input(INPUT_POST, 'name');
        $email = filter_input(INPUT_POST, 'email');
        $role = filter_input(INPUT_POST, 'role');
        $special = filter_input(INPUT_POST, 'special');
        $position = filter_input(INPUT_POST, 'position');
        $username = filter_input(INPUT_POST, 'username');
        $epassword = filter_input(INPUT_POST, 'epassword');
        $oldpassword = filter_input(INPUT_POST, 'oldpassword');
        $id = filter_input(INPUT_POST, 'id');


        //new password or old password
        if (empty($epassword)) {
            $user_password_hash = $oldpassword;
        } else {
            $hash_cost_factor = (defined('HASH_COST_FACTOR') ? HASH_COST_FACTOR : null);
            $user_password_hash = password_hash($epassword, PASSWORD_DEFAULT, array('cost' => $hash_cost_factor));
        }


        $user_name = trim($username);
        $user_activation_hash = sha1(uniqid(mt_rand(), true));
        $user_email = trim($email);
        $user_registration_ip = $_SERVER['REMOTE_ADDR'];


        try {
            //here the add
            $edituser = R::exec('UPDATE awusers SET user_name = ?,user_password_hash = ?, user_email = ? ,role_id = ?,name = ? , specialid = ? ,positionid = ? WHERE user_id = ?', [$user_name, $user_password_hash, $user_email, $role, $name,$special , $position , $id]);

            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/controllers/error.php?ac=notupdate&id=' . $this->controllname);
        }
    }

    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');


        try {
            $edituser = R::exec('Delete from awusers where user_id = ?', [$itemid]);
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/controllers/error.php?ac=notdel');
        }
    }
    
    public function getposition() {

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

        //get id for item
        $roleId = filter_input(INPUT_POST, 'roleId');
        $positions = R::findAll('awroleposition', 'roleid = ?' , [$roleId]);
        $result = '<option value=""></option>';
        foreach ($positions as $value) 
        {
            $result .='<option value = '.$value->id.' > '.$value->name.' </option>';
        }
        echo $result;
    }
    
    public function getspecial() 
    {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //get id for item
        $roleId = filter_input(INPUT_POST, 'roleId');
        $positions = R::findAll('awrolespecialize', 'roleid = ?' , [$roleId]);
        $result = '<option value=""></option>';
        foreach ($positions as $value) 
        {
            $result .='<option value = '.$value->id.' > '.$value->name.' </option>';
        }
        echo $result;
    }

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

}
