Merge branch 'feature/Work_on_Service_and_Repository' into develop

This commit is contained in:
Olivier PEREZ 2015-04-07 22:26:13 +02:00
commit 663f08f607
15 changed files with 620 additions and 100 deletions

View File

@ -52,7 +52,7 @@ $poll_to_delete = null;
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$superAdminService = new SuperAdminService($connect);
$superAdminService = new SuperAdminService();
$securityService = new SecurityService();
/* GET */

View File

@ -21,7 +21,6 @@ use Framadate\Services\AdminPollService;
use Framadate\Services\InputService;
use Framadate\Services\LogService;
use Framadate\Message;
use Framadate\Utils;
use Framadate\Editable;
include_once __DIR__ . '/app/inc/init.php';
@ -113,7 +112,6 @@ if (isset($_POST['update_poll_info'])) {
}
} elseif ($field == 'expiration_date') {
$expiration_date = filter_input(INPUT_POST, 'expiration_date', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]+[-/][0-9]+[-/][0-9]+#']]);
$expiration_date = strtotime($expiration_date);
if ($expiration_date) {
$poll->end_date = $expiration_date;
$updated = true;

View File

@ -328,5 +328,8 @@ SELECT p.*,
$prepared->closeCursor();
return $result->nb;
}
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
abstract class AbstractRepository {
/**
* @var FramaDB
*/
private $connect;
/**
* PollRepository constructor.
* @param FramaDB $connect
*/
function __construct(FramaDB $connect) {
$this->connect = $connect;
}
public function beginTransaction() {
$this->connect->beginTransaction();
}
public function commit() {
$this->connect->commit();
}
function rollback() {
$this->connect->rollback();
}
public function prepare($sql) {
return $this->connect->prepare($sql);
}
function query($sql) {
return $this->connect->query($sql);
}
function lastInsertId() {
return $this->connect->lastInsertId();
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class CommentRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
function findAllByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('comment') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
/**
* Insert a new comment.
*
* @param $poll_id
* @param $name
* @param $comment
* @return bool
*/
function insert($poll_id, $name, $comment) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('comment') . '` (poll_id, name, comment) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $name, $comment]);
}
function deleteById($poll_id, $comment_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $comment_id]);
}
/**
* Delete all comments of a given poll.
*
* @param $poll_id int The ID of the given poll.
* @return bool|null true if action succeeded.
*/
function deleteByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
public function exists($poll_id, $name, $comment) {
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('comment') . '` WHERE poll_id = ? AND name = ? AND comment = ?');
$prepared->execute(array($poll_id, $name, $comment));
return $prepared->rowCount() > 0;
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
use PDO;
class PollRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
public function insertPoll($poll_id, $admin_poll_id, $form) {
$sql = 'INSERT INTO `' . Utils::table('poll') . '`
(id, admin_id, title, description, admin_name, admin_mail, end_date, format, editable, receiveNewVotes, receiveNewComments)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?,?)';
$prepared = $this->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes, $form->receiveNewComments));
}
function findById($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE id = ?');
$prepared->execute(array($poll_id));
$poll = $prepared->fetch();
$prepared->closeCursor();
return $poll;
}
public function existsById($poll_id) {
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE id = ?');
$prepared->execute(array($poll_id));
return $prepared->rowCount() > 0;
}
function update($poll) {
$prepared = $this->prepare('UPDATE `' . Utils::table('poll') . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=? WHERE id = ?');
return $prepared->execute([$poll->title, $poll->admin_name, $poll->admin_mail, $poll->description, $poll->end_date, $poll->active, $poll->editable, $poll->id]);
}
function deleteById($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('poll') . '` WHERE id = ?');
return $prepared->execute([$poll_id]);
}
/**
* Find old polls. Limit: 20.
*
* @return array Array of old polls
*/
public function findOldPolls() {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE DATE_ADD(`end_date`, INTERVAL ' . PURGE_DELAY . ' DAY) < NOW() AND `end_date` != 0 LIMIT 20');
$prepared->execute([]);
return $prepared->fetchAll();
}
/**
* Search polls in databse.
*
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
* @return array The found polls
*/
public function findAll($search) {
// Polls
$prepared = $this->prepare('
SELECT p.*,
(SELECT count(1) FROM `' . Utils::table('vote') . '` v WHERE p.id=v.poll_id) votes
FROM `' . Utils::table('poll') . '` p
WHERE (:id = "" OR p.id LIKE :id)
AND (:title = "" OR p.title LIKE :title)
AND (:name = "" OR p.admin_name LIKE :name)
ORDER BY p.title ASC
');
$poll = $search['poll'] . '%';
$title = '%' . $search['title'] . '%';
$name = '%' . $search['name'] . '%';
$prepared->bindParam(':id', $poll, PDO::PARAM_STR);
$prepared->bindParam(':title', $title, PDO::PARAM_STR);
$prepared->bindParam(':name', $name, PDO::PARAM_STR);
$prepared->execute();
return $prepared->fetchAll();
}
/**
* Get the total number of polls in databse.
*
* @return int The number of polls
*/
public function count() {
// Total count
$stmt = $this->query('SELECT count(1) nb FROM `' . Utils::table('poll') . '`');
$count = $stmt->fetch();
$stmt->closeCursor();
return $count->nb;
}
}

