|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author René Gieling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Polls\Service; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
28
|
|
|
use OCA\Polls\Exceptions\NotAuthorizedException; |
|
29
|
|
|
|
|
30
|
|
|
use OCA\Polls\Db\Option; |
|
31
|
|
|
use OCA\Polls\Db\OptionMapper; |
|
32
|
|
|
use OCA\Polls\Service\LogService; |
|
33
|
|
|
use OCA\Polls\Model\Acl; |
|
34
|
|
|
|
|
35
|
|
|
class OptionService { |
|
36
|
|
|
|
|
37
|
|
|
private $optionMapper; |
|
38
|
|
|
private $option; |
|
39
|
|
|
private $logService; |
|
40
|
|
|
private $acl; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* OptionController constructor. |
|
44
|
|
|
* @param OptionMapper $optionMapper |
|
45
|
|
|
* @param Option $option |
|
46
|
|
|
* @param LogService $logService |
|
47
|
|
|
* @param Acl $acl |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
OptionMapper $optionMapper, |
|
52
|
|
|
Option $option, |
|
53
|
|
|
LogService $logService, |
|
54
|
|
|
Acl $acl |
|
55
|
|
|
) { |
|
56
|
|
|
$this->optionMapper = $optionMapper; |
|
57
|
|
|
$this->option = $option; |
|
58
|
|
|
$this->logService = $logService; |
|
59
|
|
|
$this->acl = $acl; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set properties from option array |
|
64
|
|
|
* @NoAdminRequired |
|
65
|
|
|
* @param Array $option |
|
66
|
|
|
*/ |
|
67
|
|
|
private function set($option) { |
|
68
|
|
|
|
|
69
|
|
|
$this->option->setPollId($option['pollId']); |
|
70
|
|
|
$this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); |
|
71
|
|
|
$this->option->setTimestamp($option['timestamp']); |
|
72
|
|
|
|
|
73
|
|
|
if ($option['timestamp']) { |
|
74
|
|
|
$this->option->setOrder($option['timestamp']); |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->option->setOrder($option['order']); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($option['confirmed']) { |
|
80
|
|
|
// do not update confirmation date, if option is already confirmed |
|
81
|
|
|
if (!$this->option->getConfirmed()) { |
|
82
|
|
|
$this->option->setConfirmed(time()); |
|
83
|
|
|
} |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->option->setConfirmed(0); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get all options of given poll |
|
91
|
|
|
* @NoAdminRequired |
|
92
|
|
|
* @param integer $pollId |
|
93
|
|
|
* @param string $token |
|
94
|
|
|
* @return array Array of Option objects |
|
95
|
|
|
*/ |
|
96
|
|
|
public function list($pollId = 0, $token = '') { |
|
97
|
|
|
|
|
98
|
|
|
if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { |
|
99
|
|
|
throw new NotAuthorizedException; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->optionMapper->findByPoll($pollId); |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add a new Option to poll |
|
109
|
|
|
* @NoAdminRequired |
|
110
|
|
|
* @param Array $option |
|
111
|
|
|
* @return Option |
|
112
|
|
|
*/ |
|
113
|
|
|
public function add($option) { |
|
114
|
|
|
|
|
115
|
|
|
if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { |
|
116
|
|
|
throw new NotAuthorizedException; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->option = new Option(); |
|
120
|
|
|
$this->set($option); |
|
121
|
|
|
$this->optionMapper->insert($this->option); |
|
122
|
|
|
$this->logService->setLog($option['pollId'], 'addOption'); |
|
123
|
|
|
|
|
124
|
|
|
return $this->option; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Remove a single option |
|
129
|
|
|
* @NoAdminRequired |
|
130
|
|
|
* @param Option $option |
|
131
|
|
|
* @return array Array of Option objects |
|
132
|
|
|
*/ |
|
133
|
|
|
public function delete($optionId) { |
|
134
|
|
|
$this->option = $this->optionMapper->find($optionId); |
|
135
|
|
|
|
|
136
|
|
|
if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) { |
|
137
|
|
|
throw new NotAuthorizedException; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->optionMapper->delete($this->option); |
|
141
|
|
|
|
|
142
|
|
|
return $this->option; |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Update poll option |
|
148
|
|
|
* @NoAdminRequired |
|
149
|
|
|
* @param array $option |
|
150
|
|
|
* @return Option |
|
151
|
|
|
*/ |
|
152
|
|
|
public function update($option) { |
|
153
|
|
|
if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { |
|
154
|
|
|
throw new NotAuthorizedException; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
try { |
|
158
|
|
|
$this->option = $this->optionMapper->find($option['id']); |
|
159
|
|
|
$this->set($option); |
|
160
|
|
|
$this->optionMapper->update($this->option); |
|
161
|
|
|
$this->logService->setLog($option['pollId'], 'updateOption'); |
|
162
|
|
|
|
|
163
|
|
|
return $this->option; |
|
164
|
|
|
} catch (Exception $e) { |
|
165
|
|
|
return new DoesNotExistException($e); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Set order by order of the given array |
|
172
|
|
|
* @NoAdminRequired |
|
173
|
|
|
* @param array $options |
|
174
|
|
|
* @return array Array of Option objects |
|
175
|
|
|
*/ |
|
176
|
|
|
public function reorder($pollId, $options) { |
|
177
|
|
|
|
|
178
|
|
|
if (!$this->acl->setPollId($pollId)->getAllowEdit()) { |
|
179
|
|
|
throw new NotAuthorizedException; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$i = 0; |
|
183
|
|
|
foreach ($options as $option) { |
|
184
|
|
|
$this->option = $this->optionMapper->find($option['id']); |
|
185
|
|
|
if ($pollId === intval($this->option->getPollId())) { |
|
186
|
|
|
$this->option->setOrder(++$i); |
|
187
|
|
|
$this->optionMapper->update($this->option); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->optionMapper->findByPoll($pollId); |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set order by order of the given array |
|
197
|
|
|
* @NoAdminRequired |
|
198
|
|
|
* @param integer $fromPollId |
|
199
|
|
|
* @param integer $toPollId |
|
200
|
|
|
* @return array Array of Option objects |
|
201
|
|
|
*/ |
|
202
|
|
|
public function clone($fromPollId, $toPollId) { |
|
203
|
|
|
|
|
204
|
|
|
if (!$this->acl->setPollId($fromPollId)->getAllowView()) { |
|
205
|
|
|
throw new NotAuthorizedException; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
foreach ($this->optionMapper->findByPoll($fromPollId) as $option) { |
|
209
|
|
|
$option->setPollId($toPollId); |
|
210
|
|
|
$this->optionMapper->insert($option); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $this->optionMapper->findByPoll($toPollId); |
|
214
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|