date.chapril.org-framadate/app/classes/Framadate/Repositories/PollRepository.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

246 lines
7.5 KiB
PHP

<?php
namespace Framadate\Repositories;
use Framadate\Utils;
use PDO;
class PollRepository extends AbstractRepository {
/**
* @param $poll_id
* @param $admin_poll_id
* @param $form
*/
public function insertPoll($poll_id, $admin_poll_id, $form)
{
$this->connect->insert(Utils::table('poll'), [
'id' => $poll_id,
'admin_id' => $admin_poll_id,
'title' => $form->title,
'description' => $form->description,
'admin_name' => $form->admin_name,
'admin_mail' => $form->admin_mail,
'end_date' => (new \DateTime)->setTimestamp($form->end_date)->format('Y-m-d H:i:s'),
'format' => $form->format,
'editable' => ($form->editable>=0 && $form->editable<=2) ? $form->editable : 0,
'receiveNewVotes' => $form->receiveNewVotes ? 1 : 0,
'receiveNewComments' => $form->receiveNewComments ? 1 : 0,
'hidden' => $form->hidden ? 1 : 0,
'password_hash' => $form->password_hash,
'results_publicly_visible' => $form->results_publicly_visible ? 1 : 0,
'ValueMax' => $form->ValueMax,
'collect_users_mail' => $form->collect_users_mail? 1 : 0,
]);
}
/**
* @param $poll_id
* @throws \Doctrine\DBAL\DBALException
* @return mixed
*/
public function findById($poll_id)
{
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE id = ?');
$prepared->execute([$poll_id]);
$poll = $prepared->fetch();
$prepared->closeCursor();
return $poll;
}
/**
* @param $admin_poll_id
* @throws \Doctrine\DBAL\DBALException
* @return mixed
*/
public function findByAdminId($admin_poll_id) {
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE admin_id = ?');
$prepared->execute([$admin_poll_id]);
$poll = $prepared->fetch();
$prepared->closeCursor();
return $poll;
}
/**
* @param $poll_id
* @throws \Doctrine\DBAL\DBALException
* @return bool
*/
public function existsById($poll_id) {
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('poll') . ' WHERE id = ?');
$prepared->execute([$poll_id]);
return $prepared->rowCount() > 0;
}
/**
* @param $admin_poll_id
* @throws \Doctrine\DBAL\DBALException
* @return bool
*/
public function existsByAdminId($admin_poll_id) {
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('poll') . ' WHERE admin_id = ?');
$prepared->execute([$admin_poll_id]);
return $prepared->rowCount() > 0;
}
/**
* @param $poll
* @return bool
*/
public function update($poll)
{
return $this->connect->update(Utils::table('poll'), [
'title' => $poll->title,
'admin_name' => $poll->admin_name,
'admin_mail' => $poll->admin_mail,
'description' => $poll->description,
'end_date' => $poll->end_date, # TODO : Harmonize dates between here and insert
'active' => $poll->active,
'editable' => $poll->editable >= 0 && $poll->editable <= 2 ? $poll->editable : 0,
'hidden' => $poll->hidden ? 1 : 0,
'password_hash' => $poll->password_hash,
'results_publicly_visible' => $poll->results_publicly_visible ? 1 : 0
], [
'id' => $poll->id
]) > 0;
}
/**
* @param $poll_id
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException
* @return bool
*/
public function deleteById($poll_id)
{
return $this->connect->delete(Utils::table('poll'), ['id' => $poll_id]) > 0;
}
/**
* Find old polls. Limit: 20.
*
* @param int $limit
* @throws \Doctrine\DBAL\DBALException
* @return array Array of old polls
*/
public function findOldPolls($limit = 20)
{
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE DATE_ADD(end_date, INTERVAL ? DAY) < NOW() AND end_date != 0 LIMIT ?');
$prepared->execute([PURGE_DELAY, $limit]);
return $prepared->fetchAll();
}
/**
* Search polls in database.
*
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>..., 'mail'=>...]
* @param int $start The number of first entry to select
* @param int $limit The number of entries to find
* @throws \Doctrine\DBAL\DBALException
* @return array The found polls
*/
public function findAll($search, $start, $limit) {
// Polls
$request = "";
$request .= "SELECT p.*,";
$request .= " (SELECT count(1) FROM " . Utils::table('vote') . " v WHERE p.id=v.poll_id) votes";
$request .= " FROM " . Utils::table('poll') . " p";
$request .= " WHERE 1";
$values = [];
if (!empty($search["poll"])) {
$request .= " AND p.id LIKE :poll";
$values["poll"] = "{$search["poll"]}%";
}
$fields = [
// key of $search => column name
"title" => "title",
"name" => "admin_name",
"mail" => "admin_mail",
];
foreach ($fields as $searchKey => $columnName) {
if (empty($search[$searchKey])) {
continue;
}
$request .= " AND p.$columnName LIKE :$searchKey";
$values[$searchKey] = "%{$search[$searchKey]}%";
}
$request .= " ORDER BY p.title ASC";
$request .= " LIMIT :start, :limit";
$prepared = $this->prepare($request);
foreach ($values as $searchKey => $value) {
$prepared->bindParam(":$searchKey", $value, PDO::PARAM_STR);
}
$prepared->bindParam(':start', $start, PDO::PARAM_INT);
$prepared->bindParam(':limit', $limit, PDO::PARAM_INT);
$prepared->execute();
return $prepared->fetchAll();
}
/**
* Find all polls that are created with the given admin mail.
*
* @param string $mail Email address of the poll admin
* @throws \Doctrine\DBAL\DBALException
* @return array The list of matching polls
*/
public function findAllByAdminMail($mail) {
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE admin_mail = :admin_mail');
$prepared->execute(['admin_mail' => $mail]);
return $prepared->fetchAll();
}
/**
* Get the total number of polls in database.
*
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
* @throws \Doctrine\DBAL\DBALException
* @return int The number of polls
*/
public function count($search = null) {
// Total count
$prepared = $this->prepare('
SELECT count(1) nb
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 === null ? '' : $search['poll'] . '%';
$title = $search === null ? '' : '%' . $search['title'] . '%';
$name = $search === null ? '' : '%' . $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();
$count = $prepared->fetch();
/*echo '---';
print_r($count);
echo '---';
exit;*/
return $count->nb;
}
}