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

reportingpage.controller.js « reporting-page « angularjs « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fac13374164ce12c95847a7feceddafadbd79df (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
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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
(function () {
    angular.module('piwikApp').controller('ReportingPageController', ReportingPageController);

    ReportingPageController.$inject = ['$scope', 'piwik', '$rootScope', '$location', 'reportingPageModel', 'reportingPagesModel', 'notifications', 'piwikUrl', 'piwikPeriods', 'piwikApi'];

    function ReportingPageController($scope, piwik, $rootScope, $location, pageModel, pagesModel, notifications, piwikUrl, $piwikPeriods, piwikApi) {
        pageModel.resetPage();
        $scope.pageModel = pageModel;

        var currentCategory = null;
        var currentSubcategory = null;
        var currentPeriod = null;
        var currentDate = null;
        var currentSegment = null;

        var currentCompareDates = null;
        var currentComparePeriods = null;
        var currentCompareSegments = null;

        var hasRawData = false;
        var hasNoVisits = false;
        var dateLastChecked = null;

        var UI = require('piwik/UI');
        var notification = new UI.Notification();

        function renderInitialPage()
        {
            var $search = $location.search();
            currentPeriod = piwikUrl.getSearchParam('period');
            currentDate = piwikUrl.getSearchParam('date');
            currentSegment = $search.segment;
            currentCompareSegments = piwikUrl.getSearchParam('compareSegments');
            currentCompareDates = piwikUrl.getSearchParam('compareDates');
            currentComparePeriods = piwikUrl.getSearchParam('comparePeriods');
            $scope.renderPage($search.category, $search.subcategory);
        }

        function showOnlyRawDataNotification() {
            var attributes = {};
            attributes.id = 'onlyRawData';
            attributes.animate = false;
            attributes.context = 'info';
            var url = broadcast.buildReportingUrl('category=General_Visitors&subcategory=Live_VisitorLog')
            var message = _pk_translate('CoreHome_PeriodHasOnlyRawData', ['<a href="' + url + '">', '</a>']);
            notification.show(message, attributes);
        }

        function hideOnlyRawDataNoticifation() {
            notification.remove('onlyRawData');
        }

        function showOnlyRawDataMessageIfRequired() {
            if (hasRawData && hasNoVisits) {
                showOnlyRawDataNotification();
            }

            var $search = $location.search();

            if ($search.segment) {
                hideOnlyRawDataNoticifation();
                return;
            }

            var subcategoryExceptions = [
                'Live_VisitorLog',
                'General_RealTime',
                'UserCountryMap_RealTimeMap',
                'MediaAnalytics_TypeAudienceLog',
                'MediaAnalytics_TypeRealTime',
                'FormAnalytics_TypeRealTime',
                'Goals_AddNewGoal',
            ];

            var categoryExceptions = [
                'HeatmapSessionRecording_Heatmaps',
                'HeatmapSessionRecording_SessionRecordings',
                'Marketplace_Marketplace',
            ];

            if (subcategoryExceptions.indexOf($search.subcategory) !== -1 || categoryExceptions.indexOf($search.category) !== -1 || $search.subcategory.toLowerCase().indexOf('manage') !== -1) {
                hideOnlyRawDataNoticifation();
                return;
            }

            var minuteInMilliseconds = 60000;
            if (dateLastChecked && (new Date().getTime() - dateLastChecked) < minuteInMilliseconds) {
                return;
            }

            piwikApi.fetch({ method: 'VisitsSummary.getVisits' }).then(function (json) {
                dateLastChecked = new Date().getTime();

                if (json.value > 0) {
                    hasNoVisits = false;
                    hideOnlyRawDataNoticifation();
                    return;
                }

                hasNoVisits = true;

                if (hasRawData) {
                    showOnlyRawDataNotification();
                    return;
                }

                piwikApi.fetch({ method: 'Live.getLastVisitsDetails', filter_limit: 1, doNotFetchActions: 1 }).then(function (json)  {
                    if (json.length == 0) {
                        hasRawData = false;
                        hideOnlyRawDataNoticifation();
                        return;
                    }

                    hasRawData = true;
                    showOnlyRawDataNotification();
                });
            });
        }

        $scope.renderPage = function (category, subcategory) {
            if (!category || !subcategory) {
                pageModel.resetPage();
                $scope.loading = false;
                return;
            }

            try {
                $piwikPeriods.parse(currentPeriod, currentDate);
            } catch (e) {
                var attributes = {};
                attributes.id = 'invalidDate';
                attributes.animate = false;
                attributes.context = 'error';
                notification.show(_pk_translate('CoreHome_DateInvalid'), attributes);

                pageModel.resetPage();
                $scope.loading = false;
                return;
            }

            notification.remove('invalidDate');

            $rootScope.$emit('piwikPageChange', {});

            currentCategory = category;
            currentSubcategory = subcategory;

            notifications.clearTransientNotifications();

            if ($piwikPeriods.parse(currentPeriod, currentDate).containsToday()) {
                showOnlyRawDataMessageIfRequired();
            }

            if (category === 'Dashboard_Dashboard' && $.isNumeric(subcategory) && $('[piwik-dashboard]').length) {
                // hack to make loading of dashboards faster since all the information is already there in the
                // piwik-dashboard widget, we can let the piwik-dashboard widget render the page. We need to find
                // a proper solution for this. A workaround for now could be an event or something to let other
                // components render a specific page.
                $scope.loading = true;
                var element = $('[piwik-dashboard]');
                var scope = angular.element(element).scope();
                scope.fetchDashboard(parseInt(subcategory, 10)).then(function () {
                    $scope.loading = false;
                }, function () {
                    $scope.loading = false;
                });
                return;
            }

            pageModel.fetchPage(category, subcategory).then(function () {
                if (!pageModel.page) {
                    var page = pagesModel.findPageInCategory(category);
                    if (page && page.subcategory) {
                        var $search = $location.search();
                        $search.subcategory = page.subcategory.id;
                        $location.search($search);
                        return;
                    }
                }

                $scope.hasNoPage = !pageModel.page;
                $scope.loading = false;
            });
        };

        $scope.loading = true; // we only set loading on initial load

        renderInitialPage();

        $rootScope.$on('$locationChangeSuccess', function () {
            var $search = $location.search();

            // should be handled by $route
            var category = $search.category;
            var subcategory = $search.subcategory;
            var period = piwikUrl.getSearchParam('period');
            var date = piwikUrl.getSearchParam('date');
            var segment = $search.segment;

            // $location does not handle array parameters properly
            var compareSegments = piwikUrl.getSearchParam('compareSegments');
            var compareDates = piwikUrl.getSearchParam('compareDates');
            var comparePeriods = piwikUrl.getSearchParam('comparePeriods');

            if (category === currentCategory
                && subcategory === currentSubcategory
                && period === currentPeriod
                && date === currentDate
                && segment === currentSegment
                && JSON.stringify(compareDates) === JSON.stringify(currentCompareDates)
                && JSON.stringify(comparePeriods) === JSON.stringify(currentComparePeriods)
                && JSON.stringify(compareSegments) === JSON.stringify(currentCompareSegments)
            ) {
                // this page is already loaded
                return;
            }

            if (date !== currentDate || period !== currentPeriod) {
                hideOnlyRawDataNoticifation();
                dateLastChecked = null;
                hasRawData = false;
                hasNoVisits = false;
            }

            currentPeriod = period;
            currentDate = date;
            currentSegment = segment;
            currentCompareDates = compareDates;
            currentComparePeriods = comparePeriods;
            currentCompareSegments = compareSegments;

            $scope.renderPage(category, subcategory);
        });

        $rootScope.$on('loadPage', function (event, category, subcategory) {
            $scope.renderPage(category, subcategory);
        });
    }
})();