Last active
August 29, 2015 14:19
-
-
Save maddy2101/183400d08ef1dae4f6aa to your computer and use it in GitHub Desktop.
how to find and display records in a list view filtered by user defined input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Controller ### | |
public function listAction($search = NULL) { | |
if ($search === NULL) { | |
/** @var ...\Domain\Model\Search $search */ | |
$search = $this->objectManager->get('...\\Domain\\Model\\Search'); | |
$this->getCategorySearchFilterFromFlexForm($search); // this sets some preconfigured values from a flexform | |
} | |
$records = $this->recordRepository->findBySearchParameters($search); | |
$this->view->assign('search', $search); | |
$this->view->assign('records', $records); | |
} | |
### Repository ### | |
/** | |
* @param Search $search | |
* | |
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface | |
*/ | |
public function findBySearchParameters(Search $search) { | |
/** @var $query \TYPO3\CMS\Extbase\Persistence\QueryInterface */ | |
$query = $this->createQuery(); | |
$query->setOrderings(array('sorting' => QueryInterface::ORDER_DESCENDING)); | |
$properties = array(); | |
if ($search->getCategories()) { | |
$this->getCategoryProperties($search->getCategories(), $query, $properties); | |
} | |
if ($search->getPremiumFilter() === TRUE || $search->getPremiumFilter() === FALSE) { | |
$properties[] = $query->equals('premium', $search->getPremiumFilter()); | |
} | |
// in case premium only is set, don't care about other filters | |
if ($search->getPremiumFilter() === TRUE) { | |
if (count($properties) == 0) { | |
return $this->findAll(); | |
} elseif (count($properties) == 1) { | |
$query->matching( | |
$properties[0] | |
); | |
} else { | |
$query->matching( | |
$query->logicalAnd( | |
$properties | |
) | |
); | |
} | |
$result = $query->execute(); | |
return $result; | |
} | |
// get the searchWord properties | |
if (strlen($search->getSearchword()) > 0) { | |
$this->getSearchWordProperties($search->getSearchword(), $query, $properties, $search->getModel()); | |
} | |
if ($search->getLocationRange() > 0) { | |
$properties[] = $query->equals('locationRange', $search->getLocationRange()); | |
} | |
if ($search->getPriceRange() > 0) { | |
$properties[] = $query->equals('priceRange', $search->getPriceRange()); | |
} | |
if ($search->getClassification() > 0) { | |
$properties[] = $query->equals('classification', $search->getClassification()); | |
} | |
if (count($properties) == 0) { | |
return $this->findAll(); | |
} elseif (count($properties) == 1) { | |
$query->matching( | |
$properties[0] | |
); | |
} else { | |
$query->matching( | |
$query->logicalAnd( | |
$properties | |
) | |
); | |
} | |
$result = $query->execute(); | |
return $result; | |
} | |
### View ### | |
Search form to filter the record list: | |
<f:form enctype="multipart/form-data" action="list" controller="Records" | |
object="{search}" name="search" class="search"> | |
{f:if(condition:search_show, then: '', else: 'style="display:none;"')} > | |
<p class="column left"> | |
<label for="searchword"> | |
<f:translate key="search.find"/> | |
</label> | |
<f:form.textfield property="searchword"/> | |
<label for="category"> | |
<f:translate key="category"/> | |
</label> | |
<f:form.select property="categories" options="{all_categories}" | |
optionValueField="uid" | |
optionLabelField="title" | |
multiple="1"/> | |
</p> | |
<p class="column right"> | |
<label for="locationRange"> | |
<f:translate | |
key="location_range"/> | |
</label> | |
<f:form.select property="locationRange" options="{all_locationRanges}" | |
optionLabelField="showTitle" | |
prependOptionLabel="{f:translate(key:'choose')}" | |
prependOptionValue="0"/> | |
<br/> | |
<label for="priceRange"> | |
<f:translate | |
key="price_range"/> | |
</label> | |
<f:form.select property="priceRange" options="{all_priceRanges}" | |
optionLabelField="showTitle" | |
prependOptionLabel="{f:translate(key:'choose')}" | |
prependOptionValue="0"/> | |
<br/> | |
<label for="classification"> | |
<f:translate | |
key="classification"/> | |
</label> | |
<f:form.select property="classification" options="{all_classifications}" | |
optionLabelField="showTitle" | |
prependOptionLabel="{f:translate(key:'choose')}" | |
prependOptionValue="0"/> | |
</p> | |
<br class="clearfix" /> | |
<f:form.submit value="{f:translate(key:'search.submit')}" class="submit button"/> | |
<f:form.button type="reset" value="{f:translate(key:'search.reset')}" | |
class="reset button" >{f:translate(key:'search.reset')}</f:form.button> | |
<br class="clearfix"/> | |
</f:form> | |
List of records: | |
<f:widget.paginate objects="{records}" as="paginatedObjects" | |
configuration="{itemsPerPage: settings.listView.default.itemsPerPage, insertAbove: 1, insertBelow: 1}"> | |
<br class="clearfix"/> | |
<f:for each="{paginatedObjects}" as="object" iteration="iterator"> | |
<f:render partial="Records/ListItem" arguments="{_all}" | |
section="Main"/> | |
</f:for> | |
</f:widget.paginate> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment