Removed redundant code from ole implementation

This commit is contained in:
Christian Wolf 2019-05-31 14:30:52 +02:00
parent 4ce8fd274d
commit dd52d7ca31
10 changed files with 0 additions and 1302 deletions

View File

@ -1,207 +0,0 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
abstract class AbstractClubsModel
{
protected $id;
/**
* @return int
*/
public function getId()
{
return $this->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){}
}

View File

@ -1,177 +0,0 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
abstract class AbstractClubsModelFactory
{
protected $tableName;
protected $className;
public function __construct($tableName, $className)
{
$this->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;
}
}

View File

@ -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");

View File

@ -1,252 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsClub extends AbstractClubsModel
{
protected $id;
protected $name;
protected $address;
protected $city;
protected $homepage;
protected $mail;
protected $iban;
protected $bic;
protected $charitable;
protected $president;
/**
* @return string
*/
public function getAddress()
{
return $this->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;
}
}

View File

@ -1,78 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsOffer extends AbstractClubsModel
{
protected $name;
/**
* @return string
*/
public function getName()
{
return $this->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;
}
}

View File

@ -1,78 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsOfferAssociation extends AbstractClubsModel
{
protected $offerid;
protected $clubid;
public function getOffer()
{
return ClubsOffer::loadOffer($this->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;
}
}

View File

@ -1,32 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsOfferAssociationFactory extends AbstractClubsModelFactory
{
public function __construct()
{
parent::__construct('#__club_offer_assocs', 'ClubsOfferAssociation');
}
protected function getJoins($dbo)
{
$ret = array();
$ret[] = array(
'name' => '#__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;
}
}

View File

@ -1,113 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsPlace extends AbstractClubsModel
{
protected $name;
protected $address;
protected $area;
protected $clubid;
/**
* @return string
*/
public function getName()
{
return $this->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;
}
}

View File

@ -1,66 +0,0 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsPosition extends AbstractClubsModel
{
protected $name;
/**
* @return string
*/
public function getName()
{
return $this->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;
}
}

View File

@ -1,298 +0,0 @@
<?php
use Joomla\CMS\Factory;
// No direct access.
defined('_JEXEC') or die;
class UserInvalidException extends Exception
{}
class PasswordInvalidException extends Exception
{}
class ClubsUser extends AbstractClubsModel
{
protected $user;
protected $password;
protected $name;
protected $address;
protected $city;
protected $mail;
protected $phone;
protected $mobile;
/**
* @return string
*/
public function getMail()
{
return $this->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;
}
}