Update phone to use name instead of number and update sended to use phone id instead of origin

This commit is contained in:
osaajani 2020-03-31 01:19:21 +02:00
parent 62c7f69395
commit d5be760843
12 changed files with 91 additions and 93 deletions

View File

@ -28,45 +28,45 @@ namespace controllers\internals;
} }
/** /**
* Return a phone by his number. * Return a phone by his name
* *
* @param string $number : Phone number * @param string $name : Phone name
* *
* @return array * @return array
*/ */
public function get_by_number(string $number) public function get_by_name(string $name)
{ {
return $this->get_model()->get_by_number($number); return $this->get_model()->get_by_name($name);
} }
/** /**
* Return a phone for a user by a number. * Return a phone for a user by a name
* *
* @param int $id_user : user id * @param int $id_user : user id
* @param string $number : Phone number * @param string $name : Phone name
* *
* @return array * @return array
*/ */
public function get_by_number_and_user(int $id_user, string $number) public function get_by_name_and_user(int $id_user, string $name)
{ {
return $this->get_model()->get_by_number_and_user($id_user, $number); return $this->get_model()->get_by_name_and_user($id_user, $name);
} }
/** /**
* Create a phone. * Create a phone.
* *
* @param int $id_user : User to insert phone for * @param int $id_user : User to insert phone for
* @param string $number : The number of the phone * @param string $name : The name of the phone
* @param string $adapter : The adapter to use the phone * @param string $adapter : The adapter to use the phone
* @param string json $adapter_datas : A JSON string representing adapter's datas (for example credentials for an api) * @param string json $adapter_datas : A JSON string representing adapter's datas (for example credentials for an api)
* *
* @return bool : false on error, true on success * @return bool : false on error, true on success
*/ */
public function create(int $id_user, string $number, string $adapter, string $adapter_datas): bool public function create(int $id_user, string $name, string $adapter, string $adapter_datas): bool
{ {
$phone = [ $phone = [
'id_user' => $id_user, 'id_user' => $id_user,
'number' => $number, 'name' => $name,
'adapter' => $adapter, 'adapter' => $adapter,
'adapter_datas' => $adapter_datas, 'adapter_datas' => $adapter_datas,
]; ];
@ -79,17 +79,17 @@ namespace controllers\internals;
* *
* @param int $id_user : User to insert phone for * @param int $id_user : User to insert phone for
* @param int $id : Phone id * @param int $id : Phone id
* @param string $number : The number of the phone * @param string $name : The name of the phone
* @param string $adapter : The adapter to use the phone * @param string $adapter : The adapter to use the phone
* @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api) * @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api)
* *
* @return bool : false on error, true on success * @return bool : false on error, true on success
*/ */
public function update_for_user(int $id_user, int $id, string $number, string $adapter, array $adapter_datas): bool public function update_for_user(int $id_user, int $id, string $name, string $adapter, array $adapter_datas): bool
{ {
$phone = [ $phone = [
'id_user' => $id_user, 'id_user' => $id_user,
'number' => $number, 'name' => $name,
'adapter' => $adapter, 'adapter' => $adapter,
'adapter_datas' => json_encode($adapter_datas), 'adapter_datas' => json_encode($adapter_datas),
]; ];

View File

@ -19,9 +19,9 @@ namespace controllers\internals;
* Create a sended. * Create a sended.
* *
* @param int $id_user : Id of user to create sended message for * @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date * @param $at : Reception date
* @param $text : Text of the message * @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver * @param string $destination : Number of the receiver
* @param string $uid : Uid of the sms on the adapter service used * @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message * @param string $adapter : Name of the adapter service used to send the message
@ -30,10 +30,11 @@ namespace controllers\internals;
* *
* @return bool : false on error, new sended id else * @return bool : false on error, new sended id else
*/ */
public function create(int $id_user, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool
{ {
$sended = [ $sended = [
'id_user' => $id_user, 'id_user' => $id_user,
'id_phone' => $id_phone,
'at' => $at, 'at' => $at,
'text' => $text, 'text' => $text,
'origin' => $origin, 'origin' => $origin,
@ -47,38 +48,6 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($sended); return (bool) $this->get_model()->insert($sended);
} }
/**
* Update a sended for a user.
*
* @param int $id_user : user id
* @param int $id_sended : Sended id
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param ?string $status : Status of a the sms. By default null -> unknown
*
* @return bool : false on error, true on success
*/
public function update_for_user(int $id_user, int $id_sended, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = null): bool
{
$sended = [
'at' => $at,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'uid' => $uid,
'adapter' => $adapter,
'flash' => $flash,
'status' => $status,
];
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
}
/** /**
* Update a sended status for a user. * Update a sended status for a user.
* *

View File

@ -101,7 +101,7 @@ class Phone extends \descartes\Controller
* Create a new phone. * Create a new phone.
* *
* @param $csrf : CSRF token * @param $csrf : CSRF token
* @param string $_POST['number'] : Phone number * @param string $_POST['name'] : Phone name
* @param string $_POST['adapter'] : Phone adapter * @param string $_POST['adapter'] : Phone adapter
* @param array $_POST['adapter_datas'] : Phone adapter datas * @param array $_POST['adapter_datas'] : Phone adapter datas
*/ */
@ -115,29 +115,21 @@ class Phone extends \descartes\Controller
} }
$id_user = $_SESSION['user']['id']; $id_user = $_SESSION['user']['id'];
$number = $_POST['number'] ?? false; $name = $_POST['name'] ?? false;
$adapter = $_POST['adapter'] ?? false; $adapter = $_POST['adapter'] ?? false;
$adapter_datas = !empty($_POST['adapter_datas']) ? $_POST['adapter_datas'] : []; $adapter_datas = !empty($_POST['adapter_datas']) ? $_POST['adapter_datas'] : [];
if (!$number || !$adapter) if (!$name || !$adapter)
{ {
\FlashMessage\FlashMessage::push('danger', 'Des champs obligatoires sont manquants.'); \FlashMessage\FlashMessage::push('danger', 'Des champs obligatoires sont manquants.');
return $this->redirect(\descartes\Router::url('Phone', 'add')); return $this->redirect(\descartes\Router::url('Phone', 'add'));
} }
$number = \controllers\internals\Tool::parse_phone($number); $name_exist = $this->internal_phone->get_by_name($name);
if (!$number) if ($name_exist)
{ {
\FlashMessage\FlashMessage::push('danger', 'Numéro de téléphone incorrect.'); \FlashMessage\FlashMessage::push('danger', 'Ce nom est déjà utilisé pour un autre téléphone.');
return $this->redirect(\descartes\Router::url('Phone', 'add'));
}
$number_exist = $this->internal_phone->get_by_number($number);
if ($number_exist)
{
\FlashMessage\FlashMessage::push('danger', 'Ce numéro de téléphone est déjà utilisé.');
return $this->redirect(\descartes\Router::url('Phone', 'add')); return $this->redirect(\descartes\Router::url('Phone', 'add'));
} }
@ -181,9 +173,9 @@ class Phone extends \descartes\Controller
$adapter_datas = json_encode($adapter_datas); $adapter_datas = json_encode($adapter_datas);
//Check adapter is working correctly with thoses numbers and datas //Check adapter is working correctly with thoses names and datas
$adapter_classname = $find_adapter['meta_classname']; $adapter_classname = $find_adapter['meta_classname'];
$adapter_instance = new $adapter_classname($number, $adapter_datas); $adapter_instance = new $adapter_classname($name, $adapter_datas);
$adapter_working = $adapter_instance->test(); $adapter_working = $adapter_instance->test();
if (!$adapter_working) if (!$adapter_working)
@ -193,7 +185,7 @@ class Phone extends \descartes\Controller
return $this->redirect(\descartes\Router::url('Phone', 'add')); return $this->redirect(\descartes\Router::url('Phone', 'add'));
} }
$success = $this->internal_phone->create($id_user, $number, $adapter, $adapter_datas); $success = $this->internal_phone->create($id_user, $name, $adapter, $adapter_datas);
if (!$success) if (!$success)
{ {
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce téléphone.'); \FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce téléphone.');

View File

@ -44,6 +44,23 @@ namespace controllers\publics;
$page = (int) $page; $page = (int) $page;
$limit = 25; $limit = 25;
$sendeds = $this->internal_sended->list_for_user($_SESSION['user']['id'], $limit, $page); $sendeds = $this->internal_sended->list_for_user($_SESSION['user']['id'], $limit, $page);
foreach ($sendeds as &$sended)
{
if ($sended['id_phone'] === null)
{
continue;
}
$phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $sended['id_phone']);
if (!$phone)
{
continue;
}
$sended['phone_name'] = $phone['name'];
}
$this->render('sended/list', ['sendeds' => $sendeds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($sendeds)]); $this->render('sended/list', ['sendeds' => $sendeds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($sendeds)]);
} }

