Passed
Push — master ( c3551c...649947 )
by Marcel
03:09
created

DbController::normalizeInteger()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Nextcloud Data
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\data\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
/**
21
 * Controller class for main page.
22
 */
23
class DbController extends Controller
24
{
25
26
    private $userId;
27
    private $l10n;
28
    private $db;
29
    private $logger;
30
31
    public function __construct(
32
        $appName,
33
        IRequest $request,
34
        $userId,
35
        IL10N $l10n,
36
        IDbConnection $db,
37
        ILogger $logger
38
    )
39
    {
40
        parent::__construct($appName, $request);
41
        $this->userId = $userId;
42
        $this->l10n = $l10n;
43
        $this->db = $db;
44
        $this->logger = $logger;
45
    }
46
47
48
    /**
49
     * truncates fiels do DB-field size
50
     *
51
     * @param $string
52
     * @param $length
53
     * @param $dots
54
     * @return string
55
     */
56
    private function truncate($string, $length, $dots = "...")
57
    {
58
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
59
    }
60
61
    /**
62
     * Get file id for single track
63
     * @param int $dataset
64
     * @param  $objectDrilldown
65
     * @param  $dateDrilldown
66
     * @return array
67
     */
68
    public function getData(int $dataset, $objectDrilldown = null, $dateDrilldown = null)
69
    {
70
        $SQL = 'SELECT';
71
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
72
        if ($dateDrilldown === 'true') $SQL .= ' `dimension2`,';
73
        $SQL .= ' SUM(`dimension3`) AS `dimension3`';
74
        $SQL .= ' FROM `*PREFIX*data_facts`
75
                WHERE `dataset` = ?
76
                GROUP BY `dataset`';
77
        if ($objectDrilldown === 'true') $SQL .= ', `dimension1`';
78
        if ($dateDrilldown === 'true') $SQL .= ', `dimension2`';
79
        $SQL .= ' ORDER BY';
80
        if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,';
81
        $SQL .= ' `dimension2` ASC';
82
83
        // $this->logger->error($SQL);
84
85
        $stmt = $this->db->prepare($SQL);
86
        $stmt->execute(array($dataset));
87
        return $stmt->fetchAll();
88
    }
89
90
    /**
91
     * update data
92
     */
93
    public function updateData()
94
    {
95
    }
96
97
    /**
98
     * create data
99
     */
100
    public function deleteData()
101
    {
102
    }
103
104
    /**
105
     * create data
106
     */
107
    public function createData($datasetId, $dimension1, $dimension2, $dimension3)
108
    {
109
        $SQL = 'SELECT `id` FROM `*PREFIX*data_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
110
        $stmt = $this->db->prepare($SQL);
111
        $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2));
112
        $row = $stmt->fetch();
113
        if ($row) {
114
            $SQL = 'UPDATE `*PREFIX*data_facts` SET `dimension3` = ? WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?';
115
            $stmt = $this->db->prepare($SQL);
116
            $stmt->execute(array($dimension3, $this->userId, $datasetId, $dimension1, $dimension2));
117
            $stmt->fetch();
118
            return 'update';
119
        } else {
120
            $SQL = 'INSERT INTO `*PREFIX*data_facts` (`user_id`,`dataset`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?)';
121
            $stmt = $this->db->prepare($SQL);
122
            $stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2, $dimension3));
123
            $stmt->fetch();
124
            return 'insert';
125
        }
126
    }
127
128
    /**
129
     * create dataset
130
     */
131
    public function createDataset()
132
    {
133
        $SQL = 'INSERT INTO `*PREFIX*data_dataset` (`user_id`,`name`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?)';
134
        //$this->logger->error($SQL);
135
136
        $stmt = $this->db->prepare($SQL);
137
        $stmt->execute(array($this->userId, 'New', 'object', 'date', 'value'));
138
        return $stmt->fetch();
139
    }
140
141
    /**
142
     * update dataset
143
     */
144
    public function updateDataset($id, $name, $parent, $type, $link, $visualization, $chart, $dimension1, $dimension2, $dimension3)
145
    {
146
        $SQL = 'UPDATE `*PREFIX*data_dataset` SET `name`= ?, `type`= ?, `link`= ?, `visualization`= ?, `chart`= ?, `parent`= ?, `dimension1` = ?, `dimension2` = ?, `dimension3` = ? WHERE `user_id` = ? AND `id` = ?';
147
        $stmt = $this->db->prepare($SQL);
148
        $name = $this->truncate($name, 64);
149
        $stmt->execute(array($name, $type, $link, $visualization, $chart, $parent, $dimension1, $dimension2, $dimension3, $this->userId, $id));
150
        return $stmt->fetch();
151
    }
152
153
    /**
154
     * delete dataset
155
     */
156
    public function deleteDataset($id)
157
    {
158
        $SQL = 'DELETE FROM `*PREFIX*data_dataset` WHERE `user_id` = ? AND `id` = ?';
159
        $stmt = $this->db->prepare($SQL);
160
        $stmt->execute(array($this->userId, $id));
161
        return $stmt->fetch();
162
    }
163
164
    /**
165
     * get datasets
166
     */
