mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Add phone group support
This commit is contained in:
parent
22e5149193
commit
7c3bb65f8b
19 changed files with 1123 additions and 47 deletions
139
controllers/internals/PhoneGroup.php
Normal file
139
controllers/internals/PhoneGroup.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of RaspiSMS.
|
||||
*
|
||||
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
||||
*
|
||||
* This source file is subject to the GPL-3.0 license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace controllers\internals;
|
||||
|
||||
/**
|
||||
* Classe des groups.
|
||||
*/
|
||||
class PhoneGroup extends StandardController
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new phone group for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param stirng $name : Group name
|
||||
* @param array $phones_ids : Ids of the phones of the group
|
||||
*
|
||||
* @return mixed bool|int : false on error, new group id
|
||||
*/
|
||||
public function create(int $id_user, string $name, array $phones_ids)
|
||||
{
|
||||
$group = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
$id_group = $this->get_model()->insert($group);
|
||||
if (!$id_group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_phone = new Phone($this->bdd);
|
||||
foreach ($phones_ids as $phone_id)
|
||||
{
|
||||
$phone = $internal_phone->get_for_user($id_user, $phone_id);
|
||||
if (!$phone)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->get_model()->insert_phone_group_phone_relation($id_group, $phone_id);
|
||||
}
|
||||
|
||||
$internal_event = new Event($this->bdd);
|
||||
$internal_event->create($id_user, 'PHONE_GROUP_ADD', 'Ajout phone group : ' . $name);
|
||||
|
||||
return $id_group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a phone group for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_group : Group id
|
||||
* @param stirng $name : Group name
|
||||
* @param array $phones_ids : Ids of the phones of the group
|
||||
*
|
||||
* @return bool : False on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id_group, string $name, array $phones_ids)
|
||||
{
|
||||
$group = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
$result = $this->get_model()->update_for_user($id_user, $id_group, $group);
|
||||
|
||||
$this->get_model()->delete_phone_group_phone_relations($id_group);
|
||||
|
||||
$internal_phone = new Phone($this->bdd);
|
||||
$nb_phone_insert = 0;
|
||||
foreach ($phones_ids as $phone_id)
|
||||
{
|
||||
$phone = $internal_phone->get_for_user($id_user, $phone_id);
|
||||
if (!$phone)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->get_model()->insert_phone_group_phone_relation($id_group, $phone_id))
|
||||
{
|
||||
++$nb_phone_insert;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$result && $nb_phone_insert !== \count($phones_ids))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a group by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Group name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_for_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->get_model()->get_by_name_for_user($id_user, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups phones.
|
||||
*
|
||||
* @param int $id_group : Group id
|
||||
*
|
||||
* @return array : phones of the group
|
||||
*/
|
||||
public function get_phones($id_group)
|
||||
{
|
||||
return $this->get_model()->get_phones($id_group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*/
|
||||
protected function get_model(): \models\PhoneGroup
|
||||
{
|
||||
$this->model = $this->model ?? new \models\PhoneGroup($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ use Monolog\Logger;
|
|||
* @param $at : Scheduled date to send
|
||||
* @param string $text : Text of the message
|
||||
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
||||
* @param ?int $id_phone_group : Id of the phone group to send message with, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, by default false
|
||||
* @param bool $mms : Is the sms a mms, by default false
|
||||
* @param array $numbers : Array of numbers to send message to, a number is an array ['number' => '+33XXX', 'data' => '{"key":"value", ...}']
|
||||
|
@ -35,13 +36,14 @@ use Monolog\Logger;
|
|||
*
|
||||
* @return bool : false on error, new id on success
|
||||
*/
|
||||
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 = [])
|
||||
public function create(int $id_user, $at, string $text, ?int $id_phone = null, ?int $id_phone_group = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'id_phone' => $id_phone,
|
||||
'id_phone_group' => $id_phone_group,
|
||||
'flash' => $flash,
|
||||
'mms' => $mms,
|
||||
];
|
||||
|
@ -62,6 +64,17 @@ use Monolog\Logger;
|
|||
}
|
||||
}
|
||||
|
||||
if (null !== $id_phone_group)
|
||||
{
|
||||
$internal_phone_group = new PhoneGroup($this->bdd);
|
||||
$find_phone_group = $internal_phone_group->get_for_user($id_user, $id_phone_group);
|
||||
|
||||
if (!$find_phone_group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Use transaction to garanty atomicity
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
|
@ -147,6 +160,7 @@ use Monolog\Logger;
|
|||
* @param $at : Scheduled date to send
|
||||
* @param string $text : Text of the message
|
||||
* @param ?int $id_phone : Id of the phone to send message with, null by default
|
||||
* @param ?int $id_phone_group : Id of the phone group to send message with, null by default
|
||||
* @param bool $flash : Is the sms a flash sms, by default false
|
||||
* @param bool $mms : Is the sms a mms, by default false
|
||||
* @param array $numbers : Array of numbers to send message to, a number is an array ['number' => '+33XXX', 'data' => '{"key":"value", ...}']
|
||||
|
@ -157,13 +171,14 @@ use Monolog\Logger;
|
|||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
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 = [])
|
||||
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?int $id_phone = null, ?int $id_phone_group = null, bool $flash = false, bool $mms = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [], array $media_ids = [])
|
||||
{
|
||||
$scheduled = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at,
|
||||
'text' => $text,
|
||||
'id_phone' => $id_phone,
|
||||
'id_phone_group' => $id_phone_group,
|
||||
'mms' => $mms,
|
||||
'flash' => $flash,
|
||||
];
|
||||
|
@ -179,6 +194,17 @@ use Monolog\Logger;
|
|||
}
|
||||
}
|
||||
|
||||
if (null !== $id_phone_group)
|
||||
{
|
||||
$internal_phone_group = new PhoneGroup($this->bdd);
|
||||
$find_phone_group = $internal_phone_group->get_for_user($id_user, $id_phone_group);
|
||||
|
||||
if (!$find_phone_group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Ensure atomicity
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
|
@ -413,13 +439,14 @@ use Monolog\Logger;
|
|||
$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);
|
||||
$internal_phone_group = new \controllers\internals\PhoneGroup($this->bdd);
|
||||
$internal_smsstop = new \controllers\internals\SmsStop($this->bdd);
|
||||
$internal_sended = new \controllers\internals\Sended($this->bdd);
|
||||
|
||||
$users_smsstops = [];
|
||||
$users_settings = [];
|
||||
$users_phones = [];
|
||||
$users_mms_phones = [];
|
||||
$users_phone_groups = [];
|
||||
|
||||
$now = new \DateTime();
|
||||
$now = $now->format('Y-m-d H:i:s');
|
||||
|
@ -457,7 +484,6 @@ use Monolog\Logger;
|
|||
if (!isset($users_phones[$id_user]))
|
||||
{
|
||||
$users_phones[$id_user] = [];
|
||||
$users_mms_phones[$id_user] = [];
|
||||
|
||||
$phones = $internal_phone->gets_for_user($id_user);
|
||||
foreach ($phones as &$phone)
|
||||
|
@ -475,25 +501,25 @@ use Monolog\Logger;
|
|||
$phone['remaining_volume'] = $remaining_volume;
|
||||
$users_phones[$id_user][$phone['id']] = $phone;
|
||||
}
|
||||
|
||||
$mms_phones = $internal_phone->gets_phone_supporting_mms_for_user($id_user, $internal_phone::MMS_SENDING);
|
||||
foreach ($mms_phones as &$mms_phone)
|
||||
{
|
||||
$limits = $internal_phone->get_limits($mms_phone['id']);
|
||||
|
||||
$remaining_volume = PHP_INT_MAX;
|
||||
foreach ($limits as $limit)
|
||||
{
|
||||
$startpoint = new \DateTime($limit['startpoint']);
|
||||
$consumed = $internal_sended->count_since_for_phone_and_user($id_user, $mms_phone['id'], $startpoint);
|
||||
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
|
||||
}
|
||||
|
||||
$mms_phone['remaining_volume'] = $remaining_volume;
|
||||
$mms_phones[$id_user][$mms_phone['id']] = $mms_phone;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($users_phone_groups[$id_user]))
|
||||
{
|
||||
$users_phone_groups[$id_user] = [];
|
||||
|
||||
$phone_groups = $internal_phone_group->gets_for_user($id_user);
|
||||
foreach ($phone_groups as $phone_group)
|
||||
{
|
||||
$phones = $internal_phone_group->get_phones($phone_group['id']);
|
||||
$phone_group['phones'] = [];
|
||||
foreach ($phones as $phone)
|
||||
{
|
||||
$phone_group['phones'][] = $phone['id'];
|
||||
}
|
||||
|
||||
$users_phone_groups[$id_user][$phone_group['id']] = $phone_group;
|
||||
}
|
||||
}
|
||||
|
||||
//Add medias to mms
|
||||
$scheduled['medias'] = [];
|
||||
|
@ -509,6 +535,12 @@ use Monolog\Logger;
|
|||
$phone_to_use = $users_phones[$id_user][$scheduled['id_phone']] ?? null;
|
||||
}
|
||||
|
||||
$phone_group_to_use = null;
|
||||
if ($scheduled['id_phone_group'])
|
||||
{
|
||||
$phone_group_to_use = $users_phone_groups[$id_user][$scheduled['id_phone_group']] ?? null;
|
||||
}
|
||||
|
||||
|
||||
// We turn all contacts, groups and conditional groups into just contacts
|
||||
$contacts = $this->get_contacts($id_scheduled);
|
||||
|
@ -620,9 +652,21 @@ use Monolog\Logger;
|
|||
if (null === $phone_to_use)
|
||||
{
|
||||
$phones_subset = $users_phones[$id_user];
|
||||
|
||||
if ($phone_group_to_use)
|
||||
{
|
||||
$phones_subset = array_filter($phones_subset, function ($phone) use ($phone_group_to_use) {
|
||||
return in_array($phone['id'], $phone_group_to_use['phones']);
|
||||
});
|
||||
}
|
||||
|
||||
if ($scheduled['mms'])
|
||||
{
|
||||
$phones_subset = $users_mms_phones[$id_user] ?: $phones_subset;
|
||||
$mms_only = array_filter($phones_subset, function ($phone) {
|
||||
return $phone['adapter']::meta_support_mms_sending();
|
||||
});
|
||||
|
||||
$phones_subset = $mms_only ?: $phones_subset;
|
||||
}
|
||||
|
||||
// Keep only phones with remaining volume and available status
|
||||
|
@ -675,12 +719,8 @@ use Monolog\Logger;
|
|||
'text' => $text,
|
||||
];
|
||||
|
||||
// Consume one sms from remaining volume of phone, dont forget to do the same for the entry in mms phones
|
||||
// Consume one sms from remaining volume of phone
|
||||
$users_phones[$id_user][$id_phone]['remaining_volume'] --;
|
||||
if ($users_mms_phones[$id_user][$id_phone] ?? false)
|
||||
{
|
||||
$users_mms_phones[$id_user][$id_phone] --;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -329,13 +329,13 @@ use Exception;
|
|||
$return['error'] = true;
|
||||
$return['error_message'] = $e->getMessage();
|
||||
|
||||
$uid = uniqid();
|
||||
$status = \models\Sended::STATUS_FAILED;
|
||||
|
||||
return $return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
$uid = $uid ?? uniqid();
|
||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $uid, $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
||||
|
||||
$webhook_body = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue