Completed
Pull Request — master (#948)
by René
06:07
created

OptionController::get()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 12
rs 10
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\Controller;
25
26
use Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IRequest;
30
use OCP\ILogger;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCP\IGroupManager;
36
use OCP\Security\ISecureRandom;
37
38
use OCA\Polls\Db\Poll;
39
use OCA\Polls\Db\PollMapper;
40
use OCA\Polls\Db\Option;
41
use OCA\Polls\Db\OptionMapper;
42
use OCA\Polls\Service\LogService;
43
use OCA\Polls\Model\Acl;
44
45
class OptionController extends Controller {
46
47
	private $userId;
48
	private $optionMapper;
49
	private $options;
50
	private $option;
51
	private $groupManager;
52
	private $pollMapper;
53
	private $logger;
54
	private $logService;
55
	private $acl;
56
57
	/**
58
	 * OptionController constructor.
59
	 * @param string $appName
60
	 * @param $UserId
61
	 * @param IRequest $request
62
	 * @param ILogger $logger
63
	 * @param OptionMapper $optionMapper
64
	 * @param IGroupManager $groupManager
65
	 * @param PollMapper $pollMapper
66
	 * @param LogService $logService
67
	 * @param Acl $acl
68
	 */
69
70
	public function __construct(
71
		string $appName,
72
		$UserId,
73
		IRequest $request,
74
		OptionMapper $optionMapper,
75
		Option $option,
76
		IGroupManager $groupManager,
77
		PollMapper $pollMapper,
78
		ILogger $logger,
79
		LogService $logService,
80
		Acl $acl
81
	) {
82
		parent::__construct($appName, $request);
83
		$this->userId = $UserId;
84
		$this->optionMapper = $optionMapper;
85
		$this->option = $option;
86
		$this->groupManager = $groupManager;
87
		$this->pollMapper = $pollMapper;
88
		$this->logger = $logger;
89
		$this->logService = $logService;
90
		$this->acl = $acl;
91
	}
92
93
	/**
94
	 * Set properties from option array
95
	 * @NoAdminRequired
96
	 * @param integer $pollId
97
	 * @return array Array of Option objects
98
	 */
99
	private function set($option) {
100
101
		$this->option->setPollId($option['pollId']);
102
		$this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
103
		$this->option->setTimestamp($option['timestamp']);
104
105
		if ($option['timestamp']) {
106
			$this->option->setOrder($option['timestamp']);
107
		} else {
108
			$this->option->setOrder($option['order']);
109
		}
110
111
		if ($option['confirmed']) {
112
			// do not update confirmation date, if option is already confirmed
113
			if (!$this->option->getConfirmed()) {
114
				$this->option->setConfirmed(time());
115
			}
116
		} else {
117
			$this->option->setConfirmed(0);
118
		}
119
	}
120
121
	/**
122
	 * Get all options of given poll
123
	 * @NoAdminRequired
124
	 * @NoCSRFRequired
125
	 * @param integer $pollId
126
	 * @return array Array of Option objects
127
	 */
128
	public function get($pollId) {
129
130
		try {
131
132
			if (!$this->acl->getFoundByToken()) {
133
				$this->acl->setPollId($pollId);
134
			}
135
136
			$this->options = $this->optionMapper->findByPoll($pollId);
137
138
			return new DataResponse($this->options, Http::STATUS_OK);
139
		} catch (DoesNotExistException $e) {
140
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
141
		}
142
	}
143
144
145
	/**
146
	 * getByToken
147
	 * Read all options of a poll based on a share token and return list as array
148
	 * @NoAdminRequired
149
	 * @PublicPage
150
	 * @NoCSRFRequired
151
	 * @param string $token
152
	 * @return DataResponse
153
	 */
154
	public function getByToken($token) {
155
156
		try {
157
			$this->acl->setToken($token);
158
			// return $this->get($this->acl->getPollId());
159
			$this->options = $this->optionMapper->findByPoll($this->acl->getPollId());
160
			return new DataResponse($this->options, Http::STATUS_OK);
161
162
		} catch (DoesNotExistException $e) {
163
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
164
		}
165
	}
166
167
	/**
168
	 * Add a new Option to poll
169
	 * @NoAdminRequired
170
	 * @NoCSRFRequired
171
	 * @param Option $option
172
	 * @return DataResponse
173
	 */
174
	public function add($option) {
175
176
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
177
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
178
		}
179
180
		try {
181
			$this->option = new Option();
182
			$this->set($option);
183
			$this->optionMapper->insert($this->option);
184
			$this->logService->setLog($option['pollId'], 'addOption');
185
			return new DataResponse($this->option, Http::STATUS_OK);
186
		} catch (Exception $e) {
187
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
188
		}
189
	}
190
191
	/**
192
	 * Update poll option
193
	 * @NoAdminRequired
194
	 * @NoCSRFRequired
195
	 * @param Option $option
196
	 * @return DataResponse
197
	 */
198
	public function update($option) {
199
200
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
201
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
202
		}
203
204
		try {
205
			$this->option = $this->optionMapper->find($option['id']);
206
			$this->set($option);
207
			$this->optionMapper->update($this->option);
208
			$this->logService->setLog($option['pollId'], 'updateOption');
209
			return new DataResponse($this->option, Http::STATUS_OK);
210
		} catch (Exception $e) {
211
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
212
		}
213
	}
214
215
	/**
216
	 * Remove a single option
217
	 * @NoAdminRequired
218
	 * @NoCSRFRequired
219
	 * @param Option $option
220
	 * @return DataResponse
221
	 */
222
	public function remove($option) {
223
		try {
224
225
			if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
226
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
227
			}
228
229
			$this->optionMapper->remove($option['id']);
230
			$this->logService->setLog($option['pollId'], 'deleteOption');
231
232
			return new DataResponse(array(
233
				'action' => 'deleted',
234
				'optionId' => $option['id']
235
			), Http::STATUS_OK);
236
237
		} catch (Exception $e) {
238
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
239
		}
240
241
	}
242
243
	/**
244
	 * Set order by order of the given array
245
	 * @NoAdminRequired
246
	 * @NoCSRFRequired
247
	 * @param Array $options
248
	 * @return DataResponse
249
	 */
250
	public function reorder($pollId, $options) {
251
		$i = 0;
252
253
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
254
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
255
		}
256
257
		foreach ($options as $option) {
258
			$this->option = $this->optionMapper->find($option['id']);
259
			if ($pollId === intval($this->option->getPollId())) {
260
				$this->option->setOrder(++$i);
261
				$this->optionMapper->update($this->option);
262
			}
263
		}
264
265
		return $this->get($pollId);
266
267
	}
268
}
269