diff options
author | dartcafe <github@dartcafe.de> | 2020-05-27 18:35:15 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-05-27 18:35:15 +0300 |
commit | cb2e550d48fa96be9dbee141bdd95e078509ce69 (patch) | |
tree | 2663f21fee51800e46396b4ba6c0046016faeb06 /lib | |
parent | 8fb1cd42d05741efea4bc821387653ddc3a71fab (diff) |
move reorderimg to backend
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/OptionController.php | 129 |
1 files changed, 76 insertions, 53 deletions
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 88c64202..e078fefa 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -46,7 +46,8 @@ class OptionController extends Controller { private $userId; private $optionMapper; - + private $options; + private $option; private $groupManager; private $pollMapper; private $logger; @@ -71,6 +72,7 @@ class OptionController extends Controller { $UserId, IRequest $request, OptionMapper $optionMapper, + Option $option, IGroupManager $groupManager, PollMapper $pollMapper, ILogger $logger, @@ -80,6 +82,7 @@ class OptionController extends Controller { parent::__construct($appName, $request); $this->userId = $UserId; $this->optionMapper = $optionMapper; + $this->option = $option; $this->groupManager = $groupManager; $this->pollMapper = $pollMapper; $this->logger = $logger; @@ -87,6 +90,33 @@ class OptionController extends Controller { $this->acl = $acl; } + /** + * Set properties from option array + * @NoAdminRequired + * @param integer $pollId + * @return array Array of Option objects + */ + private function set($option) { + + $this->option->setPollId($option['pollId']); + $this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); + $this->option->setTimestamp($option['timestamp']); + + if ($option['timestamp']) { + $this->option->setOrder($option['timestamp']); + } else { + $this->option->setOrder($option['order']); + } + + if ($option['confirmed']) { + // do not update confirmation date, if option is already confirmed + if (!$this->option->getConfirmed()) { + $this->option->setConfirmed(time()); + } + } else { + $this->option->setConfirmed(0); + } + } /** * Get all options of given poll @@ -102,8 +132,9 @@ class OptionController extends Controller { $this->acl->setPollId($pollId); } - return new DataResponse($this->optionMapper->findByPoll($pollId), Http::STATUS_OK); + $this->options = $this->optionMapper->findByPoll($pollId); + return new DataResponse($this->options, Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } @@ -123,7 +154,9 @@ class OptionController extends Controller { try { $this->acl->setToken($token); - return $this->get($this->acl->getPollId()); + // return $this->get($this->acl->getPollId()); + $this->options = $this->optionMapper->findByPoll($this->acl->getPollId()); + return new DataResponse($this->options, Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); @@ -138,31 +171,19 @@ class OptionController extends Controller { */ public function add($option) { - try { - - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - $NewOption = new Option(); - - $NewOption->setPollId($option['pollId']); - $NewOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); - $NewOption->setTimestamp($option['timestamp']); - if ($option['timestamp']) { - $NewOption->setOrder($option['timestamp']); - } else { - $NewOption->setOrder($option['order']); - } + if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + } - $this->optionMapper->insert($NewOption); + try { + $this->option = new Option(); + $this->set($option); + $this->optionMapper->insert($this->option); $this->logService->setLog($option['pollId'], 'addOption'); - return new DataResponse($NewOption, Http::STATUS_OK); - + return new DataResponse($this->option, Http::STATUS_OK); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } - } /** @@ -173,38 +194,16 @@ class OptionController extends Controller { */ public function update($option) { - try { - $this->logger->alert(json_encode($option)); - $updateOption = $this->optionMapper->find($option['id']); - - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - $updateOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); - $updateOption->setTimestamp($option['timestamp']); - - if ($option['timestamp']) { - $updateOption->setOrder($option['timestamp']); - } else { - $updateOption->setOrder($option['order']); - } - - if ($option['confirmed']) { - // do not update confirmation date, if option is already confirmed - if (!$updateOption->getConfirmed()) { - $updateOption->setConfirmed(time()); - } - - } else { - $updateOption->setConfirmed(0); - } + if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + } - $this->optionMapper->update($updateOption); + try { + $this->option = $this->optionMapper->find($option['id']); + $this->set($option); + $this->optionMapper->update($this->option); $this->logService->setLog($option['pollId'], 'updateOption'); - - return new DataResponse($updateOption, Http::STATUS_OK); - + return new DataResponse($this->option, Http::STATUS_OK); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } @@ -237,4 +236,28 @@ class OptionController extends Controller { } + /** + * Set order by order of the given array + * @NoAdminRequired + * @param Array $options + * @return DataResponse + */ + public function reorder($pollId, $options) { + $i=0; + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { + return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + } + + foreach ($options as $option) { + $this->option = $this->optionMapper->find($option['id']); + if ($pollId === intval($this->option->getPollId())) { + $this->option->setOrder(++$i); + $this->optionMapper->update($this->option); + } + } + + return $this->get($pollId); + + } } |