connect = $connect; $this->pollService = $pollService; $this->logService = $logService; $this->pollRepository = RepositoryFactory::pollRepository(); $this->slotRepository = RepositoryFactory::slotRepository(); $this->voteRepository = RepositoryFactory::voteRepository(); $this->commentRepository = RepositoryFactory::commentRepository(); } public function updatePoll($poll): bool { global $config; if ($poll->end_date > $poll->creation_date) { return $this->pollRepository->update($poll); } return false; } /** * Delete a comment from a poll. * * @param string $poll_id The ID of the poll * @param $comment_id int The ID of the comment * @return mixed true is action succeeded */ public function deleteComment(string $poll_id, int $comment_id) { return $this->commentRepository->deleteById($poll_id, $comment_id); } /** * Remove all comments of a poll. * * @param string $poll_id The ID a the poll * @return bool|null true is action succeeded */ public function cleanComments(string $poll_id): ?bool { $this->logService->log("CLEAN_COMMENTS", "id:$poll_id"); return $this->commentRepository->deleteByPollId($poll_id); } /** * Delete a vote from a poll. * * @param string $poll_id The ID of the poll * @param $vote_id int The ID of the vote * @return bool true is action succeeded */ public function deleteVote(string $poll_id, int $vote_id): bool { return $this->voteRepository->deleteById($poll_id, $vote_id); } /** * Remove all votes of a poll. * * @param string $poll_id The ID of the poll * @return bool|null true is action succeeded */ public function cleanVotes(string $poll_id): ?bool { $this->logService->log('CLEAN_VOTES', 'id:' . $poll_id); return $this->voteRepository->deleteByPollId($poll_id); } /** * Delete the entire given poll. * * @param $poll_id string The ID of the poll * @return bool true is action succeeded */ public function deleteEntirePoll(string $poll_id): bool { $poll = $this->pollRepository->findById($poll_id); $this->logService->log('DELETE_POLL', "id:$poll->id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail"); // Delete the entire poll $this->voteRepository->deleteByPollId($poll_id); $this->commentRepository->deleteByPollId($poll_id); $this->slotRepository->deleteByPollId($poll_id); $this->pollRepository->deleteById($poll_id); return true; } /** * Delete a slot from a poll. * * @param object $poll The ID of the poll * @param object $slot The slot informations (datetime + moment) * @return bool true if action succeeded */ public function deleteDateSlot(object $poll, object $slot): bool { $this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . json_encode($slot)); $datetime = $slot->title; $moment = $slot->moment; $slots = $this->pollService->allSlotsByPoll($poll); // We can't delete the last slot if ($poll->format === 'D' && count($slots) === 1 && strpos($slots[0]->moments, ',') === false) { return false; } if ($poll->format === 'A' && count($slots) === 1) { return false; } $index = 0; $indexToDelete = -1; $newMoments = []; // Search the index of the slot to delete foreach ($slots as $aSlot) { $moments = explode(',', $aSlot->moments); foreach ($moments as $rowMoment) { if ($datetime === $aSlot->title) { if ($moment === $rowMoment) { $indexToDelete = $index; } else { $newMoments[] = $rowMoment; } } $index++; } } // Remove votes $this->connect->beginTransaction(); $this->voteRepository->deleteByIndex($poll->id, $indexToDelete); if (count($newMoments) > 0) { $this->slotRepository->update($poll->id, $datetime, implode(',', $newMoments)); } else { $this->slotRepository->deleteByDateTime($poll->id, $datetime); } $this->connect->commit(); return true; } public function deleteClassicSlot($poll, string $slot_title): bool { $this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . $slot_title); $slots = $this->pollService->allSlotsByPoll($poll); if (count($slots) === 1) { return false; } $index = 0; $indexToDelete = -1; // Search the index of the slot to delete foreach ($slots as $aSlot) { if ($slot_title === $aSlot->title) { $indexToDelete = $index; } $index++; } // Remove votes $this->connect->beginTransaction(); $this->voteRepository->deleteByIndex($poll->id, $indexToDelete); $this->slotRepository->deleteByDateTime($poll->id, $slot_title); $this->connect->commit(); return true; } /** * Add a new slot to a date poll. And insert default values for user's votes. *