update settings

This commit is contained in:
osaajani 2019-11-11 04:05:26 +01:00
parent a99e159b1f
commit 9b7b78b306
12 changed files with 92 additions and 52 deletions

View File

@ -21,22 +21,52 @@ namespace controllers\internals;
}
/**
* Return all settings.
*
* @return array || false
* Return all settings of a user.
* @param int $id_user : user id
* @return array
*/
public function get_all()
public function gets_for_user (int $id_user)
{
return $this->model_setting->get_all();
$settings = $this->model_setting->gets_for_user($id_user);
$settings_array = [];
foreach ($settings as $setting)
{
$settings_array[$setting['name']] = $setting['value'];
}
return $settings_array;
}
/**
* Update a setting by his name.
*
* Update a setting by his name and user id.
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value
* @return int : number of modified lines
*/
public function update(string $name, $value): bool
public function update (int $id_user, string $name, $value) : bool
{
return (bool) $this->model_setting->update($name, $value);
return (bool) $this->model_setting->update($id_user, $name, $value);
}
/**
* Create a new setting
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value : value of the setting
* @return bool
*/
public function insert (int $id_user, string $name, $value) : bool
{
$setting = [
'id_user' => $id_user,
'name' => $name,
'value' => $value,
];
return (bool) $this->model_setting->insert($setting);
}
}

View File

@ -17,6 +17,7 @@ namespace controllers\publics;
class Connect extends \descartes\Controller
{
private $internal_user;
private $internal_setting;
/**
* Cette fonction est appelée avant toute les autres :.
@ -28,6 +29,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_setting = new \controllers\internals\Setting($bdd);
}
/**
@ -62,6 +64,9 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Connect', 'login'));
}
$settings = $this->internal_setting->gets_for_user($user['id']);
$user['settings'] = $settings;
$_SESSION['connect'] = true;
$_SESSION['user'] = $user;

View File

@ -51,24 +51,16 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}
if (!\controllers\internals\Tool::is_admin())
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez être administrateur pour pouvoir modifier un réglage.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}
$setting_value = $_POST['setting_value'] ?? false;
if (false === $setting_value)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner une valeure pour le réglage.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}
$update_setting_result = $this->internal_setting->update($setting_name, $setting_value);
$update_setting_result = $this->internal_setting->update($_SESSION['user']['id'], $setting_name, $setting_value);
if (false === $update_setting_result)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.');
@ -77,7 +69,6 @@ namespace controllers\publics;
}
\FlashMessage\FlashMessage::push('success', 'Le réglage a bien été mis à jour.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}
}

View File

@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS setting
value VARCHAR(1000) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (name)
UNIQUE (id_user, name)
);
CREATE TABLE IF NOT EXISTS scheduled

View File

@ -18,21 +18,35 @@ namespace models;
{
/**
* Return array of all settings.
* @param int $id_user : user id
* @return array
*/
public function get_all(): array
public function gets_for_user (int $id_user): array
{
return $this->_select('setting');
return $this->_select('setting', ['id_user' => $id_user]);
}
/**
* Create a new setting
* @param array $setting
* @return bool
*/
public function insert (array $setting) : bool
{
return (bool) $this->_insert('setting', $setting);
}
/**
* Update a setting by his name.
*
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value
*
* @return int : number of modified lines
*/
public function update(string $name, $value): int
public function update (int $id_user, string $name, $value): int
{
return $this->_update('setting', ['value' => $value], ['name' => $name]);
return $this->_update('setting', ['value' => $value], ['id_user' => $id_user, 'name' => $name]);
}
}

View File

@ -67,8 +67,8 @@
var number_input = jQuery('#phone-international-input')[0];
var iti_number_input = window.intlTelInput(number_input, {
hiddenInput: 'number',
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});

View File

@ -74,8 +74,8 @@
var hidden_input_name = 'contacts[' + jQuery(number_input).attr('contact-id') + '][number]';
var iti_number_input = window.intlTelInput(number_input, {
hiddenInput: hidden_input_name,
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});

View File

