raspisms/controllers/publics/Command.php

178 lines
5.8 KiB
PHP
Raw 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 commandes.
2019-10-29 18:36:25 +01:00
*/
class Command extends \descartes\Controller
2019-10-29 14:57:13 +01:00
{
private $internal_command;
private $internal_event;
2019-10-29 14:57:13 +01:00
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_command = new \controllers\internals\Command($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
if (!ENABLE_COMMAND)
{
\FlashMessage\FlashMessage::push('danger', 'Les commandes sont désactivées.');
$this->redirect(\descartes\Router::url('Dashboard', 'show'));
exit(0);
}
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne tous les users, sous forme d'un tableau permettant l'administration de ces users.
2019-10-29 18:36:25 +01:00
*/
2020-04-07 03:02:33 +02:00
public function list()
2019-10-29 14:57:13 +01:00
{
2020-04-07 03:02:33 +02:00
$commands = $this->internal_command->list_for_user($_SESSION['user']['id']);
2019-10-29 14:57:13 +01:00
$this->render('command/list', ['commands' => $commands]);
2019-10-29 18:36:25 +01:00
}
/**
* Cette fonction va supprimer une liste de commands.
*
2019-10-29 18:36:25 +01:00
* @param array int $_GET['ids'] : Les id des commandes à supprimer
* @param mixed $csrf
*
2019-10-29 18:36:25 +01:00
* @return boolean;
*/
public function delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
$this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 18:36:25 +01:00
return false;
}
2019-10-29 14:57:13 +01:00
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
2019-11-14 22:33:00 +01:00
$this->internal_command->delete_for_user($_SESSION['user']['id'], $id);
2019-10-29 14:57:13 +01:00
}
2019-11-04 17:12:52 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 18:36:25 +01:00
}
/**
* Cette fonction retourne la page d'ajout d'une commande.
2019-10-29 18:36:25 +01:00
*/
public function add()
{
$this->render('command/add');
}
/**
* Cette fonction retourne la page d'édition des commandes.
*
2019-10-29 18:36:25 +01:00
* @param array int $_GET['ids'] : Les id des commandes à editer
*/
public function edit()
{
2019-10-29 14:57:13 +01:00
$ids = $_GET['ids'] ?? [];
2019-11-14 22:33:00 +01:00
$commands = $this->internal_command->gets_in_for_user($_SESSION['user']['id'], $ids);
2019-10-29 14:57:13 +01:00
$this->render('command/edit', [
2019-10-29 18:36:25 +01:00
'commands' => $commands,
]);
2019-10-29 18:36:25 +01:00
}
/**
* Cette fonction insert une nouvelle commande.
*
2019-10-29 18:36:25 +01:00
* @param $csrf : Le jeton CSRF
* @param string $_POST['name'] : Le nom de la commande
2019-10-29 18:36:25 +01:00
* @param string $_POST['script'] : Le script a appeler
* @param bool $_POST['admin'] : Si la commande necessite les droits d'admin (par défaut non)
*
2019-10-29 18:36:25 +01:00
* @return boolean;
*/
public function create($csrf)
{
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
2019-11-04 17:12:52 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
$name = $_POST['name'] ?? false;
$script = $_POST['script'] ?? false;
2019-11-04 17:12:52 +01:00
$admin = $_POST['admin'] ?? false;
2019-10-29 14:57:13 +01:00
if (!$name || !$script)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Renseignez au moins un nom et un script.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-11-14 22:33:00 +01:00
if (!$this->internal_command->create($_SESSION['user']['id'], $name, $script, $admin))
{
2019-11-10 01:40:03 +01:00
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer cette commande.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('commands', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('success', 'La commande a bien été crée.');
2019-11-10 17:36:42 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 18:36:25 +01:00
}
/**
* Cette fonction met à jour une commande.
*
2019-10-29 18:36:25 +01:00
* @param $csrf : Le jeton CSRF
* @param array $_POST['commands'] : Un tableau des commandes avec leur nouvelle valeurs
*
2019-10-29 18:36:25 +01:00
* @return boolean;
*/
public function update($csrf)
{
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
2019-11-04 17:12:52 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 14:57:13 +01:00
}
$nb_commands_update = 0;
foreach ($_POST['commands'] as $command)
{
2019-11-14 22:33:00 +01:00
$update_command = $this->internal_command->update_for_user($_SESSION['user']['id'], $command['id'], $command['name'], $command['script'], $command['admin']);
2019-10-29 14:57:13 +01:00
$nb_commands_update += (int) $update_command;
}
if ($nb_commands_update !== \count($_POST['commands']))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Certaines commandes n\'ont pas pu êtres mises à jour.');
2019-11-04 17:12:52 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('success', 'Toutes les commandes ont été modifiées avec succès.');
2019-11-04 17:12:52 +01:00
return $this->redirect(\descartes\Router::url('Command', 'list'));
2019-10-29 18:36:25 +01:00
}
}