View File

@ -141,7 +141,7 @@ class Phone extends AbstractDaemon
$message['at'] = $at; $message['at'] = $at;
$message['origin'] = $this->phone['number']; $message['id_phone'] = $this->phone['id'];
$this->logger->info('Try send message : ' . json_encode($message)); $this->logger->info('Try send message : ' . json_encode($message));
@ -149,7 +149,7 @@ class Phone extends AbstractDaemon
if (!$sended_sms_uid) if (!$sended_sms_uid)
{ {
$this->logger->error('Failed send message : ' . json_encode($message)); $this->logger->error('Failed send message : ' . json_encode($message));
$internal_sended->create($this->phone['id_user'], $at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed'); $internal_sended->create($this->phone['id_user'], $this->phone['id'], $at, $message['text'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed');
continue; continue;
} }
@ -161,7 +161,7 @@ class Phone extends AbstractDaemon
$this->logger->info('Successfully send message : ' . json_encode($message)); $this->logger->info('Successfully send message : ' . json_encode($message));
$internal_sended->create($this->phone['id_user'], $at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']); $internal_sended->create($this->phone['id_user'], $this->phone['id'], $at, $message['text'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']);
} }
} }

View File

@ -0,0 +1,29 @@
<?php
use Phinx\Migration\AbstractMigration;
class UpdatePhoneToReplaceNumber extends AbstractMigration
{
public function change()
{
$table = $this->table('phone');
$table->removeColumn('number');
$table->addColumn('name', 'string', ['null' => false, 'limit' => 150]);
$table->addIndex('name', ['unique' => true]);
$table->update();
$table = $this->table('sended');
$table->removeColumn('origin');
$table->addColumn('id_phone', 'integer', ['null' => true]);
$table->addForeignKey('id_phone', 'phone', 'id', ['delete' => 'SET_NULL', 'update' => 'CASCADE']);
$table->update();
$table = $this->table('received');
$table->removeColumn('destination');
$table->addColumn('id_phone', 'integer', ['null' => true]);
$table->addForeignKey('id_phone', 'phone', 'id', ['delete' => 'SET_NULL', 'update' => 'CASCADE']);
$table->update();
}
}

