diff --git a/controller/404.php b/controller/404.php index 46658b9..e7444fc 100644 --- a/controller/404.php +++ b/controller/404.php @@ -1,2 +1,2 @@ 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'); } \ No newline at end of file diff --git a/index.php b/index.php index f110a7e..bff4fd9 100644 --- a/index.php +++ b/index.php @@ -21,7 +21,7 @@ if(!isset($_SESSION['USER'])){ /* * Check if user still exist */ - $_SESSION['USER']->update(); + $_SESSION['USER']->refresh(); } /* * Lang diff --git a/models/Accomodation.php b/models/Accomodation.php index 99caa88..bcf9b0b 100644 --- a/models/Accomodation.php +++ b/models/Accomodation.php @@ -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 = []; diff --git a/models/User.php b/models/User.php index b5170a4..533b138 100644 --- a/models/User.php +++ b/models/User.php @@ -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']])); diff --git a/src/model.php b/src/model.php index 865201a..84e1a30 100644 --- a/src/model.php +++ b/src/model.php @@ -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 { diff --git a/view/404.php b/view/404.php new file mode 100644 index 0000000..e5bb4c6 --- /dev/null +++ b/view/404.php @@ -0,0 +1,9 @@ + +
+

404 :/

+
+ \ No newline at end of file diff --git a/view/add_accomodation.php b/view/add_accomodation.php index 2ba7790..3864e11 100644 --- a/view/add_accomodation.php +++ b/view/add_accomodation.php @@ -52,7 +52,7 @@ require_once('template/head.php'); } ?>
- + diff --git a/view/date_accomodation.php b/view/date_accomodation.php new file mode 100644 index 0000000..bc0e1a5 --- /dev/null +++ b/view/date_accomodation.php @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/view/edit_accomodation.php b/view/edit_accomodation.php index 78df95d..c900655 100644 --- a/view/edit_accomodation.php +++ b/view/edit_accomodation.php @@ -42,17 +42,18 @@ require_once('template/head.php');
getServices(); foreach ($services as $service){ $c++; ?>
- + getName(),$accomService)){ echo 'checked'; } ?> class="form-check-input" type="checkbox" name='accomodationService[]' id="inlineCheckbox" value="getName())?>">

- +