2015-04-02 23:10:41 +02:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Repositories;
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
use Framadate\FramaDB;
|
2015-04-02 23:10:41 +02:00
|
|
|
use Framadate\Utils;
|
2015-04-03 00:11:36 +02:00
|
|
|
use PDO;
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
class PollRepository extends AbstractRepository {
|
2021-12-20 17:46:50 +01:00
|
|
|
public function insertPoll(string $poll_id, string $admin_poll_id, $form): void
|
|
|
|
{
|
2015-04-02 23:10:41 +02:00
|
|
|
$sql = 'INSERT INTO `' . Utils::table('poll') . '`
|
2018-02-20 13:06:58 +01:00
|
|
|
(id, admin_id, title, description, admin_name, admin_mail, end_date, format, editable, receiveNewVotes, receiveNewComments, hidden, password_hash, results_publicly_visible,ValueMax)
|
|
|
|
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?,?,?,?,?,?)';
|
2015-04-02 23:23:34 +02:00
|
|
|
$prepared = $this->prepare($sql);
|
2018-02-20 13:12:46 +01:00
|
|
|
$prepared->execute([$poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, ($form->editable>=0 && $form->editable<=2) ? $form->editable : 0, $form->receiveNewVotes ? 1 : 0, $form->receiveNewComments ? 1 : 0, $form->hidden ? 1 : 0, $form->password_hash, $form->results_publicly_visible ? 1 : 0,$form->ValueMax]);
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findById(string $poll_id) {
|
2015-04-02 23:23:34 +02:00
|
|
|
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE id = ?');
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id]);
|
2015-04-02 23:23:34 +02:00
|
|
|
$poll = $prepared->fetch();
|
|
|
|
$prepared->closeCursor();
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
return $poll;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findByAdminId(string $admin_poll_id) {
|
2015-10-28 22:11:00 +01:00
|
|
|
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE admin_id = ?');
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$admin_poll_id]);
|
2015-10-28 22:11:00 +01:00
|
|
|
$poll = $prepared->fetch();
|
2015-04-02 23:23:34 +02:00
|
|
|
$prepared->closeCursor();
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
return $poll;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function existsById(string $poll_id): bool
|
|
|
|
{
|
2015-04-02 23:23:34 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE id = ?');
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id]);
|
2015-04-02 23:23:34 +02:00
|
|
|
|
2015-12-05 16:30:49 +01:00
|
|
|
return $prepared->rowCount() > 0;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function existsByAdminId(string $admin_poll_id): bool
|
|
|
|
{
|
2015-12-05 16:30:49 +01:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE admin_id = ?');
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$admin_poll_id]);
|
2015-12-05 16:30:49 +01:00
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
return $prepared->rowCount() > 0;
|
|
|
|
}
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function update($poll): bool
|
|
|
|
{
|
2015-10-30 20:45:18 +01:00
|
|
|
$prepared = $this->prepare('UPDATE `' . Utils::table('poll') . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=?, hidden=?, password_hash=?, results_publicly_visible=? WHERE id = ?');
|
2015-04-02 23:10:41 +02:00
|
|
|
|
2021-03-08 16:27:42 +01:00
|
|
|
return $prepared->execute([$poll->title, $poll->admin_name, $poll->admin_mail, $poll->description, $poll->end_date, $poll->active ? 1 : 0, ($poll->editable>=0 && $poll->editable<=2) ? $poll->editable : 0, $poll->hidden ? 1 : 0, $poll->password_hash, $poll->results_publicly_visible ? 1 : 0, $poll->id]);
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteById($poll_id): bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$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
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findOldPolls(): array
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-02 09:12:51 +02:00
|
|
|
* Search polls in database.
|
2015-04-03 00:11:36 +02:00
|
|
|
*
|
2015-10-28 13:06:02 +01:00
|
|
|
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>..., 'mail'=>...]
|
2015-09-13 00:53:03 +02:00
|
|
|
* @param int $start The number of first entry to select
|
|
|
|
* @param int $limit The number of entries to find
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return array The found polls
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findAll(array $search, int $start, int $limit): array
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
// Polls
|
2021-12-20 17:46:50 +01:00
|
|
|
|
|
|
|
$request = "SELECT p.*,";
|
2018-04-02 09:12:51 +02:00
|
|
|
$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";
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$values = [];
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
if (!empty($search["poll"])) {
|
|
|
|
$request .= " AND p.id LIKE :poll";
|
|
|
|
$values["poll"] = "{$search["poll"]}%";
|
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$fields = [
|
|
|
|
// key of $search => column name
|
|
|
|
"title" => "title",
|
|
|
|
"name" => "admin_name",
|
|
|
|
"mail" => "admin_mail",
|
|
|
|
];
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
foreach ($fields as $searchKey => $columnName) {
|
|
|
|
if (empty($search[$searchKey])) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$request .= " AND p.$columnName LIKE :$searchKey";
|
2021-12-20 17:46:50 +01:00
|
|
|
$values[$searchKey] = "%$search[$searchKey]%";
|
2018-04-02 09:12:51 +02:00
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$request .= " ORDER BY p.title ASC";
|
|
|
|
$request .= " LIMIT :start, :limit";
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$prepared = $this->prepare($request);
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
foreach ($values as $searchKey => $value) {
|
|
|
|
$prepared->bindParam(":$searchKey", $value, PDO::PARAM_STR);
|
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2015-09-13 00:53:03 +02:00
|
|
|
$prepared->bindParam(':start', $start, PDO::PARAM_INT);
|
|
|
|
$prepared->bindParam(':limit', $limit, PDO::PARAM_INT);
|
2021-12-20 17:46:50 +01:00
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared->execute();
|
|
|
|
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:52:09 +02:00
|
|
|
/**
|
|
|
|
* Find all polls that are created with the given admin mail.
|
|
|
|
*
|
|
|
|
* @param string $mail Email address of the poll admin
|
|
|
|
* @return array The list of matching polls
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findAllByAdminMail(string $mail): array
|
|
|
|
{
|
2015-04-08 21:52:09 +02:00
|
|
|
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE admin_mail = :admin_mail');
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute(['admin_mail' => $mail]);
|
2015-04-08 21:52:09 +02:00
|
|
|
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
/**
|
2021-12-20 17:46:50 +01:00
|
|
|
* Get the total number of polls in database.
|
2015-04-03 00:11:36 +02:00
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param array|null $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return int The number of polls
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function count(array $search = null): int
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
// Total count
|
2015-09-13 00:53:03 +02:00
|
|
|
$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');
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
$poll = $search === null ? '' : $search['poll'] . '%';
|
|
|
|
$title = $search === null ? '' : '%' . $search['title'] . '%';
|
|
|
|
$name = $search === null ? '' : '%' . $search['name'] . '%';
|
2015-09-13 00:53:03 +02:00
|
|
|
$prepared->bindParam(':id', $poll, PDO::PARAM_STR);
|
|
|
|
$prepared->bindParam(':title', $title, PDO::PARAM_STR);
|
|
|
|
$prepared->bindParam(':name', $name, PDO::PARAM_STR);
|
|
|
|
|
|
|
|
$prepared->execute();
|
2015-04-03 00:11:36 +02:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
return $prepared->fetch()->nb;
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|