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 '