View File

@ -14,28 +14,28 @@ namespace models;
class Phone extends StandardModel class Phone extends StandardModel
{ {
/** /**
* Return a phone by his number and user. * Return a phone by his name and user.
* *
* @param int $id_user : user id * @param int $id_user : user id
* @param string $number : phone number * @param string $name : phone name
* *
* @return array * @return array
*/ */
public function get_by_number_and_user(int $id_user, string $number) public function get_by_name_and_user(int $id_user, string $name)
{ {
return $this->_select_one('phone', ['number' => $number, 'id_user' => $id_user]); return $this->_select_one('phone', ['name' => $name, 'id_user' => $id_user]);
} }
/** /**
* Return a phone by his number. * Return a phone by his name.
* *
* @param string $number : phone number * @param string $name : phone name
* *
* @return array * @return array
*/ */
public function get_by_number(string $number) public function get_by_name(string $name)
{ {
return $this->_select_one('phone', ['number' => $number]); return $this->_select_one('phone', ['name' => $name]);
} }
/** /**

View File

@ -39,12 +39,12 @@
<div class="panel-body"> <div class="panel-body">
<form action="<?php echo \descartes\Router::url('Phone', 'create', ['csrf' => $_SESSION['csrf']]);?>" method="POST"> <form action="<?php echo \descartes\Router::url('Phone', 'create', ['csrf' => $_SESSION['csrf']]);?>" method="POST">
<div class="form-group"> <div class="form-group">
<label>Numéro de téléphone</label> <label>Nom du téléphone</label>
<p class="italic small help"> <p class="italic small help">
Le numéro de téléphone qui enverra et recevra les messages. Le nom du téléphone qui enverra et recevra les messages.
</p> </p>
<div class="form-group"> <div class="form-group">
<input required="required" name="" class="form-control" type="tel" id="phone-international-input" placeholder="Numéro de téléphone à utiliser."> <input required="required" name="name" class="form-control" placeholder="Nom du téléphone">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -126,15 +126,6 @@
{ {
change_adapter(); change_adapter();
}); });
var number_input = jQuery('#phone-international-input')[0];
var iti_number_input = window.intlTelInput(number_input, {
hiddenInput: 'number',
defaultCountry: '<?php $this->s($_SESSION['user']['settings']['default_phone_country']); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', $_SESSION['user']['settings']['preferred_phone_country'])), false, false); ?>,
nationalMode: true,
utilsScript: '<?php echo HTTP_PWD_JS; ?>/intlTelInput/utils.js'
});
}); });
</script> </script>
<?php <?php

View File

@ -42,7 +42,7 @@
<table class="table table-bordered table-hover table-striped" id="table-phones"> <table class="table table-bordered table-hover table-striped" id="table-phones">
<thead> <thead>
<tr> <tr>
<th>Numéro</th> <th>Nom</th>
<th>Adaptateur</th> <th>Adaptateur</th>
<th style="width:5%;">Sélectionner</th> <th style="width:5%;">Sélectionner</th>
</tr> </tr>
@ -50,7 +50,7 @@
<tbody> <tbody>
<?php foreach ($phones as $phone) { ?> <?php foreach ($phones as $phone) { ?>
<tr> <tr>
<td><?php $this->s(\controllers\internals\Tool::phone_format($phone['number'])); ?></td> <td><?php $this->s(\controllers\internals\Tool::phone_format($phone['name'])); ?></td>
<td><?php $this->s($phone['adapter']); ?></td> <td><?php $this->s($phone['adapter']); ?></td>
<td><input type="checkbox" value="<?php $this->s($phone['id']); ?>" name="ids[]"></td> <td><input type="checkbox" value="<?php $this->s($phone['id']); ?>" name="ids[]"></td>
</tr> </tr>

View File

@ -115,7 +115,7 @@
<select name="id_phone" class="form-control"> <select name="id_phone" class="form-control">
<option value="">N'importe lequel</option> <option value="">N'importe lequel</option>
<?php foreach ($phones as $phone) { ?> <?php foreach ($phones as $phone) { ?>
<option value="<?php $this->s($phone['id']); ?>"><?php $this->s($phone['number']); ?></option> <option value="<?php $this->s($phone['id']); ?>"><?php $this->s($phone['name']); ?></option>
<?php } ?> <?php } ?>
</select> </select>
</div> </div>

View File

@ -122,7 +122,7 @@
<select name="scheduleds[<?php $this->s($scheduled['id']); ?>][id_phone]" class="form-control"> <select name="scheduleds[<?php $this->s($scheduled['id']); ?>][id_phone]" class="form-control">
<option <?php echo ($scheduled['id_phone'] ? '' : 'selected="selected"'); ?> value="">N'importe lequel</option> <option <?php echo ($scheduled['id_phone'] ? '' : 'selected="selected"'); ?> value="">N'importe lequel</option>
<?php foreach ($phones as $phone) { ?> <?php foreach ($phones as $phone) { ?>
<option <?php echo ($scheduled['id_phone'] == $phone['id'] ? 'selected="selected"' : '' ); ?> value="<?php $this->s($phone['id']); ?>"><?php $this->s($phone['number']); ?></option> <option <?php echo ($scheduled['id_phone'] == $phone['id'] ? 'selected="selected"' : '' ); ?> value="<?php $this->s($phone['id']); ?>"><?php $this->s($phone['name']); ?></option>
<?php } ?> <?php } ?>
</select> </select>
</div> </div>

View File

@ -55,7 +55,7 @@
<tbody> <tbody>
<?php foreach ($sendeds as $sended) { ?> <?php foreach ($sendeds as $sended) { ?>
<tr> <tr>
<td class="no-wrap"><?php echo(\controllers\internals\Tool::phone_link($sended['origin'])); ?></td> <td class="no-wrap"><?php $this->s($sended['phone_name'] ?? 'Inconnu'); ?></td>
<td class="no-wrap"><?php echo(\controllers\internals\Tool::phone_link($sended['destination'])); ?></td> <td class="no-wrap"><?php echo(\controllers\internals\Tool::phone_link($sended['destination'])); ?></td>
<td><?php $this->s($sended['text']); ?></td> <td><?php $this->s($sended['text']); ?></td>
<td><?php $this->s($sended['at']); ?></td> <td><?php $this->s($sended['at']); ?></td>