raspisms/controllers/publics/Setting.php

94 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
2019-10-29 14:57:13 +01:00
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
/**
* Page des settings.
2019-10-29 18:36:25 +01:00
*/
class Setting extends \descartes\Controller
2019-10-29 14:57:13 +01:00
{
private $internal_setting;
2019-10-29 18:36:25 +01:00
public function __construct()
2019-10-29 14:57:13 +01:00
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
2019-10-29 14:57:13 +01:00
$this->internal_setting = new \controllers\internals\Setting($bdd);
\controllers\internals\Tool::verifyconnect();
2019-10-29 14:57:13 +01:00
}
/**
* Return all settings to administrate them.
2019-10-29 18:36:25 +01:00
*/
public function show()
2019-10-29 14:57:13 +01:00
{
return $this->render('setting/show');
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
/**
* Update a setting value identified by his name.
*
2019-10-29 14:57:13 +01:00
* @param string $setting_name : Name of the setting to modify
* @param $csrf : CSRF token
2021-06-17 00:51:33 +02:00
* @param string $_POST['setting_value'] : Setting's new value
* @param bool $_POST['allow_no_value'] : Default false, if true then allow $_POST['setting_value'] to dont exists, and treat it as empty string
*
2019-10-29 14:57:13 +01:00
* @return boolean;
*/
2019-10-29 18:36:25 +01:00
public function update(string $setting_name, string $csrf)
2019-10-29 14:57:13 +01:00
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2020-01-17 18:19:25 +01:00
2019-10-29 14:57:13 +01:00
$setting_value = $_POST['setting_value'] ?? false;
2021-06-14 21:33:06 +02:00
$allow_no_value = $_POST['allow_no_value'] ?? false;
//if no value allowed and no value fund, default to ''
2021-06-17 00:51:33 +02:00
if ($allow_no_value && (false === $setting_value))
2021-06-14 21:33:06 +02:00
{
$setting_value = '';
}
2019-10-29 14:57:13 +01:00
if (false === $setting_value)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner une valeure pour le réglage.');
2020-01-17 18:19:25 +01:00
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2021-06-14 21:33:06 +02:00
//If setting is an array, join with comas
if (is_array($setting_value))
{
$setting_value = json_encode($setting_value);
}
$update_setting_result = $this->internal_setting->update_for_user($_SESSION['user']['id'], $setting_name, $setting_value);
if (false === $update_setting_result)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-11-12 17:37:20 +01:00
$settings = $this->internal_setting->gets_for_user($_SESSION['user']['id']);
$_SESSION['user']['settings'] = $settings;
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('success', 'Le réglage a bien été mis à jour.');
2020-01-17 18:19:25 +01:00
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
}