1
0
Fork 0

Added Accomodation Edition

This commit is contained in:
p1907961 2021-01-11 17:30:13 +01:00
parent 240492d94f
commit 88cea3d0bb
10 changed files with 135 additions and 53 deletions

View file

@ -1,2 +1,2 @@
<?php
echo '404';
require_once(VIEW_PATH . '404.php');

View file

@ -1,77 +1,98 @@
<?php
if($_SESSION['USER']->isLoggedIn()) {
$accomodationType = ['Hôtel', 'Gîte','Camping','Villa en location'];
if ($_SESSION['USER']->isLoggedIn()) {
$accomodationType = ['Hôtel', 'Gîte', 'Camping', 'Villa en location'];
$alert = '';
if(isset($path[1])) {
$availableServices = AccomodationServices::getAll();
$re_name = '/^[a-zA-Z -\'?*éàèôê0-9"()+&]{1,}$/';
$re_cp = '/[0-9]{5}/';
if (isset($path[1])) {
switch ($path[1]) {
case 'add':
if(!$_SESSION['USER']->getAccomodationId())
{
if (!$_SESSION['USER']->getAccomodationId()) {
/*
* Creating Accomodation
*/
if(isset(
if (isset(
$_POST['accomodationName'],
$_POST['postalCode'],
$_POST['address'],
$_POST['accomodationType']
)){
$availableServices = AccomodationServices::getAll();
$re_name = '/^[a-zA-Z -\'?*éàèôê0-9"()+&]{1,}$/';
$re_cp = '/[0-9]{5}/';
)) {
/*
* Checking inputs
*/
if(!preg_match($re_name, $_POST['accomodationName']) || !preg_match($re_cp, $_POST['postalCode']) || ((isset($_POST["accomodationService"]) && gettype($_POST['accomodationService']) === 'array' && array_diff($_POST["accomodationService"], $availableServices))))
{
$alert = alert('danger','Le formulaire est invalide');
}else{
if(Accomodation::insertUser($_SESSION['USER'], array(
if (!preg_match($re_name, $_POST['accomodationName']) || !preg_match($re_cp, $_POST['postalCode']) || ((isset($_POST["accomodationService"]) && gettype($_POST['accomodationService']) === 'array' && array_diff($_POST["accomodationService"], $availableServices)))) {
$alert = alert('danger', 'Le formulaire est invalide');
} else {
if (Accomodation::insertUser($_SESSION['USER'], array(
"name" => $_POST['accomodationName'],
"address" => $_POST['address'],
"postalCode" => $_POST['postalCode'],
"type" => $_POST['accomodationType'])) != false){
"type" => $_POST['accomodationType'])) != false) {
/*
* Update users information
*/
$_SESSION['USER']->update();
$_SESSION['USER']->refresh();
/*
* Insert ok
*/
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
$accomodation->setServices($_POST["accomodationService"]);
$alert = alert('success','Ajout du logement réussi.');
}else{
$alert = alert('danger','Erreur lors de l\'insertion du logement.');
$alert = alert('success', 'Ajout du logement réussi.');
} else {
$alert = alert('danger', 'Erreur lors de l\'insertion du logement.');
}
}
}
$services = AccomodationServices::fetch();
require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php');
}else{
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else {
redirect();
}
break;
case 'edit':
if($_SESSION['USER']->getAccomodationId())
{
$accomodation = Accomodation::fetchByuser($_SESSION['USER']);
if ($_SESSION['USER']->getAccomodationId()) {
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
if (isset(
$_POST['accomodationName'],
$_POST['postalCode'],
$_POST['address'],
$_POST['accomodationType']
)) {
if (!preg_match($re_name, $_POST['accomodationName']) || !preg_match($re_cp, $_POST['postalCode']) || ((isset($_POST["accomodationService"]) && gettype($_POST['accomodationService']) === 'array' && array_diff($_POST["accomodationService"], $availableServices)))) {
$alert = alert('danger', 'Le formulaire est invalide');
} else {
Accomodation::update(array(
"name" => $_POST['accomodationName'],
"address" => $_POST['address'],
"postalCode" => $_POST['postalCode'],
"type" => $_POST['accomodationType']), array(['id','=',$accomodation->getId()]));
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
$accomodation->setServices($_POST["accomodationService"]);
$alert = alert('success', 'Le logement a été modifié.');
}
$accomodation->setServices($_POST["accomodationService"]);
}
$services = AccomodationServices::fetch();
require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php');
}else{
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else {
redirect();
}
break;
case 'date':
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
break;
default:
redirect();
}
}else{
} else {
redirect();
}
}else{
} else {
redirect('login');
}

View file

@ -21,7 +21,7 @@ if(!isset($_SESSION['USER'])){
/*
* Check if user still exist
*/
$_SESSION['USER']->update();
$_SESSION['USER']->refresh();
}
/*
* Lang

View file

@ -20,6 +20,27 @@ class Accomodation extends Model
}
return $this;
}
public function getId()
{
return $this->data['id'];
}
public function getName()
{
return $this->data['name'];
}
public function getAddress()
{
return $this->data['address'];
}
public function getPostalCode()
{
return $this->data['postalCode'];
}
public function getType()
{
return $this->data['type'];
}
public static function insertUser(User $user, $data): bool
{
$inserted = Accomodation::insert($data);
@ -52,24 +73,6 @@ class Accomodation extends Model
}
return false;
}
public function getName()
{
return $this->data['name'];
}
public function getAddress()
{
return $this->data['address'];
}
public function getPostalCode()
{
return $this->data['postalCode'];
}
public function getType()
{
return $this->data['type'];
}
public function getServices()
{
$out = [];

View file

@ -63,7 +63,7 @@ class User extends Model
/*
* Session
*/
public function update(): void
public function refresh(): void
{
if (isset($this->data['email'])){
$exist = User::fetch(array(['email','=',$this->data['email']]));

View file

@ -83,6 +83,47 @@ class Model {
}
return false;
}
/*
* Update
*/
public static function update($data,$filters = []){
$query = 'UPDATE ' . get_called_class() . ' SET ';
$_col = get_called_class()::getColumns();
$c = $z = 0;
$args = [];
if($data !== null){
forEach($data as $key=>$value){
if(!key_exists($key, $_col)){
throw new Exception('Invalid data entry: ' . $key);
}else{
$args[] = $value;
if($c+1 === sizeof($data)){
$query .= $key . ' = ?';
}else{
$query .= $key . ' = ?, ';
}
$c++;
}
}
forEach($filters as $filter){
if (is_array($filter) && count($filter) === 3 && array_key_exists($filter[0], $_col) && in_array($filter[1], ['=','<','>','<>','LIKE'])){
if($z === 0){
$query .= ' WHERE';
}else{
$query .= ' AND';
}
$query .= ' ' . $filter[0] . ' ' . $filter[1] . ' ?';
$args[] = $filter[2];
}else{
throw new Exception('Invalid SQL filters');
}
$z++;
}
$q = Model::$db->prepare($query);
return ($q->execute($args) == true);
}
return false;
}
protected function getColumns(): array
{

9
view/404.php Normal file
View file

@ -0,0 +1,9 @@
<?php
require_once('template/head.php');
?>
<div class="container-fluid section">
<h1>404 :/</h1>
</div>
<?php
require_once('template/footer.php');
?>

View file

@ -52,7 +52,7 @@ require_once('template/head.php');
}
?>
</div><br>
<button type="submit" class="btn btn-primary">Ajouter</button>
<button type="submit" class="btn btn-success">Ajouter</button>
</div>
</div>
</form>

View file

@ -0,0 +1,7 @@
<?php
require_once('template/head.php');
?>
<?php
require_once('template/footer.php');
?>

View file

@ -42,17 +42,18 @@ require_once('template/head.php');
<label for="typeSelect">Quels services proposez-vous ?</label><br>
<?php
$c = 0;
$accomService = $accomodation->getServices();
foreach ($services as $service){
$c++;
?><div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name='accomodationService[]' id="inlineCheckbox<?=$c?>" value="<?=htmlspecialchars($service->getName())?>">
<input <?php if(in_array($service->getName(),$accomService)){ echo 'checked'; } ?> class="form-check-input" type="checkbox" name='accomodationService[]' id="inlineCheckbox<?=$c?>" value="<?=htmlspecialchars($service->getName())?>">
<label class="form-check-label" for="inlineCheckbox<?=$c?>"><?=htmlspecialchars($service->getName())?></label>
</div>
<?php
}
?>
</div><br>
<button type="submit" class="btn btn-primary">Ajouter</button>
<button type="submit" class="btn btn-primary">Modifier</button>
</div>
</div>
</form>