diff --git a/src/admin/abstract/modelfactory.php b/src/admin/abstract/modelfactory.php new file mode 100644 index 0000000..f23cc98 --- /dev/null +++ b/src/admin/abstract/modelfactory.php @@ -0,0 +1,177 @@ +tableName = $tableName; + $this->className = $className; + } + + /** + * + * @param JDatabaseDriver $dbo + * @return array + */ + protected function getJoins($dbo) + { + /* + * Desired structure: + * array, ech element describing one join type. + * Each element of the array is a assosiated array describing the join: + * - Name of the table (must not be main) + * - Table alias + * - Type of the join (inner, left, right, outer), default is inner + * - Condition as a string (might need escaping) + * - Columns to be inserted in the join, defaults to * + * example: + * $ret = array(); + * $ret[] = array( + * 'name' => '#__table_name', + * 'alias' => 't1', + * 'type' => 'right', + * 'on' => 'main.extid = t1.id' + * 'select' => array('id','name') + * ); + * return $ret; + */ + return array(); + } + + /** + * + * @param JDatabaseDriver $dbo + * @param JDatabaseQuery $q + */ + private function insertJoinRelations($dbo, $q) + { + $joins = $this->getJoins($dbo); + foreach($joins as $j) + { + if(is_null($j['name'])) + throw new Exception('No name was given in the join.'); + if(is_null($j['alias'])) + throw new Exception('No alisas was given in the join.'); + + $jstr = $dbo->qn($j['name'], $j['alias']); + + if(is_null($j['on'])) + throw new Exception('No on clause was provided.'); + + $jstr .= " ON {$j['on']}"; + + if(is_null($j['type'])) + $j['type'] = 'inner'; + + switch($j['type']) + { + case 'inner': + $q->innerJoin($jstr); + break; + + case 'outer': + $q->outerJoin($jstr); + break; + + case 'left': + $q->leftJoin($jstr); + break; + + case 'right': + $q->rightJoin($jstr); + break; + + default: + throw new Exception("Type of join unknown: {$j['type']}."); + } + + $this->addJoinSelectColumns($j, $q); + } + } + + /** + * + * @param array $j + * @param JDatabaseQuery $q + */ + private function addJoinSelectColumns($j, $q) + { + // TODO Quote names + if(is_null($j['select'])) + $j['select'] = '*'; + + if(is_array($j['select'])) + { + array_map(function(&$str) use ($j) { + $str = "{$j['alias']}.$str"; + }, $j['select']); + } + else + { + $q->select("{$j['alias']}.{$j['select']}"); + } + } + + public function loadElement(int $id) + { + $condition = "id = $id"; + + return $this->loadFirstElement($condition); + } + + public function loadFirstElement($condition) + { + $objs = $this->loadElements($condition); + + if(sizeof($objs) == 0) + { + throw new Exception("No object of class {$this->className} found."); + // TODO + } + + return $objs[0]; + } + + public function loadElements($condition = null) + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + + $q->select('main.*')->from($this->tableName, 'main'); + $this->insertJoinRelations($dbo, $q); + + if(isset($condition)) + { + $q->where($condition); + } + $dbo->setQuery($q); + $dbo->execute(); + + $list = $dbo->loadAssocList(); + + $ret = array(); + foreach($list as $row) + { + $ret[] = $this->createElement($row); + } + + return $ret; + } + + protected function createElement($row) + { + $obj = new $this->className(); + $obj->loadData($row); + $obj->loadCustomData($row); + return $obj; + } +} diff --git a/src/admin/abstract/view/single.php b/src/admin/abstract/view/single.php index d9df3e2..70d1a6a 100644 --- a/src/admin/abstract/view/single.php +++ b/src/admin/abstract/view/single.php @@ -14,8 +14,11 @@ abstract class AbstractClubsViewSingle extends HtmlView protected $object; protected $isNew; - function display($tpl = null) + private $prepared = FALSE; + + public function prepareDisplay() { + $this->prepared = TRUE; $input = Factory::getApplication()->input; $id = $input->get->get('id'); @@ -48,6 +51,12 @@ abstract class AbstractClubsViewSingle extends HtmlView $controller->applyData($this->object, $data); } + } + + public function display($tpl = null) + { + if(!$this->prepared) + $this->prepareDisplay(); parent::display($tpl); } diff --git a/src/admin/mymodels/club.php b/src/admin/mymodels/club.php index 406d414..7a5c464 100644 --- a/src/admin/mymodels/club.php +++ b/src/admin/mymodels/club.php @@ -209,10 +209,10 @@ class ClubsClub extends AbstractClubsModel } -// public function getOffers() -// { - -// } + public function getOffers() + { + return 0; + } protected function loadCustomData($assoc) diff --git a/src/admin/mymodels/offer.php b/src/admin/mymodels/offer.php index 127d479..b3d1f1c 100644 --- a/src/admin/mymodels/offer.php +++ b/src/admin/mymodels/offer.php @@ -30,6 +30,23 @@ class ClubsOffer extends AbstractClubsModel private const tableName = '#__club_offers'; private const className = 'ClubsOffer'; + public static function getFactory() + { + return new class extends AbstractClubsModelFactory { + public function __construct() + { + parent::__construct($this->tableName, $this->className); + } + + protected function getJoins($dbo) + { + $ret = array(); + return $ret; + } + + }; + } + public static function loadOffers() { return self::loadElements(self::tableName, self::className); diff --git a/src/admin/mymodels/offerassociation.php b/src/admin/mymodels/offerassociation.php index 2f7e77a..2329066 100644 --- a/src/admin/mymodels/offerassociation.php +++ b/src/admin/mymodels/offerassociation.php @@ -37,6 +37,11 @@ class ClubsOfferAssociation extends AbstractClubsModel protected function __construct() {} + public static function getFactory() + { + return new ClubsOfferAssociationFactory(); + } + private const tableName = '#__club_offer_assocs'; private const className = 'ClubsOfferAssociation'; diff --git a/src/admin/mymodels/offerassociationfactory.php b/src/admin/mymodels/offerassociationfactory.php new file mode 100644 index 0000000..5853657 --- /dev/null +++ b/src/admin/mymodels/offerassociationfactory.php @@ -0,0 +1,32 @@ + '#__club_offers', + 'alias' => 'offer', + 'on' => 'main.offerid = offer.id', + 'select' => 'id as offerId' + ); + $ret[] = array( + 'name' => '#__club_clubs', + 'alias' => 'club', + 'on' => 'main.clubid = club.id', + 'select' => 'id AS clubId' + ); + return $ret; + } + +} diff --git a/src/admin/views/club/tmpl/default.php b/src/admin/views/club/tmpl/default.php index b9588c8..59167e6 100644 --- a/src/admin/views/club/tmpl/default.php +++ b/src/admin/views/club/tmpl/default.php @@ -66,6 +66,15 @@ defined('_JEXEC') or die; isNew): ?> + + Angebote + + offers as $o): ?> + getId(); ?>' > + getName()); ?>
+ + + ID object->getId(); ?> diff --git a/src/admin/views/club/view.html.php b/src/admin/views/club/view.html.php index 92cecfb..1d1e477 100644 --- a/src/admin/views/club/view.html.php +++ b/src/admin/views/club/view.html.php @@ -13,8 +13,21 @@ class ClubsViewClub extends AbstractClubsViewSingle { ToolbarHelper::title('Club-Management - Verein'); + $this->prepareDisplay(); + $this->users = ClubsUser::loadUsers(); + if(! $this->isNew) + { + $offers = ClubsOffer::loadOffers(); + $currentOffers = $this->object->getOffers(); + + $this->offers = array_map(function($offer) use ($currentOffers){ + $mark = False; + return array('offer'=>$offer, 'mark'=>$mark); + }, $offers); + } + parent::display($tpl); }