Welcome to mirror list, hosted at ThFree Co, Russian Federation.

API.php « Dashboard « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dea5f45526e9203b743a9291a6b4d848328b02ab (plain)
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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link     http://piwik.org
 * @license  http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * @category Piwik_Plugins
 * @package  Piwik_Dashboard
 */
use Piwik\Core\Piwik;


/**
 * This API is the <a href='http://piwik.org/docs/analytics-api/reference/' target='_blank'>Dashboard API</a>: it gives information about dashboards.
 *
 * @package Piwik_API
 */
class Piwik_Dashboard_API
{
    /**
     * @var Piwik_Dashboard_API
     */
    static private $instance = null;

    private $dashboard = null;

    public function __construct()
    {
        $this->dashboard = new Piwik_Dashboard();
    }

    /**
     * @return Piwik_Dashboard_API
     */
    static public function getInstance()
    {
        if (null == self::$instance) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * 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.
     *
     * @return array[]
     */
    public function getDashboards()
    {
        $dashboards = $this->getUserDashboards();

        if (empty($dashboards)) {
            $dashboards = array($this->getDefaultDashboard());
        }

        return $dashboards;
    }

    /**
     * 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);

        $widgets = $this->getExistingWidgetsWithinDashboard($defaultDashboard);

        return $this->buildDashboard($defaultDashboard, $widgets);
    }

    /**
     * Get all dashboards which a user has created.
     *
     * @return array[]
     */
    private function getUserDashboards()
    {
        $userLogin      = Piwik::getCurrentUserLogin();
        $userDashboards = $this->dashboard->getAllDashboards($userLogin);

        $dashboards = array();

        foreach ($userDashboards as $userDashboard) {

            if ($this->hasDashboardColumns($userDashboard)) {
                $widgets      = $this->getExistingWidgetsWithinDashboard($userDashboard);
                $dashboards[] = $this->buildDashboard($userDashboard, $widgets);
            }

        }

        return $dashboards;
    }

    private function getExistingWidgetsWithinDashboard($dashboard)
    {
        $columns = $this->getColumnsFromDashboard($dashboard);

        $widgets = array();
        $columns = array_filter($columns);

        foreach ($columns as $column) {
            foreach ($column as $widget) {

                if ($this->widgetIsNotHidden($widget) && $this->widgetExists($widget)) {
                    $module    = $widget->parameters->module;
                    $action    = $widget->parameters->action;

                    $widgets[] = array('module' => $module, 'action' => $action);
                }
            }
        }

        return $widgets;
    }

    private function getColumnsFromDashboard($dashboard)
    {
        if (is_array($dashboard['layout'])) {

            return $dashboard['layout'];
        }

        return $dashboard['layout']->columns;
    }

    private function hasDashboardColumns($dashboard)
    {
        if (is_array($dashboard['layout'])) {

            return !empty($dashboard['layout']);
        }

        return !empty($dashboard['layout']->columns);
    }

    private function buildDashboard($dashboard, $widgets)
    {
        return array('name' => $dashboard['name'], 'widgets' => $widgets);
    }

    private function widgetExists($widget)
    {
        if (empty($widget->parameters)) {
            return false;
        }

        $module = $widget->parameters->module;
        $action = $widget->parameters->action;

        return Piwik_IsWidgetDefined($module, $action);
    }

    private function widgetIsNotHidden($widget)
    {
        return empty($widget->isHidden);
    }
}