Add phone group support

This commit is contained in:
osaajani 2023-02-20 03:17:53 +01:00
parent 22e5149193
commit 7c3bb65f8b
19 changed files with 1123 additions and 47 deletions

View file

@ -49,6 +49,7 @@ namespace controllers\publics;
private $internal_user;
private $internal_phone;
private $internal_phone_group;
private $internal_received;
private $internal_sended;
private $internal_scheduled;
@ -72,6 +73,7 @@ namespace controllers\publics;
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_user = new \controllers\internals\User($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
$this->internal_phone_group = new \controllers\internals\PhoneGroup($bdd);
$this->internal_received = new \controllers\internals\Received($bdd);
$this->internal_sended = new \controllers\internals\Sended($bdd);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
@ -118,14 +120,14 @@ namespace controllers\publics;
/**
* List all entries of a certain type for the current user, sorted by id.
*
* @param string $entry_type : Type of entries we want to list ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'media']
* @param string $entry_type : Type of entries we want to list ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'phone_group', 'media']
* @param int $page : Pagination number, Default = 0. Group of 25 results.
*
* @return : List of entries
*/
public function get_entries(string $entry_type, int $page = 0)
{
$entry_types = ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'media'];
$entry_types = ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'phone_group', 'media'];
if (!\in_array($entry_type, $entry_types, true))
{
@ -191,6 +193,26 @@ namespace controllers\publics;
unset($entries[$key]['adapter_data']);
}
}
// Special case for phone group we must add phones because its a join
elseif ('phone_group' === $entry_type)
{
foreach ($entries as $key => $entry)
{
$phones = $this->internal_phone_group->get_phones($entry['id']);
// Hide meta data of phones if needed
foreach ($phones as &$phone)
{
if (!$phone['adapter']::meta_hide_data())
{
continue;
}
unset($phone['adapter_data']);
}
$entries[$key]['phones'] = $phones;
}
}
$return = self::DEFAULT_RETURN;
$return['response'] = $entries;
@ -215,7 +237,8 @@ namespace controllers\publics;
*
* @param string $_POST['at'] : Date to send message at format Y-m-d H:i:s
* @param string $_POST['text'] : Text of the message to send
* @param string $_POST['id_phone'] : Default null. Id of phone to send the message from. If null use a random phone
* @param string $_POST['id_phone'] : Default null. Id of phone to send the message from. If null and id_phone_group null, use a random phone
* @param string $_POST['id_phone_group'] : Default null. Id of phone group to send the message from. If null abd id_phone null, use a random phone
* @param string $_POST['flash'] : Default false. Is the sms a flash sms.
* @param string $_POST['mms'] : Default false. Is the sms a mms.
* @param string $_POST['numbers'] : Array of numbers to send message to
@ -231,6 +254,7 @@ namespace controllers\publics;
$at = $_POST['at'] ?? false;
$text = $_POST['text'] ?? false;
$id_phone = empty($_POST['id_phone']) ? null : $_POST['id_phone'];
$id_phone_group = empty($_POST['id_phone_group']) ? null : $_POST['id_phone_group'];
$flash = (bool) ($_POST['flash'] ?? false);
$mms = (bool) ($_POST['mms'] ?? false);
$numbers = $_POST['numbers'] ?? [];
@ -417,6 +441,16 @@ namespace controllers\publics;
return $this->json($return);
}
if ($id_phone && $id_phone_group)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'id_phone, id_phone_group : You must specify at most one of id_phone or id_phone_group, not both.';
$this->auto_http_code(false);
return $this->json($return);
}
$phone = null;
if ($id_phone)
{
@ -433,6 +467,22 @@ namespace controllers\publics;
return $this->json($return);
}
$phone_group = null;
if ($id_phone_group)
{
$phone_group = $this->internal_phone_group->get_for_user($this->user['id'], $id_phone_group);
}
if ($id_phone_group && !$phone_group)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'id_phone_group : You must specify an id_phone_group number among thoses of user phone groups.';
$this->auto_http_code(false);
return $this->json($return);
}
if ($mms)
{
foreach ($files_arrays as $file)
@ -455,7 +505,7 @@ namespace controllers\publics;
}
}
$scheduled_id = $this->internal_scheduled->create($this->user['id'], $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
$scheduled_id = $this->internal_scheduled->create($this->user['id'], $at, $text, $id_phone, $id_phone_group, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
if (!$scheduled_id)
{
$return = self::DEFAULT_RETURN;

View file

@ -315,7 +315,7 @@ namespace controllers\publics;
//Destinations must be an array of number
$destinations = [['number' => $destination, 'data' => '[]']];
if (!$this->internal_scheduled->create($id_user, $at, $text, $id_phone, false, $mms, $destinations, [], [], [], $media_ids))
if (!$this->internal_scheduled->create($id_user, $at, $text, $id_phone, null, false, $mms, $destinations, [], [], [], $media_ids))
{
$return['success'] = false;
$return['message'] = 'Impossible de créer le Sms';

View file

@ -551,4 +551,13 @@ class Phone extends \descartes\Controller
return $this->redirect(\descartes\Router::url('Phone', 'list'));
}
/**
* Return a list of phones as a JSON array
*/
public function json_list()
{
header('Content-Type: application/json');
echo json_encode($this->internal_phone->list_for_user($_SESSION['user']['id']));
}
}

View file

@ -0,0 +1,245 @@
<?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\publics;
/**
* Page of phone groups.
*/
class PhoneGroup extends \descartes\Controller
{
private $internal_phone_group;
private $internal_phone;
private $internal_event;
/**
* Call before any other func to check user is connected
*
* @return void;
*/
public function __construct()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_phone_group = new \controllers\internals\PhoneGroup($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
}
/**
* Return all groups as an array for administration.
*/
public function list()
{
$this->render('phone_group/list');
}
/**
* Return groups as json.
*/
public function list_json()
{
$entities = $this->internal_phone_group->list_for_user($_SESSION['user']['id']);
header('Content-Type: application/json');
echo json_encode(['data' => $entities]);
}
/**
* Delete a list of phone groups
*
* @param array int $_GET['ids'] : Ids of phone groups to delete
* @param mixed $csrf
*
* @return boolean;
*/
public function delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$this->internal_phone_group->delete_for_user($_SESSION['user']['id'], $id);
}
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
/**
* Return the creation page of a group
*/
public function add()
{
$this->render('phone_group/add');
}
/**
* Return the edition page for phone groups
*
* @param array $_GET['ids'] : Ids of phone groups to edit
*/
public function edit()
{
$ids = $_GET['ids'] ?? [];
$groups = $this->internal_phone_group->gets_in_for_user($_SESSION['user']['id'], $ids);
foreach ($groups as $key => $group)
{
$groups[$key]['phones'] = $this->internal_phone_group->get_phones($group['id']);
}
$this->render('phone_group/edit', [
'phone_groups' => $groups,
]);
}
/**
* Create a new phone group
*
* @param $csrf : CSRF token
* @param string $_POST['name'] : Name of phone group
* @param array $_POST['phones'] : Ids of phones to put in the group
*/
public function create($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'add'));
}
$name = $_POST['name'] ?? false;
$phones_ids = $_POST['phones'] ?? false;
if (!$name || !$phones_ids)
{
\FlashMessage\FlashMessage::push('danger', 'Des champs sont manquants !');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'add'));
}
$id_group = $this->internal_phone_group->create($_SESSION['user']['id'], $name, $phones_ids);
if (!$id_group)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce groupe.');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'add'));
}
\FlashMessage\FlashMessage::push('success', 'Le groupe a bien été créé.');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
/**
* Update a list of phone groups
*
* @param $csrf : CSRF token
* @param array $_POST['phone_groups'] : An array of phone groups with group id as keys
*
* @return boolean;
*/
public function update($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
$groups = $_POST['phone_groups'] ?? [];
$nb_groups_update = 0;
foreach ($groups as $id => $group)
{
foreach ($group['phones_ids'] as $key => $value)
{
$group['phones_ids'][$key] = (int) $value;
}
$nb_groups_update += (int) $this->internal_phone_group->update_for_user($_SESSION['user']['id'], $id, $group['name'], $group['phones_ids']);
}
if ($nb_groups_update !== \count($groups))
{
\FlashMessage\FlashMessage::push('danger', 'Certains groupes n\'ont pas pu êtres mis à jour.');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Tous les groupes ont été modifiés avec succès.');
return $this->redirect(\descartes\Router::url('PhoneGroup', 'list'));
}
/**
* Return phones of a group as json array
* @param int $id_group = PhoneGroup id
*
* @return json
*/
public function preview (int $id_group)
{
$return = [
'success' => false,
'result' => 'Une erreur inconnue est survenue.',
];
$group = $this->internal_phone_group->get_for_user($_SESSION['user']['id'], $id_group);
if (!$group)
{
$return['result'] = 'Ce groupe n\'existe pas.';
echo json_encode($return);
return false;
}
$phones = $this->internal_phone_group->get_phones($id_group);
if (!$phones)
{
$return['result'] = 'Aucun téléphone dans le groupe.';
echo json_encode($return);
return false;
}
foreach ($phones as &$phone)
{
$phone['adapter_name'] = call_user_func([$phone['adapter'], 'meta_name']);
}
$return['success'] = true;
$return['result'] = $phones;
echo json_encode($return);
return true;
}
/**
* Cette fonction retourne la liste des groups sous forme JSON.
*/
public function json_list()
{
header('Content-Type: application/json');
echo json_encode($this->internal_phone_group->list_for_user($_SESSION['user']['id']));
}
}

View file

@ -18,6 +18,7 @@ namespace controllers\publics;
{
private $internal_scheduled;
private $internal_phone;
private $internal_phone_group;
private $internal_contact;
private $internal_group;
private $internal_conditional_group;
@ -34,6 +35,7 @@ namespace controllers\publics;
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
$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);
@ -118,6 +120,7 @@ namespace controllers\publics;
$contacts = $this->internal_contact->gets_for_user($id_user);
$phones = $this->internal_phone->gets_for_user($id_user);
$phone_groups = $this->internal_phone_group->gets_for_user($id_user);
$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'] : [];
@ -153,6 +156,7 @@ namespace controllers\publics;
'now' => $now->format('Y-m-d H:i'),
'contacts' => $contacts,
'phones' => $phones,
'phone_groups' => $phone_groups,
'prefilled_contacts' => $prefilled_contacts,
'prefilled_groups' => $prefilled_groups,
'prefilled_conditional_groups' => $prefilled_conditional_groups,
@ -179,6 +183,7 @@ namespace controllers\publics;
$all_contacts = $this->internal_contact->gets_for_user($_SESSION['user']['id']);
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
$phone_groups = $this->internal_phone_group->gets_for_user($id_user);
$scheduleds = $this->internal_scheduled->gets_in_for_user($_SESSION['user']['id'], $ids);
//Pour chaque message on ajoute les numéros, les contacts & les groups
@ -226,6 +231,7 @@ namespace controllers\publics;
$this->render('scheduled/edit', [
'scheduleds' => $scheduleds,
'phones' => $phones,
'phone_groups' => $phone_groups,
'contacts' => $all_contacts,
]);
}
@ -238,7 +244,7 @@ namespace controllers\publics;
* @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)
* @param ?int $_POST['id_phone'] : Id of the phone to send message from, if null use random phone
* @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
@ -433,7 +439,12 @@ namespace controllers\publics;
$mms = (bool) count($media_ids);
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
// Check if we must send message to a phone or a phone_group based on if id_phone start with 'phone_' or 'phonegroup_'
$original_id_phone = $id_phone;
$id_phone = str_starts_with($original_id_phone, 'phone_') ? mb_substr($original_id_phone, mb_strlen('phone_')) : null;
$id_phone_group = str_starts_with($original_id_phone, 'phonegroup_') ? mb_substr($original_id_phone, mb_strlen('phonegroup_')) : null;
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $id_phone, $id_phone_group, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
if (!$scheduled_id)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
@ -650,7 +661,11 @@ namespace controllers\publics;
$mms = (bool) count($media_ids);
$this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
$original_id_phone = $id_phone;
$id_phone = str_starts_with($original_id_phone, 'phone_') ? mb_substr($original_id_phone, mb_strlen('phone_')) : null;
$id_phone_group = str_starts_with($original_id_phone, 'phonegroup_') ? mb_substr($original_id_phone, mb_strlen('phonegroup_')) : null;
$this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $id_phone, $id_phone_group, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
++$nb_update;
}