Removed redundant code from ole implementation
This commit is contained in:
@@ -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){}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user