Add update to phones
This commit is contained in:
parent
298bba0c39
commit
fb58802240
|
@ -184,20 +184,52 @@ namespace controllers\internals;
|
|||
* @param int $id : Phone id
|
||||
* @param string $name : The name of the phone
|
||||
* @param string $adapter : The adapter to use the phone
|
||||
* @param array $adapter_data : An array of the data of the adapter (for example credentials for an api)
|
||||
* @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api)
|
||||
* @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint
|
||||
*
|
||||
* @return bool : false on error, true on success
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $name, string $adapter, array $adapter_data): bool
|
||||
public function update_for_user(int $id_user, int $id, string $name, string $adapter, string $adapter_data, array $limits = []): bool
|
||||
{
|
||||
$phone = [
|
||||
'id_user' => $id_user,
|
||||
'name' => $name,
|
||||
'adapter' => $adapter,
|
||||
'adapter_data' => json_encode($adapter_data),
|
||||
'adapter_data' => $adapter_data,
|
||||
];
|
||||
|
||||
return (bool) $this->get_model()->update_for_user($id_user, $id, $phone);
|
||||
//Use transaction to garanty atomicity
|
||||
$this->bdd->beginTransaction();
|
||||
|
||||
$nb_delete = $this->get_model()->delete_phone_limits($id);
|
||||
|
||||
foreach ($limits as $limit)
|
||||
{
|
||||
$limit_id = $this->get_model()->insert_phone_limit($id, $limit['volume'], $limit['startpoint']);
|
||||
|
||||
if (!$limit_id)
|
||||
{
|
||||
$this->bdd->rollBack();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$nb_update = $this->get_model()->update_for_user($id_user, $id, $phone);
|
||||
|
||||
$success = $this->bdd->commit();
|
||||
|
||||
if (!$success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($nb_update == 0 && count($limits) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -506,6 +506,7 @@ namespace controllers\publics;
|
|||
* @param string $_POST['name'] : Phone name
|
||||
* @param string $_POST['adapter'] : Phone adapter
|
||||
* @param array $_POST['adapter_data'] : Phone adapter data
|
||||
* @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
|
||||
*
|
||||
* @return int : id phone the new phone on success
|
||||
*/
|
||||
|
@ -516,6 +517,8 @@ namespace controllers\publics;
|
|||
$name = $_POST['name'] ?? false;
|
||||
$adapter = $_POST['adapter'] ?? false;
|
||||
$adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : [];
|
||||
$limits = $_POST['limits'] ?? [];
|
||||
$limits = is_array($limits) ? $limits : [$limits];
|
||||
|
||||
if (!$name)
|
||||
{
|
||||
|
@ -545,6 +548,36 @@ namespace controllers\publics;
|
|||
return $this->json($return);
|
||||
}
|
||||
|
||||
if ($limits)
|
||||
{
|
||||
foreach ($limits as $key => $limit)
|
||||
{
|
||||
if (!is_array($limit))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startpoint = $limit['startpoint'] ?? false;
|
||||
$volume = $limit['volume'] ?? false;
|
||||
|
||||
if (!$startpoint || !$volume)
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$volume = (int) $volume;
|
||||
$limits[$key]['volume'] = max($volume, 1);
|
||||
|
||||
if (!\controllers\internals\Tool::validate_relative_date($startpoint))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$adapters = $this->internal_adapter->list_adapters();
|
||||
$find_adapter = false;
|
||||
foreach ($adapters as $metas)
|
||||
|
@ -627,7 +660,7 @@ namespace controllers\publics;
|
|||
return $this->json($return);
|
||||
}
|
||||
|
||||
$phone_id = $this->internal_phone->create($this->user['id'], $name, $adapter, $adapter_data);
|
||||
$phone_id = $this->internal_phone->create($this->user['id'], $name, $adapter, $adapter_data, $limits);
|
||||
if (false === $phone_id)
|
||||
{
|
||||
$return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
|
||||
|
@ -667,10 +700,14 @@ namespace controllers\publics;
|
|||
return $this->json($return);
|
||||
}
|
||||
|
||||
$limits = $this->internal_phone->get_limits(($phone['id']));
|
||||
|
||||
$name = $_POST['name'] ?? $phone['name'];
|
||||
$adapter = $_POST['adapter'] ?? $phone['adapter'];
|
||||
$adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : json_decode($phone['adapter_data']);
|
||||
$adapter_data = is_array($adapter_data) ? $adapter_data : [$adapter_data];
|
||||
$limits = $_POST['limits'] ?? $limits;
|
||||
$limits = is_array($limits) ? $limits : [$limits];
|
||||
|
||||
|
||||
if (!$name && !$adapter && !$adapter_data)
|
||||
|
@ -693,6 +730,36 @@ namespace controllers\publics;
|
|||
return $this->json($return);
|
||||
}
|
||||
|
||||
if ($limits)
|
||||
{
|
||||
foreach ($limits as $key => $limit)
|
||||
{
|
||||
if (!is_array($limit))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startpoint = $limit['startpoint'] ?? false;
|
||||
$volume = $limit['volume'] ?? false;
|
||||
|
||||
if (!$startpoint || !$volume)
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$volume = (int) $volume;
|
||||
$limits[$key]['volume'] = max($volume, 1);
|
||||
|
||||
if (!\controllers\internals\Tool::validate_relative_date($startpoint))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$adapters = $this->internal_adapter->list_adapters();
|
||||
$find_adapter = false;
|
||||
foreach ($adapters as $metas)
|
||||
|
@ -775,7 +842,7 @@ namespace controllers\publics;
|
|||
return $this->json($return);
|
||||
}
|
||||
|
||||
$success = $this->internal_phone->update_for_user($this->user['id'], $phone['id'], $name, $adapter, $adapter_data);
|
||||
$success = $this->internal_phone->update_for_user($this->user['id'], $phone['id'], $name, $adapter, $adapter_data_json, $limits);
|
||||
if (!$success)
|
||||
{
|
||||
$return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];
|
||||
|
|
|
@ -134,7 +134,7 @@ class Phone extends \descartes\Controller
|
|||
* @param string $_POST['name'] : Phone name
|
||||
* @param string $_POST['adapter'] : Phone adapter
|
||||
* @param ?array $_POST['adapter_data'] : Phone adapter data
|
||||
* @param ?array $_POST['limits'] : Limits in number of SMS for a period to be applied to this phone.
|
||||
* @param ?array $_POST['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
|
||||
*/
|
||||
public function create($csrf)
|
||||
{
|
||||
|
@ -171,6 +171,12 @@ class Phone extends \descartes\Controller
|
|||
{
|
||||
foreach ($limits as $key => $limit)
|
||||
{
|
||||
if (!is_array($limit))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startpoint = $limit['startpoint'] ?? false;
|
||||
$volume = $limit['volume'] ?? false;
|
||||
|
||||
|
@ -284,4 +290,215 @@ class Phone extends \descartes\Controller
|
|||
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'list'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the edit page for phones
|
||||
*
|
||||
* @param int... $ids : Phones ids
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$ids = $_GET['ids'] ?? [];
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
|
||||
$phones = $this->internal_phone->gets_in_for_user($id_user, $ids);
|
||||
|
||||
if (!$phones)
|
||||
{
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'list'));
|
||||
}
|
||||
|
||||
foreach ($phones as &$phone)
|
||||
{
|
||||
$limits = $this->internal_phone->get_limits($phone['id']);
|
||||
$phone['limits'] = $limits;
|
||||
}
|
||||
|
||||
$phone_data = json_decode($phone['adapter_data'], true);
|
||||
$adapters = $this->internal_adapter->list_adapters();
|
||||
foreach ($adapters as &$adapter)
|
||||
{
|
||||
foreach ($adapter['meta_data_fields'] as &$data_field)
|
||||
{
|
||||
if (key_exists($data_field['name'], $phone_data))
|
||||
{
|
||||
$data_field['value'] = $phone_data[$data_field['name']];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->render('phone/edit', [
|
||||
'phones' => $phones,
|
||||
'adapters' => $adapters,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update multiple phones.
|
||||
*
|
||||
* @param $csrf : CSRF token
|
||||
* @param string $_POST['phones']['id']['name'] : Phone name
|
||||
* @param string $_POST['phones']['id']['adapter'] : Phone adapter
|
||||
* @param ?array $_POST['phones']['id']['adapter_data'] : Phone adapter data
|
||||
* @param ?array $_POST['phones']['id']['limits'] : Array of limits in number of SMS for a period to be applied to this phone.
|
||||
*/
|
||||
public function update($csrf)
|
||||
{
|
||||
if (!$this->verify_csrf($csrf))
|
||||
{
|
||||
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
||||
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'add'));
|
||||
}
|
||||
|
||||
if (!$_POST['phones'])
|
||||
{
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'list'));
|
||||
}
|
||||
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
|
||||
$nb_update = 0;
|
||||
foreach ($_POST['phones'] as $id_phone => $phone)
|
||||
{
|
||||
$name = $phone['name'] ?? false;
|
||||
$adapter = $phone['adapter'] ?? false;
|
||||
$adapter_data = !empty($phone['adapter_data']) ? $phone['adapter_data'] : [];
|
||||
$limits = $phone['limits'] ?? [];
|
||||
$limits = is_array($limits) ? $limits : [$limits];
|
||||
|
||||
if (!$name || !$adapter)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$phone_with_same_name = $this->internal_phone->get_by_name_and_user($id_user, $name);
|
||||
if ($phone_with_same_name && $phone_with_same_name['id'] != $id_phone)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($limits)
|
||||
{
|
||||
foreach ($limits as $key => $limit)
|
||||
{
|
||||
if (!is_array($limit))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startpoint = $limit['startpoint'] ?? false;
|
||||
$volume = $limit['volume'] ?? false;
|
||||
|
||||
if (!$startpoint || !$volume)
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$volume = (int) $volume;
|
||||
$limits[$key]['volume'] = max($volume, 1);
|
||||
|
||||
if (!\controllers\internals\Tool::validate_relative_date($startpoint))
|
||||
{
|
||||
unset($limits[$key]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$adapters = $this->internal_adapter->list_adapters();
|
||||
$find_adapter = false;
|
||||
foreach ($adapters as $metas)
|
||||
{
|
||||
if ($metas['meta_classname'] === $adapter)
|
||||
{
|
||||
$find_adapter = $metas;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$find_adapter)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($find_adapter['meta_hidden'])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//If missing required data fields, error
|
||||
foreach ($find_adapter['meta_data_fields'] as $field)
|
||||
{
|
||||
if (false === $field['required'])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($adapter_data[$field['name']]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
//If field phone number is invalid
|
||||
foreach ($find_adapter['meta_data_fields'] as $field)
|
||||
{
|
||||
if ('phone_number' !== ($field['type'] ?? false))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($adapter_data[$field['name']]))
|
||||
{
|
||||
$adapter_data[$field['name']] = \controllers\internals\Tool::parse_phone($adapter_data[$field['name']]);
|
||||
|
||||
if ($adapter_data[$field['name']])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$adapter_data = json_encode($adapter_data);
|
||||
|
||||
//Check adapter is working correctly with thoses names and data
|
||||
$adapter_classname = $find_adapter['meta_classname'];
|
||||
$adapter_instance = new $adapter_classname($adapter_data);
|
||||
$adapter_working = $adapter_instance->test();
|
||||
|
||||
if (!$adapter_working)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$success = $this->internal_phone->update_for_user($id_user, $id_phone, $name, $adapter, $adapter_data, $limits);
|
||||
if (!$success)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$nb_update ++;
|
||||
}
|
||||
|
||||
if ($nb_update !== \count($_POST['phones']))
|
||||
{
|
||||
\FlashMessage\FlashMessage::push('danger', 'Certains téléphones n\'ont pas pu êtres mis à jour.');
|
||||
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'list'));
|
||||
}
|
||||
|
||||
\FlashMessage\FlashMessage::push('success', 'Tous les téléphones ont été modifiés avec succès.');
|
||||
|
||||
return $this->redirect(\descartes\Router::url('Phone', 'list'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,6 +160,8 @@
|
|||
'add' => '/phone/add/',
|
||||
'create' => '/phone/create/{csrf}/',
|
||||
'delete' => '/phone/delete/{csrf}/',
|
||||
'edit' => '/phone/edit/',
|
||||
'update' => '/phone/update/{csrf}/',
|
||||
],
|
||||
|
||||
'Call' => [
|
||||
|
|
|
@ -198,7 +198,7 @@
|
|||
'<div class="row phone-limits-group">'+
|
||||
'<div class="col-xs-4">'+
|
||||
'<label>Période</label><br/>'+
|
||||
'<select name="limits[' + random_id + '][startpoint]" class="form-control" id="adapter-select" required>'+
|
||||
'<select name="limits[' + random_id + '][startpoint]" class="form-control" required>'+
|
||||
'<option value="" disabled selected>Période sur laquelle appliquer la limite</option>'+
|
||||
'<option value="today">Par jour</option>'+
|
||||
'<option value="-24 hours">24 heures glissantes</option>'+
|
||||
|
|
|
@ -0,0 +1,271 @@
|
|||
<?php
|
||||
//Template dashboard
|
||||
|
||||
$this->render('incs/head', ['title' => 'Phones - Edit'])
|
||||
?>
|
||||
<div id="wrapper">
|
||||
<?php
|
||||
$this->render('incs/nav', ['page' => 'phones'])
|
||||
?>
|
||||
<div id="page-wrapper">
|
||||
<div class="container-fluid">
|
||||
<!-- Page Heading -->
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<h1 class="page-header">
|
||||
Modification téléphones
|
||||
</h1>
|
||||
<ol class="breadcrumb">
|
||||
<li>
|
||||
<i class="fa fa-dashboard"></i> <a href="<?php echo \descartes\Router::url('Dashboard', 'show'); ?>">Dashboard</a>
|
||||
</li>
|
||||
<li>
|
||||
<i class="fa fa-phone"></i> <a href="<?php echo \descartes\Router::url('Phone', 'list'); ?>">Téléphones</a>
|
||||
</li>
|
||||
<li class="active">
|
||||
<i class="fa fa-edit"></i> Modifier
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /.row -->
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title"><i class="fa fa-phone fa-fw"></i> Modification de téléphones</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form action="<?php echo \descartes\Router::url('Phone', 'update', ['csrf' => $_SESSION['csrf']]);?>" method="POST">
|
||||
<?php foreach ($phones as $phone) { ?>
|
||||
<div class="entry-container" data-entry-id="<?php $this->s($phone['id']); ?>">
|
||||
<div class="form-group">
|
||||
<label>Nom du téléphone</label>
|
||||
<p class="italic small help">
|
||||
Le nom du téléphone qui enverra et recevra les messages.
|
||||
</p>
|
||||
<div class="form-group">
|
||||
<input required="required" name="phones[<?php $this->s($phone['id']); ?>][name]" class="form-control" placeholder="Nom du téléphone" value="<?php $this->s($phone['name']); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Type de téléphone</label>
|
||||
<p class="italic small help description-adapter-general">
|
||||
Le type de téléphone utilisé par RaspiSMS pour envoyer ou recevoir les SMS. Pour plus d'information, consultez <a href="https://documentation.raspisms.fr/users/adapters/overview.html" target="_blank">la documentation de RaspiSMS</a> concernant les différents types de téléphones.
|
||||
</p>
|
||||
<select name="phones[<?php $this->s($phone['id']); ?>][adapter]" class="form-control adapter-select">
|
||||
<?php foreach ($adapters as $adapter) { ?>
|
||||
<?php if ($adapter['meta_hidden'] === false) { ?>
|
||||
<option
|
||||
value="<?= $adapter['meta_classname'] ?>"
|
||||
data-description="<?php $this->s($adapter['meta_description']); ?>"
|
||||
data-data-fields="<?php $this->s(json_encode($adapter['meta_data_fields'])); ?>"
|
||||
<?= ($phone['adapter'] ?? '') == $adapter['meta_classname'] ? 'selected' : '' ?>
|
||||
>
|
||||
<?php $this->s($adapter['meta_name']); ?>
|
||||
</option>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group well adapter-data-container">
|
||||
<div class="adapter-data-description-container">
|
||||
<h4>Description du téléphone</h4>
|
||||
<div class="adapter-data-description"></div>
|
||||
</div>
|
||||
|
||||
<div class="adapter-data-fields-container">
|
||||
<h4>Réglages du téléphone</h4>
|
||||
<div class="adapter-data-fields"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Limites des volumes d'envoi du téléphone</label>
|
||||
<p class="italic small help">
|
||||
Défini le nombre maximum de SMS qui pourront être envoyés avec ce téléphone sur des périodes de temps données.
|
||||
</p>
|
||||
<div class="form-group phone-limits-container container-fluid">
|
||||
<?php foreach ($phone['limits'] as $limit) { ?>
|
||||
<div class="row phone-limits-group">
|
||||
<div class="col-xs-4">
|
||||
<label>Période</label><br/>
|
||||
<?php $random_id = uniqid(); ?>
|
||||
<select name="phones[<?= $phone['id']; ?>][limits][<?= $random_id; ?>][startpoint]" class="form-control" required>
|
||||
<option value="" disabled selected>Période sur laquelle appliquer la limite</option>
|
||||
<option <?= $limit['startpoint'] == 'today' ? 'selected' : ''; ?> value="today">Par jour</option>
|
||||
<option <?= $limit['startpoint'] == '-24 hours' ? 'selected' : ''; ?> value="-24 hours">24 heures glissantes</option>
|
||||
<option <?= $limit['startpoint'] == 'this week midnight' ? 'selected' : ''; ?> value="this week midnight">Cette semaine</option>
|
||||
<option <?= $limit['startpoint'] == '-7 days' ? 'selected' : ''; ?> value="-7 days">7 jours glissants</option>
|
||||
<option <?= $limit['startpoint'] == 'this week midnight -1 week' ? 'selected' : ''; ?> value="this week midnight -1 week">Ces deux dernières semaines</option>
|
||||
<option <?= $limit['startpoint'] == '-14 days' ? 'selected' : ''; ?> value="-14 days">14 jours glissants</option>
|
||||
<option <?= $limit['startpoint'] == 'this month midnight' ? 'selected' : ''; ?> value="this month midnight">Ce mois</option>
|
||||
<option <?= $limit['startpoint'] == '-1 month' ? 'selected' : ''; ?> value="-1 month">1 mois glissant</option>
|
||||
<option <?= $limit['startpoint'] == '-28 days' ? 'selected' : ''; ?> value="-28 days">28 jours glissants</option>
|
||||
<option <?= $limit['startpoint'] == '-30 days' ? 'selected' : ''; ?> value="-30 days">30 jours glissants</option>
|
||||
<option <?= $limit['startpoint'] == '-31 days' ? 'selected' : ''; ?> value="-31 days">31 jours glissants</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="scheduleds-number-data-container col-xs-8">
|
||||
<label>Volume</label>
|
||||
<div class="form-group">
|
||||
<input name="phones[<?= $phone['id']; ?>][limits][<?= $random_id; ?>][volume]" class="form-control" type="number" min="1" value="<?php $this->s($limit['volume']); ?>" placeholder="Nombre de SMS maximum sur la période.">
|
||||
</div>
|
||||
</div>
|
||||
<a href="#" class="phone-limits-group-remove"><span class="fa fa-times"></span></a>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<div class="text-center"><div class="add-phone-limit-button fa fa-plus-circle"></div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr/>
|
||||
<?php } ?>
|
||||
<a class="btn btn-danger" href="<?php echo \descartes\Router::url('Phone', 'list'); ?>">Annuler</a>
|
||||
<input type="submit" class="btn btn-success" value="Enregistrer le téléphone" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
function change_adapter (target)
|
||||
{
|
||||
var phone_id = target.parents('.entry-container').attr('data-entry-id');
|
||||
|
||||
var option = target.find('option:selected');
|
||||
target.parents('.entry-container').find('.adapter-data-description').html(option.attr('data-description'));
|
||||
target.parents('.entry-container').find('.description-adapter-data').text(option.attr('data-data-help'));
|
||||
|
||||
var data_fields = option.attr('data-data-fields');
|
||||
data_fields = JSON.parse(data_fields);
|
||||
|
||||
var numbers = [];
|
||||
|
||||
var html = '';
|
||||
jQuery.each(data_fields, function (index, field)
|
||||
{
|
||||
value = field.value ? field.value : (field.default_value ? field.default_value : null);
|
||||
|
||||
if (field.type == 'phone_number')
|
||||
{
|
||||
var random_id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||
html += '' +
|
||||
'<div class="form-group">' +
|
||||
'<label>' + field.title + '</label>' +
|
||||
'<p class="italic small help">' + field.description + '</p>' +
|
||||
'<div class="form-group">' +
|
||||
'<input name="" class="form-control phone-international-input" type="tel" id="' + random_id + '" ' + (field.required ? 'required' : '') + ' ' + (value ? 'value="' + value + '"' : '') + '>' +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
|
||||
var number = {
|
||||
'id': random_id,
|
||||
'name': field.name,
|
||||
};
|
||||
|
||||
numbers.push(number);
|
||||
}
|
||||
else if (field.type == 'boolean')
|
||||
{
|
||||
html += '' +
|
||||
'<div class="form-group">' +
|
||||
'<label>' + field.title + '</label>' +
|
||||
'<p class="italic small help">' + field.description + '</p>' +
|
||||
'<div class="form-group">' +
|
||||
'<input type="checkbox" name="phones[' + phone_id + '][adapter_data][' + field.name + ']" class="form-control" ' + (field.required ? 'required' : '') + ' ' + (value ? 'value="' + value + '" checked' : 'value="1"') + '><label class="switch" for="adapter_data[' + field.name + ']"></label>' +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
}
|
||||
else
|
||||
{
|
||||
html += '' +
|
||||
'<div class="form-group">' +
|
||||
'<label>' + field.title + '</label>' +
|
||||
'<p class="italic small help">' + field.description + '</p>' +
|
||||
'<div class="form-group">' +
|
||||
'<input name="phones[' + phone_id + '][adapter_data][' + field.name + ']" class="form-control" ' + (field.required ? 'required' : '') + ' ' + (value ? 'value="' + value + '"' : '') + '>' +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
}
|
||||
});
|
||||
|
||||
if (html == '')
|
||||
{
|
||||
html = 'Pas de réglages.';
|
||||
}
|
||||
|
||||
target.parents('.entry-container').find('.adapter-data-fields').html(html);
|
||||
|
||||
for (i = 0; i < numbers.length; i++)
|
||||
{
|
||||
var iti_number_input = window.intlTelInput(document.getElementById(numbers[i].id), {
|
||||
hiddenInput: 'phones[' + phone_id + '][adapter_data][' + numbers[i].name + ']',
|
||||
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',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
jQuery('document').ready(function($)
|
||||
{
|
||||
jQuery('.adapter-select').each(function () {
|
||||
change_adapter(jQuery(this));
|
||||
});
|
||||
|
||||
jQuery('.adapter-select').on('change', function (e)
|
||||
{
|
||||
change_adapter(jQuery(this));
|
||||
});
|
||||
|
||||
jQuery('body').on('click', '.phone-limits-group-remove', function (e)
|
||||
{
|
||||
e.preventDefault();
|
||||
jQuery(this).parent('.phone-limits-group').remove();
|
||||
return false;
|
||||
});
|
||||
|
||||
jQuery('body').on('click', '.add-phone-limit-button', function(e)
|
||||
{
|
||||
var phone_id = jQuery(this).parents('.entry-container').attr('data-entry-id');
|
||||
var random_id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||
var newLimit = '' +
|
||||
'<div class="row phone-limits-group">'+
|
||||
'<div class="col-xs-4">'+
|
||||
'<label>Période</label><br/>'+
|
||||
'<select name="phones[' + phone_id + '][limits][' + random_id + '][startpoint]" class="form-control adapter-select" required>'+
|
||||
'<option value="" disabled selected>Période sur laquelle appliquer la limite</option>'+
|
||||
'<option value="today">Par jour</option>'+
|
||||
'<option value="-24 hours">24 heures glissantes</option>'+
|
||||
'<option value="this week midnight">Cette semaine</option>'+
|
||||
'<option value="-7 days">7 jours glissants</option>'+
|
||||
'<option value="this week midnight -1 week">Ces deux dernières semaines</option>'+
|
||||
'<option value="-14 days">14 jours glissants</option>'+
|
||||
'<option value="this month midnight">Ce mois</option>'+
|
||||
'<option value="-1 month">1 mois glissant</option>'+
|
||||
'<option value="-28 days">28 jours glissants</option>'+
|
||||
'<option value="-30 days">30 jours glissants</option>'+
|
||||
'<option value="-31 days">31 jours glissants</option>'+
|
||||
'</select>'+
|
||||
'</div>'+
|
||||
'<div class="scheduleds-number-data-container col-xs-8">'+
|
||||
'<label>Volume</label>'+
|
||||
'<div class="form-group">'+
|
||||
'<input name="phones[' + phone_id + '][limits][' + random_id + '][volume]" class="form-control" type="number" min="1" placeholder="Nombre de SMS maximum sur la période.">'+
|
||||
'</div>'+
|
||||
'</div>'+
|
||||
'<a href="#" class="phone-limits-group-remove"><span class="fa fa-times"></span></a>'+
|
||||
'</div>';
|
||||
|
||||
jQuery(this).parent('div').before(newLimit);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
$this->render('incs/footer');
|
|
@ -56,6 +56,7 @@
|
|||
</div>
|
||||
<div class="text-right col-xs-6 no-padding">
|
||||
<strong>Action pour la séléction :</strong>
|
||||
<button class="btn btn-default" type="submit" formaction="<?php echo \descartes\Router::url('Phone', 'edit'); ?>"><span class="fa fa-edit"></span> Modifier</button>
|
||||
<button class="btn btn-default btn-confirm" type="submit" formaction="<?php echo \descartes\Router::url('Phone', 'delete', ['csrf' => $_SESSION['csrf']]); ?>"><span class="fa fa-trash-o"></span> Supprimer</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue