Commit 46748b40 authored by François Agneray's avatar François Agneray
Browse files

CRUD salle => DONE

parent d046359d
......@@ -101,6 +101,10 @@ $container[App\Action\DashboardAdminSalleAction::class] = function ($c) {
return new App\Action\DashboardAdminSalleAction($c->get('view'), $c->get('logger'), $c->get('em'));
};
$container[App\Action\DashboardAdminAddEditSalleAction::class] = function ($c) {
return new App\Action\DashboardAdminAddEditSalleAction($c->get('view'), $c->get('logger'), $c->get('em'));
};
$container[App\Action\DashboardAdminThematiqueAction::class] = function ($c) {
return new App\Action\DashboardAdminThematiqueAction($c->get('view'), $c->get('logger'), $c->get('em'));
};
......@@ -84,6 +84,10 @@ $app->get('/dashboard-admin-salle', App\Action\DashboardAdminSalleAction::class)
->add(App\Middleware\TokenMiddleware::class)
->setName('dashboard_admin_salle');
$app->map(['GET', 'POST', 'PUT', 'DELETE'], '/dashboard-admin-add-edit-salle', App\Action\DashboardAdminAddEditSalleAction::class)
->add(App\Middleware\TokenMiddleware::class)
->setName('dashboard_admin_add_edit_salle');
$app->get('/dashboard-admin-thematique', App\Action\DashboardAdminThematiqueAction::class)
->add(App\Middleware\TokenMiddleware::class)
->setName('dashboard_admin_thematique');
<?php
namespace App\Action;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
final class DashboardAdminAddEditSalleAction
{
private $view;
private $logger;
private $em;
public function __construct(Twig $view, LoggerInterface $logger, EntityManagerInterface $em)
{
$this->view = $view;
$this->logger = $logger;
$this->em = $em;
}
public function __invoke(Request $request, Response $response, $args)
{
$this->logger->info("dashboard admin add edit salle page action dispatched");
$params = $request->getQueryParams();
$token = $params['token'];
$roleSI = $request->getAttribute('roleSI');
if ($roleSI != 'admin') {
return $response->withStatus(401);
}
if($request->isGet()) {
$a = [
'page' => 'dashboard-admin-add-edit-salle',
'token' => $token
];
if (array_key_exists('salle_id', $params)) {
$salle = $this->em->find('App\Entity\Salle', $params['salle_id']);
$a['salle'] = $salle;
};
$this->view->render($response, 'dashboard_admin_add_edit_salle.twig', $a);
}
if($request->isPost()) {
$parsedBody = $request->getParsedBody();
$salle = new \App\Entity\Salle();
$salle->setNom($parsedBody['nom']);
$salle->setEtage($parsedBody['etage']);
$salle->setAile($parsedBody['aile']);
$salle->setDescription($parsedBody['description']);
$salle->setQuotaOfficiel($parsedBody['quota_officiel']);
$salle->setQuotaPhysique($parsedBody['quota_physique']);
$this->em->persist($salle);
$this->em->flush();
$response = $response->write('Nouvel salle enregistrée')->withStatus(201);
}
if($request->isPut()) {
$parsedBody = $request->getParsedBody();
$salle = $this->em->find('App\Entity\Salle', $parsedBody['salle_id']);
$salle->setNom($parsedBody['nom']);
$salle->setEtage($parsedBody['etage']);
$salle->setAile($parsedBody['aile']);
$salle->setDescription($parsedBody['description']);
$salle->setQuotaOfficiel($parsedBody['quota_officiel']);
$salle->setQuotaPhysique($parsedBody['quota_physique']);
$this->em->flush();
$response = $response->write('Salle n°' . $salle->getId() . ' modifiée')->withStatus(201);
}
if ($request->isDelete()) {
$salle = $this->em->find('App\Entity\Salle', $params['salle_id']);
$this->em->remove($salle);
$this->em->flush();
$response = $response->write('Salle id: ' . $params['salle_id'] . ' supprimée')->withStatus(200);
}
return $response;
}
}
......@@ -32,11 +32,21 @@ final class DashboardAdminSalleAction
return $response->withStatus(401);
}
$salles = $this->getSalles();
$this->view->render($response, 'dashboard_admin_salle.twig', [
'page' => 'dashboard-admin-salle',
'token' => $token
'token' => $token,
'salles' => $salles
]);
return $response;
}
private function getSalles()
{
$dql = "SELECT s FROM App\Entity\Salle s ORDER BY s.id DESC";
$query = $this->em->createQuery($dql);
return $query->getResult();
}
}
......@@ -71,6 +71,6 @@
<script src="js/inscription.js"></script>
<script src="js/connexion.min.js"></script>
<script src="js/new_passwd.min.js"></script>
<script src="js/admin.min.js"></script>
<script src="js/admin.js"></script>
</body>
</html>
{% extends "base_dashboard_admin.twig" %}
{% block title %}Admin{% endblock %}
{% block content_dashboard_admin %}
<div class="content-dashboard">
<div class="title-main">
<div class="title-main-content">Ajout d'une salle</div>
</div>
<div class="row">
<div class="col-md-8">
{% if salle %}
<h4><i class="fa fa-sign-in"></i> Formulaire d'édition d'une salle</h4>
{% else %}
<h4><i class="fa fa-sign-in"></i> Formulaire d'ajout d'une salle</h4>
{% endif %}
<br>
<form id="form_add_edit_salle" action="dashboard-admin-add-edit-salle?token={{ token }}" role="form" data-toggle="validator">
{% if salle %}
<input type="hidden" id="salle_id" name="salle_id" value="{{ salle.getId() }}">
{% else %}
<input type="hidden" id="salle_id" name="salle_id" value="0">
{% endif %}
<div class="form-group">
<label for="nom">Nom</label> <span style="color: red;">*</span>
<input type="text" class="form-control" name="nom" id="nom" placeholder="Nom" {% if salle %}value="{{ salle.getNom() }}"{% endif %} required>
</div>
<div class="form-group">
<label for="etage">Etage</label> <span style="color: red;">*</span>
<input type="number" class="form-control" name="etage" id="etage" placeholder="Etage" {% if salle %}value="{{ salle.getEtage() }}"{% endif %} required>
</div>
<div class="form-group">
<label for="aile">Aile</label> <span style="color: red;">*</span>
<input type="text" class="form-control" name="aile" id="aile" placeholder="Aile" {% if salle %}value="{{ salle.getAile() }}"{% endif %} required>
</div>
<div class="form-group">
<label for="description">Description</label> <span style="color: red;">*</span>
<textarea class="form-control" rows="3" name="description" id="description" placeholder="Description" required>{% if salle %}{{ salle.getDescription() }}{% endif %}</textarea>
</div>
<div class="form-group">
<label for="quota_officiel">Quota officiel</label> <span style="color: red;">*</span>
<input type="number" class="form-control" name="quota_officiel" id="quota_officiel" placeholder="Quota officiel" {% if salle %}value="{{ salle.getQuotaOfficiel() }}"{% endif %} required>
</div>
<div class="form-group">
<label for="quota_physique">Quota physique</label> <span style="color: red;">*</span>
<input type="number" class="form-control" name="quota_physique" id="quota_physique" placeholder="Quota physique" {% if salle %}value="{{ salle.getQuotaPhysique() }}"{% endif %} required>
</div>
<a href="dashboard-admin-salle?token={{ token }}" id="btn-back-salles" class="btn btn-danger">Retour liste des salles</a>
<button type="submit" id="btn-ajout-salle" class="btn btn-primary" value="Envoyer">{% if salle %}Editer salle{% else %}Ajouter salle{% endif %}</button>
</form>
</div>
</div>
</div>
{% endblock %}
......@@ -8,13 +8,46 @@
</div>
<div>
<a href="dashboard-admin-add-edit-salle?token={{ token }}" class="btn btn-treehouse pull-right">Ajouter une salle</a>
<h4><i class="fa fa-check-square-o"></i> Gestion des Salles</h4>
<ul>
<li>Affichage</li>
<li>Ajout</li>
<li>Edition</li>
<li>Suppression</li>
</ul>
<br>
{% if salles %}
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Nom</th>
<th>Etage</th>
<th>Aile</th>
<th>Description</th>
<th>Quota Officiel</th>
<th>Quota physique</th>
<th colspan="2" class="text-center">Actions</th>
</tr>
</thead>
<tbody>
{% for salle in salles %}
<tr>
<td>{{ salle.getNom() }}</td>
<td>{{ salle.getEtage() }}</td>
<td>{{ salle.getAile() }}</td>
<td>{{ salle.getDescription() }}</td>
<td>{{ salle.getQuotaOfficiel() }}</td>
<td>{{ salle.getQuotaPhysique() }}</td>
<td class="text-center">
<a href="dashboard-admin-add-edit-salle?salle_id={{ salle.getId() }}&token={{ token }}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
</td>
<td class="text-center">
<a class="btn-delete-salle" href="dashboard-admin-add-edit-salle?salle_id={{ salle.getId() }}&token={{ token }}"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<p>Pas encore de salles</p>
{% endif %}
</div>
</div>
{% endblock %}
......@@ -17,4 +17,50 @@ $(document).ready(function() {
});
});
// Formulaire d'ajout ou de modification d'une salle
$('#form_add_edit_salle').validator().on('submit', function(e) {
if (e.isDefaultPrevented())
{
alert('Veuillez remplir tous les champs obligatoires');
}
else
{
e.preventDefault();
var salle_id = $('#salle_id').val();
if (salle_id > 0) {
var vtype = 'PUT';
} else {
var vtype = 'POST';
}
var form = $(this);
$.ajax({
url: form.attr('action'),
type: vtype,
data: form.serialize(),
success: function(text) {
document.location.href = $('#btn-back-salles').attr('href');
},
error: function() {
alert('La salle n\'a pas pu être ajoutée');
form.trigger('reset');
}
});
}
});
$('.btn-delete-salle').on('click', function(e) {
e.preventDefault();
$.ajax({
url: $(this).attr('href'),
type: 'DELETE',
success: function(text) {
location.reload();
},
error: function() {
alert('La salle n\'a pas pu être supprimée');
}
});
});
});
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment