function dashboard_controller()
{
    require "Models/dashboard_model.php";
    global $path, $session, $action, $subaction, $format;
    $output['content'] = "";
    $output['message'] = "";
    //----------------------------------------------------------------------------------------------------------------------
    // New dashboard
    //----------------------------------------------------------------------------------------------------------------------
    if ($action == 'new' && $session['write']) {
        $dashid = new_dashboard($session['userid']);
        $output['message'] = _("dashboards new");
        if ($format == 'html') {
            header("Location: ../dashboard/edit?id=" . $dashid);
        }
    } elseif ($action == 'delete' && $session['write']) {
        $output['message'] = delete_dashboard($session['userid'], intval($_POST["id"]));
    } elseif ($action == 'clone' && $session['write']) {
        $output['message'] = clone_dashboard($session['userid'], intval($_POST["id"]));
    } elseif ($action == 'list' && $session['read']) {
        $_SESSION['editmode'] = TRUE;
        if ($session['read']) {
            $apikey = get_apikey_read($session['userid']);
        }
        $dashboards = get_dashboard_list($session['userid'], 0, 0);
        $menu = build_dashboard_menu($session['userid'], "edit");
        $user = get_user($session['userid']);
        if ($format == 'html') {
            $output['content'] = view("dashboard/dashboard_list_view.php", array('apikey' => $apikey, 'dashboards' => $dashboards, 'menu' => $menu, 'user' => $user));
        }
    } elseif ($action == 'public') {
        $userlist = get_user_list();
        $dashboard_list = array();
        foreach ($userlist as $user) {
            $user_dash_list = get_dashboard_list($user['userid'], 1, 1);
            foreach ($user_dash_list as $user_dash) {
                $user_dash['username'] = $user['name'];
                $dashboard_list[] = $user_dash;
            }
        }
        if ($format == 'html') {
            $output['content'] = view("dashboard/dashboard_publiclist_view.php", array('dashboards' => $dashboard_list));
        }
    } elseif ($action == 'thumb' && $session['read']) {
        $_SESSION['editmode'] = TRUE;
        if ($session['read']) {
            $apikey = get_apikey_read($session['userid']);
        }
        $dashboards = get_dashboard_list($session['userid'], 0, 0);
        $menu = build_dashboard_menu($session['userid'], "edit");
        if ($format == 'html') {
            $output['content'] = view("dashboard/dashboard_thumb_view.php", array('apikey' => $apikey, 'dashboards' => $dashboards, 'menu' => $menu));
        }
    } elseif (($action == 'run' || $action == 'view') && $session['read']) {
        $id = intval($_GET['id']);
        $alias = preg_replace('/[^a-z]/', '', $subaction);
        if ($action == "run") {
            $public = !$session['write'];
            $published = 1;
        } else {
            $public = 0;
            $published = 0;
        }
        if ($id) {
            // If a dashboard id is given we get the coresponding dashboard
            $dashboard = get_dashboard_id($session['userid'], $id, $public, $published);
        } elseif ($alias) {
            $dashboard = get_dashboard_alias($session['userid'], $alias, $public, $published);
        } else {
            // Otherwise we get the main dashboard
            $dashboard = get_main_dashboard($session['userid']);
        }
        // URL ENCODE...
        if ($format == 'json') {
            $output['content'] = urlencode($dashboard['content']);
            return $output;
        }
        $menu = build_dashboard_menu($session['userid'], $action);
        if ($action == "run") {
            // In run mode dashboard menu becomes the main menu
            $_SESSION['editmode'] = FALSE;
            $output['runmenu'] = '<div class="nav-collapse collapse">';
            $output['runmenu'] .= '<ul class="nav">' . $menu . '</ul>';
            if ($session['write']) {
                $output['runmenu'] .= "<ul class='nav pull-right'><li><a href='" . $GLOBALS['path'] . "user/logout'>" . _("Logout") . "</a></li></ul>";
            }
            $output['runmenu'] .= "</div>";
        } else {
            // Otherwise in view mode the dashboard menu is an additional grey menu
            $_SESSION['editmode'] = TRUE;
            $output['submenu'] = view("dashboard/dashboard_menu.php", array('id' => $dashboard['id'], 'menu' => $menu, 'type' => "view"));
        }
        //if ($dashboard_arr)
        //{
        $apikey = get_apikey_read($session['userid']);
        $output['content'] = view("dashboard/dashboard_view.php", array('dashboard' => $dashboard, "apikey_read" => $apikey));
        // If run mode avoid include dashboard configuration (this makes dashboard page lighter)
        if ($action != "run") {
            $output['content'] .= view("dashboard/dashboard_config.php", array('dashboard' => $dashboard));
        }
        //}
        //else
        //{
        //  $output['content'] = view("dashboard_run_errornomain.php",array());
        //}
    } elseif ($action == 'edit' && $session['write']) {
        $id = intval($_GET['id']);
        $alias = preg_replace('/[^a-z]/', '', $subaction);
        if ($id) {
            // If a dashboard id is given we get the coresponding dashboard
            $dashboard = get_dashboard_id($session['userid'], $id, 0, 0);
        } elseif ($alias) {
            $dashboard = get_dashboard_alias($session['userid'], $alias, 0, 0);
        } else {
            // Otherwise we get the main dashboard
            $dashboard = get_main_dashboard($session['userid']);
        }
        $apikey = get_apikey_read($session['userid']);
        $menu = build_dashboard_menu($session['userid'], "edit");
        $output['content'] = view("dashboard/dashboard_edit_view.php", array('dashboard' => $dashboard, "apikey_read" => $apikey));
        $output['content'] .= view("dashboard/dashboard_config.php", array('dashboard' => $dashboard));
        $output['submenu'] = view("dashboard/dashboard_menu.php", array('id' => $dashboard['id'], 'menu' => $menu, 'type' => "edit"));
    } elseif ($action == 'ckeditor' && $session['write']) {
        $id = intval($_GET['id']);
        $alias = preg_replace('/[^a-z]/', '', $subaction);
        if ($id) {
            // If a dashboard id is given we get the coresponding dashboard
            $dashboard = get_dashboard_id($session['userid'], $id, 0, 0);
        } elseif ($alias) {
            $dashboard = get_dashboard_alias($session['userid'], $alias, 0, 0);
        } else {
            // Otherwise we get the main dashboard
            $dashboard = get_main_dashboard($session['userid']);
        }
        $menu = build_dashboard_menu($session['userid'], "ckeditor");
        $output['content'] = view("dashboard/dashboard_ckeditor_view.php", array('dashboard' => $dashboard, 'menu' => $menu));
        $output['submenu'] = view("dashboard/dashboard_menu.php", array('id' => $dashboard['id'], 'menu' => $menu, 'type' => "ckeditor"));
    }
    //----------------------------------------------------------------------------------------------------------------------
    // SET dashboard
    // dashboard/set?content=<h2>HelloWorld</h2>
    //----------------------------------------------------------------------------------------------------------------------
    if ($action == 'set' && $session['write']) {
        $content = $_POST['content'];
        if (!$content) {
            $content = $_GET['content'];
        }
        $id = intval($_POST['id']);
        if (!$id) {
            $id = intval($_GET['id']);
        }
        // IMPORTANT: if you get problems with characters being removed check this line:
        $content = preg_replace('/[^\\w\\s-.#<>?",;:=&\\/%]/', '', $content);
        // filter out all except characters usually used
        $content = db_real_escape_string($content);
        set_dashboard_content($session['userid'], $content, $id);
        if ($format == 'html') {
            $output['message'] = _("dashboard set");
        } else {
            $output['message'] = "ok";
        }
    } elseif ($action == 'setconf' && $session['write']) {
        $id = intval($_POST['id']);
        $name = preg_replace('/[^\\w\\s-]/', '', $_POST['name']);
        $alias = preg_replace('/[^a-z]/', '', $_POST['alias']);
        $description = preg_replace('/[^\\w\\s-]/', '', $_POST['description']);
        // Separated functions to allow set values in independent way
        if (isset($_POST['main'])) {
            set_dashboard_main($session['userid'], $id, intval($_POST['main']));
        }
        if (isset($_POST['published'])) {
            set_dashboard_publish($session['userid'], $id, intval($_POST['published']));
        }
        if (isset($_POST['public'])) {
            set_dashboard_public($session['userid'], $id, intval($_POST['public']));
        }
        if (isset($_POST['name'])) {
            set_dashboard_name($session['userid'], $id, $name);
        }
        if (isset($_POST['alias'])) {
            set_dashboard_alias($session['userid'], $id, $alias);
        }
        if (isset($_POST['description'])) {
            set_dashboard_description($session['userid'], $id, $description);
        }
        if (isset($_POST['showdescription'])) {
            set_dashboard_showdescription($session['userid'], $id, intval($_POST['showdescription']));
        }
        //set_dashboard_conf($session['userid'],$id,$name,$alias,$description,$main,$public,$published);
        $output['message'] = _("dashboard set configuration");
    }
    return $output;
}
function set_dashboard_main($userid, $id, $main)
{
    // set user main dashboard
    if ($main == '1') {
        // set main to false all other user dashboards
        db_query("UPDATE dashboard SET main = FALSE WHERE userid='{$userid}' AND id<>'{$id}'");
        // set main to the main dashboard
        db_query("UPDATE dashboard SET main = TRUE WHERE userid='{$userid}' AND id='{$id}'");
        // main dashboard must be published
        set_dashboard_publish($userid, $id, '1');
    } else {
        db_query("UPDATE dashboard SET main = FALSE WHERE userid='{$userid}' AND id='{$id}'");
    }
}