<?php

    class Requests extends Controller{







        public function getAll()

        {

			       /******** start header code ********/

            if(FALSE !== $this->loadHelper("UserHelper")->check_user_login())

            {

                // get user id from session

                $user_id = $this->loadSession()->getSession("user_id");

                // get user data for header

                $dataView['userData'] = $this->loadModel("UserModel")->retriveDataById("id",$user_id);

                if($dataView['userData'] == NULL)

                {

                    $this->redirect("error","index");

                    exit();

                }

            }else{

                $this->redirect("admin","login");

            }



            /******* end header code *******/



            $dataView['requests'] = $this->loadModel("RequestModel")->retrieveAllData("WHERE `deleted` =? ",array(1));





            $dataView['title'] = "Show All Requests";

            // global Template Config

            global $TEMPLATE_BACK_END ;

            // set content

            $TEMPLATE_BACK_END['files']['content'] = BACK_END_CONTROL_PATH."requests_getall";

            // load template

            $this->loadTemplate($TEMPLATE_BACK_END,$dataView)->render();

        }

        public function show_selected()

        {

			       /******** start header code ********/

            if(FALSE !== $this->loadHelper("UserHelper")->check_user_login())

            {

                // get user id from session

                $user_id = $this->loadSession()->getSession("user_id");

                // get user data for header

                $dataView['userData'] = $this->loadModel("UserModel")->retriveDataById("id",$user_id);

                if($dataView['userData'] == NULL)

                {

                    $this->redirect("error","index");

                    exit();

                }

            }else{

                $this->redirect("admin","login");

            }



            /******* end header code *******/

            // get id from url

            $id = (int)$this->loadRoute()->getSegment(2);

            if($id == 0)

            {

                $this->redirect("error","index");

                exit();

            }

            // get car data

            $dataView['request'] = $this->loadModel("RequestModel")->retrieveAllData("WHERE `id` =? AND `deleted` =?",array($id,1));

            if(!is_array($dataView['request']))

            {

                $this->redirect("error","index");

                exit();

            }

            $dataView['request'] = array_shift($dataView['request']);





            // get all interests

			       $dataView['title'] = "Show User Request";

            // global Template Config

            global $TEMPLATE_BACK_END ;

            // set content

            $TEMPLATE_BACK_END['files']['content'] = BACK_END_CONTROL_PATH."request_show_selected";

            // load template

            $this->loadTemplate($TEMPLATE_BACK_END,$dataView)->render();

        }



        public function updateStatus()

        {

                if(isset($_POST['action'],$_POST['id'],$_POST['status']) && $_POST['action'] == "updateStatus")

                {

                    // get User id

                    $id = (int)$_POST['id'];

                    if($id == 0)

                    {

                        $this->redirect("error","index");

                        exit;

                    }else{

                        // get current status

                        $currentStatus = $_POST['status'];

                        // if current status = on set it off

                        if($currentStatus == "on")

                        {

                            $setStatus = 2;

                        }else{

                            $setStatus = 1;

                        }

                        $dataModel['status'] = $setStatus;



                        if(false !== $this->loadModel("RequestModel",$dataModel)->updateData("id", $id))

                        {

                         echo $this->loadAjax()->DataToJson(array("status"=>"success"));

                         exit;

                        }else{

                            echo $this->loadAjax()->DataToJson(array("status"=>"faild"));

                            exit;

                        }

                    }

                }

         }



        public function delete()

        {

                if(isset($_POST['action'],$_POST['id']) && $_POST['action'] == "deleteData")

                {

                    $id = (int)$_POST['id'];

                    if($id == 0)

                    {

                        $this->redirect("error","index");

                        exit;

                    }else{

                        if(FALSE !== $this->loadModel("RequestModel",array("deleted"=>2))->updateData("id",$id))

                        {

                            echo $this->loadAjax()->DataToJson(array("status"=>"success"));

                            exit;

                        }else{

                            echo $this->loadAjax()->DataToJson(array("status"=>"faild"));

                            exit;

                        }

                    }

                 }

         }



    }



?>
