From 86dc860290c716d0c2d2b5185828f3c3c16e5416 Mon Sep 17 00:00:00 2001 From: p1907961 Date: Sun, 20 Dec 2020 04:25:09 +0100 Subject: [PATCH] Completed Routing and Added User sessions updates --- controller/accomodation.php | 15 +++++++++++++-- models/Accomodation.php | 2 +- models/AccomodationServices.php | 23 +++++++++++++++++++++++ models/User.php | 2 +- view/add_accomodation.php | 7 ++++--- view/template/navbar.php | 7 +++++-- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/controller/accomodation.php b/controller/accomodation.php index 60c0165..6b5f211 100644 --- a/controller/accomodation.php +++ b/controller/accomodation.php @@ -3,10 +3,21 @@ if($_SESSION['USER']->isLoggedIn()) { if(isset($path[1])) { switch ($path[1]) { case 'add': - require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); + if(!$_SESSION['USER']->getAccomodationId()) + { + $services = AccomodationServices::fetch(); + require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); + }else{ + redirect(); + } break; case 'edit': - require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); + if($_SESSION['USER']->getAccomodationId()) + { + require_once(VIEW_PATH.$path[1] . '_' . $path[0].'.php'); + }else{ + redirect(); + } break; default: redirect(); diff --git a/models/Accomodation.php b/models/Accomodation.php index 2805194..627b208 100644 --- a/models/Accomodation.php +++ b/models/Accomodation.php @@ -21,7 +21,7 @@ class Accomodation extends Model return $this; } - public static function getByUser(User $user) + public static function fetchByUser(User $user) { if($user->getAccomodationId()) { $data = Accomodation::fetch(array(['id','=', $user->getAccomodationId()])); diff --git a/models/AccomodationServices.php b/models/AccomodationServices.php index 556636d..92a8274 100644 --- a/models/AccomodationServices.php +++ b/models/AccomodationServices.php @@ -3,5 +3,28 @@ class AccomodationServices extends Model { + private $data; + public function __construct($data = null) + { + parent::__construct(); + $_col = get_class()::getColumns(); + if($data !== null){ + forEach($data as $key=>$value){ + if(!key_exists($key, $_col)){ + throw new Exception('Invalid data entry'); + }else{ + $this->data[$key] = $value; + } + } + } + return $this; + } + + public function getName(): string + { + if(isset($this->data['name'])) + return $this->data['name']; + return false; + } } \ No newline at end of file diff --git a/models/User.php b/models/User.php index 44562f2..b5170a4 100644 --- a/models/User.php +++ b/models/User.php @@ -68,7 +68,7 @@ class User extends Model if (isset($this->data['email'])){ $exist = User::fetch(array(['email','=',$this->data['email']])); if(count($exist) === 1){ - + $_SESSION['USER'] = $exist[0]; }else{ /* * Account must have been deleted diff --git a/view/add_accomodation.php b/view/add_accomodation.php index 2170524..de5efae 100644 --- a/view/add_accomodation.php +++ b/view/add_accomodation.php @@ -2,7 +2,7 @@ require_once('template/head.php'); ?>
-

Bienvenue M. getLastName()))?>,

+

Bienvenue M. getLastName()))?>,

Il semblerait que vous n'ayez pas encore ajouté d'hebergement...

@@ -39,12 +39,13 @@ require_once('template/head.php');
- - + +
isLoggedIn()){ switch($_SESSION['USER']->getType()){ case 'AccomodationOwner': - echo navItem('Ajout Hebergement',genURL('accomodation/add')); - echo navItem('Edition de l\'hebergement',genURL('accomodation/edit')); + if(!$_SESSION['USER']->getAccomodationId()){ + echo navItem('Ajout Hebergement',genURL('accomodation/add')); + } else { + echo navItem('Edition de l\'hebergement', genURL('accomodation/edit')); + } break; case 'Staff': echo navItem('Gestion des disponibilités',genURL('accomodation/manager'));