From 19ae7db77a9b0b8c2bf6097375c4b722cda4a6b4 Mon Sep 17 00:00:00 2001 From: jerome Date: Sat, 26 Apr 2014 20:53:35 +0200 Subject: [PATCH] Saving status layout mode --- .../Controller/StatusController.class.php | 23 ++++++- src/psm/Service/User.class.php | 63 ++++++++++++++++++- src/psm/Util/Install/Installer.class.php | 15 +++++ src/templates/server/status.tpl.html | 8 +-- static/js/scripts.js | 8 +++ 5 files changed, 111 insertions(+), 6 deletions(-) diff --git a/src/psm/Module/Server/Controller/StatusController.class.php b/src/psm/Module/Server/Controller/StatusController.class.php index c09e890d..fe42a311 100644 --- a/src/psm/Module/Server/Controller/StatusController.class.php +++ b/src/psm/Module/Server/Controller/StatusController.class.php @@ -38,7 +38,7 @@ class StatusController extends AbstractServerController { function __construct(Database $db, Template $tpl) { parent::__construct($db, $tpl); - $this->setActions(array('index'), 'index'); + $this->setActions(array('index', 'saveLayout'), 'index'); } /** @@ -50,13 +50,21 @@ class StatusController extends AbstractServerController { $this->black_background = true; // add header accessories + $layout = $this->user->getUserPref('status_layout', 0); + $layout_data = array( + 'block_layout_active' => ($layout == 0) ? 'active' : '', + 'list_layout_active' => ($layout != 0) ? 'active' : '', + ); $this->tpl->newTemplate('status_layout_selector', 'server/status.tpl.html'); + $this->tpl->addTemplateData('status_layout_selector', $layout_data); $html_accessories = $this->tpl->getTemplate('status_layout_selector'); $this->setHeaderAccessories($html_accessories); $this->setTemplateId('server_status', 'server/status.tpl.html'); $this->addFooter(false); + $this->tpl->addTemplateData($this->getTemplateId(), $layout_data); + // get the active servers from database $servers = $this->getServers(); @@ -97,6 +105,19 @@ class StatusController extends AbstractServerController { } } + protected function executeSaveLayout() { + if($this->isXHR()) { + $layout = psm_POST('layout', 0); + $this->user->setUserPref('status_layout', $layout); + + $response = new \Symfony\Component\HttpFoundation\JsonResponse(); + $response->setData(array( + 'layout' => $layout, + )); + return $response; + } + } + protected function createHTMLLabels() { $this->tpl->addTemplateData( $this->getTemplateId(), diff --git a/src/psm/Service/User.class.php b/src/psm/Service/User.class.php index 81ec48f4..7996d8aa 100644 --- a/src/psm/Service/User.class.php +++ b/src/psm/Service/User.class.php @@ -68,7 +68,13 @@ class User { */ protected $user_id; - /** + /** + *Current user preferences + * @var array $user_preferences + */ + protected $user_preferences; + + /** * The user's login status * @var boolean $user_is_logged_in */ @@ -435,6 +441,61 @@ class User { } } + /** + * read current user preferences from the database + * @return boolean return false is user not connected + */ + private function getPreferences() { + if($this->user_preferences === null) { + if(!$this->getUser()) { + return false; + } + + $this->user_preferences = array(); + foreach($this->db_connection->query('SELECT * FROM ' . PSM_DB_PREFIX . 'users_preferences WHERE user_id = ' . $this->user_id) as $row) { + $this->user_preferences[$row['key']] = $row['value']; + } + } + return true; + } + + /** + * Get a user preference value + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getUserPref($key, $default = '') { + if(!$this->getPreferences() || !isset($this->user_preferences[$key])) { + return $default; + } + + $value = $this->user_preferences[$key]; + settype($value, gettype($default)); + return $value; + } + + /** + * Set a user preference value + * @param string $key + * @param mixed $value + */ + public function setUserPref($key, $value) { + if($this->getPreferences()) { + if(isset($this->user_preferences[$key])) { + if($this->user_preferences[$key] == $value) { + return; // no change + } + $sql = 'UPDATE ' . PSM_DB_PREFIX . 'users_preferences SET `key` = ?, `value` = ? WHERE `user_id` = ?'; + } else{ + $sql = 'INSERT INTO ' . PSM_DB_PREFIX . 'users_preferences SET `key` = ?, `value` = ?, `user_id` = ?'; + } + $sth = $this->db_connection->prepare($sql); + $sth->execute(array($key, $value, $this->user_id)); + $this->user_preferences[$key] = $value; + } + } + /** * Get session object * @return \Symfony\Component\HttpFoundation\Session\SessionInterface diff --git a/src/psm/Util/Install/Installer.class.php b/src/psm/Util/Install/Installer.class.php index 2198d3bd..681b2562 100644 --- a/src/psm/Util/Install/Installer.class.php +++ b/src/psm/Util/Install/Installer.class.php @@ -264,6 +264,10 @@ class Installer { // upgrade to 3.0.0 $this->upgrade300(); } + if(version_compare($version_from, '3.1.0', '<')) { + // upgrade to 3.1.0 + $this->upgrade310(); + } psm_update_conf('version', $version_to); } @@ -378,4 +382,15 @@ class Installer { } $this->execSQL("ALTER TABLE `".PSM_DB_PREFIX."users` DROP `server_id`;"); } + + protected function upgrade310() { + $queries = array(); + $queries[] = "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "users_preferences` ( + `user_id` int(11) unsigned NOT NULL, + `key` varchar(255) NOT NULL, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`user_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; + $this->execSQL($queries); + } } diff --git a/src/templates/server/status.tpl.html b/src/templates/server/status.tpl.html index f26a7c4a..11d732f4 100755 --- a/src/templates/server/status.tpl.html +++ b/src/templates/server/status.tpl.html @@ -1,6 +1,6 @@
-
+
@@ -24,7 +24,7 @@ {servers_online}
-
+
@@ -67,7 +67,7 @@
- - + +
diff --git a/static/js/scripts.js b/static/js/scripts.js index 77a8b6f9..a8a2c355 100755 --- a/static/js/scripts.js +++ b/static/js/scripts.js @@ -63,6 +63,14 @@ function psm_xhr(mod, params, method, on_complete, options) { return result; } +function psm_saveLayout(layout) { + var params = { + action: 'saveLayout', + layout: layout + }; + psm_xhr('server_status', params, 'POST'); +} + function psm_tooltips() { $('input[data-toggle="tooltip"]').tooltip({ 'trigger':'hover',