Passed
Push — master ( 222752...4454ce )
by Marcel
02:38
created

DbController::deleteData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Data Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2019 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\IDbConnection;
16
use OCP\IL10N;
17
use OCP\ILogger;
18
use OCP\IRequest;
19
20
class DbController extends Controller
21
{
22
23
    private $userId;
24
    private $l10n;
25
    private $db;
26
    private $logger;
27
28
    public function __construct(
29
        $appName,
30
        IRequest $request,
31
        $userId,
32
        IL10N $l10n,
33
        IDbConnection $db,
34
        ILogger $logger
35
    )
36
    {
37
        parent::__construct($appName, $request);
38
        $this->userId = $userId;
39
        $this->l10n = $l10n;
40
        $this->db = $db;
41
        $this->logger = $logger;
42
    }
43
44
45
    /**
46
     * truncates fiels do DB-field size
47
     *
48
     * @param $string
49
     * @param $length
50
     * @param $dots
51
     * @return string
52
     */
53
    private function truncate($string, $length, $dots = "...")
54
    {
55
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
56
    }
57
58
    /**
59
     * Get file id for single track
60
     * @param int $dataset
61
     * @param  $objectDrilldown
62
     * @param  $dateDrilldown
63
     * @return array
64
     */
65
    public function getData(int $dataset, $objectDrilldown = null, $dateDrilldown = null)
66
    {
67
        $SQL = 'SELECT';
68
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
69
        if ($dateDrilldown === 'true') $SQL .= ' `dimension2`,';
70
        $SQL .= ' SUM(`dimension3`) AS `dimension3`';
71
        $SQL .= ' FROM `*PREFIX*analytics_facts`
72
                WHERE `dataset` = ?
73
                GROUP BY `dataset`';
74
        if ($objectDrilldown === 'true') $SQL .= ', `dimension1`';
75
        if ($dateDrilldown === 'true') $SQL .= ', `dimension2`';
76
        $SQL .= ' ORDER BY';
77
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
78
        $SQL .= ' `dimension2` ASC';
79
80
        //$this->logger->error($SQL);
81
82
        $stmt = $this->db->prepare($SQL);
83
        $stmt->execute(array($dataset));
84
        return $stmt->fetchAll();
85
    }
86
87
    /**
88
     * delete data
89
     */
90
    public function deleteData(int $datasetId, $dimension1, $dimension2)
91
    {
92
        $SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
93
        $stmt = $this->db->prepare($SQL);
94
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
95
        return true;
96
    }
97
98
    /**
99
     * delete all data of a dataset
100
     */
101
    public function deleteDataByDataset(int $datasetId)
102
    {
103
        $SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ?';
104
        $stmt = $this->db->prepare($SQL);
105
        $stmt->execute(array($this->userId, $datasetId));
106
        return true;
107
    }
108
109
    /**
110
     * create data
111
     * @param int $datasetId
112
     * @param $dimension1
113
     * @param $dimension2
114
     * @param $dimension3
115
     * @return string
116
     */
117
    public function createData(int $datasetId, $dimension1, $dimension2, $dimension3)
118
    {
119
        $SQL = 'SELECT `id` FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
120
        $stmt = $this->db->prepare($SQL);
121
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
122
        $row = $stmt->fetch();
123
        if ($row) {
124
            $SQL = 'UPDATE `*PREFIX*analytics_facts` SET `dimension3` = ? WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
125
            $stmt = $this->db->prepare($SQL);
126
            $stmt->execute(array($dimension3, $this->userId, $datasetId, $dimension1, $dimension2));
127
            //$stmt->fetch();
128
            return 'update';
129
        } else {
130
            $SQL = 'INSERT INTO `*PREFIX*analytics_facts` (`user_id`,`dataset`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?)';
131
            $stmt = $this->db->prepare($SQL);
132
            $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2, $dimension3));
133
            return 'insert';
134
        }
135
    }
136
137
    /**
138
     * create dataset
139
     */
140
    public function createDataset()
