Created structure to access the offers of the clubs

This commit is contained in:
Christian Wolf 2019-04-16 16:08:11 +02:00
parent 63caca5b80
commit 0bafe438e2
7 changed files with 409 additions and 19 deletions

View File

@ -0,0 +1,108 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
class ClubsControllerOffer extends BaseController
{
function new()
{
$app = Factory::getApplication();
$input = $app->input;
$o = ClubsOffer::createOffer();
// Fetch the posted data
$name = $input->post->getString('name');
// Check the input data
$error = false;
// Check existence of the other fields
$fields = array('name'=>'Bezeichnung');
foreach ($fields as $f => $fname)
{
$fvalue = $$f;
if(! isset($fvalue) || empty(trim($fvalue)))
{
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$error = true;
}
}
if($error)
{
$data = array();
foreach(array('name') as $i)
$data[$i] = $$i;
$urldata = urlencode(json_encode($data));
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=new&data=' . $urldata, false));
return;
}
$o->setName($name);
// Do the actual work
$o->save();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
}
function change()
{
$app = Factory::getApplication();
$input = $app->input;
$id = (int) $input->post->getInt('id');
$o = ClubsOffer::loadOffer((int) $id);
// Fetch the posted data
$name = $input->post->getString('name');
// Check the input data
$error = false;
// Check existence of the other fields
$fields = array('name'=>'Bezeichnung');
foreach ($fields as $f => $fname)
{
$fvalue = $$f;
if(! isset($fvalue) || empty(trim($fvalue)))
{
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$error = true;
}
}
if($error)
{
$data = array();
foreach(array('name') as $i)
$data[$i] = $$i;
$urldata = urlencode(json_encode($data));
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=' . $id . '&data=' . $urldata, false));
return;
}
$o->setName($name);
// Do the actual work
$o->save();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
}
function delete()
{
$app = Factory::getApplication();
$id = $app->input->get->getInt('id');
$app->enqueueMessage("Removal of offer with id $id.");
$user = ClubsOffer::loadOffer($id);
$user->delete();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
}
}

View File

@ -0,0 +1,157 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsOffer
{
protected $id;
protected $name;
/**
* @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)
{
$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

@ -13,7 +13,6 @@ class PasswordInvalidException extends Exception
class ClubsUser class ClubsUser
{ {
protected $dbo;
protected $id; protected $id;
protected $user; protected $user;
protected $hash; protected $hash;
@ -184,9 +183,7 @@ class ClubsUser
} }
protected function __construct() protected function __construct()
{ {}
$this->dbo = Factory::getDbo();
}
public static function loadUsers() public static function loadUsers()
{ {
@ -261,7 +258,8 @@ class ClubsUser
private function insertUser() private function insertUser()
{ {
$q = $this->dbo->getQuery(true); $dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vuser = $q->q($this->user); $vuser = $q->q($this->user);
$vpassword = $q->q($this->hash); $vpassword = $q->q($this->hash);
@ -277,16 +275,17 @@ class ClubsUser
->values("$vuser, $vpassword, $vname, $vaddress, $vcity, $vmail, $vphone, $vmobile") ->values("$vuser, $vpassword, $vname, $vaddress, $vcity, $vmail, $vphone, $vmobile")
; ;
$this->dbo->transactionStart(); $dbo->transactionStart();
$this->dbo->setQuery($q); $dbo->setQuery($q);
$this->dbo->execute(); $dbo->execute();
$this->id = $this->dbo->insertid(); $this->id = $dbo->insertid();
$this->dbo->transactionCommit(); $dbo->transactionCommit();
} }
private function updateUser() private function updateUser()
{ {
$q = $this->dbo->getQuery(true); $dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vuser = $q->q($this->user); $vuser = $q->q($this->user);
$vpassword = $q->q($this->hash); $vpassword = $q->q($this->hash);
@ -311,8 +310,8 @@ class ClubsUser
->where("id=". (int) $this->id) ->where("id=". (int) $this->id)
; ;
$this->dbo->setQuery($q); $dbo->setQuery($q);
$this->dbo->execute(); $dbo->execute();
} }
private function checkHash(string $password) private function checkHash(string $password)
@ -323,10 +322,13 @@ class ClubsUser
if(password_needs_rehash($this->hash, PASSWORD_DEFAULT)) if(password_needs_rehash($this->hash, PASSWORD_DEFAULT))
{ {
$this->hash = password_hash($password, PASSWORD_DEFAULT); $this->hash = password_hash($password, PASSWORD_DEFAULT);
$q = $this->dbo->getQuery(true);
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id); $q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id);
$this->dbo->setQuery($q); $dbo->setQuery($q);
$this->dbo->execute(); $dbo->execute();
} }
} }
@ -334,13 +336,14 @@ class ClubsUser
{ {
if($this->id === 'new') if($this->id === 'new')
return; return;
$dbo = Factory::getDbo();
$q = $this->dbo->getQuery(true); $q = $dbo->getQuery(true);
$q->delete('#__club_users') $q->delete('#__club_users')
->where('id=' . (int) $this->id); ->where('id=' . (int) $this->id);
$this->dbo->setQuery($q); $dbo->setQuery($q);
$this->dbo->execute(); $dbo->execute();
} }
public static function isUserNameFree($username, int $id = -1) public static function isUserNameFree($username, int $id = -1)

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 ClubsViewOffers extends HtmlView
{
function display($tpl = null)
{
$this->offers = ClubsOffer::loadOffers();
ToolbarHelper::title('Club-Management - Angebote');
parent::display($tpl);
}
}