2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace models;
|
2019-10-29 18:33:49 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette classe gère les accès bdd pour les scheduledes.
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
|
|
|
class Scheduled extends \descartes\Model
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une entrée par son id.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id : L'id de l'entrée
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : L'entrée
|
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function get($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$scheduleds = $this->_select('scheduled', ['id' => $id]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return isset($scheduleds[0]) ? $scheduleds[0] : false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une liste de scheduledes sous forme d'un tableau.
|
|
|
|
*
|
|
|
|
* @param int $limit : Nombre de résultat maximum à retourner
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $offset : Nombre de résultat à ingnorer
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function list($limit, $offset)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-10 00:27:42 +01:00
|
|
|
return $this->_select('scheduled', [], null, false, $limit, $offset);
|
2019-10-29 18:33:49 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une liste de scheduledes sous forme d'un tableau.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $ids : un ou plusieurs id d'entrées à récupérer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : La liste des entrées
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function gets($ids)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT * FROM scheduled
|
2019-10-30 00:30:39 +01:00
|
|
|
WHERE id ';
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
|
|
|
|
$generated_in = $this->_generate_in_from_array($ids);
|
2019-10-29 14:57:13 +01:00
|
|
|
$query .= $generated_in['QUERY'];
|
|
|
|
$params = $generated_in['PARAMS'];
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 18:32:07 +01:00
|
|
|
/**
|
2019-11-06 20:34:26 +01:00
|
|
|
* Get scheduleds message not in progress and before a date.
|
|
|
|
*
|
2019-11-06 18:32:07 +01:00
|
|
|
* @param string $date : Limit date
|
2019-11-06 20:34:26 +01:00
|
|
|
*
|
2019-11-06 18:32:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-06 20:34:26 +01:00
|
|
|
public function get_scheduleds_not_in_progress_before($date)
|
2019-11-06 18:32:07 +01:00
|
|
|
{
|
|
|
|
return $this->_select('scheduleds', ['<=at' => $date, 'progress' => false]);
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne les messages programmés avant une date et pour un numéro.
|
|
|
|
*
|
|
|
|
* @param \DateTime $date : La date avant laquelle on veux le message
|
|
|
|
* @param string $number : Le numéro
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : Les messages programmés avant la date
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function get_before_date_for_number($date, $number)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT *
|
|
|
|
FROM scheduled
|
|
|
|
WHERE at <= :date
|
|
|
|
AND (
|
|
|
|
id IN (
|
|
|
|
SELECT id_scheduled
|
|
|
|
FROM scheduled_number
|
|
|
|
WHERE number = :number
|
|
|
|
)
|
|
|
|
OR id IN (
|
|
|
|
SELECT id_scheduled
|
|
|
|
FROM scheduled_contact
|
|
|
|
WHERE id_contact IN (
|
|
|
|
SELECT id
|
|
|
|
FROM contact
|
|
|
|
WHERE number = :number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
OR id IN (
|
|
|
|
SELECT id_scheduled
|
2019-10-30 00:17:10 +01:00
|
|
|
FROM scheduled_group
|
2019-10-29 14:57:13 +01:00
|
|
|
WHERE id_group IN (
|
|
|
|
SELECT id_group
|
2019-11-10 17:32:59 +01:00
|
|
|
FROM `group_contact`
|
2019-10-29 14:57:13 +01:00
|
|
|
WHERE id_contact IN (
|
|
|
|
SELECT id
|
|
|
|
FROM contact
|
|
|
|
WHERE number = :number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-10-30 00:30:39 +01:00
|
|
|
';
|
|
|
|
|
|
|
|
$params = [
|
2019-10-29 14:57:13 +01:00
|
|
|
'date' => $date,
|
|
|
|
'number' => $number,
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une liste de scheduledes sous forme d'un tableau.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $ids : un ou plusieurs id d'entrées à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed $id
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nombre de lignes supprimées
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function delete($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
DELETE FROM scheduled
|
2019-10-30 00:30:39 +01:00
|
|
|
WHERE id = :id';
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
|
2019-10-29 14:57:13 +01:00
|
|
|
$params = ['id' => $id];
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Insert une schedulede.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $scheduled : La schedulede à insérer avec les champs name, script, admin & admin
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return mixed bool|int : false si echec, sinon l'id de la nouvelle lignée insérée
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function insert($scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$result = $this->_insert('scheduled', $scheduled);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_last_id();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Met à jour une schedulede par son id.
|
|
|
|
*
|
|
|
|
* @param int $id : L'id de la scheduled à modifier
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $scheduled : Les données à mettre à jour pour la schedulede
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : le nombre de ligne modifiées
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function update($id, $scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_update('scheduled', $scheduled, ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Compte le nombre d'entrées dans la table.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nombre d'entrées
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function count()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_count('scheduled');
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne une liste de numéro pour un scheduled.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel on veux le numéro
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : Les numéros des scheduled
|
|
|
|
*/
|
2019-11-07 16:17:18 +01:00
|
|
|
public function get_numbers($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_select('scheduled_number', ['id_scheduled' => $id_scheduled]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne une liste de contact pour un scheduled.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel on veux le numéro
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : Les contact du scheduled
|
|
|
|
*/
|
2019-11-07 16:17:18 +01:00
|
|
|
public function get_contacts($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$query = 'SELECT * FROM contact WHERE id IN (SELECT id_contact FROM scheduled_contact WHERE id_scheduled = :id_scheduled)';
|
|
|
|
|
|
|
|
$params = ['id_scheduled' => $id_scheduled];
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne une liste de groups pour un scheduled.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel on veux le numéro
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return array : Les groups du scheduled
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-07 16:17:18 +01:00
|
|
|
public function get_groups($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-10 00:39:34 +01:00
|
|
|
$query = 'SELECT * FROM `group` WHERE id IN (SELECT id_group FROM scheduled_group WHERE id_scheduled = :id_scheduled)';
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$params = ['id_scheduled' => $id_scheduled];
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Insert un liens scheduled/number.
|
|
|
|
*
|
|
|
|
* @param int $id_scheduled : L'id du scheduled
|
|
|
|
* @param string $number : Le numéro à lier
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : le nombre d'entrées
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function insert_scheduled_number($id_scheduled, $number)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$result = $this->_insert('scheduled_number', ['id_scheduled' => $id_scheduled, 'number' => $number]);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_last_id();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Insert un liens scheduled/contact.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param int $id_contact : L'id du contact
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : le nombre d'entrées
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function insert_scheduled_contact($id_scheduled, $id_contact)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$result = $this->_insert('scheduled_contact', ['id_scheduled' => $id_scheduled, 'id_contact' => $id_contact]);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_last_id();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Insert un liens scheduled/group.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param int $id_group : L'id du group
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : le nombre d'entrées
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function insert_scheduled_group($id_scheduled, $id_group)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$result = $this->_insert('scheduled_group', ['id_scheduled' => $id_scheduled, 'id_group' => $id_group]);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_last_id();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Supprime les liens scheduled/number pour un scheduled précis.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nmbre d'entrées modifiées
|
|
|
|
*/
|
2019-11-06 17:22:54 +01:00
|
|
|
public function delete_scheduled_numbers($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_delete('scheduled_number', ['id_scheduled' => $id_scheduled]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Supprime les liens scheduled/contact pour un scheduled précis.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nmbre d'entrées modifiées
|
|
|
|
*/
|
2019-11-06 17:22:54 +01:00
|
|
|
public function delete_scheduled_contacts($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_delete('scheduled_contact', ['id_scheduled' => $id_scheduled]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Supprime les liens scheduled/group pour un scheduled précis.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id_scheduled : L'id du scheduled pour lequel supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nmbre d'entrées modifiées
|
|
|
|
*/
|
2019-11-06 17:22:54 +01:00
|
|
|
public function delete_scheduled_groups($id_scheduled)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->_delete('scheduled_group', ['id_scheduled' => $id_scheduled]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
}
|