141
    {
142
        $SQL = 'INSERT INTO `*PREFIX*analytics_dataset` (`user_id`,`name`,`type`,`parent`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?,?,?)';
143
        //$this->logger->error($SQL);
144
145
        $stmt = $this->db->prepare($SQL);
146
        $stmt->execute(array($this->userId, $this->l10n->t('New'), 2, 0, $this->l10n->t('Object'), $this->l10n->t('Date'), $this->l10n->t('Value')));
147
        $insertid = $this->db->lastInsertId('*PREFIX*analytics_dataset');
148
        return $insertid;
149
    }
150
151
    /**
152
     * update dataset
153
     * @param $id
154
     * @param $name
155
     * @param $subheader
156
     * @param $parent
157
     * @param $type
158
     * @param $link
159
     * @param $visualization
160
     * @param $chart
161
     * @param $dimension1
162
     * @param $dimension2
163
     * @param $dimension3
164
     * @return bool
165
     */
166
    public function updateDataset($id, $name, $subheader, $parent, $type, $link, $visualization, $chart, $dimension1, $dimension2, $dimension3)
167
    {
168
        $SQL = 'UPDATE `*PREFIX*analytics_dataset` SET `name`= ?, `subheader`= ?, `type`= ?, `link`= ?, `visualization`= ?, `chart`= ?, `parent`= ?, `dimension1` = ?, `dimension2` = ?, `dimension3` = ? WHERE `user_id` = ? AND `id` = ?';
169
        $stmt = $this->db->prepare($SQL);
170
        $name = $this->truncate($name, 64);
171
        $stmt->execute(array($name, $subheader, $type, $link, $visualization, $chart, $parent, $dimension1, $dimension2, $dimension3, $this->userId, $id));
172
        return true;
173
    }
174
175
    /**
176
     * delete dataset
177
     * @param $id
178
     * @return
179
     */
180
    public function deleteDataset($id)
181
    {
182
        $SQL = 'DELETE FROM `*PREFIX*analytics_dataset` WHERE `user_id` = ? AND `id` = ?';
183
        $stmt = $this->db->prepare($SQL);
184
        $stmt->execute(array($this->userId, $id));
185
        return true;
186
    }
187
188
    /**
189
     * get datasets
190
     */
191
    public function getDatasets()
192
    {
193
        $SQL = 'SELECT id, name, type, parent FROM `*PREFIX*analytics_dataset` WHERE  `user_id` = ? ORDER BY `parent` ASC, `name` ASC';
194
        //$this->logger->error($SQL);
195
196
        $stmt = $this->db->prepare($SQL);
197
        $stmt->execute(array($this->userId));
198
        return $stmt->fetchAll();
199
    }
200
201
    /**
202
     * get datasets
203
     * @param $id
204
     * @return
205
     */
206
    public function getOwnDataset($id)
207
    {
208
        $SQL = 'SELECT * FROM `*PREFIX*analytics_dataset` WHERE `id` = ? AND `user_id` = ?';
209
        //$this->logger->error($SQL);
210
        $stmt = $this->db->prepare($SQL);
211
        $stmt->execute(array($id, $this->userId));
212
        return $stmt->fetch();
213
    }
214
215
    /**
216
     * get datasets
217
     * @param $id
218
     * @return
219
     */
220
    public function getDatasetOptions($id)
221
    {
222
        $SQL = 'SELECT `name`, `visualization`, `chart` FROM `*PREFIX*analytics_dataset` WHERE `id` = ?';
223
        //$this->logger->error($SQL);
224
225
        $stmt = $this->db->prepare($SQL);
226
        $stmt->execute(array($id));
227
        return $stmt->fetch();
228
    }
229
230
    // ********** SHARE ************
231
    // ********** SHARE ************
232
    // ********** SHARE ************
233
234
    /**
235
     * get datasets
236
     * @param $token
237
     * @return
238
     */
239
    public function getDatasetByToken($token)
