From 4ce8fd274d0a05ec63835f7e7258c0457d2cc441 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Fri, 31 May 2019 14:18:34 +0200 Subject: [PATCH] Created models for all database objects --- src/admin/common/abstract/model.php | 41 ++++- src/admin/common/abstract/model/factory.php | 18 +- src/admin/common/abstract/model/join.php | 22 +++ src/admin/common/models/club.php | 54 ++++++ src/admin/common/models/factory/offer.php | 24 +++ .../common/models/factory/offerassoc.php | 25 +++ src/admin/common/models/factory/position.php | 25 +++ src/admin/common/models/factory/user.php | 10 +- src/admin/common/models/factory/userassoc.php | 33 ++++ src/admin/common/models/offer.php | 37 +++++ src/admin/common/models/offerassoc.php | 49 ++++++ src/admin/common/models/position.php | 32 ++++ src/admin/common/models/user.php | 156 +++++++++++++++++- src/admin/common/models/userassoc.php | 119 +++++++++++++ 14 files changed, 634 insertions(+), 11 deletions(-) create mode 100644 src/admin/common/models/factory/offer.php create mode 100644 src/admin/common/models/factory/offerassoc.php create mode 100644 src/admin/common/models/factory/position.php create mode 100644 src/admin/common/models/factory/userassoc.php create mode 100644 src/admin/common/models/offer.php create mode 100644 src/admin/common/models/offerassoc.php create mode 100644 src/admin/common/models/position.php create mode 100644 src/admin/common/models/userassoc.php diff --git a/src/admin/common/abstract/model.php b/src/admin/common/abstract/model.php index 2a7dce1..90babdc 100644 --- a/src/admin/common/abstract/model.php +++ b/src/admin/common/abstract/model.php @@ -107,6 +107,12 @@ abstract class AbstractCommonClubsModel $q->from($factory->getTableName()); $q->where("id = {$this->id}"); + $joins = $factory->getJoins(); + foreach($joins as $j) + { + $j->join($q); + } + $db->setQuery($q); $db->execute(); @@ -130,12 +136,20 @@ abstract class AbstractCommonClubsModel private function unpackExternalReferencesFromKeys($vals) { - foreach($this->getFactory()->getAttributes() as $a) + $factory = $this->getFactory(); + + foreach($factory->getAttributes() as $a) { $alias = $a->getAlias(); $vals[$alias] = $a->unpackValue($vals[$alias]); } + $joins = $factory->getJoins(); + foreach($joins as $join) + { + $join->unpackExternalReferencesFromKeys($vals); + } + return $vals; } @@ -243,6 +257,10 @@ abstract class AbstractCommonClubsModel $q->where("id = {$this->id}"); } + /** + * + * @param JDatabaseDriver $db + */ protected function prepareDelete($db) {} @@ -267,11 +285,28 @@ abstract class AbstractCommonClubsModel * * @param AbstractCommonClubsModelFactory $factory * @param string $colName + * @param array $constraints */ - protected function fetchAssociatedElements($factory, $colName) + protected function fetchAssociatedElements($factory, $colName, $constraints = null, $sorting = null) { $condition = "main.$colName = {$this->id}"; - return $factory->loadElements($condition); + + if(isset($constraints)) + { + if(is_array($constraints)) + $allConstraints = clone $constraints; + elseif(is_string($constraints)) + $allConstraints = array($constraints); + else + throw new Exception('Unknown type of constraint'); + + // Add the manual condition to match the current object + $allConstraints[] = $condition; + + return $factory->loadElements($allConstraints, $sorting); + } + else + $factory->loadElements($condition, $sorting); } protected function filterPackData($values) diff --git a/src/admin/common/abstract/model/factory.php b/src/admin/common/abstract/model/factory.php index 391ad64..1513bcd 100644 --- a/src/admin/common/abstract/model/factory.php +++ b/src/admin/common/abstract/model/factory.php @@ -81,20 +81,24 @@ abstract class AbstractCommonClubsModelFactory } private $joins = null; + /** + * @return AbstractCommonClubsModelJoin[] + */ public function getJoins($force = False) { if($this->joins === null || $force) $this->joins = $this->fetchJoins(); - return $this->joins; + return $this->joins; } /** * * @param string $condition + * @param string|array $sorting * @return array */ - public function loadElements($condition = null) + public function loadElements($condition = null, $sorting = null, $callback = null) { $db = Factory::getDbo(); $q = $db->getQuery(true); @@ -109,11 +113,17 @@ abstract class AbstractCommonClubsModelFactory $q->select('main.id AS id');//->select($columns); $q->from($this->getTableName() . ' AS main'); - // TODO Joins - if($condition !== null) $q->where($condition); + if($sorting !== null) + $q->order($sorting); + + if($callback !== null) + { + $callback($q); + } + $db->setQuery($q); $db->execute(); diff --git a/src/admin/common/abstract/model/join.php b/src/admin/common/abstract/model/join.php index e039a99..878f146 100644 --- a/src/admin/common/abstract/model/join.php +++ b/src/admin/common/abstract/model/join.php @@ -14,6 +14,19 @@ abstract class AbstractCommonClubsModelJoin */ protected $columns; + public function __construct($alias, $condition, $colums, $tablename = null) + { + /// XXX Some checks need to be performed + $this->alias = $alias; + $this->condition = $condition; + $this->columns = $colums; + + if(empty($tablename)) + $this->tablename = $alias; + else + $this->tablename = $alias; + } + public function getAlias() { return $this->alias; @@ -59,4 +72,13 @@ abstract class AbstractCommonClubsModelJoin */ protected abstract function addJoin($q, $str); + public function unpackExternalReferencesFromKeys(&$vals) + { + foreach($this->columns as $col) + { + $alias = $col->getAlias(); + $vals[$alias] = $col->unpackValue($vals[$alias]); + } + } + } diff --git a/src/admin/common/models/club.php b/src/admin/common/models/club.php index d523b29..94eea67 100644 --- a/src/admin/common/models/club.php +++ b/src/admin/common/models/club.php @@ -60,4 +60,58 @@ class CommonClubsModelClub extends AbstractCommonClubsModel { return $this->fetchAssociatedElements(new CommonClubsModelFactoryPlace(), 'clubid'); } + + public function getOffers() + { + $assocs = $this->fetchAssociatedElements(new CommonClubsModelFactoryOfferassocs(), 'clubid'); + + $offersFactory = new CommonClubsModelFactoryOffer(); + $allOffers = $offersFactory->loadElements(); + + $ret = array(); + foreach($allOffers as $offer) + { + $arr = array( + 'offer' => $offer, + 'valid' => false + ); + + $id = $offer->getId(); + foreach($assocs as $a) + { + $oid = $a->getOffer()->getId(); + if($id === $oid) + { + $arr['valid'] = true; + break; + } + } + + $ret[] = $arr; + } + return $ret; + } + + public function getUsers() + { + return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'clubid'); + } + + protected function prepareDelete($db) + { + $q = $db->getQuery(true); + $q->delete('#__club_user_assocs')->where("clubid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + + $q = $db->getQuery(true); + $q->delete('#__club_places')->where("clubid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + + $q = $db->getQuery(true); + $q->delete('#__club_offer_assocs')->where("clubid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + } } diff --git a/src/admin/common/models/factory/offer.php b/src/admin/common/models/factory/offer.php new file mode 100644 index 0000000..4f3a875 --- /dev/null +++ b/src/admin/common/models/factory/offer.php @@ -0,0 +1,24 @@ + `state` enum('regular', 'vacant', 'temporary') NOT NULL DEFAULT 'vacant', + ); + } + + public function getTableName() + { + return '#__club_user_assocs'; + } + + public function getClassName() + { + return 'CommonClubsModelUserassoc'; + } + +} diff --git a/src/admin/common/models/offer.php b/src/admin/common/models/offer.php new file mode 100644 index 0000000..8040504 --- /dev/null +++ b/src/admin/common/models/offer.php @@ -0,0 +1,37 @@ +getValues()['name']; + } + + public function setName($name) + { + $this->setValue('name', $name); + } + + /*public function getClubs() + { + return $this->fetchAssociatedElements(new CommonClubsModelFactoryClub(), ''); + }*/ + + protected function prepareDelete($db) + { + $q = $db->getQuery(true); + $q->delete('#__club_offer_assocs'); + $q->where("offerid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + } +} + diff --git a/src/admin/common/models/offerassoc.php b/src/admin/common/models/offerassoc.php new file mode 100644 index 0000000..066c1b9 --- /dev/null +++ b/src/admin/common/models/offerassoc.php @@ -0,0 +1,49 @@ +getValues()['club']; + } + + /** + * + * @param CommonClubsModelClub $club + */ + public function setClub($club) + { + $this->setValue('club', $club); + } + + /** + * + * @return CommonClubsModelOffer + */ + public function getOffer() + { + return $this->getValues()['offer']; + } + + /** + * + * @param CommonClubsModelOffer $offer + */ + public function setOffer($offer) + { + $this->setValue('offer', $offer); + } + +} diff --git a/src/admin/common/models/position.php b/src/admin/common/models/position.php new file mode 100644 index 0000000..a2e9133 --- /dev/null +++ b/src/admin/common/models/position.php @@ -0,0 +1,32 @@ +getValues()['name']; + } + + public function setName($name) + { + $this->setValue('name', $name); + } + + protected function prepareDelete($db) + { + $q = $db->getQuery(true); + $q->delete('#__club_user_assocs'); + $q->where("positionid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + } + +} diff --git a/src/admin/common/models/user.php b/src/admin/common/models/user.php index ce8c821..62f880e 100644 --- a/src/admin/common/models/user.php +++ b/src/admin/common/models/user.php @@ -14,6 +14,158 @@ class CommonClubsModelUser extends AbstractCommonClubsModel { return $this->getValues()['name']; } - -} \ No newline at end of file + public function setName($name) + { + $this->setValue('name', $name); + } + + public function getUsername() + { + return $this->getValues()['user']; + } + + public function setUsername($user) + { + // XXX Check for validity + $this->setValue('user', $user); + } + + public function getAddress() + { + return $this->getValues()['address']; + } + + public function setAddress($address) + { + $this->setValue('address', $address); + } + + public function getCity() + { + return $this->getValues()['city']; + } + + public function setCity($city) + { + $this->setValue('city', $city); + } + + public function getMail() + { + return $this->getValues()['mail']; + } + + public function setMail($mail) { + $this->setValue('mail', $mail); + } + + public function getPhone() + { + return $this->getValues()['phone']; + } + + public function setPhone($phone) + { + $this->setValue('phone', $phone); + } + + public function getMobile() + { + return $this->getValues()['mobile']; + } + + public function setMobile($mobile) + { + $this->setValue('mobile', $mobile); + } + + public function isPasswordValid($password) + { + $hash = $this->getValues()['password']; + + if(password_verify($password, $hash)) + return true; + else + return false; + } + + public function checkRehashNeeded($newPassword, $check = false) + { + $hash = $this->getValues()['password']; + if(password_needs_rehash($hash, PASSWORD_DEFAULT)) + { + if($check) + { + if(! $this->isPasswordValid($newPassword)) + throw new Exception('Password did not match.'); + } + + $this->setPassword($newPassword); + return true; + } + else + return false; + } + + public function setPassword($password) + { + $hash = password_hash($password, PASSWORD_DEFAULT); + $this->setValue('password', $hash); + } + + public function getPositions() + { + return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'userid'); + } + + public function isPasswordSuitable($password) + { + if(strlen($password) < 8) + return false; + + $pwdLower = strtolower($password); + $userLower = strtolower($this->getName()); + + if(strpos($pwdLower, $userLower) || strpos($userLower, $pwdLower)) + return false; + + if( + preg_match('/.*[a-z].*/', $password) === false || + preg_match('/.*[A-Z].*/', $password) === false + ) + return false; + + return true; + } + + public function isUsernameSuitable($user) + { + $factory = new CommonClubsModelFactoryUser(); + $users = $factory->loadElements(null, null, function($q) use ($user){ + $q->where('main.user = ' . $q->q($user)); + }); + + if(sizeof($users) == 0) + return true; + elseif(sizeof($users) == 1) + { + if($this->getId() == $users[0]->getId()) + return true; + else + return false; + } + else + throw new Exception('The database is inconsistent!'); + } + + protected function prepareDelete($db) + { + $q = $db->getQuery(true); + $q->delete('#__club_user_assocs')->where("userid = {$this->getId()}"); + $db->setQuery($q); + $db->execute(); + } + +} + \ No newline at end of file diff --git a/src/admin/common/models/userassoc.php b/src/admin/common/models/userassoc.php new file mode 100644 index 0000000..ab1aa10 --- /dev/null +++ b/src/admin/common/models/userassoc.php @@ -0,0 +1,119 @@ +getValues()['user']; + } + + /** + * + * @return CommonClubsModelPosition + */ + public function getPosition() + { + return $this->getValues()['position']; + } + + /** + * + * @return CommonClubsModelClub + */ + public function getClub() + { + return $this->getValues()['club']; + } + + public function setUser($user) + { + $this->setValue('user', $user); + } + + public function setPosition($position) + { + $this->setValue('position', $position); + } + + public function setClub($club) + { + $this->setValue('club', $club); + } + + public function isAdmin() + { + return $this->getValues()['admin'] == 1; + } + + public function setAdmin($admin) + { + if($admin) + $this->setValue('admin', 1); + else + $this->setValue('admin', 0); + } + + public function getAddress() + { + return $this->getValues()['address']; + } + + public function setAddress($address) + { + $this->setValue('address', $address); + } + + public function getMail() + { + return $this->getValues()['mail']; + } + + public function setMail($mail) + { + $this->setValue('mail', $mail); + } + + public function getPhone() + { + return $this->getValues()['phone']; + } + + public function setPhone($phone) + { + $this->setValue('phone', $phone); + } + + public function getState() + { + return $this->getValues()['state']; + } + + public function setState($state) + { + switch(strtolower($state)) + { + case 'vacant': + case 'temporary': + case 'regular': + $state = strtolower($state); + break; + + default: + throw new Exception("Unknown state \"$state\" found."); + } + $this->setValue('state', $state); + } + +}