raspisms/controllers/publics/Setting.php

64 lines
2.6 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
/**
* Page des settings
*/
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
* @param string $setting_name : Name of the setting to modify
* @param $csrf : CSRF token
* @param string $_POST['setting_value'] : Setting's new value
* @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)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!\controllers\internals\Tool::is_admin()) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez être administrateur pour pouvoir modifier un réglage.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
$setting_value = $_POST['setting_value'] ?? false;
2019-10-29 18:36:25 +01:00
if ($setting_value === false) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez renseigner une valeure pour le réglage.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
$update_setting_result = $this->internal_setting->update($setting_name, $setting_value);
2019-10-29 18:36:25 +01:00
if ($update_setting_result === false) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de mettre à jour ce réglage.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le réglage a bien été mis à jour.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
}