@ -70,7 +70,7 @@
$.each(data.messages, function(key, message) {
<?php if (RASPISMS_SETTINGS_DETECT_URL) { ?>
<?php if ($_SESSION['user']['settings']['detect_url']) { ?>
//On ajoute la detection de lien dans le texte du message
message.text = Autolinker.link(message.text, {newWindow:true});
<?php } ?>

View File

@ -3,7 +3,7 @@
Copyright 2014. RaspiSMS est un programme sous <a href="https://www.gnu.org/licenses/gpl.txt" rel="nofollow">licence GNU GPL</a>.<br/>
</footer>
<?php if (RASPISMS_SETTINGS_SMS_RECEPTION_SOUND) { ?>
<?php if ($_SESSION['user']['settings']['sms_reception_sound'] ?? false) { ?>
<audio id="reception-sound">
<source src="<?php echo HTTP_PWD_SOUND; ?>/receptionSound.ogg" type="audio/ogg">
<source src="<?php echo HTTP_PWD_SOUND; ?>/receptionSound.mp3" type="audio/mpeg">

View File

@ -64,12 +64,12 @@
<label>Groupes cibles</label>
<input class="add-groupes form-control" name="groups[]"/>
</div>
<?php if (RASPISMS_SETTINGS_SMS_FLASH) { ?>
<?php if ($_SESSION['user']['settings']['sms_flash']) { ?>
<div class="form-group">
<label>Envoyer comme un SMS Flash : </label>
<div class="form-group">
<input name="admin" type="radio" value="1" required /> Oui
<input name="admin" type="radio" value="0" required checked/> Non
<input name="flash" type="radio" value="1" required /> Oui
<input name="flash" type="radio" value="0" required checked/> Non
</div>
</div>
<?php } ?>
@ -135,8 +135,8 @@
var number_input = jQuery('#' + random_id)[0];
var iti_number_input = window.intlTelInput(number_input, {
hiddenInput: 'numbers[]',
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});
@ -150,8 +150,8 @@
var number_input = jQuery('.phone-international-input')[0];
var iti_number_input = window.intlTelInput(number_input, {
hiddenInput: 'numbers[]',
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});

View File

@ -67,7 +67,7 @@
<label>Groupes cibles</label>
<input class="add-groupes form-control" name="scheduleds[<?php $this->s($scheduled['id']); ?>][groups][]" value="<?php $this->s(json_encode($scheduled['groups'])); ?>" />
</div>
<?php if (RASPISMS_SETTINGS_SMS_FLASH) { ?>
<?php if ($_SESSION['user']['settings']['sms_flash']) { ?>
<div class="form-group">
<label>Envoyer comme un SMS Flash : </label>
<div class="form-group">
@ -127,8 +127,8 @@
var hidden_input_name = 'scheduleds[' + jQuery(this).attr('scheduled-id') + '][numbers][]';
window.intlTelInput(this, {
hiddenInput: hidden_input_name,
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});
@ -150,8 +150,8 @@
var phone_input = jQuery('#' + random_id)[0];
window.intlTelInput(phone_input, {
hiddenInput: hidden_input_name,
defaultCountry: '<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>',
preferredCountries: <?php $this->s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>,
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'
});

View File

@ -45,7 +45,7 @@
<label>Transfert activé : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo RASPISMS_SETTINGS_TRANSFER ? 'selected' : ''; ?>>Oui</option>
<option value="1" <?php echo $_SESSION['user']['settings']['transfer'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
@ -64,7 +64,7 @@
<label>Détection activé : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo RASPISMS_SETTINGS_DETECT_URL ? 'selected' : ''; ?>>Oui</option>
<option value="1" <?php echo $_SESSION['user']['settings']['detect_url'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
@ -81,7 +81,7 @@
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'preferred_phone_country', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
<div class="form-group">
<label>Code des pays (norme ISO 3166-1 alpha-2) séparés par des virgules : </label>
<input name="setting_value" class="form-control" value="<?php $this->s(RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY); ?>" />
<input name="setting_value" class="form-control" value="<?php $this->s($_SESSION['user']['settings']['preferred_phone_country']); ?>" />
</div>
<div class="text-center">
<button class="btn btn-success">Mettre à jour les données</button>
@ -99,7 +99,7 @@
<label>Jouer un son quand vous recevez un SMS : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo RASPISMS_SETTINGS_SMS_RECEPTION_SOUND ? 'selected' : ''; ?>>Oui</option>
<option value="1" <?php echo $_SESSION['user']['settings']['sms_reception_sound'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
@ -120,7 +120,7 @@
<label>SMS STOP activé : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo RASPISMS_SETTINGS_SMSSTOP ? 'selected' : ''; ?>>Oui</option>
<option value="1" <?php echo $_SESSION['user']['settings']['smsstop'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
@ -137,7 +137,7 @@
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'default_phone_country', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
<div class="form-group">
<label>Code du pays (norme ISO 3166-1 alpha-2) : </label>
<input name="setting_value" class="form-control" value="<?php $this->s(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>" />
<input name="setting_value" class="form-control" value="<?php $this->s($_SESSION['user']['settings']['default_phone_country']); ?>" />
</div>
<div class="text-center">
<button class="btn btn-success">Mettre à jour les données</button>
@ -155,7 +155,7 @@
<label>SMS Flash activé : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo RASPISMS_SETTINGS_SMS_FLASH ? 'selected' : ''; ?>>Oui</option>
<option value="1" <?php echo $_SESSION['user']['settings']['sms_flash'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">