Skip to content

Instantly share code, notes, and snippets.

@daKmoR
Created March 4, 2011 01:15
Show Gist options
  • Save daKmoR/853970 to your computer and use it in GitHub Desktop.
Save daKmoR/853970 to your computer and use it in GitHub Desktop.
implementing a demand overwrite in Extbase
<?php
/**
* Displays all Labors
*
* @param Tx_CdgLabors_Domain_Model_Demand $demand
* @param array $demandOverwrite
* @return string The rendered list view
*/
public function listAction(Tx_CdgLabors_Domain_Model_Demand $demand = NULL, array $demandOverwrite = NULL) {
if (count($demandOverwrite)> 0) {
if($demand === NULL) {
$demand = t3lib_div::makeInstance('Tx_CdgLabors_Domain_Model_Demand');
}
$demandOverwrite = $this->initDemandOverwrite($demandOverwrite);
$demand->overrideWithArray($demandOverwrite);
}
$this->view->assign('demand', $demand);
//[...]
}
/*
* populates the demandOverwrite with correct Objects
*
* @param array the demandOverwrite to init
* @return array initialized demandOverwrite
*/
public function initDemandOverwrite(array $demandOverwrite) {
if ($demandOverwrite['labor'] > 0) {
$demandOverwrite['labor'] = $this->laborRepository->findByUid($demandOverwrite['labor']);
}
if ($demandOverwrite['person'] > 0) {
$demandOverwrite['person'] = $this->personRepository->findByUid($demandOverwrite['person']);
}
if (count($demandOverwrite['organisations']) > 0) {
foreach($demandOverwrite['organisations'] as &$organisation) {
$organisation = $this->organisationRepository->findByUid($organisation);
}
unset($organisation);
}
if (count($demandOverwrite['subjects']) > 0) {
foreach($demandOverwrite['subjects'] as &$subject) {
$subject = $this->subjectRepository->findByUid($subject);
}
unset($subject);
}
if (count($demandOverwrite['partners']) > 0) {
foreach($demandOverwrite['partners'] as &$partner) {
$partner = $this->partnerRepository->findByUid($partner);
}
unset($partner);
}
return $demandOverwrite;
}
<?php
/**
* provides the demand object as "plain" array, so it can be used also in link action as arguments
*
* @param $nameSpace in which array it should be place
* @return array
*/
public function getOverrideArray($nameSpace = 'demandOverwrite') {
$demandOverwrite = array();
if ($this->getLabor()) {
$demandOverwrite['labor'] = $this->getLabor();
}
if ($this->getPerson()) {
$demandOverwrite['person'] = $this->getPerson();
}
if (count($this->getYears()) > 0) {
$demandOverwrite['years'] = $this->getYears();
}
if (count($this->getOrganisations()) > 0) {
$demandOverwrite['organisations'] = $this->getOrganisations();
}
if (count($this->getSubjects()) > 0) {
$demandOverwrite['subjects'] = $this->getSubjects();
}
if (count($this->getStates()) > 0) {
$demandOverwrite['states'] = $this->getStates();
}
if (count($this->getPartners()) > 0) {
$demandOverwrite['partners'] = $this->getPartners();
}
if ($this->getSearchWord() !== NULL && $this->getSearchWord() !== '') {
$demandOverwrite['searchWord'] = $this->getSearchWord();
}
if ($this->getSearchWordTime() > 0) {
$demandOverwrite['searchWordTime'] = $this->getSearchWordTime();
}
return array($nameSpace => $demandOverwrite);
}
/**
* the values of the passed array override the current values if the name of the key matches with a propertyname
*
* @param array $overrideArray
* @return $this
*/
public function overrideWithArray(array $overrideArray) {
foreach ($overrideArray as $propertyName => $propertyValue) {
$setterMethod = 'set' . ucfirst($propertyName);
if (method_exists($this, $setterMethod)) {
$this->{$setterMethod}($propertyValue);
}
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment