2014-12-20 23:59:44 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AdminPollService
|
|
|
|
* @package Framadate\Services
|
|
|
|
*/
|
|
|
|
class AdminPollService {
|
|
|
|
|
|
|
|
private $connect;
|
2014-12-21 23:48:22 +01:00
|
|
|
private $pollService;
|
2014-12-20 23:59:44 +01:00
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
function __construct($connect, $pollService) {
|
2014-12-20 23:59:44 +01:00
|
|
|
$this->connect = $connect;
|
2014-12-21 23:48:22 +01:00
|
|
|
$this->pollService = $pollService;
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePoll($poll) {
|
|
|
|
return $this->connect->updatePoll($poll);
|
|
|
|
}
|
|
|
|
|
2014-12-21 00:25:00 +01:00
|
|
|
/**
|
|
|
|
* Delete a comment from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $comment_id int The ID of the comment
|
|
|
|
* @return mixed true is action succeeded
|
|
|
|
*/
|
2014-12-20 23:59:44 +01:00
|
|
|
function deleteComment($poll_id, $comment_id) {
|
|
|
|
return $this->connect->deleteComment($poll_id, $comment_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all comments of a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID a the poll
|
|
|
|
* @return bool|null true is action succeeded
|
|
|
|
*/
|
|
|
|
function cleanComments($poll_id) {
|
2014-12-21 00:25:00 +01:00
|
|
|
return $this->connect->deleteCommentsByAdminPollId($poll_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a vote from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $vote_id int The ID of the vote
|
|
|
|
* @return mixed true is action succeeded
|
|
|
|
*/
|
|
|
|
function deleteVote($poll_id, $vote_id) {
|
|
|
|
return $this->connect->deleteVote($poll_id, $vote_id);
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all votes of a poll.
|
|
|
|
*
|
2014-12-21 00:25:00 +01:00
|
|
|
* @param $poll_id int The ID of the poll
|
2014-12-20 23:59:44 +01:00
|
|
|
* @return bool|null true is action succeeded
|
|
|
|
*/
|
|
|
|
function cleanVotes($poll_id) {
|
|
|
|
return $this->connect->deleteVotesByAdminPollId($poll_id);
|
|
|
|
}
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
/**
|
|
|
|
* Delete a slot from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $slot string The name of the slot
|
|
|
|
*/
|
|
|
|
public function deleteSlot($poll_id, $slot) {
|
|
|
|
$ex = explode('@', $slot);
|
|
|
|
$datetime = $ex[0];
|
|
|
|
$moment = $ex[1];
|
|
|
|
|
|
|
|
$slots = $this->pollService->allSlotsByPollId($poll_id);
|
|
|
|
|
|
|
|
$index = 0;
|
|
|
|
$indexToDelete = -1;
|
|
|
|
$newMoments = [];
|
|
|
|
|
|
|
|
// Search the index of the slot to delete
|
|
|
|
foreach ($slots as $aSlot) {
|
|
|
|
$ex = explode('@', $aSlot->sujet);
|
|
|
|
$moments = explode(',', $ex[1]);
|
|
|
|
|
|
|
|
foreach ($moments as $rowMoment) {
|
|
|
|
if ($datetime == $ex[0]) {
|
|
|
|
if ($moment == $rowMoment) {
|
|
|
|
$indexToDelete = $index;
|
|
|
|
} else {
|
|
|
|
$newMoments[] = $rowMoment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove votes
|
|
|
|
$this->connect->beginTransaction();
|
|
|
|
$this->connect->deleteVotesByIndex($poll_id, $indexToDelete);
|
|
|
|
if (count($newMoments) > 0) {
|
|
|
|
$this->connect->updateSlot($poll_id, $datetime, $datetime . '@' . implode(',', $newMoments));
|
|
|
|
} else {
|
|
|
|
$this->connect->deleteSlot($poll_id, $datetime);
|
|
|
|
}
|
|
|
|
$this->connect->commit();
|
|
|
|
}
|
|
|
|
|
2014-12-22 14:18:33 +01:00
|
|
|
public function addSlot($poll_id, $newdate, $newmoment) {
|
|
|
|
$ex = explode('/', $newdate);
|
|
|
|
$datetime = mktime(0,0,0, $ex[1], $ex[0], $ex[2]);
|
|
|
|
|
|
|
|
$slot = $this->connect->findSlotByPollIdAndDatetime($poll_id, $datetime);
|
|
|
|
|
|
|
|
if ($slot != null) {
|
|
|
|
// Update found slot
|
|
|
|
$moments = explode('@', $slot->sujet)[1];
|
|
|
|
foreach ($moments as $moment) {
|
|
|
|
if ($moment == $newmoment) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO Implements
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO Found index of insertion, in order to update user votes
|
|
|
|
$this->connect->insertSlot($poll_id, $datetime . '@' . $newmoment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|