167
    public function getDatasets()
168
    {
169
        $SQL = 'SELECT id, name, type FROM `*PREFIX*data_dataset` WHERE  `user_id` = ? ORDER BY `name` ASC';
170
        //$this->logger->error($SQL);
171
172
        $stmt = $this->db->prepare($SQL);
173
        $stmt->execute(array($this->userId));
174
        return $stmt->fetchAll();
175
    }
176
177
    /**
178
     * get datasets
179
     * @param $id
180
     * @return
181
     */
182
    public function getOwnDataset($id)
183
    {
184
        $SQL = 'SELECT * FROM `*PREFIX*data_dataset` WHERE `id` = ? AND `user_id` = ?';
185
        //$this->logger->error($SQL);
186
        $stmt = $this->db->prepare($SQL);
187
        $stmt->execute(array($id, $this->userId));
188
        return $stmt->fetch();
189
    }
190
191
    /**
192
     * get datasets
193
     * @param $id
194
     * @return
195
     */
196
    public function getDatasetOptions($id)
197
    {
198
        $SQL = 'SELECT `name`, `visualization`, `chart` FROM `*PREFIX*data_dataset` WHERE `id` = ?';
199
        //$this->logger->error($SQL);
200
201
        $stmt = $this->db->prepare($SQL);
202
        $stmt->execute(array($id));
203
        return $stmt->fetch();
204
    }
205
206
    // ********** SHARE ************
207
    // ********** SHARE ************
208
    // ********** SHARE ************
209
210
    /**
211
     * get datasets
212
     * @param $token
213
     * @return
214
     */
215
    public function getDatasetByToken($token)
216
    {
217
        $SQL = 'SELECT DS.*, SH.password AS password FROM `*PREFIX*data_dataset` AS DS JOIN `*PREFIX*data_share` AS SH ON DS.id = SH.dataset WHERE SH.token = ?';
218
        $stmt = $this->db->prepare($SQL);
219
        $stmt->execute([$token]);
220
        return $stmt->fetch();
221
        //$this->logger->error($results['password']);
222
    }
223
224
    /**
225
     * get shared datasets
226
     * @return
227
     */
228
    public function getSharedDatasets()
229
    {
230
        $SQL = 'SELECT DS.id, DS.name, \'S\' as type FROM `*PREFIX*data_dataset` AS DS JOIN `*PREFIX*data_share` AS SH ON DS.id = SH.dataset WHERE SH.uid_owner = ? ORDER BY DS.name ASC';
231
        $this->logger->error($this->userId);
232
        $stmt = $this->db->prepare($SQL);
233
        $stmt->execute([$this->userId]);
234
        return $stmt->fetchAll();
235
    }
236
237
    /**
238
     * get shared datasets
239
     * @return
240
     */
241
    public function getSharedDataset($id)
242
    {
243
        $SQL = 'SELECT DS.* FROM `*PREFIX*data_dataset` AS DS JOIN `*PREFIX*data_share` AS SH ON DS.id = SH.dataset WHERE SH.uid_owner = ? AND DS.id = ?';
244
        //$this->logger->error($id);
245
        $stmt = $this->db->prepare($SQL);
246
        $stmt->execute([$this->userId, $id]);
247
        return $stmt->fetch();
248
    }
249
250
    public function createShare($datasetId, $share_type, $uid_owner, $token)
251
    {
252
        $SQL = 'INSERT INTO `*PREFIX*data_share` (`dataset`,`share_type`,`uid_owner`,`uid_initiator`,`token`) VALUES(?,?,?,?,?)';
253
        //$this->logger->error($datasetId, $share_type, $uid_owner, $token);
254
        $stmt = $this->db->prepare($SQL);
255
        $stmt->execute(array($datasetId, $share_type, $uid_owner, $this->userId, $token));
256
        return $stmt->fetch();
257
    }
258
259
    public function getShare($datasetId)
260
    {
261
        $SQL = 'SELECT id, share_type, uid_owner, token, (CASE  WHEN password IS NOT NULL THEN true ELSE false END) AS pass FROM `*PREFIX*data_share` WHERE uid_initiator = ? AND dataset = ?';
262
        $stmt = $this->db->prepare($SQL);
263
        $stmt->execute([$this->userId, $datasetId]);
264
        return $stmt->fetchAll();
265
    }
266
267
    public function updateShare($shareId, $password)
268
    {
269
        $SQL = 'UPDATE `*PREFIX*data_share` SET `password`= ? WHERE `uid_initiator` = ? AND `id` = ?';
270
        //$this->logger->error($shareId. $password);
271
        $stmt = $this->db->prepare($SQL);
272
        $stmt->execute([$password, $this->userId, $shareId]);
273
        return $stmt->fetch();
274
    }
275
276
    public function deleteShare($shareId)
277
    {
278
        $SQL = 'DELETE FROM `*PREFIX*data_share` WHERE `uid_initiator` = ? AND `id` = ?';
279
        $stmt = $this->db->prepare($SQL);
280
        $stmt->execute([$this->userId, $shareId]);
281
        return $stmt->fetch();
282
    }
283
284
}
285