date.chapril.org-framadate/app/classes/Framadate/Repositories/VoteRepository.php
Thomas Citharel 006a191544
Make Framadate compatible with PostgreSQL and great again !
* Move the database handling to Doctrine DBAL
* Move Migrations to Doctrine Migrations
* Rename migrations for Doctrine Migrations Uses
* Fix Migrations
* Change config parameters, introduce db name, host and port parameters and get rid of database url
* Change install form for this
* Add a CLI command to make migrations
* Add config.test.php to be used with APP_ENV=test for testing

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add sqlite to CI and execute migration in test env

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Typo

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

SQLite is already inside the image...

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Rebase two new migrations

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move from trait to abstract class and remove legacy migration table after checks

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move doctrine command path inside CI

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move abstract migration class to correct namespace and remove unused command

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Check for legacy migration table existence

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Check if legacy migration table exists before deleting it

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-22 18:37:53 +02:00

142 lines
4.6 KiB
PHP

<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class VoteRepository extends AbstractRepository {
/**
* @param $poll_id
* @throws \Doctrine\DBAL\DBALException
* @return array
*/
public function allUserVotesByPollId($poll_id)
{
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('vote') . ' WHERE poll_id = ? ORDER BY id');
$prepared->execute([$poll_id]);
return $prepared->fetchAll();
}
/**
* @param $poll_id
* @param $insert_position
* @throws \Doctrine\DBAL\DBALException
* @return bool
*/
public function insertDefault($poll_id, $insert_position)
{
# TODO : Handle this on PHP's side
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = CONCAT(SUBSTRING(choices, 1, ?), " ", SUBSTRING(choices, ?)) WHERE poll_id = ?'); //#51 : default value for unselected vote
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
}
function insert($poll_id, $name, $choices, $token, $mail) {
$this->connect->insert(Utils::table('vote'), ['poll_id' => $poll_id, 'name' => $name, 'choices' => $choices, 'uniqId' => $token, 'mail' => $mail]);
$newVote = new \stdClass();
$newVote->poll_id = $poll_id;
$newVote->id = $this->lastInsertId();
$newVote->name = $name;
$newVote->choices = $choices;
$newVote->uniqId = $token;
$newVote->mail=$mail;
return $newVote;
}
/**
* @param $poll_id
* @param $vote_id
* @throws \Doctrine\DBAL\DBALException
* @return bool
*/
public function deleteById($poll_id, $vote_id)
{
return $this->connect->delete(Utils::table('vote'), ['poll_id' => $poll_id, 'id' => $vote_id]) > 0;
}
public function deleteOldVotesByPollId($poll_id, $votesToDelete) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ? ORDER BY `poll_id` ASC LIMIT ' . $votesToDelete);
return $prepared->execute([$poll_id]);
}
/**
* Delete all votes of a given poll.
*
* @param $poll_id int The ID of the given poll.
* @throws \Doctrine\DBAL\DBALException
* @return bool|null true if action succeeded.
*/
public function deleteByPollId($poll_id)
{
return $this->connect->delete(Utils::table('vote'), ['poll_id' => $poll_id]) > 0;
}
/**
* 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
* @throws \Doctrine\DBAL\DBALException
* @return bool|null true if action succeeded.
*/
public 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]);
}
/**
* @param $poll_id
* @param $vote_id
* @param $name
* @param $choices
* @return bool
*/
public function update($poll_id, $vote_id, $name, $choices, $mail)
{
return $this->connect->update(Utils::table('vote'), [
'choices' => $choices,
'name' => $name,
'mail' => $mail,
], [
'poll_id' => $poll_id,
'id' => $vote_id,
]) > 0;
}
/**
* Check if name is already used for the given poll.
*
* @param int $poll_id ID of the poll
* @param string $name Name of the vote
* @throws \Doctrine\DBAL\DBALException
* @return bool true if vote already exists
*/
public function existsByPollIdAndName($poll_id, $name) {
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('vote') . ' WHERE poll_id = ? AND name = ?');
$prepared->execute([$poll_id, $name]);
return $prepared->rowCount() > 0;
}
/**
* Check if name is already used for the given poll and another vote.
*
* @param int $poll_id ID of the poll
* @param string $name Name of the vote
* @param int $vote_id ID of the current vote
* @throws \Doctrine\DBAL\DBALException
* @return bool true if vote already exists
*/
public function existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id) {
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('vote') . ' WHERE poll_id = ? AND name = ? AND id != ?');
$prepared->execute([$poll_id, $name, $vote_id]);
return $prepared->rowCount() > 0;
}
}