Skip to content

Commit

Permalink
Merge pull request #161 from darnuria/rewrite/objets
Browse files Browse the repository at this point in the history
Rewrite/objets
  • Loading branch information
mart1ver authored Mar 27, 2017
2 parents 8669979 + bd8ecd3 commit a138884
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 164 deletions.
57 changes: 56 additions & 1 deletion core/requetes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,59 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

function grilles_objets_id($bdd, $id_dechet) {
function objet_id_dechet($bdd, $id_dechet) {
$req = $bdd->prepare("SELECT * FROM grille_objets WHERE id_type_dechet = :id_type_dechet");
$req->bindValue(':id_type_dechet', $id_dechet, PDO::PARAM_INT);
$req->execute();
return $req->fetchAll(PDO::FETCH_ASSOC);
}

function objet_id($bdd, $id_obj) {
$req = $bdd->prepare("SELECT * FROM grille_objets WHERE id = :id_obj");
$req->bindValue(':id_obj', $id_obj, PDO::PARAM_INT);
$req->execute();
$result = $req->fetch(PDO::FETCH_ASSOC);
$req->closeCursor();
return $result;
}

function objet_update_visible($bdd, $id, $visible) {
$req = $bdd->prepare('update grille_objets set visible = :visible where id = :id');
$req->bindValue(':id', $id, PDO::PARAM_INT);
$req->bindValue(':visible', $visible, PDO::PARAM_STR);
$req->execute();
$req->closeCursor();
}

function objet_update_nom($bdd, $id, $nom) {
$req = $bdd->prepare('update grille_objets set nom = :nom where id = :id');
$req->bindValue(':id', $id, PDO::PARAM_INT);
$req->bindValue(':nom', $nom, PDO::PARAM_STR);
$req->execute();
$req->closeCursor();
}

function objet_update($bdd, $id, $prix, $nom, $description) {
$req = $bdd->prepare('
update grille_objets
set nom = :nom1,
description = :description,
prix = :prix
where BINARY nom <> :nom2
and id = :id');
$req->bindValue(':id', $id, PDO::PARAM_INT);
$req->bindValue(':prix', $prix);
$req->bindParam(':nom1', $nom, PDO::PARAM_STR);
$req->bindParam(':nom2', $nom, PDO::PARAM_STR);
$req->bindParam(':description', $description, PDO::PARAM_STR);
$req->execute();
if ($req->rowCount() === 0) {
$req->closeCursor();
throw new UnexpectedValueException('Un objet avec le meme nom existe deja.');
}
$req->closeCursor();
}

function convention_sortie(PDO $bdd) {
$sql = 'SELECT id, nom FROM conventions_sorties WHERE visible = "oui"';
$stmt = $bdd->prepare($sql);
Expand Down Expand Up @@ -82,6 +128,15 @@ function points_ventes(PDO $bdd) {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function points_ventes_id(PDO $bdd, $id_point_vente) {
$stmt = $bdd->prepare('SELECT id, nom, adresse FROM points_vente WHERE id = :id');
$stmt->bindValue(':id', $id_point_vente, PDO::PARAM_INT);
$stmt->execute();
$point_sortie = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
return $point_sortie;
}

function types_contenants(PDO $bdd) {
$stmt = $bdd->prepare('SELECT masse, nom FROM type_contenants WHERE visible = "oui"');
$stmt->execute();
Expand Down
4 changes: 4 additions & 0 deletions core/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function is_allowed_gestion() {
return strpos($_SESSION['niveau'], 'g') !== false;
}

function is_allowed_gestion_id($id) {
return strpos($_SESSION['niveau'], 'g' . ((string) $id)) !== false;
}

// Test si l'utilisateur a les droits sur un point de collecte donnee.
function is_allowed_collecte_id($id) {
return strpos($_SESSION['niveau'], 'c' . ((string) $id)) !== false;
Expand Down
13 changes: 13 additions & 0 deletions core/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// TODO a virer une fois la base nettoyee des oui et non.
function oui_non_to_bool($s) {
if ($s === 'oui') { return true; }
else if ($s === 'non') { return false; }
else { throw new InvalidArgumentException('$s different de oui ou non.'); }
}

// TODO a virer une fois la base nettoyee des oui et non.
function bool_to_oui_non($b) {
if ($b === true) { return 'oui'; }
else { return 'non'; }
}

function validate_json_login($unsafe_json) {
$unsafe_json['username'] = filter_var($unsafe_json['username'], FILTER_VALIDATE_EMAIL);
return $unsafe_json;
Expand Down
43 changes: 20 additions & 23 deletions ifaces/grilles_prix.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
Oressource
Copyright (C) 2014-2017 Martin Vert and Oressource devellopers
Expand All @@ -24,26 +23,30 @@
require_once('../core/requetes.php');
require_once('../core/session.php');

require_once('../core/validation.php'); // pour oui/non -> bool vis-versa

$type_obj = filter_input(INPUT_GET, 'typo', FILTER_VALIDATE_INT);
$type_obj = filter_input(INPUT_GET, 'id_type_dechet', FILTER_VALIDATE_INT);

if (isset($_SESSION['id'])
&& $_SESSION['systeme'] === "oressource"
&& is_allowed_gestion()
&& $type_obj !== false) {
require_once("tete.php");

if ($type_obj === null) {
$type_obj = 1;
}

$type_dechets = types_dechets($bdd);
$grille = grilles_objets_id($bdd, $type_obj);
$grille = objet_id_dechet($bdd, $type_obj);
?>

<div class="container">
<h1>Grille des prix</h1>
<ul class="nav nav-tabs">
<?php foreach ($type_dechets as $type_dechet) { ?>
<li class="<?= ($type_obj === $type_dechet['id'] ? 'active' : '') ?>">
<a href="grilles_prix.php?typo=<?= $type_dechet['id'] ?>"><?= $type_dechet['nom'] ?></a>
<a href="grilles_prix.php?id_type_dechet=<?= $type_dechet['id'] ?>"><?= $type_dechet['nom'] ?></a>
</li>
<?php } ?>
</ul>
Expand All @@ -53,20 +56,20 @@
<div class="row input-group">
<div class="col-lg-3">
<label for="nom">Nom:</label>
<input id="nom" class="form-control" type="text" placeholder="nom" name="nom" required autofocus>
<input id="nom" class="form-control" type="text" placeholder="nom" name="nom" required autofocus>
</div>
<div class="col-lg-3">
<label for="description">Description:</label>
<input id="description" class="form-control" type="text" placeholder="description" name="description" required>
</div>
<div class="col-lg-3">
<label for="prix">Prix:</label>
<input id="prix" class="form-control" type="text" placeholder="prix" name="prix" required >
<input id="prix" class="form-control" type="text" placeholder="prix" name="prix" required>
<input class="form-control" type="hidden" value="<?= $type_obj ?>" name="typo">
</div>
<div class="col-lg-3">
<br> <!-- TODO: trouver plus elegant en CSS que ce hack... -->
<button name="creer" class="btn btn-default">créer</button>
<button name="creer" class="btn btn-default">Créer</button>
</div>
</div>
</form>
Expand All @@ -75,6 +78,7 @@
<table class="table">
<thead>
<tr>
<th>identifiant</th>
<th>Nom</th>
<th>Date de création</th>
<th>Description</th>
Expand All @@ -88,6 +92,7 @@
<tbody>
<?php foreach ($grille as $item) { ?>
<tr>
<td><?= $item['id'] ?></td>
<td><?= $item['nom'] ?></td>
<td><?= $item['timestamp'] ?></td>
<td><?= $item['description'] ?></td>
Expand All @@ -102,28 +107,20 @@

<td>
<form action="../moteur/objet_visible.php" method="post">
<input type="hidden" name="typo" value="<?= $type_obj ?>">
<input type="hidden" name="id" value="<?= $item['id'] ?>">
<input type="hidden" name="visible" value="<?=
($item['visible'] === 'oui' ? 'non' : 'oui')
?>">
<?php if ($item['visible'] === 'oui') { ?>
<button class="btn btn-info btn-sm"><?= $item['visible'] ?></button>
<input type="hidden" name="visible" value="<?= oui_non_to_bool($item['visible']) ?>">
<?php if ($item['visible'] === 'oui') { ?>
<button class="btn btn-sm btn-info "><?= $item['visible'] ?></button>
<?php } else { ?>
<button class="btn btn-danger btn-sm"><?= $item['visible'] ?></button>
<button class="btn btn-sm btn-danger "><?= $item['visible'] ?></button>
<?php } ?>
</form>
</td>

<td>
<form action="modification_objet.php" method="post">
<input type="hidden" name="typo" value="<?= $type_obj ?>">
<input type="hidden" name="id" value="<?= $item['id'] ?>">
<input type="hidden" name="nom" value="<?= $item['nom'] ?>">
<input type="hidden" name="description" value="<?= $item['description'] ?>">
<input type="hidden" name="prix" value="<?= $item['prix'] ?>">
<button class="btn btn-warning btn-sm">Modifier</button>
</form>
<!-- TODO faire avec une infobulle JS -->
<a class="btn btn-warning btn-sm"
href="modification_objet.php?id_obj=<?= $item['id'] ?>">Modifier</a>
</td>
</tr>
<?php } ?>
Expand All @@ -132,7 +129,7 @@
</div><!-- /.container -->

<?php
include "pied.php";
require_once "pied.php";
} else {
header('Location: ../moteur/destroy.php');
}
120 changes: 59 additions & 61 deletions ifaces/modification_objet.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,73 @@
<?php session_start();
<?php
/*
+ Oressource
+ Copyright (C) 2014-2017 Martin Vert and Oressource devellopers
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+// Oressource 2017,
Oressource
Copyright (C) 2014-2017 Martin Vert and Oressource devellopers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
//Vérification des autorisations de l'utilisateur et des variables de session requisent pour l'affichage de cette page:
if (isset($_SESSION['id']) AND $_SESSION['systeme'] = "oressource" AND (strpos($_SESSION['niveau'], 'g') !== false))
{ include "tete.php" ?>
<div class="container">
<h1>Grille des prix</h1>
<div class="panel-heading">Modifier les données concernant l'objet n° <?php echo $_POST['id']?>, <?php echo $_POST['nom']?>. </div>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

session_start();

<div class="panel-body">
<div class="row">
<form action="../moteur/modification_objet_post.php" method="post">
<input type="hidden" name ="id" id="id" value="<?php echo $_POST['id']?>">
<input type="hidden" name ="typo" id="typo" value="<?php echo $_POST['typo']?>">

<div class="col-md-2"><label for="nom">Nom:</label> <input type="text"value ="<?php echo $_POST['nom']?>" name="nom" id="nom" class="form-control " required autofocus></div>
<div class="col-md-3"><label for="description">Description:</label> <input type="text"value ="<?php echo $_POST['description']?>" name="description" id="description" class="form-control " required ></div>
<div class="col-md-1"><label for="prix">Prix:</label> <input type="text"value ="<?php echo $_POST['prix']?>" name="prix" id="prix" class="form-control " required ></div>

<div class="col-md-1"><br><button name="creer" class="btn btn-warning">Modifier</button></div>
</form>
<br>

require_once('../moteur/dbconfig.php');
require_once('../core/session.php');
require_once('../core/requetes.php');

if (isset($_SESSION['id'])
&& $_SESSION['systeme'] === 'oressource'
&& is_allowed_bilan()) {

require_once 'tete.php';

$id_obj = filter_input(INPUT_GET, 'id_obj', FILTER_VALIDATE_INT);

<a href="grilles_prix.php<?php echo"?typo=".$_POST['typo']?>">
<button name="creer" class="btn btn">Anuler</button>
</a>

</div>
</div>

<br>

$obj = objet_id($bdd, $id_obj);

?>
<div class="container">
<h1>Grille des prix</h1>
<div class="panel-heading">Modifier les données concernant l'objet n° <?= $obj['id'] ?>, <?= $obj['nom'] ?>.</div>
<div class="panel-body">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"><br> </div>
<div class="col-md-4"></div>
</div>
</div>
<form action="../moteur/modification_objet_post.php" method="post">
<input type="hidden" name="id" id="id" value="<?= $obj['id'] ?>">
<div class="col-md-2">
<label for="nom">Nom:</label>
<input type="text" value="<?= $obj['nom'] ?>" name="nom" id="nom" class="form-control" required autofocus>
</div>
<div class="col-md-3">
<label for="description">Description:</label>
<input type="text" value="<?= $obj['description'] ?>" name="description" id="description" class="form-control" required>
</div>
<div class="col-md-1">
<label for="prix">Prix:</label>
<input type="text" value="<?= $obj['prix'] ?>" name="prix" id="prix" class="form-control" required>
</div>
<div class="col-md-1">
<br>
<button name="creer" class="btn btn-warning">Modifier</button>
</div>
</form>
<br>
<a href="grilles_prix.php?type_dechet=<?= $obj['id_type_dechet'] ?>">
<button name="creer" class="btn btn">Annuler</button>
</a>
</div>
</div>
</div>
</div><!-- /.container -->

<?php include "pied.php";
}
else
{
header('Location: ../moteur/destroy.php') ;
<?php
require_once "pied.php";
} else {
header('Location: ../moteur/destroy.php');
}
?>
Loading

0 comments on commit a138884

Please sign in to comment.