2015-04-02 23:23:34 +02:00
|
|
|
<?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
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphael DROZ
|
2016-08-04 22:26:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2015-04-02 23:23:34 +02:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
2015-04-02 23:23:34 +02:00
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphael DROZ
|
2015-04-02 23:23:34 +02:00
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
|
|
|
namespace Framadate\Repositories;
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
use Framadate\FramaDB;
|
|
|
|
use Framadate\Utils;
|
|
|
|
|
2015-04-02 23:23:34 +02:00
|
|
|
class SlotRepository extends AbstractRepository {
|
|
|
|
/**
|
|
|
|
* Insert a bulk of slots.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id
|
2015-04-02 23:23:34 +02:00
|
|
|
* @param array $choices
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function insertSlots(string $poll_id, array $choices): void
|
|
|
|
{
|
2015-04-02 23:23:34 +02:00
|
|
|
$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)) {
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id, $choice->getName(), null]);
|
2015-04-02 23:23:34 +02:00
|
|
|
} else {
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id, $choice->getName(), $joinedSlots]);
|
2015-04-02 23:23:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function listByPollId(string $poll_id) {
|
2015-05-30 23:36:04 +02:00
|
|
|
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY id');
|
2018-02-19 00:18:43 +01:00
|
|
|
$prepared->execute([$poll_id]);
|
2015-04-02 23:23:34 +02:00
|
|
|
|
|
|
|
return $prepared->fetchAll();
|
|
|
|
}
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
/**
|
|
|
|
* Find the slot into poll for a given datetime.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the poll
|
2015-04-03 00:11:36 +02:00
|
|
|
* @param $datetime int The datetime of the slot
|
|
|
|
* @return mixed Object The slot found, or null
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function findByPollIdAndDatetime(string $poll_id, $datetime) {
|
2015-04-03 00:11:36 +02:00
|
|
|
$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.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the poll
|
2015-04-03 00:11:36 +02:00
|
|
|
* @param $title mixed The title of the slot
|
|
|
|
* @param $moments mixed|null The moments joined with ","
|
|
|
|
* @return bool true if action succeeded
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function insert(string $poll_id, string $title, ?string $moments): bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$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.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the poll
|
2015-04-03 00:11:36 +02:00
|
|
|
* @param $datetime int The datetime of the slot to update
|
|
|
|
* @param $newMoments mixed The new moments
|
|
|
|
* @return bool|null true if action succeeded.
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function update(string $poll_id, $datetime, $newMoments): ?bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$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.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id int The ID of the poll
|
2015-04-03 00:11:36 +02:00
|
|
|
* @param $datetime mixed The datetime of the slot
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteByDateTime(string $poll_id, $datetime): void
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('DELETE FROM `' . Utils::table('slot') . '` WHERE poll_id = ? AND title = ?');
|
|
|
|
$prepared->execute([$poll_id, $datetime]);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function deleteByPollId(string $poll_id): bool
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$prepared = $this->prepare('DELETE FROM `' . Utils::table('slot') . '` WHERE poll_id = ?');
|
|
|
|
|
|
|
|
return $prepared->execute([$poll_id]);
|
|
|
|
}
|
2015-04-02 23:23:34 +02:00
|
|
|
}
|