Added all modified files to allow branching

This commit is contained in:
Christian Wolf 2019-05-21 11:15:41 +02:00
parent 6209d1aca6
commit 852f110967
8 changed files with 267 additions and 5 deletions

View File

@ -0,0 +1,177 @@
<?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

@ -14,8 +14,11 @@ abstract class AbstractClubsViewSingle extends HtmlView
protected $object; protected $object;
protected $isNew; protected $isNew;
function display($tpl = null) private $prepared = FALSE;
public function prepareDisplay()
{ {
$this->prepared = TRUE;
$input = Factory::getApplication()->input; $input = Factory::getApplication()->input;
$id = $input->get->get('id'); $id = $input->get->get('id');
@ -48,6 +51,12 @@ abstract class AbstractClubsViewSingle extends HtmlView
$controller->applyData($this->object, $data); $controller->applyData($this->object, $data);
} }
}
public function display($tpl = null)
{
if(!$this->prepared)
$this->prepareDisplay();
parent::display($tpl); parent::display($tpl);
} }

View File

@ -209,10 +209,10 @@ class ClubsClub extends AbstractClubsModel
} }
// public function getOffers() public function getOffers()
// { {
return 0;
// } }
protected function loadCustomData($assoc) protected function loadCustomData($assoc)

View File

@ -30,6 +30,23 @@ class ClubsOffer extends AbstractClubsModel
private const tableName = '#__club_offers'; private const tableName = '#__club_offers';
private const className = 'ClubsOffer'; 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() public static function loadOffers()
{ {
return self::loadElements(self::tableName, self::className); return self::loadElements(self::tableName, self::className);

View File

@ -37,6 +37,11 @@ class ClubsOfferAssociation extends AbstractClubsModel
protected function __construct() protected function __construct()
{} {}
public static function getFactory()
{
return new ClubsOfferAssociationFactory();
}
private const tableName = '#__club_offer_assocs'; private const tableName = '#__club_offer_assocs';
private const className = 'ClubsOfferAssociation'; private const className = 'ClubsOfferAssociation';

View File

@ -0,0 +1,32 @@
<?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

@ -66,6 +66,15 @@ defined('_JEXEC') or die;
<?php if(! $this->isNew): ?> <?php if(! $this->isNew): ?>
<tr>
<td>Angebote</td>
<td>
<?php foreach($this->offers as $o): ?>
<input type='checkbox' name='offers' value='<?php echo $o['offer']->getId(); ?>' <?php if($o['mark']) echo 'checked="checked"';?>>
<?php echo htmlentities($o['offer']->getName()); ?><br />
<?php endforeach; ?>
</td>
</tr>
<tr> <tr>
<td>ID</td> <td>ID</td>
<td><?php echo $this->object->getId(); ?></td> <td><?php echo $this->object->getId(); ?></td>

View File

@ -13,8 +13,21 @@ class ClubsViewClub extends AbstractClubsViewSingle
{ {
ToolbarHelper::title('Club-Management - Verein'); ToolbarHelper::title('Club-Management - Verein');
$this->prepareDisplay();
$this->users = ClubsUser::loadUsers(); $this->users = ClubsUser::loadUsers();
if(! $this->isNew)
{
$offers = ClubsOffer::loadOffers();
$currentOffers = $this->object->getOffers();
$this->offers = array_map(function($offer) use ($currentOffers){
$mark = False;
return array('offer'=>$offer, 'mark'=>$mark);
}, $offers);
}
parent::display($tpl); parent::display($tpl);
} }