raspisms/controllers/publics/Scheduled.php

692 lines
27 KiB
PHP
Raw Permalink 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;
2023-02-20 03:17:53 +01:00
private $internal_phone_group;
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);
2023-02-20 03:17:53 +01:00
$this->internal_phone_group = new \controllers\internals\PhoneGroup($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.
2019-10-29 18:36:25 +01:00
*/
2020-04-07 03:02:33 +02:00
public function list()
2019-10-29 14:57:13 +01:00
{
$this->render('scheduled/list');
}
2021-01-14 03:32:17 +01:00
/**
2021-01-14 03:32:17 +01:00
* Return scheduleds as json.
*/
public function list_json()
{
$entities = $this->internal_scheduled->list_for_user($_SESSION['user']['id']);
foreach ($entities as &$entity)
{
if ($entity['mms'])
{
$entity['medias'] = $this->internal_media->gets_for_scheduled($entity['id']);
}
}
header('Content-Type: application/json');
echo json_encode(['data' => $entities]);
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.
2020-01-17 18:19:25 +01:00
*
2021-02-23 00:31:54 +01:00
* @param array? int $contacts_ids : Ids of contacts to prefilled
* @param array? int $groups_ids : Ids of groups to prefilled
* @param array? int $conditional_groups_ids : Ids of conditional groups to prefilled
* @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()
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);
2023-02-20 03:17:53 +01:00
$phone_groups = $this->internal_phone_group->gets_for_user($id_user);
2020-01-17 18:19:25 +01:00
2021-02-23 00:31:54 +01:00
$contact_ids = (isset($_GET['contact_ids']) && \is_array($_GET['contact_ids'])) ? $_GET['contact_ids'] : [];
$group_ids = (isset($_GET['group_ids']) && \is_array($_GET['group_ids'])) ? $_GET['group_ids'] : [];
$conditional_group_ids = (isset($_GET['conditional_group_ids']) && \is_array($_GET['conditional_group_ids'])) ? $_GET['conditional_group_ids'] : [];
$prefilled_contacts = [];
$prefilled_groups = [];
$prefilled_conditional_groups = [];
if ($contact_ids)
{
foreach ($this->internal_contact->gets_in_for_user($id_user, $contact_ids) as $contact)
{
$prefilled_contacts[] = $contact['id'];
}
}
elseif ($group_ids)
{
foreach ($this->internal_group->gets_in_for_user($id_user, $group_ids) as $group)
{
$prefilled_groups[] = $group['id'];
}
}
elseif ($conditional_group_ids)
{
foreach ($this->internal_conditional_group->gets_in_for_user($id_user, $conditional_group_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,
2023-02-20 03:17:53 +01:00
'phone_groups' => $phone_groups,
'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 !');
2020-01-17 18:19:25 +01:00
2019-11-12 17:37:20 +01:00
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']);
2023-02-20 03:17:53 +01:00
$phone_groups = $this->internal_phone_group->gets_for_user($id_user);
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)
{
$number['data'] = json_decode($number['data'] ?? '[]');
$scheduleds[$key]['numbers'][] = $number;
2019-10-29 14:57:13 +01:00
}
$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
2021-03-22 01:29:06 +01:00
$medias = $this->internal_media->gets_for_scheduled($scheduled['id']);
$scheduleds[$key]['medias'] = $medias;
2019-12-04 03:04:45 +01:00
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,
2023-02-20 03:17:53 +01:00
'phone_groups' => $phone_groups,
'contacts' => $all_contacts,
]);
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Create a new scheduled message
* (you must provide at least one entry in any of numbers, contacts, groups or conditional_groups).
*
2019-10-29 18:36:25 +01:00
* @param $csrf : Le jeton CSRF
* @param string $_POST['at'] : Date to send message for
* @param string $_POST['text'] : Text of the message
* @param ?bool $_POST['flash'] : Is the message a flash message (by default false)
2023-02-20 03:17:53 +01:00
* @param ?int $_POST['id_phone'] : Id of the phone or phone group to send message from. id will be preceed by phone_ of phonegroup_ depending on type of ressource to use, if null use random phone
* @param ?array $_POST['numbers'] : Numbers to send the message to
* @param ?array $_POST['contacts'] : Numbers to send the message to
* @param ?array $_POST['groups'] : Numbers to send the message to
* @param ?array $_POST['conditional_groups'] : Numbers to send the message to
2021-06-17 00:51:33 +02:00
* @param ?array $_FILES['medias'] : 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);
2023-02-24 16:29:10 +01:00
$tag = ($_POST['tag'] ?? null) ?: null;
$id_phone = empty($_POST['id_phone']) ? null : $_POST['id_phone'];
2019-10-29 14:57:13 +01:00
$numbers = $_POST['numbers'] ?? [];
$numbers = is_array($numbers) ? $numbers : [$numbers];
2019-10-29 14:57:13 +01:00
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
2019-11-28 01:55:11 +01:00
$conditional_groups = $_POST['conditional_groups'] ?? [];
2021-03-22 01:29:06 +01:00
$files = $_FILES['medias'] ?? false;
$csv_file = $_FILES['csv'] ?? false;
2021-03-22 01:29:06 +01:00
//Iterate over files to re-create individual $_FILES array
2021-06-17 00:51:33 +02:00
$files_arrays = [];
2021-03-22 01:29:06 +01:00
if ($files && is_array($files['name']))
{
foreach ($files as $property_name => $files_values)
{
foreach ($files_values as $file_key => $property_value)
{
if (!isset($files_arrays[$file_key]))
{
$files_arrays[$file_key] = [];
}
$files_arrays[$file_key][$property_name] = $property_value;
}
}
}
2021-06-17 00:51:33 +02:00
2021-03-22 01:29:06 +01:00
//Remove empty files input
foreach ($files_arrays as $key => $file)
{
2021-06-17 00:51:33 +02:00
if (UPLOAD_ERR_NO_FILE === $file['error'])
2021-03-22 01:29:06 +01:00
{
unset($files_arrays[$key]);
}
}
2019-10-29 14:57:13 +01:00
2022-03-19 22:56:56 +01:00
//Remove empty csv file input
if ($csv_file && UPLOAD_ERR_NO_FILE === $csv_file['error'])
{
$csv_file = false;
}
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 (empty($at))
{
\FlashMessage\FlashMessage::push('danger', 'Vous ne pouvez pas créer un Sms sans date.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
if (!is_string($at))
{
\FlashMessage\FlashMessage::push('danger', 'La date doit être valide.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
if (!is_string($text))
{
\FlashMessage\FlashMessage::push('danger', 'Votre message doit être un texte.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
2022-09-26 17:17:41 +02:00
if (mb_strlen($text) > \models\Scheduled::SMS_LENGTH_LIMIT)
{
\FlashMessage\FlashMessage::push('danger', 'Votre message doit faire moins de ' . \models\Scheduled::SMS_LENGTH_LIMIT . ' caractères.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
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
}
if ($csv_file)
{
$uploaded_file = \controllers\internals\Tool::read_uploaded_file($csv_file);
if (!$uploaded_file['success'])
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de traiter ce fichier CSV : ' . $uploaded_file['content']);
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
try
{
$csv_numbers = $this->internal_scheduled->parse_csv_numbers_file($uploaded_file['content']);
if (!$csv_numbers)
{
\FlashMessage\FlashMessage::push('danger', 'Aucun destinataire valide dans le fichier CSV, assurez-vous de fournir un fichier CSV valide.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$numbers = array_merge($csv_numbers, $numbers);
}
catch (\Exception $e)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de traiter ce fichier CSV : ' . $e->getMessage());
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
}
foreach ($numbers as $key => $number)
{
// If number is not an array turn it into an array
$number = is_array($number) ? $number : ['number' => $number, 'data' => []];
$number['data'] = $number['data'] ?? [];
$number['number'] = \controllers\internals\Tool::parse_phone($number['number'] ?? '');
2019-10-29 14:57:13 +01:00
if (!$number['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;
}
$clean_data = [];
foreach ($number['data'] as $data_key => $value)
{
if ('' === $value)
{
continue;
}
$data_key = mb_ereg_replace('[\W]', '', $data_key);
$clean_data[$data_key] = (string) $value;
}
$clean_data = json_encode($clean_data);
$number['data'] = $clean_data;
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.');
2020-01-17 18:19:25 +01:00
2019-11-12 17:37:20 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
2021-06-17 00:51:33 +02:00
2021-03-22 01:29:06 +01:00
//If mms is enable and we have medias uploaded
$media_ids = [];
if ($_SESSION['user']['settings']['mms'] && $files_arrays)
2019-12-04 03:04:45 +01:00
{
2021-03-22 01:29:06 +01:00
foreach ($files_arrays as $file)
{
try
2021-03-22 01:29:06 +01:00
{
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
}
catch (\Exception $e)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name'] . ':' . $e->getMessage());
2021-06-17 00:51:33 +02:00
2021-03-22 01:29:06 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
2020-01-17 18:19:25 +01:00
2021-06-17 00:51:33 +02:00
$media_ids[] = $new_media_id;
2021-03-22 01:29:06 +01:00
}
2019-12-04 03:04:45 +01:00
}
2021-03-22 01:29:06 +01:00
$mms = (bool) count($media_ids);
2021-06-17 00:51:33 +02:00
2023-02-20 03:17:53 +01:00
// Check if we must send message to a phone or a phone_group based on if id_phone start with 'phone_' or 'phonegroup_'
$id_phone_group = null;
if ($id_phone)
{
$id_phone_group = str_starts_with($id_phone, 'phonegroup_') ? mb_substr($id_phone, mb_strlen('phonegroup_')) : null;
$id_phone = str_starts_with($id_phone, 'phone_') ? mb_substr($id_phone, mb_strlen('phone_')) : null;
}
2023-02-20 03:17:53 +01:00
2023-02-24 16:29:10 +01:00
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $id_phone, $id_phone_group, $flash, $mms, $tag, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
2021-03-22 01:29:06 +01:00
if (!$scheduled_id)
2019-12-04 03:04:45 +01:00
{
2021-03-22 01:29:06 +01:00
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
2020-01-17 18:19:25 +01:00
2021-03-22 01:29:06 +01:00
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
2019-12-04 03:04:45 +01:00
}
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le ' . $at . '.');
2020-01-17 18:19:25 +01:00
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;
$id_phone = empty($scheduled['id_phone']) ? null : $scheduled['id_phone'];
$flash = (bool) ($scheduled['flash'] ?? false);
2023-02-24 16:29:10 +01:00
$tag = ($scheduled['tag'] ?? null) ?: null;
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'] ?? [];
2021-03-22 01:29:06 +01:00
$files = $_FILES['scheduleds_' . $id_scheduled . '_medias'] ?? false;
$csv_file = $_FILES['scheduleds_' . $id_scheduled . '_csv'] ?? false;
2021-03-22 01:29:06 +01:00
$media_ids = $scheduled['media_ids'] ?? [];
2019-10-29 14:57:13 +01:00
2021-03-22 01:29:06 +01:00
//Check scheduled exists and belong to user
2019-11-12 17:37:20 +01:00
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
{
continue;
}
2020-01-17 18:19:25 +01:00
2021-03-22 01:29:06 +01:00
//Iterate over files to re-create individual $_FILES array
2021-06-17 00:51:33 +02:00
$files_arrays = [];
2021-03-22 01:29:06 +01:00
if ($files && is_array($files['name']))
{
foreach ($files as $property_name => $files_values)
{
foreach ($files_values as $file_key => $property_value)
{
if (!isset($files_arrays[$file_key]))
{
$files_arrays[$file_key] = [];
}
$files_arrays[$file_key][$property_name] = $property_value;
}
}
}
//Remove empty files input
foreach ($files_arrays as $key => $file)
{
2021-06-17 00:51:33 +02:00
if (UPLOAD_ERR_NO_FILE === $file['error'])
2021-03-22 01:29:06 +01:00
{
unset($files_arrays[$key]);
}
}
2022-03-19 22:56:56 +01:00
//Remove empty csv file input
if ($csv_file && UPLOAD_ERR_NO_FILE === $csv_file['error'])
{
$csv_file = false;
}
if (empty($text))
{
2019-10-29 14:57:13 +01:00
continue;
}
if (empty($at))
{
\FlashMessage\FlashMessage::push('danger', 'Vous ne pouvez pas créer un Sms sans date.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
if (!is_string($at))
{
\FlashMessage\FlashMessage::push('danger', 'La date doit être valide.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
if (!is_string($text))
{
\FlashMessage\FlashMessage::push('danger', 'Votre message doit être un texte.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
2022-09-26 17:17:41 +02:00
if (mb_strlen($text) > \models\Scheduled::SMS_LENGTH_LIMIT)
{
\FlashMessage\FlashMessage::push('danger', 'Votre message doit faire moins de ' . \models\Scheduled::SMS_LENGTH_LIMIT . ' caractères.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
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;
}
if ($csv_file)
{
$uploaded_file = \controllers\internals\Tool::read_uploaded_file($csv_file);
if (!$uploaded_file['success'])
{
continue;
}
try
{
$csv_numbers = $this->internal_scheduled->parse_csv_numbers_file($uploaded_file['content']);
if (!$csv_numbers)
{
continue;
}
$numbers = array_merge($csv_numbers, $numbers);
}
catch (\Exception $e)
{
continue;
}
}
$numbers = is_array($numbers) ? $numbers : [$numbers];
foreach ($numbers as $key => $number)
{
// If number is not an array turn it into an array
$number = is_array($number) ? $number : ['number' => $number, 'data' => []];
$number['data'] = $number['data'] ?? [];
$number['number'] = \controllers\internals\Tool::parse_phone($number['number'] ?? '');
if (!$number['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;
}
$clean_data = [];
foreach ($number['data'] as $data_key => $value)
{
if ('' === $value)
{
continue;
}
$data_key = mb_ereg_replace('[\W]', '', $data_key);
$clean_data[$data_key] = (string) $value;
}
$clean_data = json_encode($clean_data);
$number['data'] = $clean_data;
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;
}
2021-06-17 00:51:33 +02:00
2021-03-22 01:29:06 +01:00
//If mms is enable and we have medias uploaded
if ($_SESSION['user']['settings']['mms'] && $files_arrays)
{
2021-03-22 01:29:06 +01:00
foreach ($files_arrays as $file)
{
try
{
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
}
catch (\Exception $e)
2021-03-22 01:29:06 +01:00
{
continue 2;
}
2019-11-10 17:36:42 +01:00
2021-06-17 00:51:33 +02:00
$media_ids[] = $new_media_id;
2021-03-22 01:29:06 +01:00
}
2019-10-29 14:57:13 +01:00
}
2019-12-04 03:04:45 +01:00
2021-03-22 01:29:06 +01:00
//Ensure media_ids point to medias belongings to the current user
foreach ($media_ids as $key => $media_id)
2019-12-04 03:04:45 +01:00
{
2021-03-22 01:29:06 +01:00
$media = $this->internal_media->get($media_id);
if (!$media || $media['id_user'] !== $_SESSION['user']['id'])
{
unset($media_ids[$key]);
}
2019-12-04 03:04:45 +01:00
}
2021-03-22 01:29:06 +01:00
$mms = (bool) count($media_ids);
$id_phone_group = null;
if ($id_phone)
{
$id_phone_group = str_starts_with($id_phone, 'phonegroup_') ? mb_substr($id_phone, mb_strlen('phonegroup_')) : null;
$id_phone = str_starts_with($id_phone, 'phone_') ? mb_substr($id_phone, mb_strlen('phone_')) : null;
}
2023-02-24 16:29:10 +01:00
$this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $id_phone, $id_phone_group, $flash, $mms, $tag, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
2021-06-17 00:51:33 +02:00
++$nb_update;
2019-10-29 14:57:13 +01:00
}
2020-01-17 18:19:25 +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.');
2020-01-17 18:19:25 +01:00
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.');
2020-01-17 18:19:25 +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
}