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.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
namespace controllers\internals;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
class Scheduled extends StandardController
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a scheduled.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_user : User to insert scheduled for
|
|
|
|
* @param $at : Scheduled date to send
|
2020-03-04 01:40:47 +01:00
|
|
|
* @param string $text : Text of the message
|
|
|
|
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
|
|
|
* @param bool $flash : Is the sms a flash sms, by default false
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a mms, by default false
|
2020-03-04 01:40:47 +01:00
|
|
|
* @param array $numbers : Numbers to send message to
|
|
|
|
* @param array $contacts_ids : Contact ids to send message to
|
|
|
|
* @param array $groups_ids : Group ids to send message to
|
|
|
|
* @param array $conditional_group_ids : Conditional Groups ids to send message to
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param array $media_ids : Ids of the medias to link to scheduled message
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return bool : false on error, new id on success
|
2019-11-12 17:37:20 +01:00
|
|
|
*/
|
2021-03-19 02:45:12 +01:00
|
|
|
public function create(int $id_user, $at, string $text, ?int $id_phone = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
2019-11-12 17:37:20 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$scheduled = [
|
2019-11-12 17:37:20 +01:00
|
|
|
'id_user' => $id_user,
|
2019-11-10 17:36:42 +01:00
|
|
|
'at' => $at,
|
2019-11-10 16:53:47 +01:00
|
|
|
'text' => $text,
|
2020-03-04 01:40:47 +01:00
|
|
|
'id_phone' => $id_phone,
|
2019-11-10 16:53:47 +01:00
|
|
|
'flash' => $flash,
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $mms,
|
2019-11-10 16:53:47 +01:00
|
|
|
];
|
|
|
|
|
2020-03-04 05:10:45 +01:00
|
|
|
if (null !== $id_phone)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
$internal_phone = new Phone($this->bdd);
|
2020-03-04 01:40:47 +01:00
|
|
|
$find_phone = $internal_phone->get_for_user($id_user, $id_phone);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
if (!$find_phone)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
//Use transaction to garanty atomicity
|
|
|
|
$this->bdd->beginTransaction();
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
$id_scheduled = $this->get_model()->insert($scheduled);
|
|
|
|
if (!$id_scheduled)
|
|
|
|
{
|
2021-03-19 02:45:12 +01:00
|
|
|
$this->bdd->rollBack();
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$internal_media = new Media($this->bdd);
|
|
|
|
foreach ($media_ids as $media_id)
|
|
|
|
{
|
|
|
|
$id_media_scheduled = $internal_media->link_to($media_id, 'scheduled', $id_scheduled);
|
|
|
|
if (!$id_media_scheduled)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($numbers as $number)
|
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$internal_contact = new Contact($this->bdd);
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($contacts_ids as $contact_id)
|
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
$find_contact = $internal_contact->get_for_user($id_user, $contact_id);
|
2019-11-14 02:02:50 +01:00
|
|
|
if (!$find_contact)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$this->get_model()->insert_scheduled_contact_relation($id_scheduled, $contact_id);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$internal_group = new Group($this->bdd);
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($groups_ids as $group_id)
|
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
$find_group = $internal_group->get_for_user($id_user, $group_id);
|
2019-11-14 02:02:50 +01:00
|
|
|
if (!$find_group)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
$internal_conditional_group = new ConditionalGroup($this->bdd);
|
|
|
|
foreach ($conditional_group_ids as $conditional_group_id)
|
|
|
|
{
|
|
|
|
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
|
|
|
|
if (!$find_group)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$success = $this->bdd->commit();
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:40:47 +01:00
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
$internal_event = new Event($this->bdd);
|
|
|
|
$internal_event->create($id_user, 'SCHEDULED_ADD', 'Ajout d\'un Sms pour le ' . $date . '.');
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return $id_scheduled;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a scheduled.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User to insert scheduled for
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_scheduled : Scheduled id
|
|
|
|
* @param $at : Scheduled date to send
|
2020-03-04 01:40:47 +01:00
|
|
|
* @param string $text : Text of the message
|
|
|
|
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
|
|
|
* @param bool $flash : Is the sms a flash sms, by default false
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a mms, by default false
|
2020-03-04 01:40:47 +01:00
|
|
|
* @param array $numbers : Numbers to send message to
|
|
|
|
* @param array $contacts_ids : Contact ids to send message to
|
|
|
|
* @param array $groups_ids : Group ids to send message to
|
|
|
|
* @param array $conditional_group_ids : Conditional Groups ids to send message to
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param array $media_ids : Ids of the medias to link to scheduled message
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2021-03-19 02:45:12 +01:00
|
|
|
* @return bool : false on error, true on success
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2021-03-19 02:45:12 +01:00
|
|
|
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?string $id_phone = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$scheduled = [
|
2019-11-12 17:37:20 +01:00
|
|
|
'id_user' => $id_user,
|
2019-11-06 20:34:26 +01:00
|
|
|
'at' => $at,
|
2019-11-10 16:53:47 +01:00
|
|
|
'text' => $text,
|
2020-03-04 01:40:47 +01:00
|
|
|
'id_phone' => $id_phone,
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $mms,
|
2019-11-06 17:22:54 +01:00
|
|
|
'flash' => $flash,
|
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2020-03-04 05:10:45 +01:00
|
|
|
if (null !== $id_phone)
|
2019-11-14 02:02:50 +01:00
|
|
|
{
|
|
|
|
$internal_phone = new Phone($this->bdd);
|
2020-03-04 01:40:47 +01:00
|
|
|
$find_phone = $internal_phone->get_for_user($id_user, $id_phone);
|
2019-11-14 02:02:50 +01:00
|
|
|
|
|
|
|
if (!$find_phone)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
//Ensure atomicity
|
|
|
|
$this->bdd->beginTransaction();
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
$success = (bool) $this->get_model()->update_for_user($id_user, $id_scheduled, $scheduled);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$this->get_model()->delete_scheduled_numbers($id_scheduled);
|
|
|
|
$this->get_model()->delete_scheduled_contact_relations($id_scheduled);
|
|
|
|
$this->get_model()->delete_scheduled_group_relations($id_scheduled);
|
2019-11-28 01:55:11 +01:00
|
|
|
$this->get_model()->delete_scheduled_conditional_group_relations($id_scheduled);
|
2021-03-19 02:45:12 +01:00
|
|
|
$internal_media = new Media($this->bdd);
|
|
|
|
$internal_media->unlink_all_of('scheduled', $id_scheduled);
|
|
|
|
|
|
|
|
foreach ($media_ids as $media_id)
|
|
|
|
{
|
|
|
|
$id_media_scheduled = $internal_media->link_to($media_id, 'scheduled', $id_scheduled);
|
|
|
|
if (!$id_media_scheduled)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-10 16:53:47 +01:00
|
|
|
foreach ($numbers as $number)
|
2019-11-06 17:22:54 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
|
2019-11-06 17:22:54 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$internal_contact = new Contact($this->bdd);
|
2019-11-12 17:37:20 +01:00
|
|
|
foreach ($contacts_ids as $contact_id)
|
2019-11-06 17:22:54 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
$find_contact = $internal_contact->get_for_user($id_user, $contact_id);
|
2019-11-14 02:02:50 +01:00
|
|
|
if (!$find_contact)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$this->get_model()->insert_scheduled_contact_relation($id_scheduled, $contact_id);
|
2019-11-06 17:22:54 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$internal_group = new Group($this->bdd);
|
2019-11-10 16:53:47 +01:00
|
|
|
foreach ($groups_ids as $group_id)
|
2019-11-06 17:22:54 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
$find_group = $internal_group->get_for_user($id_user, $group_id);
|
2019-11-14 02:02:50 +01:00
|
|
|
if (!$find_group)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
$internal_conditional_group = new ConditionalGroup($this->bdd);
|
|
|
|
foreach ($conditional_group_ids as $conditional_group_id)
|
|
|
|
{
|
|
|
|
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
|
|
|
|
if (!$find_group)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
return $this->bdd->commit();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get messages scheduled before a date for a number and a user.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param $date : Date before which we want messages
|
|
|
|
* @param string $number : Number for which we want messages
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function gets_before_date_for_number_and_user(int $id_user, $date, string $number)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
return $this->get_model()->gets_before_date_for_number_and_user($id_user, $date, $number);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get all messages to send and the number to use to send theme.
|
|
|
|
*
|
2021-03-19 21:59:49 +01:00
|
|
|
* @return array : [['id_scheduled', 'text', 'id_phone', 'destination', 'flash', 'mms', 'medias'], ...]
|
2019-12-12 00:56:30 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_smss_to_send()
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
|
|
|
$smss_to_send = [];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
$internal_templating = new \controllers\internals\Templating();
|
|
|
|
$internal_setting = new \controllers\internals\Setting($this->bdd);
|
|
|
|
$internal_group = new \controllers\internals\Group($this->bdd);
|
|
|
|
$internal_conditional_group = new \controllers\internals\ConditionalGroup($this->bdd);
|
|
|
|
$internal_phone = new \controllers\internals\Phone($this->bdd);
|
|
|
|
|
|
|
|
$users_settings = [];
|
|
|
|
$users_phones = [];
|
2021-03-19 02:45:12 +01:00
|
|
|
$users_mms_phones = [];
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
|
|
|
$scheduleds = $this->get_model()->gets_before_date($now);
|
|
|
|
foreach ($scheduleds as $scheduled)
|
|
|
|
{
|
|
|
|
if (!isset($users_settings[$scheduled['id_user']]))
|
|
|
|
{
|
|
|
|
$users_settings[$scheduled['id_user']] = [];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
$settings = $internal_setting->gets_for_user($scheduled['id_user']);
|
|
|
|
foreach ($settings as $name => $value)
|
|
|
|
{
|
|
|
|
$users_settings[$scheduled['id_user']][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($users_phones[$scheduled['id_user']]))
|
|
|
|
{
|
|
|
|
$phones = $internal_phone->gets_for_user($scheduled['id_user']);
|
2021-03-19 02:45:12 +01:00
|
|
|
$mms_phones = $internal_phone->gets_phone_supporting_mms_for_user($scheduled['id_user'], $internal_phone::MMS_SENDING);
|
2021-01-14 03:32:17 +01:00
|
|
|
$users_phones[$scheduled['id_user']] = $phones ?: [];
|
2021-03-19 02:45:12 +01:00
|
|
|
$users_mms_phones[$scheduled['id_user']] = $mms_phones ?: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add medias to mms
|
|
|
|
if ($scheduled['mms'])
|
|
|
|
{
|
|
|
|
$internal_media = new Media($this->bdd);
|
2021-03-21 17:08:05 +01:00
|
|
|
$scheduled['medias'] = $internal_media->gets_for_scheduled($scheduled['id']);
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 01:40:47 +01:00
|
|
|
$phone_to_use = null;
|
|
|
|
foreach ($users_phones[$scheduled['id_user']] as $phone)
|
|
|
|
{
|
|
|
|
if ($phone['id'] !== $scheduled['id_phone'])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phone_to_use = $phone;
|
|
|
|
}
|
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
$messages = [];
|
|
|
|
|
|
|
|
//Add messages for numbers
|
|
|
|
$numbers = $this->get_numbers($scheduled['id']);
|
|
|
|
foreach ($numbers as $number)
|
|
|
|
{
|
2021-02-23 18:57:14 +01:00
|
|
|
if (null === $phone_to_use)
|
|
|
|
{
|
2021-03-19 02:45:12 +01:00
|
|
|
if ($scheduled['mms'])
|
|
|
|
{
|
|
|
|
$rnd_key = array_rand($users_mms_phones[$scheduled['id_user']]);
|
|
|
|
$random_phone = $users_mms_phones[$scheduled['id_user']][$rnd_key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
|
|
|
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
|
|
|
}
|
2021-02-23 18:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
$message = [
|
2020-01-06 23:40:29 +01:00
|
|
|
'id_user' => $scheduled['id_user'],
|
2019-12-20 18:31:19 +01:00
|
|
|
'id_scheduled' => $scheduled['id'],
|
2021-02-23 18:57:14 +01:00
|
|
|
'id_phone' => $phone_to_use['id'] ?? $random_phone['id'],
|
2019-12-12 00:56:30 +01:00
|
|
|
'destination' => $number['number'],
|
|
|
|
'flash' => $scheduled['flash'],
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $scheduled['mms'],
|
|
|
|
'medias' => $scheduled['medias'],
|
2019-12-12 00:56:30 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
|
|
|
{
|
|
|
|
$render = $internal_templating->render($scheduled['text']);
|
|
|
|
|
|
|
|
if (!$render['success'])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message['text'] = $render['result'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$message['text'] = $scheduled['text'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$messages[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add messages for contacts
|
|
|
|
$contacts = $this->get_contacts($scheduled['id']);
|
|
|
|
|
|
|
|
$groups = $this->get_groups($scheduled['id']);
|
|
|
|
foreach ($groups as $group)
|
|
|
|
{
|
|
|
|
$contacts_to_add = $internal_group->get_contacts($group['id']);
|
|
|
|
$contacts = array_merge($contacts, $contacts_to_add);
|
|
|
|
}
|
|
|
|
|
|
|
|
$conditional_groups = $this->get_conditional_groups($scheduled['id']);
|
|
|
|
foreach ($conditional_groups as $conditional_group)
|
|
|
|
{
|
|
|
|
$contacts_to_add = $internal_conditional_group->get_contacts_for_condition_and_user($scheduled['id_user'], $conditional_group['condition']);
|
|
|
|
$contacts = array_merge($contacts, $contacts_to_add);
|
|
|
|
}
|
|
|
|
|
|
|
|
$added_contacts = [];
|
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
|
|
|
if ($added_contacts[$contact['id']] ?? false)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$added_contacts[$contact['id']] = true;
|
2021-02-23 18:57:14 +01:00
|
|
|
|
|
|
|
if (null === $phone_to_use)
|
|
|
|
{
|
2021-03-19 02:45:12 +01:00
|
|
|
if ($scheduled['mms'])
|
|
|
|
{
|
|
|
|
$rnd_key = array_rand($users_mms_phones[$scheduled['id_user']]);
|
|
|
|
$random_phone = $users_mms_phones[$scheduled['id_user']][$rnd_key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rnd_key = array_rand($users_phones[$scheduled['id_user']]);
|
|
|
|
$random_phone = $users_phones[$scheduled['id_user']][$rnd_key];
|
|
|
|
}
|
2021-02-23 18:57:14 +01:00
|
|
|
}
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
$message = [
|
2020-01-06 23:40:29 +01:00
|
|
|
'id_user' => $scheduled['id_user'],
|
2019-12-20 18:31:19 +01:00
|
|
|
'id_scheduled' => $scheduled['id'],
|
2021-02-23 18:57:14 +01:00
|
|
|
'id_phone' => $phone_to_use['id'] ?? $random_phone['id'],
|
2020-03-04 03:28:34 +01:00
|
|
|
'destination' => $contact['number'],
|
2019-12-12 00:56:30 +01:00
|
|
|
'flash' => $scheduled['flash'],
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $scheduled['mms'],
|
|
|
|
'medias' => $scheduled['medias'],
|
2019-12-12 00:56:30 +01:00
|
|
|
];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
|
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$contact['data'] = json_decode($contact['data'], true);
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
//Add metas of contact by adding contact without data
|
2020-09-26 21:34:42 +02:00
|
|
|
$metas = $contact;
|
2021-01-17 03:16:57 +01:00
|
|
|
unset($metas['data'], $metas['id_user']);
|
2020-09-26 21:34:42 +02:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$data = ['contact' => $contact['data'], 'contact_metas' => $metas];
|
2020-09-26 21:34:42 +02:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$render = $internal_templating->render($scheduled['text'], $data);
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
if (!$render['success'])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message['text'] = $render['result'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$message['text'] = $scheduled['text'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$messages[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($messages as $message)
|
|
|
|
{
|
|
|
|
//Remove empty messages
|
2020-01-17 18:19:25 +01:00
|
|
|
if ('' === trim($message['text']))
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$smss_to_send[] = $message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $smss_to_send;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return numbers for a scheduled message.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_scheduled : Scheduled id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_numbers(int $id_scheduled)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_numbers($id_scheduled);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return contacts for a scheduled message.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_scheduled : Scheduled id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_contacts(int $id_scheduled)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_contacts($id_scheduled);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-11-06 20:27:16 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return groups for a scheduled message.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_scheduled : Scheduled id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
2019-11-06 20:27:16 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_groups(int $id_scheduled)
|
2019-11-06 20:27:16 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_groups($id_scheduled);
|
2019-11-06 20:27:16 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return conditional groups for a scheduled message.
|
|
|
|
*
|
2019-11-28 01:55:11 +01:00
|
|
|
* @param int $id_scheduled : Scheduled id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-28 01:55:11 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_conditional_groups(int $id_scheduled)
|
|
|
|
{
|
|
|
|
return $this->get_model()->get_conditional_groups($id_scheduled);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the model for the Controller.
|
|
|
|
*/
|
|
|
|
protected function get_model(): \descartes\Model
|
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\Scheduled($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|