1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Dashboard;
use Piwik\API\Request;
use Piwik\Piwik;
/**
* This API is the <a href='https://matomo.org/docs/analytics-api/reference/' rel='noreferrer' target='_blank'>Dashboard API</a>: it gives information about dashboards.
*
* @method static \Piwik\Plugins\Dashboard\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
private $dashboard = null;
private $model = null;
public function __construct(Dashboard $dashboard, Model $model)
{
$this->dashboard = $dashboard;
$this->model = $model;
}
/**
* Get each dashboard that belongs to a user including the containing widgets that are placed within each dashboard.
* If the user has not created any dashboard yet, the default dashboard will be returned unless
* $returnDefaultIfEmpty is set to `false`
*
* @param string $login Login of the user [defaults to current user]
* @param bool $returnDefaultIfEmpty disable return of default dashboard
*
* @return array[]
*/
public function getDashboards($login = '', $returnDefaultIfEmpty = true)
{
$login = $login ? $login : Piwik::getCurrentUserLogin();
$dashboards = [];
if (!Piwik::isUserIsAnonymous()) {
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$dashboards = $this->getUserDashboards($login);
}
if (empty($dashboards) && $returnDefaultIfEmpty) {
$dashboards = array($this->getDefaultDashboard());
}
return $dashboards;
}
/**
* Creates a new dashboard for the given login
*
* Note: Only a super user is able to create dashboards for other users
*
* @param string $login login of the user that dashboard should be created for
* @param string $dashboardName name of the new dashboard
* @param bool $addDefaultWidgets whether to add the current default widget collection or not
* @return int|string
*/
public function createNewDashboardForUser($login, $dashboardName = '', $addDefaultWidgets = true)
{
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$layout = '{}';
if ($addDefaultWidgets) {
$layout = $this->dashboard->getDefaultLayout();
}
return $this->model->createNewDashboardForUser($login, $dashboardName, $layout);
}
/**
* Removes a dashboard according to given dashboard id and login
*
* Note: Only a super user is able to remove dashboards for other users
*
* Also note: It is allowed to delete the first dashboard for a user, BUT
* that will cause buggy behavior if a new dashboard is not immediately added.
* Deleting the first dashboard (with ID = 1) should only be done for automation
* purposes.
*
* @param int $idDashboard id of the dashboard to be removed
* @param string $login Login of the dashboard user [defaults to current user]
*/
public function removeDashboard($idDashboard, $login = '')
{
$login = $login ? $login : Piwik::getCurrentUserLogin();
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$this->model->deleteDashboardForUser($idDashboard, $login);
}
/**
* Copy a dashboard of current user to another user
*
* Note: current user needs super user access
*
* @param int $idDashboard Id of the dashboard that should be copied
* @param string $copyToUser User the dashboard should be copied to
* @param string $dashboardName Name of the new dashboard (defaults to 'Dashboard of {user}')
* @return int id of the new dashboard
* @throws \Exception if an error occurs, or dashboard can't be found
*/
public function copyDashboardToUser($idDashboard, $copyToUser, $dashboardName = '')
{
Piwik::checkUserHasSomeAdminAccess();
// get users only returns users of sites the current user has at least admin access to
$users = Request::processRequest('UsersManager.getUsers', ['filter_limit' => -1]);
$userFound = false;
foreach ($users as $user) {
if ($user['login'] === $copyToUser) {
$userFound = true;
break;
}
}
if (!$userFound) {
throw new \Exception(sprintf('Cannot copy dashboard to user %s, user not found.', $copyToUser));
}
$login = Piwik::getCurrentUserLogin();
$layout = $this->dashboard->getLayoutForUser($login, $idDashboard);
if ($layout !== false) {
return $this->model->createNewDashboardForUser($copyToUser, $dashboardName, $layout);
}
throw new \Exception('Dashboard not found');
}
/**
* Resets a dashboard to the default widget configuration
*
* Note: Only a super user is able to reset dashboards for other users
* @param int $idDashboard dashboard id
* @param string $login user the dashboard belongs
*
*/
public function resetDashboardLayout($idDashboard, $login = '')
{
$login = $login ?: Piwik::getCurrentUserLogin();
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$layout = $this->dashboard->getDefaultLayout();
$this->model->updateLayoutForUser($login, $idDashboard, $layout);
}
/**
* Get the default dashboard.
* @return \array[]
*/
private function getDefaultDashboard()
{
$defaultLayout = $this->dashboard->getDefaultLayout();
$defaultLayout = $this->dashboard->decodeLayout($defaultLayout);
$defaultDashboard = array('name' => Piwik::translate('Dashboard_Dashboard'), 'layout' => $defaultLayout, 'iddashboard' => 1);
$widgets = $this->getVisibleWidgetsWithinDashboard($defaultDashboard);
return $this->buildDashboard($defaultDashboard, $widgets);
}
/**
* Get all dashboards which a user has created.
*
* @param string $userLogin login of the user
* @return \array[]
*/
private function getUserDashboards($userLogin)
{
$userDashboards = $this->dashboard->getAllDashboards($userLogin);
$dashboards = array();
foreach ($userDashboards as $userDashboard) {
$widgets = $this->getVisibleWidgetsWithinDashboard($userDashboard);
$dashboards[] = $this->buildDashboard($userDashboard, $widgets);
}
return $dashboards;
}
private function getVisibleWidgetsWithinDashboard($dashboard)
{
$columns = $this->getColumnsFromDashboard($dashboard);
$widgets = array();
$columns = array_filter($columns);
foreach ($columns as $column) {
foreach ($column as $widget) {
if ($this->widgetIsNotHidden($widget) && !empty($widget->parameters->module)) {
$module = $widget->parameters->module;
$action = $widget->parameters->action;
$widgets[] = array('module' => $module, 'action' => $action);
}
}
}
return $widgets;
}
private function checkLoginIsNotAnonymous($login)
{
Piwik::checkUserIsNotAnonymous();
if ($login === 'anonymous') {
throw new \Exception('This method can\'t be performed for anonymous user');
}
}
private function getColumnsFromDashboard($dashboard)
{
if (empty($dashboard['layout'])) {
return array();
}
if (is_array($dashboard['layout'])) {
return $dashboard['layout'];
}
if (!empty($dashboard['layout']->columns)) {
return $dashboard['layout']->columns;
}
return array();
}
private function buildDashboard($dashboard, $widgets)
{
return array('name' => $dashboard['name'], 'id' => $dashboard['iddashboard'], 'widgets' => $widgets);
}
private function widgetIsNotHidden($widget)
{
return empty($widget->isHidden);
}
}
|