209 lines
5.7 KiB
PHP

<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ElementNotFoundException extends Exception
{}
abstract class AbstractCommonClubsModelFactory
{
// TODO Attribures as objects allowing to use polymophism in filtering/parsing/checking/sql building
/*
* This method should return an array to configure the trivially accessible values.
* The key must be a unique key describing the information and will be reused in the getValues() method.
* The value of each key should be an associative array describing the object field.
* Example of an array returned by this method is
* array(
* 'name'=>array('col'=>'Name', type=>'string'),
* 'size'=>array('col'=>'area', type=>'int')
* )
*
* The inner arrays contain the following entries:
* - col: The name of the column in the database. Defaults to the key name
* - type: The type of the column. If nothing is specified, string is assumed.
* - string
* - int
* - float
* - ref
* - optional: boolean, if true, the field can be NULL
* - ref: (only with type='ref') The name of the class that is referenced
*/
protected abstract function fetchAttributes();
private $attributes = null;
/**
* @param boolean $force
* @return AbstractCommonClubsModelColumn[]
*/
public function getAttributes($force = False)
{
if($this->attributes === null || $force)
$this->attributes = $this->fetchAttributes();
return $this->attributes;
}
public abstract function getTableName();
public abstract function getClassName();
// TODO Als Klassen formulieren
/*
* This method returns the relevant join operations on the data.
* The return value is an associated arrray whose keys are the names of the tables in the join.
* Each value of such joins is again an associative array.
* These arrays have the following entries:
* - alias: The alias name of the table to use. Mandatory
* - type: The type of the join. Can be inner, left, right or outer. Defaults to inner.
* - on: Join constraint as a string representation.
* - cols: The columns to insert in the query. Same structure as fetchAttributes() method
*
* Example:
* array(
* 'mytable'=>array(
* 'alias'=>'t1',
* 'type'=>'left',
* 'on'=>'main.id = t1.clubid',
* 'cols'=>array(
* 'name'=>array(),
* 'count'=>array('type'=>'int', 'col'=>'numElements'
* )
* )
* )
*/
protected function fetchJoins()
{
return array();
}
private $joins = null;
/**
* @return AbstractCommonClubsModelJoin[]
*/
public function getJoins($force = False)
{
if($this->joins === null || $force)
$this->joins = $this->fetchJoins();
return $this->joins;
}
/**
*
* @param string $condition
* @param string|array $sorting
* @return AbstractCommonClubsModel[]
*/
public function loadElements($condition = null, $sorting = null, $callback = null)
{
$db = Factory::getDbo();
$q = $db->getQuery(true);
// $columns = array_map(function($arr) use ($q){ return $q->qn('main' . $arr['col']); }, $this->fetchAttributes());
// $columns = array();
// foreach($this->fetchAttributes() as $k=>$v)
// {
// $columns[] = $q->qn('main' . $v['col'], $k);
// }
$q->select('main.id AS id');//->select($columns);
$q->from($this->getTableName() . ' AS main');
if($condition !== null)
$q->where($condition);
if($sorting !== null)
$q->order($sorting);
if($callback !== null)
{
$callback($q);
}
$db->setQuery($q);
$db->execute();
$rows = $db->loadAssocList();
$ret = array();
foreach($rows as $row)
{
$ret[] = $this->generateObject($row);
}
return $ret;
}
/**
* @param int $id
* @return AbstractCommonClubsModel
*/
public function loadById($id, $throwErr = true)
{
$arr = $this->loadElements("main.id = " . ((int)$id) );
if(sizeof($arr) == 0)
{
if($throwErr)
throw new ElementNotFoundException();
else
return null;
}
return $arr[0];
}
private function generatePlainObject($id)
{
$name = $this->getClassName();
$obj = new $name();
$obj->setId($id);
return $obj;
}
/**
* @param array $row
* @return AbstractCommonClubsModel
*/
protected function generateObject($row)
{
$obj = $this->generatePlainObject($row['id']);
$obj->markAsNew(false);
//unset($row['id']);
//$obj->setValues($row);
// Do not trigger cache if no needed
//$obj->getValues();
return $obj;
}
/**
* @return AbstractCommonClubsModel
*/
public function createNew()
{
$obj = $this->generatePlainObject('new');
$obj->markAsNew(true);
$values = array();
foreach($this->getAttributes() as $a)
{
$values[$a->getAlias()] = null;
}
// $attribs = array_map(function($v){
// return Null;
// }, $this->getAttributes());
$obj->setValues($values);
$obj->fillDefaultValues();
return $obj;
}
}