PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/Dashboard/API.php

https://github.com/CodeYellowBV/piwik
PHP | 147 lines | 88 code | 32 blank | 27 comment | 7 complexity | 6cb2d2c107c933eec3573ecf407651bd 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\Piwik;
  10. use Piwik\WidgetsList;
  11. /**
  12. * This API is the <a href='http://piwik.org/docs/analytics-api/reference/' target='_blank'>Dashboard API</a>: it gives information about dashboards.
  13. *
  14. * @method static \Piwik\Plugins\Dashboard\API getInstance()
  15. */
  16. class API extends \Piwik\Plugin\API
  17. {
  18. private $dashboard = null;
  19. protected function __construct()
  20. {
  21. $this->dashboard = new Dashboard();
  22. }
  23. /**
  24. * Get each dashboard that belongs to a user including the containing widgets that are placed within each dashboard.
  25. * If the user has not created any dashboard yet, the default dashboard will be returned.
  26. *
  27. * @return array[]
  28. */
  29. public function getDashboards()
  30. {
  31. $dashboards = $this->getUserDashboards();
  32. if (empty($dashboards)) {
  33. $dashboards = array($this->getDefaultDashboard());
  34. }
  35. return $dashboards;
  36. }
  37. /**
  38. * Get the default dashboard.
  39. *
  40. * @return array[]
  41. */
  42. private function getDefaultDashboard()
  43. {
  44. $defaultLayout = $this->dashboard->getDefaultLayout();
  45. $defaultLayout = $this->dashboard->decodeLayout($defaultLayout);
  46. $defaultDashboard = array('name' => Piwik::translate('Dashboard_Dashboard'), 'layout' => $defaultLayout);
  47. $widgets = $this->getExistingWidgetsWithinDashboard($defaultDashboard);
  48. return $this->buildDashboard($defaultDashboard, $widgets);
  49. }
  50. /**
  51. * Get all dashboards which a user has created.
  52. *
  53. * @return array[]
  54. */
  55. private function getUserDashboards()
  56. {
  57. $userLogin = Piwik::getCurrentUserLogin();
  58. $userDashboards = $this->dashboard->getAllDashboards($userLogin);
  59. $dashboards = array();
  60. foreach ($userDashboards as $userDashboard) {
  61. if ($this->hasDashboardColumns($userDashboard)) {
  62. $widgets = $this->getExistingWidgetsWithinDashboard($userDashboard);
  63. $dashboards[] = $this->buildDashboard($userDashboard, $widgets);
  64. }
  65. }
  66. return $dashboards;
  67. }
  68. private function getExistingWidgetsWithinDashboard($dashboard)
  69. {
  70. $columns = $this->getColumnsFromDashboard($dashboard);
  71. $widgets = array();
  72. $columns = array_filter($columns);
  73. foreach ($columns as $column) {
  74. foreach ($column as $widget) {
  75. if ($this->widgetIsNotHidden($widget) && $this->widgetExists($widget)) {
  76. $module = $widget->parameters->module;
  77. $action = $widget->parameters->action;
  78. $widgets[] = array('module' => $module, 'action' => $action);
  79. }
  80. }
  81. }
  82. return $widgets;
  83. }
  84. private function getColumnsFromDashboard($dashboard)
  85. {
  86. if (is_array($dashboard['layout'])) {
  87. return $dashboard['layout'];
  88. }
  89. return $dashboard['layout']->columns;
  90. }
  91. private function hasDashboardColumns($dashboard)
  92. {
  93. if (is_array($dashboard['layout'])) {
  94. return !empty($dashboard['layout']);
  95. }
  96. return !empty($dashboard['layout']->columns);
  97. }
  98. private function buildDashboard($dashboard, $widgets)
  99. {
  100. return array('name' => $dashboard['name'], 'widgets' => $widgets);
  101. }
  102. private function widgetExists($widget)
  103. {
  104. if (empty($widget->parameters)) {
  105. return false;
  106. }
  107. $module = $widget->parameters->module;
  108. $action = $widget->parameters->action;
  109. return WidgetsList::isDefined($module, $action);
  110. }
  111. private function widgetIsNotHidden($widget)
  112. {
  113. return empty($widget->isHidden);
  114. }
  115. }