Inserted basic structure for clubs and models

This commit is contained in:
Christian Wolf 2019-04-17 16:41:28 +02:00
parent b08b01689f
commit 893dc754f0
7 changed files with 788 additions and 0 deletions

View File

@ -0,0 +1,155 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
abstract class ClubsAbstractModel
{
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] = $q->q($this->$m);
$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] = $q->q($this->$m);
$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();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete($this->getTableName())
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
protected function isDataValid()
{
return true;
}
}

320
src/admin/mymodels/club.php Normal file
View File

@ -0,0 +1,320 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsClub
{
protected $id;
protected $name;
protected $address;
protected $city;
protected $homapge;
protected $mail;
protected $iban;
protected $bic;
protected $charitable;
protected $presidentId;
/**
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* @return string
*/
public function getHomapge()
{
return $this->homapge;
}
/**
* @return string
*/
public function getMail()
{
return $this->mail;
}
/**
* @return string
*/
public function getIban()
{
return $this->iban;
}
/**
* @return string
*/
public function getBic()
{
return $this->bic;
}
/**
* @return bool
*/
public function isCharitable()
{
return $this->charitable;
}
/**
* @return int
*/
public function getPresidentId()
{
return $this->presidentId;
}
/**
* @return ClubsUser
*/
public function getPresident()
{
return ClubsUser::loadUser((int) $this->presidentId);
}
/**
* @param string $address
*/
public function setAddress(string $address)
{
$this->address = $address;
}
/**
* @param string $city
*/
public function setCity(string $city)
{
$this->city = $city;
}
/**
* @param string $homapge
*/
public function setHomapge(string $homapge)
{
$this->homapge = $homapge;
}
/**
* @param string $mail
*/
public function setMail(string$mail)
{
$this->mail = $mail;
}
/**
* @param string $iban
*/
public function setIban(string $iban)
{
$this->iban = $iban;
}
/**
* @param string $bic
*/
public function setBic(string $bic)
{
$this->bic = $bic;
}
/**
* @param bool $charitable
*/
public function setCharitable(bool $charitable)
{
$this->charitable = $charitable;
}
/**
* @param int $presidentId
*/
public function setPresidentId(int $presidentId)
{
$this->presidentId = $presidentId;
}
/**
* @param ClubsUser $user
*/
public function setPresident(ClubsUser $user)
{
$this->presidentId = $user->getId();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
protected function loadData(array $data)
{
$mapping = array('id', 'name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
foreach($mapping as $m)
$this->$m = $data[$m];
}
protected function __construct()
{}
public static function loadClubs()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_clubs');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $c)
{
$co = new ClubsClub();
$co->loadData($c);
$ret[] = $co;
}
return $ret;
}
/**
* @param int $id
* @throws Exception
* @return ClubsOffer
*/
public static function loadClub(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_clubs')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No club found.");
// TODO
}
$club = new ClubsClub();
$club->loadData($row);
return $club;
}
public static function createClub()
{
$club = new ClubsClub();
$club->id = 'new';
return $club;
}
public function save()
{
if($this->id === 'new')
$this->insertClub();
else
$this->updateClub();
}
private function insertClub()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
$values = array();
foreach($mapping as $m)
$values[$m] = $q->q($this->$m);
$q->insert('#__club_clubs')
->columns(array('name'))
->values(join(',', $values))
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
}
private function updateClub()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
$values = array();
foreach($mapping as $m)
$values[$m] = $q->q($this->$m);
$q->update('#__club_clubs');
foreach($mapping as $m)
$q->set($q->qn($m) . '=' . $values[$m]);
$q->where("id=". (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_clubs')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
}

View File

@ -0,0 +1,191 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsPlace
{
protected $id;
protected $name;
protected $address;
protected $area;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* @return string
*/
public function getArea()
{
return $this->area;
}
/**
* @param string $address
*/
public function setAddress(string $address)
{
$this->address = $address;
}
/**
* @param string $area
*/
public function setArea(string $area)
{
$this->area = $area;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct()
{}
public static function loadOffers()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_offers');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $o)
{
$oo = new ClubsOffer();
$oo->loadData($o);
$ret[] = $oo;
}
return $ret;
}
public static function loadOffer(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_offers')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No offer found.");
// TODO
}
$offer = new ClubsOffer();
$offer->loadData($row);
return $offer;
}
public static function createOffer()
{
$offer = new ClubsOffer();
$offer->id = 'new';
return $offer;
}
public function save()
{
if($this->id === 'new')
$this->insertOffer();
else
$this->updateOffer();
}
private function insertOffer()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->insert('#__club_offers')
->columns(array('name'))
->values("$vname")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
}
private function updateOffer()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->update('#__club_offers')
->set(array(
"name=$vname"
))
->where("id=". (int) $this->id)
;
$dbo->setQuery($q);
$dbo->execute();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_offers')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
}

