Passed
Push — master ( 2ab278...f93ee0 )
by Marcel
03:22
created

DatasetController::setFavorite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * 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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Db\DataloadMapper;
16
use OCA\Analytics\Db\DatasetMapper;
17
use OCA\Analytics\Db\StorageMapper;
18
use OCA\Analytics\Db\ThresholdMapper;
19
use OCP\AppFramework\Controller;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\ILogger;
22
use OCP\IRequest;
23
use OCP\ITagManager;
24
25
class DatasetController extends Controller
26
{
27
    private $logger;
28
    private $tagManager;
29
    private $ShareController;
30
    private $StorageMapper;
31
    private $DatasetMapper;
32
    private $ThresholdMapper;
33
    private $DataloadMapper;
34
    private $ActivityManager;
35
36
    public function __construct(
37
        $appName,
38
        IRequest $request,
39
        ILogger $logger,
40
        ITagManager $tagManager,
41
        ShareController $ShareController,
42
        StorageMapper $StorageMapper,
43
        DatasetMapper $DatasetMapper,
44
        ThresholdMapper $ThresholdMapper,
45
        DataloadMapper $DataloadMapper,
46
        ActivityManager $ActivityManager
47
    )
48
    {
49
        parent::__construct($appName, $request);
50
        $this->logger = $logger;
51
        $this->tagManager = $tagManager;
52
        $this->ShareController = $ShareController;
53
        $this->StorageMapper = $StorageMapper;
54
        $this->DatasetMapper = $DatasetMapper;
55
        $this->ThresholdMapper = $ThresholdMapper;
56
        $this->DataloadMapper = $DataloadMapper;
57
        $this->ActivityManager = $ActivityManager;
58
    }
59
60
    /**
61
     * get all datasets
62
     *
63
     * @NoAdminRequired
64
     */
65
    public function index()
66
    {
67
        $ownDatasets = $this->DatasetMapper->getDatasets();
68
        $sharedDatasets = $this->ShareController->getSharedDatasets();
69
        $ownDatasets = array_merge($ownDatasets, $sharedDatasets);
70
        $favorites = $this->tagManager->load('analytics')->getFavorites();
71
72
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
73
        foreach ($dataloads as $dataload) {
74
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
75
            //$this->logger->debug($key);
76
            if ($key !== '') {
77
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
78
                    $dataload['schedules'] = 1;
79
                } else {
80
                    $dataload['schedules'] = 0;
81
                }
82
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
83
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
84
            }
85
        }
86
87
        foreach ($ownDatasets as &$ownDataset) {
88
            $hasTag = 0;
89
            if (is_array($favorites) and in_array($ownDataset['id'], $favorites)) {
90
                $hasTag = 1;
91
            }
92
            $ownDataset['favorite'] = $hasTag;
93
        }
94
95
        return new DataResponse($ownDatasets);
96
    }
97
98
    /**
99
     * get own dataset details
100
     *
101
     * @NoAdminRequired
102
     * @param int $datasetId
103
     * @return array
104
     */
105
    public function read(int $datasetId)
106
    {
107
        return $this->getOwnDataset($datasetId);
108
    }
109
110
    /**
111
     * search for datasets
112
     *
113
     * @NoAdminRequired
114
     * @param string $searchString
115
     * @return array
116
     */
117
    public function search(string $searchString)
118
    {
119
        return $this->DatasetMapper->search($searchString);
120
    }
121
122
    /**
123
     * get own dataset details
124
     *
125
     * @NoAdminRequired
126
     * @param int $datasetId
127
     * @param string|null $user_id
128
     * @return array
129
     */
130
    public function getOwnDataset(int $datasetId, string $user_id = null)
131
    {
132
        return $this->DatasetMapper->getOwnDataset($datasetId, $user_id);
133
    }
134
135
    /**
136
     * get own datasets which are marked as favorites
137
     *
138
     * @NoAdminRequired
139
     * @return array|bool
140
     */
141
    public function getOwnFavoriteDatasets()
142
    {
143
        return $this->tagManager->load('analytics')->getFavorites();
144
    }
145
146
    /**
147
     * set/remove the favorite flag for a report
148
     *
149
     * @NoAdminRequired
150
     * @param int $datasetId
151
     * @param string $favorite
152
     * @return bool
153
     */
154
    public function setFavorite(int $datasetId, string $favorite)
155
    {
156
        if ($favorite === 'true') {
157
            $return = $this->tagManager->load('analytics')->addToFavorites($datasetId);
158
        } else {
159
            $return = $this->tagManager->load('analytics')->removeFromFavorites($datasetId);
160
        }
161
        return $return;
162
    }
163
164
    /**
165
     * create new dataset
166
     *
167
     * @NoAdminRequired
168
     * @param string $file
169
     * @param string $link
170
     * @return int
171
     */
172
    public function create($file = '', $link = '')
0 ignored issues
show
Unused Code introduced by
The parameter $link is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

172
    public function create($file = '', /** @scrutinizer ignore-unused */ $link = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
        //$this->logger->error('datasetcontroller 82: '.$file);
175
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
176
        $datasetId = $this->DatasetMapper->createDataset();
177
178
        if ($file === 'DEMO') {
179
            $name = 'Demo Report';
180
            $subheader = 'CSV Demo Data from GitHub';
181
            $parent = 0;
182
            $type = DataSourceController::DATASET_TYPE_EXTERNAL_FILE;
183
            $link = 'https://raw.githubusercontent.com/Rello/analytics/master/sample_data/sample1.csv';
184
            $visualization = 'ct';
185
            $chart = 'line';
186
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
187
        } elseif ($file !== '') {
188
            $name = explode('.', end(explode('/', $file)))[0];
189
            $subheader = $file;
190
            $parent = 0;
191
            $type = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
192
            $link = $file;
193
            $visualization = 'table';
194
            $chart = 'line';
195
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '');
196
        }
197
        return $datasetId;
198
    }
199
200
    /**
201
     * Delete Dataset and all depending objects
202
     *
203
     * @NoAdminRequired
204
     * @param int $datasetId
205
     * @return bool
206
     */
207
    public function delete(int $datasetId)
208
    {
209
        $this->ShareController->deleteShareByDataset($datasetId);
210
        $this->StorageMapper->deleteDataByDataset($datasetId);
211
        $this->DatasetMapper->deleteDataset($datasetId);
212
        $this->ThresholdMapper->deleteThresholdByDataset($datasetId);
213
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
214
        $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
215
        return true;
216
    }
217
218
    /**
219
     * get dataset details
220
     *
221
     * @NoAdminRequired
222
     * @param int $datasetId
223
     * @param $name
224
     * @param $subheader
225
     * @param int $parent
226
     * @param int $type
227
     * @param $link
228
     * @param $visualization
229
     * @param $chart
230
     * @param $chartoptions
231
     * @param $dataoptions
232
     * @param $dimension1
233
     * @param $dimension2
234
     * @param $value
235
     * @return bool
236
     */
237
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1 = null, $dimension2 = null, $value = null)
238
    {
239
        if ($type === DataSourceController::DATASET_TYPE_GROUP) {
240
            $parent = 0;
241
        }
242
        return $this->DatasetMapper->updateDataset($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value);
243
    }
244
245
    /**
246
     * get dataset details
247
     *
248
     * @NoAdminRequired
249
     * @param int $datasetId
250
     * @param $chartoptions
251
     * @param $dataoptions
252
     * @param $filteroptions
253
     * @return bool
254
     */
255
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions)
256
    {
257
        return $this->DatasetMapper->updateDatasetOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);
258
    }
259
}