<?php
class DashboardsController{
    private $model;

    function __construct(){
        $this->model = new DashboardsModel();
    }

    /**
     * get all user dashborads ( public dashboards are also taken)
     * @return json list of dashboards
     */
    public function getUserDashboards(){
        echo json_encode($this->model->getUserDashboards());
    }

    /**
     * get selected dashbord info
     * @return json info and gadgets for selected dashboards
     */
    public function getMyDashboard(){
        $idDashboard = isset($_REQUEST['myDashboard']) ? $_REQUEST['myDashboard'] : 0;
        echo json_encode($this->model->getMyDashboard($idDashboard));
    }

    /**
     * get data required for order central gadget
     * @return json orders info
     */
    public function getOrderCentralInfo(){
        $filters = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : '';
        $sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : '';
        echo json_encode($this->model->getOrderCentralInfo($filters, $sortBy));
    }

    /**
     * get data required for assigned orders
     * @return json assigned orders info
     */
    public function getAssignedOrdersInfo(){
        $filters = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : '';
        $sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : '';
        echo json_encode($this->model->getAssignedOrdersInfo($filters, $sortBy));
    }

    /**
     * get data required for the enxt action gadget
     * @return json next actions info
     */
    public function getNextActionsInfo(){
        $filters = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : '';
        $sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : '';
        echo json_encode($this->model->getNextActionsInfo($filters, $sortBy));
    }

    /**
     * get gadgets for creating new dashboards
     * @return json list of gadgets
     */
    public function getAllGadgets(){
        $idDashboard = isset($_REQUEST['idDashboard']) ? $_REQUEST['idDashboard'] : 0;
        $selectedUserType = isset($_REQUEST['selectedUserType']) ? $_REQUEST['selectedUserType'] : 0;
        echo json_encode($this->model->getAllGadgets($idDashboard, $selectedUserType));
    }

    /**
     * get information for a selected dashboard on creation
     * @return json dashborad info
     */
    public function getDashboardInfo(){
        $idDashboard = isset($_REQUEST['idDashboard']) ? $_REQUEST['idDashboard'] : '';
        echo json_encode($this->model->getDashboardInfo($idDashboard));
    }

    /**
     * create a new adshboard
     * @return json array containg the success or error messages
     */
    public function createDashboard(){
        $selectedGadgets = isset($_REQUEST['selectedGadgets']) ? $_REQUEST['selectedGadgets'] : '[]';
        $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
        $visibility = isset($_REQUEST['visibility']) ? $_REQUEST['visibility'] : '';
        $idDashboard = isset($_REQUEST['idDashboard']) ? $_REQUEST['idDashboard'] : 0;
        $selectedUserType = isset($_REQUEST['selectedUserType']) ? $_REQUEST['selectedUserType'] : 0;
        echo json_encode($this->model->createDashboard($idDashboard, $name, $visibility, $selectedGadgets, $selectedUserType));
    }

    /**
     * get user types
     * @return json user types from the system
     */
    public function getUserTypes(){
        echo json_encode($this->model->getUserTypes());
    }

    /**
     * remove a dashborad
     * @return json update message
     */
    public function removeDashboard(){
        $idDashboard = isset($_REQUEST['idDashboard']) ? $_REQUEST['idDashboard'] : 0;
        echo json_encode($this->model->removeDashboard($idDashboard));
    }

    /**
     * get template for view dashboard
     */
    public function createDashboardTemplate(){
        global $user;
        require_once('templates/CreateDashboardTemplate.php');
    }

    /**
     * get template for view dashboard
     */
    public function dashboardsViewTemplate(){
        require_once('templates/DashboardsViewTemplate.php');
    }

    /**
     * get template for orders central gadget
     */
    public function orderCentralTemplate(){
        require_once('templates/gadgets/orderCentralTemplate.php');
    }

    /**
     * get template for assigned orders gadget
     */
    public function assignedOrdersTemplate(){
        require_once('templates/gadgets/assignedOrdersTemplate.php');
    }

    /**
     * get template for orders central gadget
     */
    public function nextActionsTemplate(){
        require_once('templates/gadgets/nextActionsTemplate.php');
    }

    /**
     * get template for gadgets fiter
     */
    public function dashboardsFiltersTemplate(){
        require_once('templates/DashboardsFiltersTemplate.php');
    }

    /**
     * get template for dashboards
     */
    public function dashboardsTemplate(){
        require_once('templates/DashboardsTemplate.php');
    }

    /**
     * show page for dashboards
     */
    public function showPage(){
        require_once('DashboardsPage.php');
    }
}
?>
