raspisms/controllers/publics/Scheduled.php

425 lines
16 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
2019-10-29 14:57:13 +01:00
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
/**
* Page des scheduleds.
2019-10-29 18:36:25 +01:00
*/
class Scheduled extends \descartes\Controller
{
private $internal_scheduled;
2019-11-12 17:37:20 +01:00
private $internal_phone;
private $internal_contact;
private $internal_group;
private $internal_conditional_group;
2019-12-04 03:04:45 +01:00
private $internal_media;
2019-10-29 18:36:25 +01:00
/**
* Cette fonction est appelée avant toute les autres :
* Elle vérifie que l'utilisateur est bien connecté.
*
2019-10-29 18:36:25 +01:00
* @return void;
*/
public function __construct()
2019-10-29 14:57:13 +01:00
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
2019-11-12 17:37:20 +01:00
$this->internal_phone = new \controllers\internals\Phone($bdd);
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_group = new \controllers\internals\Group($bdd);
$this->internal_conditional_group = new \controllers\internals\ConditionalGroup($bdd);
2019-12-04 03:04:45 +01:00
$this->internal_media = new \controllers\internals\Media($bdd);
2019-10-29 14:57:13 +01:00
\controllers\internals\Tool::verifyconnect();
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne tous les scheduleds, sous forme d'un tableau permettant l'administration de ces scheduleds.
*
* @param mixed $page
2019-10-29 18:36:25 +01:00
*/
public function list($page = 0)
2019-10-29 14:57:13 +01:00
{
$page = (int) $page;
2019-11-14 22:33:00 +01:00
$scheduleds = $this->internal_scheduled->list_for_user($_SESSION['user']['id'], 25, $page);
2019-10-29 14:57:13 +01:00
$this->render('scheduled/list', ['scheduleds' => $scheduleds]);
2019-10-29 18:36:25 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction va supprimer une liste de scheduleds.
*
2019-10-29 14:57:13 +01:00
* @param array int $_GET['ids'] : Les id des scheduledes à supprimer
* @param mixed $csrf
*
2019-10-29 14:57:13 +01:00
* @return boolean;
*/
2019-10-29 18:36:25 +01:00
public function delete($csrf)
2019-10-29 14:57:13 +01:00
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
2019-11-12 17:37:20 +01:00
$scheduled = $this->internal_scheduled->get($id);
if (!$scheduled || $scheduled['id_user'] !== $_SESSION['user']['id'])
{
continue;
}
2019-11-14 22:33:00 +01:00
$this->internal_scheduled->delete_for_user($_SESSION['user']['id'], $id);
2019-10-29 14:57:13 +01:00
}
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne la page d'ajout d'un scheduled.
* @param $prefilled : If we have prefilled some fields (possible values : 'contacts', 'groups', 'conditional_groups', false)
2019-10-29 18:36:25 +01:00
*/
public function add($prefilled = false)
2019-10-29 14:57:13 +01:00
{
$now = new \DateTime();
$less_one_minute = new \DateInterval('PT1M');
$now->sub($less_one_minute);
$id_user = $_SESSION['user']['id'];
$contacts = $this->internal_contact->gets_for_user($id_user);
$phones = $this->internal_phone->gets_for_user($id_user);
$prefilled_contacts = [];
$prefilled_groups = [];
$prefilled_conditional_groups = [];
if ($prefilled)
{
$ids = $_GET['ids'] ?? [];
}
if ($prefilled === 'contacts')
{
foreach ($this->internal_contact->gets_in_for_user($id_user, $ids) as $contact)
{
$prefilled_contacts[] = $contact['id'];
}
}
elseif ($prefilled === 'groups')
{
foreach ($this->internal_group->gets_in_for_user($id_user, $ids) as $group)
{
$prefilled_groups[] = $group['id'];
}
}
elseif ($prefilled === 'conditional_groups')
{
foreach ($this->internal_conditional_group->gets_in_for_user($id_user, $ids) as $conditional_group)
{
$prefilled_conditional_groups[] = $conditional_group['id'];
}
}
2019-11-12 17:37:20 +01:00
2019-10-29 14:57:13 +01:00
$this->render('scheduled/add', [
'now' => $now->format('Y-m-d H:i'),
'contacts' => $contacts,
2019-11-12 17:37:20 +01:00
'phones' => $phones,
'prefilled_contacts' => $prefilled_contacts,
'prefilled_groups' => $prefilled_groups,
'prefilled_conditional_groups' => $prefilled_conditional_groups,
2019-10-29 14:57:13 +01:00
]);
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne la page d'édition des scheduleds.
*
2019-10-29 18:36:25 +01:00
* @param int... $ids : Les id des scheduledes à supprimer
*/
public function edit()
2019-10-29 14:57:13 +01:00
{
$ids = $_GET['ids'] ?? [];
2019-11-12 17:37:20 +01:00
if (!$ids)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez choisir des messages à mettre à jour !');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
2019-12-04 03:04:45 +01:00
$id_user = $_SESSION['user']['id'];
$all_contacts = $this->internal_contact->gets_for_user($_SESSION['user']['id']);
2019-11-12 17:37:20 +01:00
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
2019-11-14 22:33:00 +01:00
$scheduleds = $this->internal_scheduled->gets_in_for_user($_SESSION['user']['id'], $ids);
2019-10-29 14:57:13 +01:00
//Pour chaque message on ajoute les numéros, les contacts & les groups
foreach ($scheduleds as $key => $scheduled)
{
2019-11-12 17:37:20 +01:00
if (!$scheduled || $scheduled['id_user'] !== $_SESSION['user']['id'])
{
continue;
}
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['numbers'] = [];
$scheduleds[$key]['contacts'] = [];
$scheduleds[$key]['groups'] = [];
2019-11-28 01:55:11 +01:00
$scheduleds[$key]['conditional_groups'] = [];
2019-10-29 14:57:13 +01:00
$numbers = $this->internal_scheduled->get_numbers($scheduled['id']);
foreach ($numbers as $number)
{
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['numbers'][] = $number['number'];
}
$contacts = $this->internal_scheduled->get_contacts($scheduled['id']);
foreach ($contacts as $contact)
{
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['contacts'][] = (int) $contact['id'];
}
$groups = $this->internal_scheduled->get_groups($scheduled['id']);
foreach ($groups as $group)
{
$scheduleds[$key]['groups'][] = (int) $group['id'];
2019-10-29 14:57:13 +01:00
}
2019-11-28 01:55:11 +01:00
2019-12-04 03:04:45 +01:00
$media = $this->internal_media->get_for_scheduled_and_user($id_user, $scheduled['id']);
$scheduleds[$key]['media'] = $media;
2019-11-28 01:55:11 +01:00
$conditional_groups = $this->internal_scheduled->get_conditional_groups($scheduled['id']);
foreach ($conditional_groups as $conditional_group)
{
$scheduleds[$key]['conditional_groups'][] = (int) $conditional_group['id'];
}
2019-10-29 14:57:13 +01:00
}
$this->render('scheduled/edit', [
2019-10-29 14:57:13 +01:00
'scheduleds' => $scheduleds,
2019-11-12 17:37:20 +01:00
'phones' => $phones,
'contacts' => $all_contacts,
]);
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction insert un nouveau scheduled.
*
2019-10-29 18:36:25 +01:00
* @param $csrf : Le jeton CSRF
* @param string $_POST['name'] : Le nom du scheduled
* @param string $_POST['date'] : La date d'envoie du scheduled
* @param string $_POST['numbers'] : Les numeros de téléphone du scheduled
2019-10-29 18:36:25 +01:00
* @param string $_POST['contacts'] : Les contacts du scheduled
* @param string $_POST['groups'] : Les groups du scheduled
2019-12-04 03:04:45 +01:00
* @param array $_FILES['media'] : The media to link to a scheduled
2019-10-29 18:36:25 +01:00
*/
public function create($csrf)
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-11-12 17:37:20 +01:00
$id_user = $_SESSION['user']['id'];
$at = $_POST['at'] ?? false;
$text = $_POST['text'] ?? false;
$flash = (bool) ($_POST['flash'] ?? false);
2019-11-12 17:37:20 +01:00
$origin = empty($_POST['origin']) ? null : $_POST['origin'];
2019-10-29 14:57:13 +01:00
$numbers = $_POST['numbers'] ?? [];
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
2019-11-28 01:55:11 +01:00
$conditional_groups = $_POST['conditional_groups'] ?? [];
2019-10-29 14:57:13 +01:00
if (empty($text))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Vous ne pouvez pas créer un Sms sans message.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
if (!\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i:s') && !\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i'))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Vous devez fournir une date valide.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
foreach ($numbers as $key => $number)
{
2019-10-29 14:57:13 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
{
2019-10-29 14:57:13 +01:00
unset($numbers[$key]);
2019-11-10 17:36:42 +01:00
2019-10-29 14:57:13 +01:00
continue;
}
2019-10-29 18:36:25 +01:00
$numbers[$key] = $number;
2019-10-29 14:57:13 +01:00
}
2019-11-28 01:55:11 +01:00
if (!$numbers && !$contacts && !$groups && !$conditional_groups)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner au moins un destinataire pour le Sms.');
2019-11-12 17:37:20 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
2019-11-10 17:36:42 +01:00
if ($origin && !$this->internal_phone->get_by_number_and_user($id_user, $origin))
2019-11-12 17:37:20 +01:00
{
\FlashMessage\FlashMessage::push('danger', 'Ce numéro n\'existe pas ou vous n\'en êtes pas propriétaire.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-11-12 17:37:20 +01:00
2019-11-28 01:55:11 +01:00
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $origin, $flash, $numbers, $contacts, $groups, $conditional_groups);
if (!$scheduled_id)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-11-10 17:36:42 +01:00
2019-12-04 03:04:45 +01:00
//If mms is enabled, try to process a media to link to the scheduled
$media = $_FILES['media'] ?? false;
if (!($_SESSION['user']['settings']['mms'] ?? false) || !$media)
{
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le '.$at.'.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
$success = $this->internal_media->create($id_user, $scheduled_id, $media);
if (!$success)
{
\FlashMessage\FlashMessage::push('success', 'Le SMS a bien été créé mais le média n\'as pas pu être enregistré.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le '.$at.'.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
2019-12-04 03:04:45 +01:00
* Cette fonction met à jour un scheduled.
*
2019-10-29 14:57:13 +01:00
* @param $csrf : Le jeton CSRF
* @param array $_POST['scheduleds'] : Un tableau des scheduledes avec leur nouvelle valeurs + les numbers, contacts et groups liées
*
2019-10-29 14:57:13 +01:00
* @return boolean;
*/
public function update($csrf)
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 14:57:13 +01:00
$scheduleds = $_POST['scheduleds'] ?? [];
2019-12-04 03:04:45 +01:00
$nb_update = 0;
foreach ($scheduleds as $id_scheduled => $scheduled)
{
2019-11-12 17:37:20 +01:00
$id_user = $_SESSION['user']['id'];
$at = $scheduled['at'] ?? false;
$text = $scheduled['text'] ?? false;
2019-11-12 17:37:20 +01:00
$origin = empty($scheduled['origin']) ? null : $scheduled['origin'];
$flash = (bool) ($scheduled['flash'] ?? false);
2019-10-29 14:57:13 +01:00
$numbers = $scheduled['numbers'] ?? [];
$contacts = $scheduled['contacts'] ?? [];
$groups = $scheduled['groups'] ?? [];
2019-11-28 01:55:11 +01:00
$conditional_groups = $scheduled['conditional_groups'] ?? [];
2019-10-29 14:57:13 +01:00
2019-11-12 17:37:20 +01:00
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
{
continue;
}
if (empty($text))
{
2019-10-29 14:57:13 +01:00
continue;
}
if (!\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i:s') && !\controllers\internals\Tool::validate_date($at, 'Y-m-d H:i'))
{
2019-10-29 14:57:13 +01:00
continue;
}
foreach ($numbers as $key => $number)
{
2019-10-29 14:57:13 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
{
2019-10-29 14:57:13 +01:00
unset($numbers[$key]);
2019-11-10 17:36:42 +01:00
2019-10-29 14:57:13 +01:00
continue;
}
2019-10-29 18:36:25 +01:00
$numbers[$key] = $number;
2019-10-29 14:57:13 +01:00
}
2019-11-28 01:55:11 +01:00
if (!$numbers && !$contacts && !$groups && !$conditional_groups)
{
2019-10-29 14:57:13 +01:00
continue;
}
2019-11-12 17:37:20 +01:00
if ($origin && !$this->internal_phone->get_by_number_and_user($id_user, $origin))
2019-11-12 17:37:20 +01:00
{
2019-12-04 03:04:45 +01:00
continue;
2019-11-12 17:37:20 +01:00
}
2019-10-29 14:57:13 +01:00
2019-11-28 01:55:11 +01:00
$success = $this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $origin, $flash, $numbers, $contacts, $groups, $conditional_groups);
2019-12-04 03:04:45 +01:00
//Check for media
2019-12-04 05:08:46 +01:00
/*
2019-12-04 03:04:45 +01:00
$current_media = $scheduled['current_media'] ?? false;
if (!$current_media)
{
2019-12-04 03:04:45 +01:00
$this->internal_media->delete_for_scheduled_and_user($id_user, $id_scheduled);
}
2019-11-10 17:36:42 +01:00
2019-12-04 03:04:45 +01:00
$media = $_FILES['media_' . $id_scheduled] ?? false;
if (!$media)
{
$nb_update += (int) $success;
2019-10-29 14:57:13 +01:00
continue;
}
2019-12-04 03:04:45 +01:00
$success = $this->internal_media->create($id_user, $id_scheduled, $media);
if (!$success)
{
continue;
}
2019-12-04 05:08:46 +01:00
*/
2019-12-04 03:04:45 +01:00
$nb_update += 1;
2019-10-29 14:57:13 +01:00
}
2019-12-04 05:08:46 +01:00
if ($nb_update != count($scheduleds))
{
2019-12-04 03:04:45 +01:00
\FlashMessage\FlashMessage::push('danger', 'Certains SMS n\'ont pas été mis à jour.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('success', 'Tous les SMS ont été mis à jour.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
}