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 <?php
echo '404'; require_once(VIEW_PATH . '404.php');

View file

@ -2,11 +2,13 @@
if ($_SESSION['USER']->isLoggedIn()) { if ($_SESSION['USER']->isLoggedIn()) {
$accomodationType = ['Hôtel', 'Gîte', 'Camping', 'Villa en location']; $accomodationType = ['Hôtel', 'Gîte', 'Camping', 'Villa en location'];
$alert = ''; $alert = '';
$availableServices = AccomodationServices::getAll();
$re_name = '/^[a-zA-Z -\'?*éàèôê0-9"()+&]{1,}$/';
$re_cp = '/[0-9]{5}/';
if (isset($path[1])) { if (isset($path[1])) {
switch ($path[1]) { switch ($path[1]) {
case 'add': case 'add':
if(!$_SESSION['USER']->getAccomodationId()) if (!$_SESSION['USER']->getAccomodationId()) {
{
/* /*
* Creating Accomodation * Creating Accomodation
*/ */
@ -16,16 +18,12 @@ if($_SESSION['USER']->isLoggedIn()) {
$_POST['address'], $_POST['address'],
$_POST['accomodationType'] $_POST['accomodationType']
)) { )) {
$availableServices = AccomodationServices::getAll();
$re_name = '/^[a-zA-Z -\'?*éàèôê0-9"()+&]{1,}$/';
$re_cp = '/[0-9]{5}/';
/* /*
* Checking inputs * 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)))) 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'); $alert = alert('danger', 'Le formulaire est invalide');
} else { } else {
if (Accomodation::insertUser($_SESSION['USER'], array( if (Accomodation::insertUser($_SESSION['USER'], array(
@ -36,7 +34,7 @@ if($_SESSION['USER']->isLoggedIn()) {
/* /*
* Update users information * Update users information
*/ */
$_SESSION['USER']->update(); $_SESSION['USER']->refresh();
/* /*
* Insert ok * Insert ok
*/ */
@ -57,15 +55,38 @@ if($_SESSION['USER']->isLoggedIn()) {
} }
break; break;
case 'edit': case 'edit':
if($_SESSION['USER']->getAccomodationId()) if ($_SESSION['USER']->getAccomodationId()) {
{ $accomodation = Accomodation::fetchByUser($_SESSION['USER']);
$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(); $services = AccomodationServices::fetch();
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php'); require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else { } else {
redirect(); redirect();
} }
break; break;
case 'date':
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
break;
default: default:
redirect(); redirect();
} }

View file

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

View file

@ -20,6 +20,27 @@ class Accomodation extends Model
} }
return $this; 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 public static function insertUser(User $user, $data): bool
{ {
$inserted = Accomodation::insert($data); $inserted = Accomodation::insert($data);
@ -52,24 +73,6 @@ class Accomodation extends Model
} }
return false; 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() public function getServices()
{ {
$out = []; $out = [];

View file

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

View file

@ -83,6 +83,47 @@ class Model {
} }
return false; 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 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> </div><br>
<button type="submit" class="btn btn-primary">Ajouter</button> <button type="submit" class="btn btn-success">Ajouter</button>
</div> </div>
</div> </div>
</form> </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> <label for="typeSelect">Quels services proposez-vous ?</label><br>
<?php <?php
$c = 0; $c = 0;
$accomService = $accomodation->getServices();
foreach ($services as $service){ foreach ($services as $service){
$c++; $c++;
?><div class="form-check form-check-inline"> ?><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> <label class="form-check-label" for="inlineCheckbox<?=$c?>"><?=htmlspecialchars($service->getName())?></label>
</div> </div>
<?php <?php
} }
?> ?>
</div><br> </div><br>
<button type="submit" class="btn btn-primary">Ajouter</button> <button type="submit" class="btn btn-primary">Modifier</button>
</div> </div>
</div> </div>
</form> </form>