PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Dashboard/Controller.php

https://github.com/CodeYellowBV/piwik
PHP | 332 lines | 216 code | 55 blank | 61 comment | 22 complexity | a155b8d2d68968a76d82712772065be4 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. namespace Piwik\Plugins\Dashboard;
  9. use Piwik\Common;
  10. use Piwik\DataTable\Renderer\Json;
  11. use Piwik\Db;
  12. use Piwik\Piwik;
  13. use Piwik\Session\SessionNamespace;
  14. use Piwik\View;
  15. use Piwik\WidgetsList;
  16. /**
  17. * Dashboard Controller
  18. *
  19. */
  20. class Controller extends \Piwik\Plugin\Controller
  21. {
  22. /**
  23. * @var Dashboard
  24. */
  25. private $dashboard;
  26. protected function init()
  27. {
  28. parent::init();
  29. $this->dashboard = new Dashboard();
  30. }
  31. protected function _getDashboardView($template)
  32. {
  33. $view = new View($template);
  34. $this->setGeneralVariablesView($view);
  35. $view->availableWidgets = Common::json_encode(WidgetsList::get());
  36. $view->availableLayouts = $this->getAvailableLayouts();
  37. $view->dashboardId = Common::getRequestVar('idDashboard', 1, 'int');
  38. $view->dashboardLayout = $this->getLayout($view->dashboardId);
  39. return $view;
  40. }
  41. public function embeddedIndex()
  42. {
  43. $view = $this->_getDashboardView('@Dashboard/embeddedIndex');
  44. return $view->render();
  45. }
  46. public function index()
  47. {
  48. $view = $this->_getDashboardView('@Dashboard/index');
  49. $view->dashboardSettingsControl = new DashboardManagerControl();
  50. $view->dashboards = array();
  51. if (!Piwik::isUserIsAnonymous()) {
  52. $login = Piwik::getCurrentUserLogin();
  53. $view->dashboards = $this->dashboard->getAllDashboards($login);
  54. }
  55. return $view->render();
  56. }
  57. public function getAvailableWidgets()
  58. {
  59. $this->checkTokenInUrl();
  60. Json::sendHeaderJSON();
  61. return Common::json_encode(WidgetsList::get());
  62. }
  63. public function getDashboardLayout()
  64. {
  65. $this->checkTokenInUrl();
  66. $idDashboard = Common::getRequestVar('idDashboard', 1, 'int');
  67. $layout = $this->getLayout($idDashboard);
  68. return $layout;
  69. }
  70. /**
  71. * Resets the dashboard to the default widget configuration
  72. */
  73. public function resetLayout()
  74. {
  75. $this->checkTokenInUrl();
  76. $layout = $this->dashboard->getDefaultLayout();
  77. $idDashboard = Common::getRequestVar('idDashboard', 1, 'int');
  78. if (Piwik::isUserIsAnonymous()) {
  79. $session = new SessionNamespace("Dashboard");
  80. $session->dashboardLayout = $layout;
  81. $session->setExpirationSeconds(1800);
  82. } else {
  83. $this->saveLayoutForUser(Piwik::getCurrentUserLogin(), $idDashboard, $layout);
  84. }
  85. }
  86. /**
  87. * Records the layout in the DB for the given user.
  88. *
  89. * @param string $login
  90. * @param int $idDashboard
  91. * @param string $layout
  92. */
  93. protected function saveLayoutForUser($login, $idDashboard, $layout)
  94. {
  95. $paramsBind = array($login, $idDashboard, $layout, $layout);
  96. $query = sprintf('INSERT INTO %s (login, iddashboard, layout) VALUES (?,?,?) ON DUPLICATE KEY UPDATE layout=?',
  97. Common::prefixTable('user_dashboard'));
  98. Db::query($query, $paramsBind);
  99. }
  100. /**
  101. * Updates the name of a dashboard
  102. *
  103. * @param string $login
  104. * @param int $idDashboard
  105. * @param string $name
  106. */
  107. protected function updateDashboardName($login, $idDashboard, $name)
  108. {
  109. $paramsBind = array($name, $login, $idDashboard);
  110. $query = sprintf('UPDATE %s SET name = ? WHERE login = ? AND iddashboard = ?',
  111. Common::prefixTable('user_dashboard'));
  112. Db::query($query, $paramsBind);
  113. }
  114. /**
  115. * Removes the dashboard with the given id
  116. */
  117. public function removeDashboard()
  118. {
  119. $this->checkTokenInUrl();
  120. if (Piwik::isUserIsAnonymous()) {
  121. return;
  122. }
  123. $idDashboard = Common::getRequestVar('idDashboard', 1, 'int');
  124. // first layout can't be removed
  125. if ($idDashboard != 1) {
  126. $query = sprintf('DELETE FROM %s WHERE iddashboard = ? AND login = ?',
  127. Common::prefixTable('user_dashboard'));
  128. Db::query($query, array($idDashboard, Piwik::getCurrentUserLogin()));
  129. }
  130. }
  131. /**
  132. * Outputs all available dashboards for the current user as a JSON string
  133. */
  134. public function getAllDashboards()
  135. {
  136. $this->checkTokenInUrl();
  137. if (Piwik::isUserIsAnonymous()) {
  138. Json::sendHeaderJSON();
  139. return '[]';
  140. }
  141. $login = Piwik::getCurrentUserLogin();
  142. $dashboards = $this->dashboard->getAllDashboards($login);
  143. Json::sendHeaderJSON();
  144. return Common::json_encode($dashboards);
  145. }
  146. /**
  147. * Creates a new dashboard for the current user
  148. * User needs to be logged in
  149. */
  150. public function createNewDashboard()
  151. {
  152. $this->checkTokenInUrl();
  153. if (Piwik::isUserIsAnonymous()) {
  154. return '0';
  155. }
  156. $user = Piwik::getCurrentUserLogin();
  157. $nextId = $this->getNextIdDashboard($user);
  158. $name = urldecode(Common::getRequestVar('name', '', 'string'));
  159. $type = urldecode(Common::getRequestVar('type', 'default', 'string'));
  160. $layout = '{}';
  161. if ($type == 'default') {
  162. $layout = $this->dashboard->getDefaultLayout();
  163. }
  164. $query = sprintf('INSERT INTO %s (login, iddashboard, name, layout) VALUES (?, ?, ?, ?)',
  165. Common::prefixTable('user_dashboard'));
  166. Db::query($query, array($user, $nextId, $name, $layout));
  167. Json::sendHeaderJSON();
  168. return Common::json_encode($nextId);
  169. }
  170. private function getNextIdDashboard($login)
  171. {
  172. $nextIdQuery = sprintf('SELECT MAX(iddashboard)+1 FROM %s WHERE login = ?',
  173. Common::prefixTable('user_dashboard'));
  174. $nextId = Db::fetchOne($nextIdQuery, array($login));
  175. if (empty($nextId)) {
  176. $nextId = 1;
  177. return $nextId;
  178. }
  179. return $nextId;
  180. }
  181. public function copyDashboardToUser()
  182. {
  183. $this->checkTokenInUrl();
  184. if (!Piwik::hasUserSuperUserAccess()) {
  185. return '0';
  186. }
  187. $login = Piwik::getCurrentUserLogin();
  188. $name = urldecode(Common::getRequestVar('name', '', 'string'));
  189. $user = urldecode(Common::getRequestVar('user', '', 'string'));
  190. $idDashboard = Common::getRequestVar('dashboardId', 0, 'int');
  191. $layout = $this->dashboard->getLayoutForUser($login, $idDashboard);
  192. if ($layout !== false) {
  193. $nextId = $this->getNextIdDashboard($user);
  194. $query = sprintf('INSERT INTO %s (login, iddashboard, name, layout) VALUES (?, ?, ?, ?)',
  195. Common::prefixTable('user_dashboard'));
  196. Db::query($query, array($user, $nextId, $name, $layout));
  197. Json::sendHeaderJSON();
  198. return Common::json_encode($nextId);
  199. }
  200. }
  201. /**
  202. * Saves the layout for the current user
  203. * anonymous = in the session
  204. * authenticated user = in the DB
  205. */
  206. public function saveLayout()
  207. {
  208. $this->checkTokenInUrl();
  209. $layout = Common::unsanitizeInputValue(Common::getRequestVar('layout'));
  210. $idDashboard = Common::getRequestVar('idDashboard', 1, 'int');
  211. $name = Common::getRequestVar('name', '', 'string');
  212. if (Piwik::isUserIsAnonymous()) {
  213. $session = new SessionNamespace("Dashboard");
  214. $session->dashboardLayout = $layout;
  215. $session->setExpirationSeconds(1800);
  216. } else {
  217. $this->saveLayoutForUser(Piwik::getCurrentUserLogin(), $idDashboard, $layout);
  218. if (!empty($name)) {
  219. $this->updateDashboardName(Piwik::getCurrentUserLogin(), $idDashboard, $name);
  220. }
  221. }
  222. }
  223. /**
  224. * Saves the layout as default
  225. */
  226. public function saveLayoutAsDefault()
  227. {
  228. $this->checkTokenInUrl();
  229. if (Piwik::hasUserSuperUserAccess()) {
  230. $layout = Common::unsanitizeInputValue(Common::getRequestVar('layout'));
  231. $paramsBind = array('', '1', $layout, $layout);
  232. $query = sprintf('INSERT INTO %s (login, iddashboard, layout) VALUES (?,?,?) ON DUPLICATE KEY UPDATE layout=?',
  233. Common::prefixTable('user_dashboard'));
  234. Db::query($query, $paramsBind);
  235. }
  236. }
  237. /**
  238. * Get the dashboard layout for the current user (anonymous or logged user)
  239. *
  240. * @param int $idDashboard
  241. *
  242. * @return string $layout
  243. */
  244. protected function getLayout($idDashboard)
  245. {
  246. if (Piwik::isUserIsAnonymous()) {
  247. $session = new SessionNamespace("Dashboard");
  248. if (!isset($session->dashboardLayout)) {
  249. return $this->dashboard->getDefaultLayout();
  250. }
  251. $layout = $session->dashboardLayout;
  252. } else {
  253. $layout = $this->dashboard->getLayoutForUser(Piwik::getCurrentUserLogin(), $idDashboard);
  254. }
  255. if (!empty($layout)) {
  256. $layout = $this->dashboard->removeDisabledPluginFromLayout($layout);
  257. }
  258. if (empty($layout)) {
  259. $layout = $this->dashboard->getDefaultLayout();
  260. }
  261. return $layout;
  262. }
  263. /**
  264. * Returns all available column layouts for the dashboard
  265. *
  266. * @return array
  267. */
  268. protected function getAvailableLayouts()
  269. {
  270. return array(
  271. array(100),
  272. array(50, 50), array(67, 33), array(33, 67),
  273. array(33, 33, 33), array(40, 30, 30), array(30, 40, 30), array(30, 30, 40),
  274. array(25, 25, 25, 25)
  275. );
  276. }
  277. }