View File

@ -0,0 +1,83 @@
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate\Repositories;
use Framadate\FramaDB;
class RepositoryFactory {
private static $connect;
private static $pollRepository;
private static $slotRepository;
private static $voteRepository;
private static $commentRepository;
/**
* @param FramaDB $connect
*/
static function init(FramaDB $connect) {
self::$connect = $connect;
}
/**
* @return PollRepository The singleton of PollRepository
*/
static function pollRepository() {
if (self::$pollRepository == null) {
self::$pollRepository = new PollRepository(self::$connect);
}
return self::$pollRepository;
}
/**
* @return SlotRepository The singleton of SlotRepository
*/
static function slotRepository() {
if (self::$slotRepository == null) {
self::$slotRepository = new SlotRepository(self::$connect);
}
return self::$slotRepository;
}
/**
* @return VoteRepository The singleton of VoteRepository
*/
static function voteRepository() {
if (self::$voteRepository == null) {
self::$voteRepository = new VoteRepository(self::$connect);
}
return self::$voteRepository;
}
/**
* @return CommentRepository The singleton of CommentRepository
*/
static function commentRepository() {
if (self::$commentRepository == null) {
self::$commentRepository = new CommentRepository(self::$connect);
}
return self::$commentRepository;
}
}

View File

@ -0,0 +1,132 @@
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class SlotRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
/**
* Insert a bulk of slots.
*
* @param int $poll_id
* @param array $choices
*/
public function insertSlots($poll_id, $choices) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('slot') . '` (poll_id, title, moments) VALUES (?, ?, ?)');
foreach ($choices as $choice) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName(), null));
} else {
$prepared->execute(array($poll_id, $choice->getName(), $joinedSlots));
}
}
}
function listByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY title');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
/**
* Find the slot into poll for a given datetime.
*
* @param $poll_id int The ID of the poll
* @param $datetime int The datetime of the slot
* @return mixed Object The slot found, or null
*/
function findByPollIdAndDatetime($poll_id, $datetime) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? AND SUBSTRING_INDEX(title, \'@\', 1) = ?');
$prepared->execute([$poll_id, $datetime]);
$slot = $prepared->fetch();
$prepared->closeCursor();
return $slot;
}
/**
* Insert a new slot into a given poll.
*
* @param $poll_id int The ID of the poll
* @param $title mixed The title of the slot
* @param $moments mixed|null The moments joined with ","
* @return bool true if action succeeded
*/
function insert($poll_id, $title, $moments) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('slot') . '` (poll_id, title, moments) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $title, $moments]);
}
/**
* Update a slot into a poll.
*
* @param $poll_id int The ID of the poll
* @param $datetime int The datetime of the slot to update
* @param $newMoments mixed The new moments
* @return bool|null true if action succeeded.
*/
function update($poll_id, $datetime, $newMoments) {
$prepared = $this->prepare('UPDATE `' . Utils::table('slot') . '` SET moments = ? WHERE poll_id = ? AND title = ?');
return $prepared->execute([$newMoments, $poll_id, $datetime]);
}
/**
* Delete a entire slot from a poll.
*
* @param $poll_id int The ID of the poll
* @param $datetime mixed The datetime of the slot
*/
function deleteByDateTime($poll_id, $datetime) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('slot') . '` WHERE poll_id = ? AND title = ?');
$prepared->execute([$poll_id, $datetime]);
}
function deleteByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('slot') . '` WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class VoteRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
function allUserVotesByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('vote') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function insertDefault($poll_id, $insert_position) {
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = CONCAT(SUBSTRING(choices, 1, ?), "0", SUBSTRING(choices, ?)) WHERE poll_id = ?');
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
}
function insert($poll_id, $name, $choices) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('vote') . '` (poll_id, name, choices) VALUES (?,?,?)');
$prepared->execute([$poll_id, $name, $choices]);
$newVote = new \stdClass();
$newVote->poll_id = $poll_id;
$newVote->id = $this->lastInsertId();
$newVote->name = $name;
$newVote->choices = $choices;
return $newVote;
}
function deleteById($poll_id, $vote_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $vote_id]);
}
/**
* Delete all votes of a given poll.
*
* @param $poll_id int The ID of the given poll.
* @return bool|null true if action succeeded.
*/
function deleteByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
/**
* Delete all votes made on given moment index.
*
* @param $poll_id int The ID of the poll
* @param $index int The index of the vote into the poll
* @return bool|null true if action succeeded.
*/
function deleteByIndex($poll_id, $index) {
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?');
return $prepared->execute([$index, $index + 2, $poll_id]);
}
function update($poll_id, $vote_id, $name, $choices) {
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = ?, name = ? WHERE poll_id = ? AND id = ?');
return $prepared->execute([$choices, $name, $poll_id, $vote_id]);
}
public function countByPollId($poll_id) {
$prepared = $this->prepare('SELECT count(1) nb FROM `' . Utils::table('vote') . '` WHERE poll_id = ?');
$prepared->execute([$poll_id]);
$result = $prepared->fetch();
$prepared->closeCursor();
return $result->nb;
}
}

