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

managegdpr.controller.js « manage-gdpr « angularjs « PrivacyManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efaf057895fbf9bbde73b5d831d4d0caadac60da (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
/*!
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
(function () {
    angular.module('piwikApp').controller('ManageGdprController', ManageGdprController);

    ManageGdprController.$inject = ["$scope", "piwikApi", "piwik", "$timeout"];

    function ManageGdprController($scope, piwikApi, piwik, $timeout) {

        var self = this;
        this.isLoading = false;
        this.isDeleting = false;
        this.site = {id: 'all', name: 'All Websites'};
        this.segment_filter = 'userId==';
        this.dataSubjects = [];
        this.toggleAll = true;
        this.hasSearched = false

        var sitesPromise = piwikApi.fetch({method: 'SitesManager.getSitesIdWithAdminAccess', filter_limit: '-1'});

        this.linkTo = function (action){
            var currentUrl = window.location.pathname + window.location.search;
            var newUrl = piwik.broadcast.updateParamValue('module=PrivacyManager', currentUrl);
            newUrl = piwik.broadcast.updateParamValue('action=' + action, newUrl);
            return newUrl;
        }

        function showSuccessNotification(message)
        {
            var UI = require('piwik/UI');
            var notification = new UI.Notification();
            notification.show(message, {context: 'success', id: 'manageGdpr'});

            $timeout(function () {
                notification.scrollToNotification();
            }, 200);
        }

        this.toggleActivateAll = function () {
            var toggleAll = this.toggleAll;
            angular.forEach(this.dataSubjects, function (dataSubject) {
                dataSubject.dataSubjectActive = toggleAll;
            });
        };

        this.hasActiveDataSubjects = function()
        {
            return !!this.getActivatedDataSubjects().length;
        };

        this.getActivatedDataSubjects = function () {
            var visitsToDelete = [];

            angular.forEach(this.dataSubjects, function (visit) {
                if (visit.dataSubjectActive) {
                    visitsToDelete.push({idsite: visit.idSite, idvisit: visit.idVisit});
                }
            });
            return visitsToDelete;
        }

        this.showProfile = function (visitorId, idSite) {
            require('piwik/UI').VisitorProfileControl.showPopover(visitorId, idSite);
        };

        this.exportDataSubject = function () {
            function sendContentAsDownload(filename, content) {
                var mimeType = 'text/plain';
                function downloadFile(content)
                {
                    var node = document.createElement('a');
                    node.style.display = 'none';
                    if ('string' === typeof content) {
                        node.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(content));
                    } else {
                        node.href = window.URL.createObjectURL(blob);
                    }
                    node.setAttribute('download', filename);
                    document.body.appendChild(node);
                    node.click();
                    document.body.removeChild(node);
                }

                var node;
                if ('function' === typeof Blob) {
                    // browser supports blob
                    try {
                        var blob = new Blob([content], {type: mimeType});
                        if (window.navigator.msSaveOrOpenBlob) {
                            window.navigator.msSaveBlob(blob, filename);
                            return;
                        } else {
                            downloadFile(blob);
                            return;
                        }
                    } catch (e) {
                        downloadFile(content);
                    }
                }
                downloadFile(content);
            }

            var visitsToDelete = this.getActivatedDataSubjects();
            piwikApi.post({
                module: 'API',
                method: 'PrivacyManager.exportDataSubjects',
                format: 'json',
                filter_limit: -1,
            }, {visits: visitsToDelete}).then(function (visits) {
                showSuccessNotification('Visits were successfully exported');
                sendContentAsDownload('exported_data_subjects.json', JSON.stringify(visits));
            });
        };

        this.deleteDataSubject = function () {
            piwik.helper.modalConfirm('#confirmDeleteDataSubject', {yes: function () {
                self.isDeleting = true;
                var visitsToDelete = self.getActivatedDataSubjects();

                piwikApi.post({
                    module: 'API',
                    method: 'PrivacyManager.deleteDataSubjects',
                    filter_limit: -1,
                }, {visits: visitsToDelete}).then(function (visits) {
                    self.dataSubjects = [];
                    self.isDeleting = false;
                    showSuccessNotification('Visits were successfully deleted');
                    self.findDataSubjects();
                }, function () {
                    self.isDeleting = false;
                });
            }});
        };

        this.addFilter = function (segment, value) {
            this.segment_filter += ',' + segment + '==' + value;
            this.findDataSubjects();
        };
        
        this.findDataSubjects = function () {
            this.dataSubjects = [];
            this.isLoading = true;
            this.toggleAll = true;

            function addDatePadding(number)
            {
                if (number < 10) {
                    return '0' + number;
                }
                return number;
            }

            var now = new Date();
            var dateString = (now.getFullYear() + 2) + '-' + addDatePadding(now.getMonth() + 1) + '-' + addDatePadding(now.getDay());
            // we are adding two years to make sure to also capture some requests in the future as we fetch data across
            // different sites and different timezone and want to avoid missing any possible requests

            sitesPromise.then(function (idsites) {

                var siteIds = self.site.id;
                if (siteIds === 'all' && !piwik.hasSuperUserAccess) {
                    // when superuser, we speed the request up a little and simply use 'all'
                    siteIds = idsites;
                    if (angular.isArray(idsites)) {
                        siteIds = idsites.join(',');
                    }
                }

                piwikApi.fetch({
                    idSite: siteIds,
                    period: 'range',
                    date: '1998-01-01,today',
                    module: 'API',
                    method: 'Live.getLastVisitsDetails',
                    segment: self.segment_filter,
                    filter_limit: 501,
                    doNotFetchActions: 1
                }).then(function (visits) {
                    self.hasSearched = true;
                    angular.forEach(visits, function (visit) {
                        visit.dataSubjectActive = true;
                    });
                    self.dataSubjects = visits;
                    self.isLoading = false;
                }, function () {
                    self.isLoading = false;
                });
            });
        };
    }
})();