Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reecriture/structure #238

Merged
merged 2 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions api/structures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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/>.
*/


require_once('../core/session.php');
require_once('../core/validation.php');
require_once('../core/requetes.php');

global $bdd;

session_start();

header("content-type:application/json");

if (is_valid_session() && is_allowed_config()) {
$json_raw = file_get_contents('php://input');
$unsafe_json = json_decode($json_raw, true);

require_once('../moteur/dbconfig.php');

$structure = structure_validate($unsafe_json);
structure_update($bdd, array_merge($structure, [
'id' => 1,
]));
http_response_code(200); // Unauthorized.
echo(json_encode(['success' => 'Configuration saved']));
} else {
http_response_code(401); // Unauthorized.
echo(json_encode(['error' => "Session Invalide ou expiree."], JSON_FORCE_OBJECT));
}
41 changes: 41 additions & 0 deletions core/composants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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/>.
*/

function checkBox(array $props, bool $state) {
ob_start();
?><label class="custom-control custom-checkbox">
<input
name="<?= $props['name'] ?>"
id="<?= $props['name'] ?>"
<?= $state ? 'checked' : '' ?>
type="checkbox"><?= $props['text'] ?></label><?php
return ob_get_clean();
}

function textInput(array $props, string $state) {
ob_start();
?><label><?= $props['text'] ?>
<input type="text"
name="<?= $props['name'] ?>"
id="<?= $props['name'] ?>"
value="<?= $state ?>"
class="form-control" required autofocus>
</label><?php
return ob_get_clean();
}
61 changes: 43 additions & 18 deletions core/requetes.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,39 +530,42 @@ function structure(PDO $bdd): array {
$req = $bdd->prepare($sql);
$req->execute();
$result = $req->fetch(PDO::FETCH_ASSOC);

/* a activé une fois les changements prets.
$result['tva_active'] = oui_non_to_bool($result['tva_active']);
$result['lot'] = oui_non_to_bool($result['lot']);
$result['viz'] = oui_non_to_bool($result['viz']);
$result['saisiec'] = oui_non_to_bool($result['saisiec']);
$result['affsp'] = oui_non_to_bool($result['affsp']);
$result['affss'] = oui_non_to_bool($result['affss']);
$result['affsr'] = oui_non_to_bool($result['affsr']);
$result['affsd'] = oui_non_to_bool($result['affsd']);
$result['affsde'] = oui_non_to_bool($result['affsde']);
$result['pes_vente'] = oui_non_to_bool($result['pes_vente']);
$result['force_pes_vente'] = oui_non_to_bool($result['force_pes_vente']);
*/
$req->closeCursor();
return $result;
}

function structure_bool(PDO $bdd): array {
$result = structure($bdd);
$result['tva_active'] = oui_non_to_bool($result['tva_active']);
$result['lot'] = oui_non_to_bool($result['lot']);
$result['viz'] = oui_non_to_bool($result['viz']);
$result['saisiec'] = oui_non_to_bool($result['saisiec']);
$result['affsp'] = oui_non_to_bool($result['affsp']);
$result['affss'] = oui_non_to_bool($result['affss']);
$result['affsr'] = oui_non_to_bool($result['affsr']);
$result['affsd'] = oui_non_to_bool($result['affsd']);
$result['affsde'] = oui_non_to_bool($result['affsde']);
$result['pes_vente'] = oui_non_to_bool($result['pes_vente']);
$result['force_pes_vente'] = oui_non_to_bool($result['force_pes_vente']);
return $result;
}

