From e3008b8789e55f5c61fca8a8b3ce7b96d941242a Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 11 Jun 2019 13:54:21 +0200 Subject: [PATCH] Offers can be changed in the backend --- src/admin/abstract/controller.php | 3 ++ src/admin/common/models/club.php | 42 ++++++++++++++++++++++++++- src/admin/controllers/club.php | 29 ++++++++---------- src/admin/views/club/tmpl/default.php | 3 +- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/admin/abstract/controller.php b/src/admin/abstract/controller.php index 0856cbe..168197f 100644 --- a/src/admin/abstract/controller.php +++ b/src/admin/abstract/controller.php @@ -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(); diff --git a/src/admin/common/models/club.php b/src/admin/common/models/club.php index a41e52b..37717da 100644 --- a/src/admin/common/models/club.php +++ b/src/admin/common/models/club.php @@ -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'); diff --git a/src/admin/controllers/club.php b/src/admin/controllers/club.php index b6114c3..75d2b8d 100644 --- a/src/admin/controllers/club.php +++ b/src/admin/controllers/club.php @@ -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'])) @@ -46,6 +30,17 @@ 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']); + } + } diff --git a/src/admin/views/club/tmpl/default.php b/src/admin/views/club/tmpl/default.php index e66f5b7..908952a 100644 --- a/src/admin/views/club/tmpl/default.php +++ b/src/admin/views/club/tmpl/default.php @@ -78,9 +78,10 @@ defined('_JEXEC') or die; isNew): ?>

Angebote

object->getOffers() as $oconf): ?> - getId(); ?>' > + getId(); ?>' > getName()); ?>
+

Posten