View File

@ -2,7 +2,7 @@
namespace Framadate\Services;
use Framadate\FramaDB;
use Framadate\Utils;
use Framadate\Repositories\RepositoryFactory;
/**
* Class AdminPollService
@ -15,16 +15,25 @@ class AdminPollService {
private $pollService;
private $logService;
private $pollRepository;
private $slotRepository;
private $voteRepository;
private $commentRepository;
function __construct(FramaDB $connect, PollService $pollService, LogService $logService) {
$this->connect = $connect;
$this->pollService = $pollService;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->voteRepository = RepositoryFactory::voteRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
function updatePoll($poll) {
global $config;
if ($poll->end_date > $poll->creation_date && $poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) {
return $this->connect->updatePoll($poll);
return $this->pollRepository->update($poll);
} else {
return false;
}
@ -38,7 +47,7 @@ class AdminPollService {
* @return mixed true is action succeeded
*/
function deleteComment($poll_id, $comment_id) {
return $this->connect->deleteComment($poll_id, $comment_id);
return $this->commentRepository->deleteById($poll_id, $comment_id);
}
/**
@ -49,7 +58,7 @@ class AdminPollService {
*/
function cleanComments($poll_id) {
$this->logService->log("CLEAN_COMMENTS", "id:$poll_id");
return $this->connect->deleteCommentsByPollId($poll_id);
return $this->commentRepository->deleteByPollId($poll_id);
}
/**
@ -60,7 +69,7 @@ class AdminPollService {
* @return mixed true is action succeeded
*/
function deleteVote($poll_id, $vote_id) {
return $this->connect->deleteVote($poll_id, $vote_id);
return $this->voteRepository->deleteById($poll_id, $vote_id);
}
/**
@ -71,7 +80,7 @@ class AdminPollService {
*/
function cleanVotes($poll_id) {
$this->logService->log('CLEAN_VOTES', 'id:' . $poll_id);
return $this->connect->deleteVotesByPollId($poll_id);
return $this->voteRepository->deleteByPollId($poll_id);
}
/**
@ -81,14 +90,14 @@ class AdminPollService {
* @return bool true is action succeeded
*/
function deleteEntirePoll($poll_id) {
$poll = $this->connect->findPollById($poll_id);
$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->connect->deleteVotesByPollId($poll_id);
$this->connect->deleteCommentsByPollId($poll_id);
$this->connect->deleteSlotsByPollId($poll_id);
$this->connect->deletePollById($poll_id);
$this->voteRepository->deleteByPollId($poll_id);
$this->commentRepository->deleteByPollId($poll_id);
$this->slotRepository->deleteByPollId($poll_id);
$this->pollRepository->deleteById($poll_id);
return true;
}
@ -130,11 +139,11 @@ class AdminPollService {
// Remove votes
$this->connect->beginTransaction();
$this->connect->deleteVotesByIndex($poll_id, $indexToDelete);
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete);
if (count($newMoments) > 0) {
$this->connect->updateSlot($poll_id, $datetime, implode(',', $newMoments));
$this->slotRepository->update($poll_id, $datetime, implode(',', $newMoments));
} else {
$this->connect->deleteSlot($poll_id, $datetime);
$this->slotRepository->deleteByDateTime($poll_id, $datetime);
}
$this->connect->commit();
@ -159,8 +168,8 @@ class AdminPollService {
// Remove votes
$this->connect->beginTransaction();
$this->connect->deleteVotesByIndex($poll_id, $indexToDelete);
$this->connect->deleteSlot($poll_id, $slot_title);
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete);
$this->slotRepository->deleteByDateTime($poll_id, $slot_title);
$this->connect->commit();
return true;
@ -179,7 +188,7 @@ class AdminPollService {
* @return bool true if added
*/
public function addSlot($poll_id, $datetime, $new_moment) {
$slots = $this->connect->allSlotsByPollId($poll_id);
$slots = $this->slotRepository->listByPollId($poll_id);
$result = $this->findInsertPosition($slots, $datetime, $new_moment);
// Begin transaction
@ -200,13 +209,13 @@ class AdminPollService {
// Update found slot
$moments[] = $new_moment;
sort($moments);
$this->connect->updateSlot($poll_id, $datetime, implode(',', $moments));
$this->slotRepository->update($poll_id, $datetime, implode(',', $moments));
} else {
$this->connect->insertSlot($poll_id, $datetime, $new_moment);
$this->slotRepository->insert($poll_id, $datetime, $new_moment);
}
$this->connect->insertDefaultVote($poll_id, $result->insert);
$this->voteRepository->insertDefault($poll_id, $result->insert);
// Commit transaction
$this->connect->commit();

View File

@ -28,6 +28,10 @@ class InputService {
/**
* This method filter an array calling "filter_var" on each items.
* Only items validated are added at their own indexes, the others are not returned.
* @param array $arr The array to filter
* @param int $type The type of filter to apply
* @param array|null $options The associative array of options
* @return array The filtered array
*/
function filterArray(array $arr, $type, $options = null) {
$newArr = [];

View File

@ -22,15 +22,25 @@ use Framadate\Form;
use Framadate\FramaDB;
use Framadate\Utils;
use Framadate\Security\Token;
use Framadate\Repositories\RepositoryFactory;
class PollService {
private $connect;
private $logService;
private $pollRepository;
private $slotRepository;
private $voteRepository;
private $commentRepository;
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->voteRepository = RepositoryFactory::voteRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
/**
@ -41,43 +51,71 @@ class PollService {
*/
function findById($poll_id) {
if (preg_match('/^[\w\d]{16}$/i', $poll_id)) {
return $this->connect->findPollById($poll_id);
return $this->pollRepository->findById($poll_id);
}
return null;
}
function allCommentsByPollId($poll_id) {
return $this->connect->allCommentsByPollId($poll_id);
return $this->commentRepository->findAllByPollId($poll_id);
}
function allVotesByPollId($poll_id) {
return $this->connect->allUserVotesByPollId($poll_id);
return $this->voteRepository->allUserVotesByPollId($poll_id);
}
function allSlotsByPollId($poll_id) {
return $this->connect->allSlotsByPollId($poll_id);
return $this->slotRepository->listByPollId($poll_id);
}
public function updateVote($poll_id, $vote_id, $name, $choices) {
$choices = implode($choices);
return $this->connect->updateVote($poll_id, $vote_id, $name, $choices);
return $this->voteRepository->update($poll_id, $vote_id, $name, $choices);
}
function addVote($poll_id, $name, $choices) {
$choices = implode($choices);
$token = $this->random(16);
return $this->connect->insertVote($poll_id, $name, $choices, $token);
return $this->voteRepository->insert($poll_id, $name, $choices);
}
function addComment($poll_id, $name, $comment) {
// TODO Check if there is no duplicate before to add a new comment
return $this->connect->insertComment($poll_id, $name, $comment);
if ($this->commentRepository->exists($poll_id, $name, $comment)) {
return true;
} else {
return $this->commentRepository->insert($poll_id, $name, $comment);
}
}
public function countVotesByPollId($poll_id) {
return $this->connect->countVotesByPollId($poll_id);
return $this->voteRepository->countByPollId($poll_id);
}
/**
* @param Form $form
* @return string
*/
function createPoll(Form $form) {
// Generate poll IDs, loop while poll ID already exists
do {
$poll_id = $this->random(16);
} while ($this->pollRepository->existsById($poll_id));
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->pollRepository->beginTransaction();
$this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form);
$this->slotRepository->insertSlots($poll_id, $form->getChoices());
$this->pollRepository->commit();
$this->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
return array($poll_id, $admin_poll_id);
}
function computeBestChoices($votes) {
@ -125,59 +163,6 @@ class PollService {
return $splitted;
}
/**
* @param Form $form
* @return string
*/
function createPoll(Form $form) {
// Generate poll IDs
$poll_id = $this->random(16);
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->connect->beginTransaction();
// TODO Extract this to FramaDB (or repository layer)
$sql = 'INSERT INTO ' . Utils::table('poll') . '
(id, admin_id, title, description, admin_name, admin_mail, end_date, format, editable, receiveNewVotes, receiveNewComments)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?,?)';
$prepared = $this->connect->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes, $form->receiveNewComments));
$prepared = $this->connect->prepare('INSERT INTO ' . Utils::table('slot') . ' (poll_id, title, moments) VALUES (?, ?, ?)');
foreach ($form->getChoices() as $choice) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName(), null));
} else {
$prepared->execute(array($poll_id, $choice->getName(), $joinedSlots));
}
}
$this->connect->commit();
$this->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
return [$poll_id, $admin_poll_id];
}
private function random($length) {
return Token::getToken($length);
}