function structure_update(PDO $bdd, array $structure) {
$sql = 'UPDATE description_structure
SET nom = :nom,
adresse = :adresse,
id_localite = :id_localite,
description = :description,
siret = :siret,
telephone = :telephone,
mail =:mail,
mail = :mail,
taux_tva = :taux_tva,
tva_active= :tva_active,
tva_active = :tva_active,
cr = :cr,
lot = :lot,
viz = :viz,
nb_viz = :nb_viz,
saisiec =: saisiec,
saisiec = :saisiec,
affsp = :affsp,
affss = :affss,
affsr = :affsr,
Expand All @@ -572,7 +575,29 @@ function structure_update(PDO $bdd, array $structure) {
force_pes_vente = :force_pes_vente
WHERE id = :id';
$stmt = $bdd->prepare($sql);
$stmt->execute($structure);
$stmt->bindValue(':nom', $structure['nom']);
$stmt->bindValue(':adresse', $structure['adresse']);
$stmt->bindValue(':id_localite', $structure['id_localite'], PDO::PARAM_INT);
$stmt->bindValue(':description', $structure['description']);
$stmt->bindValue(':siret', $structure['siret']);
$stmt->bindValue(':telephone', $structure['telephone']);
$stmt->bindValue(':mail', $structure['mail']);
$stmt->bindValue(':taux_tva', $structure['taux_tva']);
$stmt->bindValue(':tva_active', $structure['tva_active']);
$stmt->bindValue(':cr', $structure['cr']);
$stmt->bindValue(':lot', $structure['lot']);
$stmt->bindValue(':viz', $structure['viz']);
$stmt->bindValue(':nb_viz', $structure['nb_viz']);
$stmt->bindValue(':saisiec', $structure['saisiec']);
$stmt->bindValue(':affsp', $structure['affsp']);
$stmt->bindValue(':affss', $structure['affss']);
$stmt->bindValue(':affsr', $structure['affsr']);
$stmt->bindValue(':affsd', $structure['affsd']);
$stmt->bindValue(':affsde', $structure['affsde']);
$stmt->bindValue(':pes_vente', $structure['pes_vente']);
$stmt->bindValue(':force_pes_vente', $structure['force_pes_vente']);
$stmt->bindValue(':id', $structure['id'], PDO::PARAM_INT);
$stmt->execute();
$stmt->closeCursor();
}

Expand Down
2 changes: 2 additions & 0 deletions core/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

global $_SESSION;

require_once 'validation.php';

// Appellée au login.
function set_session(array $user, array $structure) {
$_SESSION['systeme'] = 'oressource';
Expand Down
36 changes: 21 additions & 15 deletions core/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@ function bool_to_oui_non(bool $b): string {

function structure_validate(array $json): array {
$structure = [
'nom' => filter_input($json, 'nom', FILTER_SANITIZE_STRING),
'description' => filter_input($json, 'description', FILTER_SANITIZE_STRING),
'mail' => filter_input($json, 'mail', FILTER_VALIDATE_EMAIL),
'lot' => filter_input($json, 'lot', FILTER_VALIDATE_BOOLEAN),
'viz' => filter_input($json, 'viz', FILTER_VALIDATE_BOOLEAN),
'saisiec' => filter_input($json, 'saisiec', FILTER_VALIDATE_BOOLEAN),
'affsp' => filter_input($json, 'affsp', FILTER_VALIDATE_BOOLEAN),
'affss' => filter_input($json, 'affss', FILTER_VALIDATE_BOOLEAN),
'affsr' => filter_input($json, 'affsr', FILTER_VALIDATE_BOOLEAN),
'affsde' => filter_input($json, 'affsde', FILTER_VALIDATE_BOOLEAN),
'pes_vente' => filter_input($json, 'pes_vente', FILTER_VALIDATE_BOOLEAN),
'force_pes_vente' => filter_input($json, 'force_pes_vente', FILTER_VALIDATE_BOOLEAN),
'atva' => filter_input($json, 'atva', FILTER_VALIDATE_BOOLEAN),
'taux_tva' => filter_input($json, 'taux_tva', FILTER_VALIDATE_FLOAT),
'cr' => filter_input($json, 'cr', FILTER_VALIDATE_INT), // devrait etre une regex sur les nombres.
'siret' => filter_var($json['siret'], FILTER_SANITIZE_STRING),
'nom' => filter_var($json['nom'], FILTER_SANITIZE_STRING),
'id_localite' => filter_var($json['id_localite'], FILTER_VALIDATE_INT),
'adresse' => filter_var($json['adresse'], FILTER_SANITIZE_STRING),
'description' => filter_var($json['description'], FILTER_SANITIZE_STRING),
'telephone' => filter_var($json['telephone'], FILTER_SANITIZE_STRING), // TODO: regex sur les nombres.
'mail' => filter_var($json['mail'], FILTER_VALIDATE_EMAIL),
'lot' => bool_to_oui_non(filter_var($json['lot'], FILTER_VALIDATE_BOOLEAN)),
'viz' => bool_to_oui_non(filter_var($json['viz'], FILTER_VALIDATE_BOOLEAN)),
'saisiec' => bool_to_oui_non(filter_var($json['saisiec'], FILTER_VALIDATE_BOOLEAN)),
'affsp' => bool_to_oui_non(filter_var($json['affsp'], FILTER_VALIDATE_BOOLEAN)),
'affss' => bool_to_oui_non(filter_var($json['affss'], FILTER_VALIDATE_BOOLEAN)),
'affsr' => bool_to_oui_non(filter_var($json['affsr'], FILTER_VALIDATE_BOOLEAN)),
'affsde' => bool_to_oui_non(filter_var($json['affsde'], FILTER_VALIDATE_BOOLEAN)),
'affsd' => bool_to_oui_non(filter_var($json['affsd'], FILTER_VALIDATE_BOOLEAN)),
'pes_vente' => bool_to_oui_non(filter_var($json['pes_vente'], FILTER_VALIDATE_BOOLEAN)),
'force_pes_vente' => bool_to_oui_non(filter_var($json['force_pes_vente'], FILTER_VALIDATE_BOOLEAN)),
'tva_active' => bool_to_oui_non(filter_var($json['tva_active'], FILTER_VALIDATE_BOOLEAN)),
'taux_tva' => filter_var($json['taux_tva'], FILTER_VALIDATE_FLOAT),
'nb_viz' => filter_var($json['nb_viz'], FILTER_VALIDATE_INT),
'cr' => filter_var($json['cr'], FILTER_VALIDATE_INT), // TODO: regex sur les nombres.
];
return $structure;
}
Expand Down
Loading