Created abstract structures to keep complexity and code redundancy at bay.
This commit is contained in:
187
src/admin/abstract/model.php
Normal file
187
src/admin/abstract/model.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?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)
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
$q->select('*')
|
||||
->from($tableName);
|
||||
$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)->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);
|
||||
return $obj;
|
||||
}
|
||||
|
||||
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 = array();
|
||||
foreach($mappings as $m)
|
||||
$values[$m] = $this->$m;
|
||||
|
||||
$this->filter($values);
|
||||
|
||||
foreach($mappings as $m)
|
||||
$values[$m] = $q->q($values[$m]);
|
||||
|
||||
$this->postQuoteFilter($values);
|
||||
|
||||
$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 = array();
|
||||
foreach($mapping as $m)
|
||||
$values[$m] = $this->$m;
|
||||
|
||||
$this->filter($values);
|
||||
|
||||
foreach($mapping as $m)
|
||||
$values[$m] = $q->q($values[$m]);
|
||||
|
||||
$this->postQuoteFilter($values);
|
||||
|
||||
$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();
|
||||
}
|
||||
|
||||
protected function prepareDelete(){}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if($this->id === 'new')
|
||||
return;
|
||||
|
||||
$this->prepareDelete();
|
||||
|
||||
$dbo = Factory::getDbo();
|
||||
|
||||
$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) || empty($this->$m) || $this->$m === null)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function filter(&$values){}
|
||||
protected function postQuoteFilter(&$values){}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user