diff --git a/controller/accomodation.php b/controller/accomodation.php index 6b5f211..61bca46 100644 --- a/controller/accomodation.php +++ b/controller/accomodation.php @@ -1,10 +1,55 @@ isLoggedIn()) { + $accomodationType = ['Hôtel', 'Gîte','Camping','Villa en location']; + $alert = ''; if(isset($path[1])) { switch ($path[1]) { case 'add': if(!$_SESSION['USER']->getAccomodationId()) { + /* + * Creating Accomodation + */ + 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( + "name" => $_POST['accomodationName'], + "address" => $_POST['address'], + "postalCode" => $_POST['postalCode'], + "type" => $_POST['accomodationType'])) != false){ + /* + * Update users information + */ + $_SESSION['USER']->update(); + /* + * 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.'); + } + } + } + + $services = AccomodationServices::fetch(); require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); }else{ @@ -14,6 +59,8 @@ if($_SESSION['USER']->isLoggedIn()) { case 'edit': if($_SESSION['USER']->getAccomodationId()) { + $accomodation = Accomodation::fetchByuser($_SESSION['USER']); + $services = AccomodationServices::fetch(); require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); }else{ redirect(); diff --git a/controller/login.php b/controller/login.php index 35b7296..8c968ab 100644 --- a/controller/login.php +++ b/controller/login.php @@ -13,7 +13,6 @@ if(!$_SESSION['USER']->isLoggedIn() && !isset($path[1])) { /* * Successful login */ - $_SESSION['USER'] = $userArray[0]; // Pass the returned User type data into Session redirect(); }else{ diff --git a/models/Accomodation.php b/models/Accomodation.php index 627b208..99caa88 100644 --- a/models/Accomodation.php +++ b/models/Accomodation.php @@ -20,6 +20,19 @@ class Accomodation extends Model } return $this; } + public static function insertUser(User $user, $data): bool + { + $inserted = Accomodation::insert($data); + if($inserted !== false){ + /* + * User foreign key + */ + $query = 'UPDATE User SET AccomodationId=? WHERE email=?;'; + $q = Accomodation::$db->prepare($query); + return ($q->execute([Accomodation::$db->lastInsertId(), $user->getEmail()]) == true); + } + return false; + } public static function fetchByUser(User $user) { @@ -31,4 +44,73 @@ class Accomodation extends Model } return false; } + public static function fetchById(int $id) + { + $data = Accomodation::fetch(array(['id','=', $id])); + if(count($data) === 1){ + return $data[0]; + } + 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 = []; + if(isset($this->data['id'])){ + $query = 'SELECT AccomodationServicesName FROM _AccomodationServices WHERE AccomodationId = ?;'; + $q = Accomodation::$db->prepare($query); + $q->execute([$this->data['id']]); + forEach($q->fetchAll(PDO::FETCH_ASSOC) as $item){ + $out[] = $item['AccomodationServicesName']; + } + return $out; + } + return false; + } + public function setServices(array $names) + { + /* + * Clear & Add + */ + if(isset($this->data['id'])){ + $query = 'DELETE FROM _AccomodationServices WHERE AccomodationId = ?;'; + $q = Accomodation::$db->prepare($query); + if($q->execute([$this->data['id']])){ + /* + * Add + */ + if(!empty($names)){ + $args = []; + $insertquery = 'INSERT INTO _AccomodationServices VALUES '; + $s = array_fill(0, count($names), '(?,?)'); + $insertquery .= join(',', $s) . ';'; + forEach($names as $name){ + $args[] = $this->data['id']; + $args[] = $name; + } + $q = Accomodation::$db->prepare($insertquery); + return ($q->execute($args) == true); + } + return true; + } + } + return false; + } } \ No newline at end of file diff --git a/models/AccomodationServices.php b/models/AccomodationServices.php index 92a8274..2eba85d 100644 --- a/models/AccomodationServices.php +++ b/models/AccomodationServices.php @@ -20,6 +20,14 @@ class AccomodationServices extends Model } return $this; } + public static function getAll(): array + { + $out = []; + forEach(AccomodationServices::fetch() as $item){ + $out[] = $item->getName(); + } + return $out; + } public function getName(): string { diff --git a/src/func.php b/src/func.php index 1e57a43..951a51f 100644 --- a/src/func.php +++ b/src/func.php @@ -1,4 +1,19 @@ " .htmlspecialchars($msg). ""; + break; + default: + throw new \Exception("Status d'alerte invalide"); + } +} # Navbar button render function navItem($name, $path){ $acc = ''; - if(($_SERVER['REDIRECT_URL'] === WEBSITE_PATH . $path) || ($path === ($_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"] . $_SERVER["REDIRECT_URL"]))){ + if(($_SERVER['REQUEST_URI'] === WEBSITE_PATH . $path) || ($path === ($_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]))){ $acc = 'active'; } return ''; diff --git a/src/model.php b/src/model.php index c72c426..865201a 100644 --- a/src/model.php +++ b/src/model.php @@ -18,12 +18,12 @@ class Model { Model::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); Model::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(Exception $e){ - var_dump($e); + die($e); } } } /* - * GET/SELECT Query + * Fetch */ public static function fetch($filters = []): array { @@ -55,6 +55,35 @@ class Model { } return $output; } + + /* + * Insert + */ + public static function insert($data){ + $query = 'INSERT INTO ' . get_called_class() . ' ('; + $_col = get_called_class()::getColumns(); + $c = 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 . ') VALUES ( ' . join(',',array_fill(0, sizeof($data), '?')) . ' );'; + }else{ + $query .= $key . ', '; + } + $c++; + } + } + $q = Model::$db->prepare($query); + return ($q->execute($args) == true); + } + return false; + } + protected function getColumns(): array { if(!isset(get_called_class()::$column[get_called_class()])) { diff --git a/view/add_accomodation.php b/view/add_accomodation.php index 07aced6..2ba7790 100644 --- a/view/add_accomodation.php +++ b/view/add_accomodation.php @@ -8,31 +8,34 @@ require_once('template/head.php');
+
- - We'll never share your email with anyone else. + +
- - + +
-
- - +
+ +
- + + +
@@ -42,13 +45,13 @@ require_once('template/head.php'); foreach ($services as $service){ $c++; ?>
- +
-
+

diff --git a/view/edit_accomodation.php b/view/edit_accomodation.php index bc0e1a5..78df95d 100644 --- a/view/edit_accomodation.php +++ b/view/edit_accomodation.php @@ -1,7 +1,62 @@ - +
+

Bienvenue M. getLastName()))?>,

+

Ici vous pouvez éditer votre hébérgement.

+
+
+ +
+ +
+
+ + + +
+
+ + +
+
+ + +
+
+
+
+
+ + +
+
+
+
+ + +
+ +

+ +
+
+ +
\ No newline at end of file diff --git a/view/template/footer.php b/view/template/footer.php index 771f847..3ddbf1f 100644 --- a/view/template/footer.php +++ b/view/template/footer.php @@ -1,5 +1,6 @@ - + \ No newline at end of file diff --git a/view/template/head.php b/view/template/head.php index 8a16068..1460e15 100644 --- a/view/template/head.php +++ b/view/template/head.php @@ -6,8 +6,21 @@ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> "> - + + + + + + - <?= WEBSITE_NAME ?> diff --git a/view/template/navbar.php b/view/template/navbar.php index 73e88dd..ae65ebc 100644 --- a/view/template/navbar.php +++ b/view/template/navbar.php @@ -3,7 +3,7 @@ ?>
\ No newline at end of file