Added all modified files to allow branching

This commit is contained in:
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 $isNew;
function display($tpl = null)
private $prepared = FALSE;
public function prepareDisplay()
{
$this->prepared = TRUE;
$input = Factory::getApplication()->input;
$id = $input->get->get('id');
@@ -48,6 +51,12 @@ abstract class AbstractClubsViewSingle extends HtmlView
$controller->applyData($this->object, $data);
}
}
public function display($tpl = null)
{
if(!$this->prepared)
$this->prepareDisplay();
parent::display($tpl);
}