<?php

class Messages 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['messages'] = $this->loadModel("UserMessageModel")->retrieveAllData("WHERE `deleted` =? ", array(1));

        $dataView['title'] = "Show All Messages";
        // global Template Config
        global $TEMPLATE_BACK_END;
        // set content
        $TEMPLATE_BACK_END['files']['content'] = BACK_END_CONTROL_PATH . "messages_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['message'] = $this->loadModel("UserMessageModel")->retrieveAllData("WHERE `id` =? AND `deleted` =?", array($id, 1));
        if (!is_array($dataView['message'])) {
            $this->redirect("error", "index");
            exit();
        }
        $dataView['message'] = array_shift($dataView['message']);
        // get all interests
        $dataView['title'] = "Show User Message";
        // global Template Config
        global $TEMPLATE_BACK_END;
        // set content
        $TEMPLATE_BACK_END['files']['content'] = BACK_END_CONTROL_PATH . "message_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("UserMessageModel", $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("UserMessageModel", array("deleted" => 2))->updateData("id", $id)) {
                    echo $this->loadAjax()->DataToJson(array("status" => "success"));
                    exit;
                } else {
                    echo $this->loadAjax()->DataToJson(array("status" => "faild"));
                    exit;
                }

            }

        }

    }


}


?>