View File

@ -1,6 +1,7 @@
<?php
namespace Framadate\Services;
use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory;
/**
* This service helps to purge data.
@ -9,12 +10,18 @@ use Framadate\FramaDB;
*/
class PurgeService {
private $connect;
private $logService;
private $pollRepository;
private $slotRepository;
private $voteRepository;
private $commentRepository;
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->voteRepository = RepositoryFactory::voteRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
/**
@ -23,7 +30,7 @@ class PurgeService {
* @return bool true is action succeeded
*/
function purgeOldPolls() {
$oldPolls = $this->connect->findOldPolls();
$oldPolls = $this->pollRepository->findOldPolls();
$count = count($oldPolls);
if ($count > 0) {
@ -50,16 +57,16 @@ class PurgeService {
function purgePollById($poll_id) {
$done = true;
$this->connect->beginTransaction();
$done &= $this->connect->deleteCommentsByPollId($poll_id);
$done &= $this->connect->deleteVotesByPollId($poll_id);
$done &= $this->connect->deleteSlotsByPollId($poll_id);
$done &= $this->connect->deletePollById($poll_id);
$this->pollRepository->beginTransaction();
$done &= $this->commentRepository->deleteByPollId($poll_id);
$done &= $this->voteRepository->deleteByPollId($poll_id);
$done &= $this->slotRepository->deleteByPollId($poll_id);
$done &= $this->pollRepository->deleteById($poll_id);
if ($done) {
$this->connect->commit();
$this->pollRepository->commit();
} else {
$this->connect->rollback();
$this->pollRepository->rollback();
}
return $done;

View File

@ -1,7 +1,7 @@
<?php
namespace Framadate\Services;
use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory;
/**
* The class provides action for application administrators.
@ -10,10 +10,10 @@ use Framadate\FramaDB;
*/
class SuperAdminService {
private $connect;
private $pollRepository;
function __construct(FramaDB $connect) {
$this->connect = $connect;
function __construct() {
$this->pollRepository = RepositoryFactory::pollRepository();
}
/**
@ -26,8 +26,8 @@ class SuperAdminService {
*/
public function findAllPolls($search, $page, $limit) {
$start = $page * $limit;
$polls = $this->connect->findAllPolls($search);
$total = $this->connect->countPolls();
$polls = $this->pollRepository->findAll($search);
$total = $this->pollRepository->count();
return ['polls' => array_slice($polls, $start, $limit), 'count' => count($polls), 'total' => $total];

View File

@ -17,6 +17,7 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory;
// Autoloading of dependencies with Composer
require_once __DIR__ . '/../../vendor/autoload.php';
@ -41,4 +42,5 @@ require_once __DIR__ . '/smarty.php';
// Connection to database
$connect = new FramaDB(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
RepositoryFactory::init($connect);
$err = 0;