1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Accommodation_Management/models/Accomodation.php
p1907961 240492d94f ok
2021-01-05 17:53:26 +01:00

116 lines
No EOL
3.3 KiB
PHP

<?php
class Accomodation 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 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)
{
if($user->getAccomodationId()) {
$data = Accomodation::fetch(array(['id','=', $user->getAccomodationId()]));
if(count($data) === 1){
return $data[0];
}
}
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;
}
}