View File

@ -0,0 +1,27 @@
<?php
// No direct access.
use Joomla\CMS\Router\Route;
defined('_JEXEC') or die;
?>
<form method="post" action="<?php echo $this->address; ?>">
<input type='hidden' name='id' value='<?php echo $this->offer->getId(); ?>'>
<table>
<tr>
<td>Bezeichnung</td>
<td><input type='text' name='name' value='<?php echo htmlentities($this->offer->getName()); ?>'></td>
</tr>
<?php if(! $this->isNew): ?>
<tr>
<td>ID</td>
<td><?php echo $this->offer->getId(); ?></td>
</tr>
<?php endif; ?>
</table>
<input type='submit' value='Speichern'> <br /><a href='<?php echo Route::_('index.php?option=com_clubs&view=offers'); ?>'>Zur&uuml;ck zur &Uuml;bersicht</a>
</form>

View File

@ -0,0 +1,47 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Toolbar\ToolbarHelper;
// No direct access.
defined('_JEXEC') or die;
class ClubsViewOffer extends HtmlView
{
function display($tpl = null)
{
$input = Factory::getApplication()->input;
$id = $input->get->get('id');
if($id === 'new')
{
$this->address = Route::_('index.php?option=com_clubs&task=offer.new');
$this->offer = ClubsOffer::createOffer();
$this->isNew = true;
}
else if(is_numeric($id))
{
$this->address = Route::_('index.php?option=com_clubs&task=offer.change');
$this->offer = ClubsOffer::loadOffer((int) $id);
$this->isNew = false;
}
else
throw new Exception('Need a user id.');
if($input->get->get('data', null, 'json') != null)
{
// Restore previous data
$dataurl = $input->get->get('data', null, 'json');
$data = json_decode($dataurl, true);
$this->offer->setName($data['name']);
}
ToolbarHelper::title('Club-Management - Angebot');
parent::display($tpl);
}
}

View File

@ -0,0 +1,28 @@
<?php
// No direct access.
use Joomla\CMS\Router\Route;
defined('_JEXEC') or die;
?>
<table class='table table-stiped, table-hover'>
<thead>
<tr>
<th width='30%'>Bezeichnung</th>
<th width='5%'>L&ouml;schen?</th>
<th width='5%'>id</th>
</tr>
</thead>
<?php foreach($this->offers as $offer): ?>
<?php $link = Route::_('index.php?option=com_clubs&view=offer&id=' . $offer->getId()); ?>
<tr>
<td><a href='<?php echo $link; ?>'><?php echo htmlentities($offer->getName()); ?></a></td>
<td><a href='<?php echo Route::_('index.php?option=com_clubs&task=offer.delete&id=' . $offer->getId()); ?>'>Del</a></td>
<td><?php echo htmlentities($offer->getId()); ?></td>
</tr>
<?php endforeach; ?>
</table>
<div><a href='<?php echo Route::_('index.php?option=com_clubs&view=offer&id=new'); ?>'>Neues Angebot anlegen</a></div>

View File

@ -0,0 +1,20 @@
<?php
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;
// No direct access.
defined('_JEXEC') or die;
class ClubsViewClubs extends HtmlView
{
function display($tpl = null)
{
// $this->offers = ClubsOffer::loadOffers();
ToolbarHelper::title('Club-Management - Clubs');
parent::display($tpl);
}
}