2015-04-02 23:10:41 +02:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Repositories;
|
|
|
|
|
|
|
|
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 {
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @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,
|
2018-05-08 15:03:22 +02:00
|
|
|
'collect_users_mail' => ($form->collect_users_mail >= 0 && $form->collect_users_mail <= 3) ? $form->collect_users_mail : 0,
|
2018-04-18 16:16:22 +02:00
|
|
|
]);
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function findById($poll_id)
|
|
|
|
{
|
2018-04-23 10:27:15 +02:00
|
|
|
$prepared = $this->connect->executeQuery('SELECT * FROM ' . Utils::table('poll') . ' WHERE id = ?', [$poll_id]);
|
2015-04-02 23:10:41 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $admin_poll_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 22:11:00 +01:00
|
|
|
public function findByAdminId($admin_poll_id) {
|
2018-04-23 10:27:15 +02:00
|
|
|
$prepared = $this->connect->executeQuery('SELECT * FROM ' . Utils::table('poll') . ' WHERE admin_id = ?', [$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;
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $poll_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-04-02 23:23:34 +02:00
|
|
|
public function existsById($poll_id) {
|
2018-04-18 16:16:22 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('poll') . ' WHERE id = ?');
|
2015-04-02 23:23:34 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @param $admin_poll_id
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-05 16:30:49 +01:00
|
|
|
public function existsByAdminId($admin_poll_id) {
|
2018-04-18 16:16:22 +02:00
|
|
|
$prepared = $this->prepare('SELECT 1 FROM ' . Utils::table('poll') . ' WHERE admin_id = ?');
|
2015-12-05 16:30:49 +01:00
|
|
|
|
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
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2015-04-03 00:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find old polls. Limit: 20.
|
|
|
|
*
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return array Array of old polls
|
|
|
|
*/
|
2018-04-23 10:27:15 +02:00
|
|
|
public function findOldPolls()
|
2018-04-18 16:16:22 +02:00
|
|
|
{
|
2018-04-23 10:27:15 +02:00
|
|
|
$prepared = $this->connect->executeQuery('SELECT * FROM ' . Utils::table('poll') . ' WHERE DATE_ADD(end_date, INTERVAL ? DAY) < NOW() AND end_date != 0 LIMIT 20', [PURGE_DELAY]);
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
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
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return array The found polls
|
|
|
|
*/
|
2015-09-13 00:53:03 +02:00
|
|
|
public function findAll($search, $start, $limit) {
|
2015-04-03 00:11:36 +02:00
|
|
|
// Polls
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$request = "";
|
|
|
|
$request .= "SELECT p.*,";
|
2018-04-18 16:16:22 +02:00
|
|
|
$request .= " (SELECT count(1) FROM " . Utils::table('vote') . " v WHERE p.id=v.poll_id) votes";
|
|
|
|
$request .= " FROM " . Utils::table('poll') . " p";
|
2018-04-02 09:12:51 +02:00
|
|
|
$request .= " WHERE 1";
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$values = [];
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
if (!empty($search["poll"])) {
|
|
|
|
$request .= " AND p.id LIKE :poll";
|
|
|
|
$values["poll"] = "{$search["poll"]}%";
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$fields = [
|
|
|
|
// key of $search => column name
|
|
|
|
"title" => "title",
|
|
|
|
"name" => "admin_name",
|
|
|
|
"mail" => "admin_mail",
|
|
|
|
];
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
foreach ($fields as $searchKey => $columnName) {
|
|
|
|
if (empty($search[$searchKey])) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$request .= " AND p.$columnName LIKE :$searchKey";
|
|
|
|
$values[$searchKey] = "%{$search[$searchKey]}%";
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$request .= " ORDER BY p.title ASC";
|
|
|
|
$request .= " LIMIT :start, :limit";
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
$prepared = $this->prepare($request);
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2018-04-02 09:12:51 +02:00
|
|
|
foreach ($values as $searchKey => $value) {
|
|
|
|
$prepared->bindParam(":$searchKey", $value, PDO::PARAM_STR);
|
|
|
|
}
|
2018-04-18 16:16:22 +02:00
|
|
|
|
2015-09-13 00:53:03 +02:00
|
|
|
$prepared->bindParam(':start', $start, PDO::PARAM_INT);
|
|
|
|
$prepared->bindParam(':limit', $limit, PDO::PARAM_INT);
|
2018-04-18 16:16:22 +02: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
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-08 21:52:09 +02:00
|
|
|
* @return array The list of matching polls
|
|
|
|
*/
|
|
|
|
public function findAllByAdminMail($mail) {
|
2018-04-18 16:16:22 +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
|
|
|
/**
|
2018-04-18 16:16:22 +02:00
|
|
|
* Get the total number of polls in database.
|
2015-04-03 00:11:36 +02:00
|
|
|
*
|
2015-09-13 00:53:03 +02:00
|
|
|
* @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>...]
|
2018-04-18 16:16:22 +02:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2015-04-03 00:11:36 +02:00
|
|
|
* @return int The number of polls
|
|
|
|
*/
|
2015-09-13 00:53:03 +02:00
|
|
|
public function count($search = null) {
|
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
|
2018-04-18 16:16:22 +02:00
|
|
|
FROM ' . Utils::table('poll') . ' p
|
2015-09-13 00:53:03 +02:00
|
|
|
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();
|
|
|
|
$count = $prepared->fetch();
|
|
|
|
|
|
|
|
/*echo '---';
|
|
|
|
print_r($count);
|
|
|
|
echo '---';
|
|
|
|
exit;*/
|
2015-04-03 00:11:36 +02:00
|
|
|
|
|
|
|
return $count->nb;
|
|
|
|
}
|
2015-04-02 23:10:41 +02:00
|
|
|
}
|