From dd52d7ca31a70accb000447e7a80209344aa5be7 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Fri, 31 May 2019 14:30:52 +0200 Subject: [PATCH] Removed redundant code from ole implementation --- src/admin/abstract/model.php | 207 ------------ src/admin/abstract/modelfactory.php | 177 ----------- src/admin/clubs.php | 1 - src/admin/mymodels/club.php | 252 --------------- src/admin/mymodels/offer.php | 78 ----- src/admin/mymodels/offerassociation.php | 78 ----- .../mymodels/offerassociationfactory.php | 32 -- src/admin/mymodels/place.php | 113 ------- src/admin/mymodels/position.php | 66 ---- src/admin/mymodels/user.php | 298 ------------------ 10 files changed, 1302 deletions(-) delete mode 100644 src/admin/abstract/model.php delete mode 100644 src/admin/abstract/modelfactory.php delete mode 100644 src/admin/mymodels/club.php delete mode 100644 src/admin/mymodels/offer.php delete mode 100644 src/admin/mymodels/offerassociation.php delete mode 100644 src/admin/mymodels/offerassociationfactory.php delete mode 100644 src/admin/mymodels/place.php delete mode 100644 src/admin/mymodels/position.php delete mode 100644 src/admin/mymodels/user.php diff --git a/src/admin/abstract/model.php b/src/admin/abstract/model.php deleted file mode 100644 index 5ef288f..0000000 --- a/src/admin/abstract/model.php +++ /dev/null @@ -1,207 +0,0 @@ -id; - } - - protected abstract function getDataMappings(); - protected abstract function getTableName(); - - - protected function loadData(array $data) - { - $this->id = $data['id']; - - foreach($this->getDataMappings() as $m) - $this->$m = $data[$m]; - } - - protected static function loadElements(string $tableName, string $className, $where = null) - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - $q->select('*') - ->from($tableName); - - if(isset($where)) - { - $q->where($where); - } - - $dbo->setQuery($q); - $dbo->execute(); - $list = $dbo->loadAssocList(); - - $ret = array(); - foreach($list as $row) - { - $obj = new $className(); - $obj->loadData($row); - - $ret[] = $obj; - } - - return $ret; - } - - /** - * @param int $id - * @throws Exception - * @return ClubsOffer - */ - protected static function loadElement(int $id, string $tableName, string $className) - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $q->select('*')->from($tableName); - - $q->where('id=' . (int) $id); - $dbo->setQuery($q); - $dbo->execute(); - - $row = $dbo->loadAssoc(); - - if($row == null) - { - throw new Exception("No object of class $className found."); - // TODO - } - - $obj = new $className(); - $obj->loadData($row); - $obj->loadCustomData($row); - return $obj; - } - - protected function loadCustomData($assoc) - {} - - public function save() - { - if($this->id === 'new') - $this->insertElement(); - else - $this->updateElement(); - } - - private function insertElement() - { - if(! $this->isDataValid()) - throw new Exception('Data is invalid'); - - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $mappings = $this->getDataMappings(); - $values = $this->getDataValues($mappings, $q); - - $q->insert($this->getTableName()) - ->columns(array_map(array($q, 'qn'), $mappings)) - ->values(join(',', $values)) - ; - - $dbo->transactionStart(); - $dbo->setQuery($q); - $dbo->execute(); - $this->id = $dbo->insertid(); - $dbo->transactionCommit(); - } - - private function updateElement() - { - if(! $this->isDataValid()) - throw new Exception('Data is invalid'); - - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $mapping = $this->getDataMappings(); - $values = $this->getDataValues($mapping, $q); - - $q->update($this->getTableName()); - foreach($mapping as $m) - $q->set($q->qn($m) . '=' . $values[$m]); - $q->where("id=". (int) $this->id); - - $dbo->setQuery($q); - $dbo->execute(); - } - - private function getDataValues($mapping, $q) - { - $rawValues = array(); - - foreach($mapping as $m) - $rawValues[$m] = $this->$m; - - $this->filter($rawValues); - - $quotedValues = array(); - - foreach($mapping as $m) - { - if(is_bool($rawValues[$m])) - $quotedValues[$m] = $rawValues[$m] ? 'TRUE' : 'FALSE'; - else if(is_numeric($rawValues[$m])) - $quotedValues[$m] = $rawValues[$m]; - else - $quotedValues[$m] = $q->q($rawValues[$m]); - } - - $this->postQuoteFilter($quotedValues); - - return $quotedValues; - } - - protected function prepareDelete($dbo){} - - public function delete() - { - if($this->id === 'new') - return; - - $dbo = Factory::getDbo(); - $this->prepareDelete($dbo); - - $q = $dbo->getQuery(true); - $q->delete($this->getTableName()) - ->where('id=' . (int) $this->id); - - $dbo->setQuery($q); - $dbo->execute(); - } - - protected function getRequiredDataMappings() - { - return $this->getDataMappings(); - } - - protected function isDataValid() - { - foreach($this->getRequiredDataMappings() as $m) - { - if(!isset($this->$m) || is_null($this->$m) || $this->$m === null) - return false; - } - return true; - } - - protected function filter(&$values){} - protected function postQuoteFilter(&$values){} - - -} diff --git a/src/admin/abstract/modelfactory.php b/src/admin/abstract/modelfactory.php deleted file mode 100644 index f23cc98..0000000 --- a/src/admin/abstract/modelfactory.php +++ /dev/null @@ -1,177 +0,0 @@ -tableName = $tableName; - $this->className = $className; - } - - /** - * - * @param JDatabaseDriver $dbo - * @return array - */ - protected function getJoins($dbo) - { - /* - * Desired structure: - * array, ech element describing one join type. - * Each element of the array is a assosiated array describing the join: - * - Name of the table (must not be main) - * - Table alias - * - Type of the join (inner, left, right, outer), default is inner - * - Condition as a string (might need escaping) - * - Columns to be inserted in the join, defaults to * - * example: - * $ret = array(); - * $ret[] = array( - * 'name' => '#__table_name', - * 'alias' => 't1', - * 'type' => 'right', - * 'on' => 'main.extid = t1.id' - * 'select' => array('id','name') - * ); - * return $ret; - */ - return array(); - } - - /** - * - * @param JDatabaseDriver $dbo - * @param JDatabaseQuery $q - */ - private function insertJoinRelations($dbo, $q) - { - $joins = $this->getJoins($dbo); - foreach($joins as $j) - { - if(is_null($j['name'])) - throw new Exception('No name was given in the join.'); - if(is_null($j['alias'])) - throw new Exception('No alisas was given in the join.'); - - $jstr = $dbo->qn($j['name'], $j['alias']); - - if(is_null($j['on'])) - throw new Exception('No on clause was provided.'); - - $jstr .= " ON {$j['on']}"; - - if(is_null($j['type'])) - $j['type'] = 'inner'; - - switch($j['type']) - { - case 'inner': - $q->innerJoin($jstr); - break; - - case 'outer': - $q->outerJoin($jstr); - break; - - case 'left': - $q->leftJoin($jstr); - break; - - case 'right': - $q->rightJoin($jstr); - break; - - default: - throw new Exception("Type of join unknown: {$j['type']}."); - } - - $this->addJoinSelectColumns($j, $q); - } - } - - /** - * - * @param array $j - * @param JDatabaseQuery $q - */ - private function addJoinSelectColumns($j, $q) - { - // TODO Quote names - if(is_null($j['select'])) - $j['select'] = '*'; - - if(is_array($j['select'])) - { - array_map(function(&$str) use ($j) { - $str = "{$j['alias']}.$str"; - }, $j['select']); - } - else - { - $q->select("{$j['alias']}.{$j['select']}"); - } - } - - public function loadElement(int $id) - { - $condition = "id = $id"; - - return $this->loadFirstElement($condition); - } - - public function loadFirstElement($condition) - { - $objs = $this->loadElements($condition); - - if(sizeof($objs) == 0) - { - throw new Exception("No object of class {$this->className} found."); - // TODO - } - - return $objs[0]; - } - - public function loadElements($condition = null) - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $q->select('main.*')->from($this->tableName, 'main'); - $this->insertJoinRelations($dbo, $q); - - if(isset($condition)) - { - $q->where($condition); - } - $dbo->setQuery($q); - $dbo->execute(); - - $list = $dbo->loadAssocList(); - - $ret = array(); - foreach($list as $row) - { - $ret[] = $this->createElement($row); - } - - return $ret; - } - - protected function createElement($row) - { - $obj = new $this->className(); - $obj->loadData($row); - $obj->loadCustomData($row); - return $obj; - } -} diff --git a/src/admin/clubs.php b/src/admin/clubs.php index f1320ba..debe58e 100644 --- a/src/admin/clubs.php +++ b/src/admin/clubs.php @@ -9,7 +9,6 @@ defined('_JEXEC') or die; JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels'); JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract'); JLoader::registerPrefix('AbstractCommonClubs', JPATH_ROOT . '/administrator/components/com_clubs/common/abstract'); -// JLoader::registerPrefix('ClubsHelper', JPATH_ROOT . '/administrator/components/com_clubs/common/helper'); JLoader::registerPrefix('CommonClubsModel', JPATH_ROOT . '/administrator/components/com_clubs/common/models'); $controller = BaseController::getInstance("Clubs"); diff --git a/src/admin/mymodels/club.php b/src/admin/mymodels/club.php deleted file mode 100644 index 7a5c464..0000000 --- a/src/admin/mymodels/club.php +++ /dev/null @@ -1,252 +0,0 @@ -address; - } - - /** - * @return string - */ - public function getCity() - { - return $this->city; - } - - /** - * @return string - */ - public function getHomepage() - { - return $this->homepage; - } - - /** - * @return string - */ - public function getMail() - { - return $this->mail; - } - - /** - * @return string - */ - public function getIban() - { - return $this->iban; - } - - /** - * @return string - */ - public function getBic() - { - return $this->bic; - } - - /** - * @return bool - */ - public function isCharitable() - { - return $this->charitable; - } - - /** - * @return int - */ - public function getPresidentId() - { - return $this->president->getId(); - } - - /** - * @return ClubsUser - */ - public function getPresident() - { - return $this->president; - } - - /** - * @param string $address - */ - public function setAddress( $address) - { - $this->address = $address; - } - - /** - * @param string $city - */ - public function setCity($city) - { - $this->city = $city; - } - - /** - * @param string $homapge - */ - public function setHomepage($homapge) - { - $this->homepage = $homapge; - } - - /** - * @param string $mail - */ - public function setMail($mail) - { - $this->mail = $mail; - } - - /** - * @param string $iban - */ - public function setIban($iban) - { - $this->iban = $iban; - } - - /** - * @param string $bic - */ - public function setBic($bic) - { - $this->bic = $bic; - } - - /** - * @param bool $charitable - */ - public function setCharitable(bool $charitable) - { - $this->charitable = $charitable; - } - - /** - * @param int $presidentId - */ - public function setPresidentId(int $presidentId) - { - $this->president = ClubsUser::loadUser($presidentId); - } - - /** - * @param ClubsUser $user - */ - public function setPresident(ClubsUser $user) - { - $this->president = $user; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - - - public static function loadClubs() - { - return self::loadElements(self::tableName, self::className); - } - - public static function loadClub($id) - { - return self::loadElement($id, self::tableName, self::className); - } - - protected function __construct() - {} - - public static function createClub() - { - $club = new ClubsClub(); - $club->id = 'new'; - return $club; - } - - - - - - public function getPlaces() - { - return ClubsPlace::loadPlacesOfClub($this->id); - } - - - public function getOffers() - { - return 0; - } - - - protected function loadCustomData($assoc) - { - parent::loadCustomData($assoc); - $this->president = ClubsUser::loadUser($assoc['president']); - } - - protected function postQuoteFilter(&$values) - { - parent::postQuoteFilter($values); - $values['president'] = $this->president->getId(); - } - - protected function prepareDelete($dbo) - {} - - protected function getDataMappings() - { - return array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'president'); - } - - protected function getRequiredDataMappings() - { - return array('name', 'address', 'city', 'mail', 'iban', 'bic'); - } - - private const tableName = '#__club_clubs'; - private const className = 'ClubsClub'; - protected function getTableName() - { - return self::tableName; - } - - - -} diff --git a/src/admin/mymodels/offer.php b/src/admin/mymodels/offer.php deleted file mode 100644 index b3d1f1c..0000000 --- a/src/admin/mymodels/offer.php +++ /dev/null @@ -1,78 +0,0 @@ -name; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - protected function __construct() - {} - - private const tableName = '#__club_offers'; - private const className = 'ClubsOffer'; - - public static function getFactory() - { - return new class extends AbstractClubsModelFactory { - public function __construct() - { - parent::__construct($this->tableName, $this->className); - } - - protected function getJoins($dbo) - { - $ret = array(); - return $ret; - } - - }; - } - - public static function loadOffers() - { - return self::loadElements(self::tableName, self::className); - } - - - public static function loadOffer(int $id) - { - return self::loadElement($id, self::tableName, self::className); - } - - public static function createOffer() - { - $offer = new ClubsOffer(); - $offer->id = 'new'; - return $offer; - } - - protected function getDataMappings() - { - return array('name'); - } - - protected function getTableName() - { - return self::tableName; - } - -} diff --git a/src/admin/mymodels/offerassociation.php b/src/admin/mymodels/offerassociation.php deleted file mode 100644 index 2329066..0000000 --- a/src/admin/mymodels/offerassociation.php +++ /dev/null @@ -1,78 +0,0 @@ -offerid); - } - - public function getClub() - { - return ClubsClub::loadClub($this->clubid); - } - - public function setOffer($offer) - { - $this->offerid = $offer->getId(); - } - - public function getName() - { - return $this->getOffer()->getName(); - } - - public function setClub($club) - { - $this->clubid = $club->getId(); - } - - protected function __construct() - {} - - public static function getFactory() - { - return new ClubsOfferAssociationFactory(); - } - - private const tableName = '#__club_offer_assocs'; - private const className = 'ClubsOfferAssociation'; - - public static function loadOfferAssociationsOfClub($club) - { - $cid = $club->getId(); - return self::loadElements(self::tableName, self::className, "clubid = $cid"); - // FIXME Use join to make access faster - } - - - public static function loadOfferAssociation(int $id) - { - return self::loadElement($id, self::tableName, self::className); - } - - public static function createOfferAssociation() - { - $offer = new ClubsOfferAssociation(); -// $offer->id = 'new'; - return $offer; - } - - protected function getDataMappings() - { -// return array('name'); - } - - protected function getTableName() - { - return self::tableName; - } - -} diff --git a/src/admin/mymodels/offerassociationfactory.php b/src/admin/mymodels/offerassociationfactory.php deleted file mode 100644 index 5853657..0000000 --- a/src/admin/mymodels/offerassociationfactory.php +++ /dev/null @@ -1,32 +0,0 @@ - '#__club_offers', - 'alias' => 'offer', - 'on' => 'main.offerid = offer.id', - 'select' => 'id as offerId' - ); - $ret[] = array( - 'name' => '#__club_clubs', - 'alias' => 'club', - 'on' => 'main.clubid = club.id', - 'select' => 'id AS clubId' - ); - return $ret; - } - -} diff --git a/src/admin/mymodels/place.php b/src/admin/mymodels/place.php deleted file mode 100644 index c1b7fcd..0000000 --- a/src/admin/mymodels/place.php +++ /dev/null @@ -1,113 +0,0 @@ -name; - } - - /** - * @param string $name - */ - public function setName(string $name) - { - $this->name = $name; - } - - /** - * @return string - */ - public function getAddress() - { - return $this->address; - } - - /** - * @return string - */ - public function getArea() - { - return $this->area; - } - - /** - * @param string $address - */ - public function setAddress(string $address) - { - $this->address = $address; - } - - /** - * @param string $area - */ - public function setArea(string $area) - { - $this->area = $area; - } - - - public function getClub() - { - return ClubsClub::loadClub($this->clubid); - } - - public function setClub($club) - { - $this->clubid = $club->getId(); - } - - - protected function __construct() - {} - - public static function loadPlaces() - { - return self::loadElements(self::tableName, self::className); - } - - public static function loadPlacesOfClub($clubId) - { - return self::loadElements(self::tableName, self::className, "clubid = $clubId"); - } - - public static function loadPlace(int $id) - { - return self::loadElement($id, self::tableName, self::className); - } - - public static function createPlace() - { - $place = new ClubsPlace(); - $place->id = 'new'; - return $place; - } - - protected function getDataMappings() - { - return array('name', 'address', 'area', 'clubid'); - } - - private const tableName = '#__club_places'; - private const className = 'ClubsPlace'; - - protected function getTableName() - { - return self::tableName; - } - - -} diff --git a/src/admin/mymodels/position.php b/src/admin/mymodels/position.php deleted file mode 100644 index 79d9a39..0000000 --- a/src/admin/mymodels/position.php +++ /dev/null @@ -1,66 +0,0 @@ -name; - } - - /** - * @param string $name - */ - public function setName(string $name) - { - $this->name = $name; - } - - protected function __construct() - {} - - private const tableName = '#__club_positions'; - private const className = 'ClubsPosition'; - - public static function loadPositions() - { - return self::loadElements(self::tableName, self::className); - } - -// public static function loadPositions($clubId) -// { -// return self::loadElements(self::tableName, self::className, "clubid = $clubId"); -// } - - public static function loadPosition(int $id) - { - return self::loadElement($id, self::tableName, self::className); - } - - public static function createPosition() - { - $position = new ClubsPosition(); - $position->id = 'new'; - return $position; - } - - protected function getDataMappings() - { - return array('name'); - } - - protected function getTableName() - { - return self::tableName; - } - - - -} diff --git a/src/admin/mymodels/user.php b/src/admin/mymodels/user.php deleted file mode 100644 index 892a772..0000000 --- a/src/admin/mymodels/user.php +++ /dev/null @@ -1,298 +0,0 @@ -mail; - } - - /** - * @param string $mail - */ - public function setMail($mail) - { - $this->mail = $mail; - } - - /** - * @return string - */ - public function getUser() - { - return $this->user; - } - - /** - * @return string - */ - public function getHash() - { - return $this->password; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * @return string - */ - public function getAddress() - { - return $this->address; - } - - /** - * @return string - */ - public function getCity() - { - return $this->city; - } - - /** - * @return string - */ - public function getPhone() - { - return $this->phone; - } - - /** - * @return string - */ - public function getMobile() - { - return $this->mobile; - } - - /** - * @param string $user - */ - public function setUser($user, bool $force = false) - { - if($this->id === 'new') - $valid = self::isUserNameFree($user); - else - $valid = self::isUserNameFree($user, $this->id); - - if(!$force && ! $valid) - throw new UserInvalidException(); - - $this->user = $user; - } - - /** - * @param string $hash - */ - public function setPassword(string $password) - { - if(! $this->checkPasswordStrength($password)) - throw new PasswordInvalidException(); - - $this->password = password_hash($password, PASSWORD_DEFAULT); - } - - public function isPasswordValid(string $password) - { - $valid = password_verify($password, $this->password); - - if($valid) - { - $this->checkForRehashing($password); - } - - return $valid; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - /** - * @param string $address - */ - public function setAddress($address) - { - $this->address = $address; - } - - /** - * @param string $city - */ - public function setCity($city) - { - $this->city = $city; - } - - /** - * @param string $phone - */ - public function setPhone($phone) - { - $this->phone = $phone; - } - - /** - * @param string $mobile - */ - public function setMobile($mobile) - { - $this->mobile = $mobile; - } - - protected function __construct() - {} - - private const tableName = '#__club_users'; - private const className = 'ClubsUser'; - - public static function loadUsers() - { - return self::loadElements(self::tableName, self::className); - } - - public static function loadUser(int $id) - { - return self::loadElement($id, self::tableName, self::className); - } - - public static function createUser() - { - $user = new ClubsUser(); - $user->id = 'new'; - return $user; - } - - private function updateUser() - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $vuser = $q->q($this->user); - $vpassword = $q->q($this->password); - $vname = $q->q($this->name); - $vaddress = $q->q($this->address); - $vcity = $q->q($this->city); - $vmail = $q->q($this->mail); - $vphone = empty($this->phone) ? 'NULL' : $q->q($this->phone); - $vmobile = empty($this->mobile) ? 'NULL' : $q->q($this->mobile); - // FIXME Check null vlaues - $q->update('#__club_users') - ->set(array( - "user=$vuser", - "password=$vpassword", - "name=$vname", - "address = $vaddress", - "city=$vcity", - "mail=$vmail", - "phone=$vphone", - "mobile=$vmobile" - )) - ->where("id=". (int) $this->id) - ; - - $dbo->setQuery($q); - $dbo->execute(); - } - - private function checkForRehashing(string $password) - { - if($this->id === 'new') - return; - - if(password_needs_rehash($this->password, PASSWORD_DEFAULT) || true) - { - $copy = ClubsUser::loadUser($this->id); - $copy->password = password_hash($password, PASSWORD_DEFAULT); - $copy->save(); - -// $this->password = password_hash($password, PASSWORD_DEFAULT); - -// $dbo = Factory::getDbo(); - -// $q = $dbo->getQuery(true); -// $q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id); -// $dbo->setQuery($q); -// $dbo->execute(); - } - } - - public static function isUserNameFree($username, int $id = -1) - { - $db = Factory::getDbo(); - $q = $db->getQuery(true); - $q->select('COUNT(*)')->from(self::tableName) - ->where('id <> ' . (int) $id) - ->where('user = ' . $q->q($username)); - $db->setQuery($q); - $db->execute(); - $row = $db->loadRow(); - return $row[0] == 0; - } - - public static function checkPasswordStrength($pwd) - { - if(strlen($pwd) < 6) - return false; - - if(preg_match_all('/[A-Z]/', $pwd) === false) - return false; - - if(preg_match_all('/[a-z]/', $pwd) === false) - return false; - - if(preg_match_all('/[0-9]/', $pwd) === false) - return false; - - return true; - } - - protected function getDataMappings() - { - return array('user', 'password', 'name', 'address', 'city', 'mail', 'phone', 'mobile'); - } - - protected function getRequiredDataMappings() - { - return array('user', 'password', 'name', 'address', 'city', 'mail'); - } - - protected function getTableName() - { - return self::tableName; - } - -}