Offers can be changed in the backend

This commit is contained in:
Christian Wolf 2019-06-11 13:54:21 +02:00
parent 40b88859cd
commit e3008b8789
4 changed files with 58 additions and 19 deletions

View File

@ -84,6 +84,7 @@ abstract class AbstractClubsController extends BaseController
// Do the actual work
$obj->save();
$this->saveAssocs($obj, $values);
// Redirect to the list of objects
$view = $this->getSingleViewName();
@ -105,6 +106,8 @@ abstract class AbstractClubsController extends BaseController
}
}
protected function saveAssocs($obj, $vlaues){}
protected function additionalData()
{
return array();

View File

@ -61,9 +61,14 @@ class CommonClubsModelClub extends AbstractCommonClubsModel
return $this->fetchAssociatedElements(new CommonClubsModelFactoryPlace(), 'clubid');
}
private function getOfferAssocs()
{
return $this->fetchAssociatedElements(new CommonClubsModelFactoryOfferassoc(), 'clubid');
}
public function getOffers()
{
$assocs = $this->fetchAssociatedElements(new CommonClubsModelFactoryOfferassoc(), 'clubid');
$assocs = $this->getOfferAssocs();
$offersFactory = new CommonClubsModelFactoryOffer();
$allOffers = $offersFactory->loadElements();
@ -92,6 +97,41 @@ class CommonClubsModelClub extends AbstractCommonClubsModel
return $ret;
}
/**
* @param int[] $ids
*/
public function setOfferIds($ids)
{
$dbo = $this->getFactory()->loadById($this->getId());
$currentOffersAssocs = $dbo->getOfferAssocs();
$currentIds = array_map(function($obj){
return $obj->getOffer()->getId();
}, $currentOffersAssocs);
$newIds = array_diff($ids, $currentIds);
$delIds = array_diff($currentIds, $ids);
$offerAssocFactory = new CommonClubsModelFactoryOfferassoc();
$offerFactory = new CommonClubsModelFactoryOffer();
foreach($delIds as $id)
{
$delId = (int) $id;
$delObjs = $offerAssocFactory->loadElements("clubid = {$this->getId()} AND offerid = $delId");
foreach($delObjs as $o)
$o->delete();
}
foreach($newIds as $id)
{
$newId = (int) $id;
$o = $offerAssocFactory->createNew();
$o->setOffer($offerFactory->loadById($newId));
$o->setClub($this);
$o->save();
}
}
public function getUsers()
{
return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'clubid');

View File

@ -12,22 +12,6 @@ class ClubsControllerClub extends AbstractClubsController
return 'club';
}
protected function getDataMapping()
{
return array(
// 'name' => array('required'=>true, 'name'=>'Bezeichnung', 'filter'=>'string')
'name' => array('required'=>true, 'name'=>'Club-Name', 'filter'=>'string'),
'address' => array('required'=>true, 'name'=>'Adresse', 'filter'=>'string'),
'city' => array('required'=>true, 'name'=>'Stadt', 'filter'=>'string'),
'homepage' => array('required'=>false, 'name'=>'Homepage', 'filter'=>'string'),
'mail' => array('required'=>true, 'name'=>'E-Mail', 'filter'=>'string'),
'iban' => array('required'=>true, 'name'=>'IBAN', 'filter'=>'string'),
'bic' => array('required'=>true, 'name'=>'BIC', 'filter'=>'string'),
'charitable' => array('skip_null_check'=>True),
'president' => array('required'=>true, 'name'=>'Vorsitzender', 'skip_null_check'=>True, 'setter'=>'setPresidentId')
);
}
protected function filterRaw(&$values)
{
if(is_null($values['charitable']))
@ -47,5 +31,16 @@ class ClubsControllerClub extends AbstractClubsController
return new CommonClubsModelFactoryClub();
}
protected function additionalData()
{
return array(
'offers' => new CommonClubsControllerMappingInt('Angebot')
);
}
protected function saveAssocs($obj, $values)
{
$obj->setOfferIds($values['offers']);
}
}

View File

@ -78,9 +78,10 @@ defined('_JEXEC') or die;
<?php if(! $this->isNew): ?>
<h2>Angebote</h2>
<?php foreach($this->object->getOffers() as $oconf): ?>
<input type='checkbox' name='offers' value='<?php echo $oconf['offer']->getId(); ?>' <?php if($oconf['valid']) echo 'checked="checked"';?>>
<input type='checkbox' name='offers[]' value='<?php echo $oconf['offer']->getId(); ?>' <?php if($oconf['valid']) echo 'checked="checked"';?>>
<?php echo htmlentities($oconf['offer']->getName()); ?><br />
<?php endforeach; ?>
<h2>Posten</h2>
<table width='100%' class='table table-stiped, table-hover'>
<tr>