Created models for all database objects
This commit is contained in:
parent
4fa01d4cc0
commit
4ce8fd274d
@ -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)
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
24
src/admin/common/models/factory/offer.php
Normal file
24
src/admin/common/models/factory/offer.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelFactoryOffer extends AbstractCommonClubsModelFactory
|
||||
{
|
||||
protected function fetchAttributes()
|
||||
{
|
||||
return array(
|
||||
new CommonClubsModelColumnString('name')
|
||||
);
|
||||
}
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return '#__club_offers';
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return 'CommonClubsModelOffer';
|
||||
}
|
||||
}
|
25
src/admin/common/models/factory/offerassoc.php
Normal file
25
src/admin/common/models/factory/offerassoc.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelFactoryOfferassocs extends AbstractCommonClubsModelFactory
|
||||
{
|
||||
protected function fetchAttributes()
|
||||
{
|
||||
return array(
|
||||
new CommonClubsModelColumnRef('club', 'CommonClubsModelClub', true, 'clubid'),
|
||||
new CommonClubsModelColumnRef('offer', 'CommonClubsModelOffer', true, 'offerid')
|
||||
);
|
||||
}
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return '#__club_offer_assocs';
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return 'CommonClubsModelOfferassoc';
|
||||
}
|
||||
}
|
25
src/admin/common/models/factory/position.php
Normal file
25
src/admin/common/models/factory/position.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelFactoryPosition extends AbstractCommonClubsModelFactory
|
||||
{
|
||||
protected function fetchAttributes()
|
||||
{
|
||||
return array(
|
||||
new CommonClubsModelColumnString('name')
|
||||
);
|
||||
}
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return '#__club_positions';
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return 'CommonClubsModelPosition';
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,13 @@ class CommonClubsModelFactoryUser extends AbstractCommonClubsModelFactory
|
||||
{
|
||||
return array(
|
||||
new CommonClubsModelColumnString('user'),
|
||||
new CommonClubsModelColumnString('name')
|
||||
new CommonClubsModelColumnString('name'),
|
||||
new CommonClubsModelColumnString('password'),
|
||||
new CommonClubsModelColumnString('address'),
|
||||
new CommonClubsModelColumnString('city'),
|
||||
new CommonClubsModelColumnString('mail'),
|
||||
new CommonClubsModelColumnString('phone'),
|
||||
new CommonClubsModelColumnString('mobile')
|
||||
);
|
||||
}
|
||||
|
||||
|
33
src/admin/common/models/factory/userassoc.php
Normal file
33
src/admin/common/models/factory/userassoc.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelFactoryUserassoc extends AbstractCommonClubsModelFactory
|
||||
{
|
||||
protected function fetchAttributes()
|
||||
{
|
||||
return array(
|
||||
new CommonClubsModelColumnRef('user', 'CommonClubsModelUser', true, 'userid'),
|
||||
new CommonClubsModelColumnRef('club', 'CommonClubsModelClub', true, 'clubid'),
|
||||
new CommonClubsModelColumnRef('position', 'CommonClubsModelPosition', true, 'positionid'),
|
||||
new CommonClubsModelColumnInt('admin'),
|
||||
new CommonClubsModelColumnString('address', false),
|
||||
new CommonClubsModelColumnString('mail', false),
|
||||
new CommonClubsModelColumnString('phone', false),
|
||||
new CommonClubsModelColumnString('state')
|
||||
// -> `state` enum('regular', 'vacant', 'temporary') NOT NULL DEFAULT 'vacant',
|
||||
);
|
||||
}
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return '#__club_user_assocs';
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return 'CommonClubsModelUserassoc';
|
||||
}
|
||||
|
||||
}
|
37
src/admin/common/models/offer.php
Normal file
37
src/admin/common/models/offer.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelOffer extends AbstractCommonClubsModel
|
||||
{
|
||||
protected function getFactory()
|
||||
{
|
||||
return new CommonClubsModelFactoryOffer();
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->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();
|
||||
}
|
||||
}
|
||||
|
49
src/admin/common/models/offerassoc.php
Normal file
49
src/admin/common/models/offerassoc.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelOfferassoc extends AbstractCommonClubsModel
|
||||
{
|
||||
protected function getFactory()
|
||||
{
|
||||
return new CommonClubsModelFactoryOfferassocs();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return CommonClubsModelClub
|
||||
*/
|
||||
public function getClub()
|
||||
{
|
||||
return $this->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);
|
||||
}
|
||||
|
||||
}
|
32
src/admin/common/models/position.php
Normal file
32
src/admin/common/models/position.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelPosition extends AbstractCommonClubsModel
|
||||
{
|
||||
protected function getFactory()
|
||||
{
|
||||
new CommonClubsModelFactoryPosition();
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->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();
|
||||
}
|
||||
|
||||
}
|
@ -15,5 +15,157 @@ class CommonClubsModelUser extends AbstractCommonClubsModel
|
||||
return $this->getValues()['name'];
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
119
src/admin/common/models/userassoc.php
Normal file
119
src/admin/common/models/userassoc.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class CommonClubsModelUserassoc extends AbstractCommonClubsModel
|
||||
{
|
||||
protected function getFactory()
|
||||
{
|
||||
return new CommonClubsModelFactoryUserassoc();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return CommonClubsModelUser
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user