240
    {
241
        $SQL = 'SELECT DS.*, SH.password AS password FROM `*PREFIX*analytics_dataset` AS DS JOIN `*PREFIX*analytics_share` AS SH ON DS.id = SH.dataset WHERE SH.token = ?';
242
        $stmt = $this->db->prepare($SQL);
243
        $stmt->execute([$token]);
244
        return $stmt->fetch();
245
        //$this->logger->error($results['password']);
246
    }
247
248
    /**
249
     * get shared datasets
250
     * @return
251
     */
252
    public function getSharedDatasets()
253
    {
254
        $SQL = 'SELECT DS.id, DS.name, \'99\' as type, 0 as parent FROM `*PREFIX*analytics_dataset` AS DS JOIN `*PREFIX*analytics_share` AS SH ON DS.id = SH.dataset WHERE SH.uid_owner = ? ORDER BY DS.name ASC';
255
        //$this->logger->error($this->userId);
256
        $stmt = $this->db->prepare($SQL);
257
        $stmt->execute([$this->userId]);
258
        return $stmt->fetchAll();
259
    }
260
261
    /**
262
     * get shared datasets
263
     * @return
264
     */
265
    public function getSharedDataset($id)
266
    {
267
        $SQL = 'SELECT DS.* FROM `*PREFIX*analytics_dataset` AS DS JOIN `*PREFIX*analytics_share` AS SH ON DS.id = SH.dataset WHERE SH.uid_owner = ? AND DS.id = ?';
268
        //$this->logger->error($id);
269
        $stmt = $this->db->prepare($SQL);
270
        $stmt->execute([$this->userId, $id]);
271
        return $stmt->fetch();
272
    }
273
274
    public function createShare($datasetId, $type, $uid_owner, $token)
275
    {
276
        $SQL = 'INSERT INTO `*PREFIX*analytics_share` (`dataset`,`type`,`uid_owner`,`uid_initiator`,`token`) VALUES(?,?,?,?,?)';
277
        //$this->logger->error($datasetId, $type, $uid_owner, $token);
278
        $stmt = $this->db->prepare($SQL);
279
        $stmt->execute(array($datasetId, $type, $uid_owner, $this->userId, $token));
280
        return true;
281
    }
282
283
    public function getShares($datasetId)
284
    {
285
        $SQL = 'SELECT id, type, uid_owner, token, (CASE  WHEN password IS NOT NULL THEN true ELSE false END) AS pass FROM `*PREFIX*analytics_share` WHERE uid_initiator = ? AND dataset = ?';
286
        $stmt = $this->db->prepare($SQL);
287
        $stmt->execute([$this->userId, $datasetId]);
288
        return $stmt->fetchAll();
289
    }
290
291
    public function getSharedReceiver($datasetId)
292
    {
293
        $SQL = 'SELECT uid_owner FROM `*PREFIX*analytics_share` WHERE uid_initiator = ? AND dataset = ? AND type = 0';
294
        $stmt = $this->db->prepare($SQL);
295
        $stmt->execute([$this->userId, $datasetId]);
296
        return $stmt->fetchAll();
297
    }
298
299
    public function updateShare($shareId, $password)
300
    {
301
        $SQL = 'UPDATE `*PREFIX*analytics_share` SET `password`= ? WHERE `uid_initiator` = ? AND `id` = ?';
302
        //$this->logger->error($shareId. $password);
303
        $stmt = $this->db->prepare($SQL);
304
        $stmt->execute([$password, $this->userId, $shareId]);
305
        return true;
306
    }
307
308
    public function deleteShare($shareId)
309
    {
310
        $SQL = 'DELETE FROM `*PREFIX*analytics_share` WHERE `uid_initiator` = ? AND `id` = ?';
311
        $stmt = $this->db->prepare($SQL);
312
        $stmt->execute([$this->userId, $shareId]);
313
        return true;
314
    }
315
316
    public function deleteShareByDataset($datasetId)
317
    {
318
        $SQL = 'DELETE FROM `*PREFIX*analytics_share` WHERE `uid_initiator` = ? AND `dataset` = ?';
319
        $stmt = $this->db->prepare($SQL);
320
        $stmt->execute([$this->userId, $datasetId]);
321
        return true;
322
    }
323
324
}
325