Skip to content

Instantly share code, notes, and snippets.

@stefansl
Last active October 1, 2016 13:32
Show Gist options
  • Save stefansl/c090b300d00d72cd5c08eeeebcd95c76 to your computer and use it in GitHub Desktop.
Save stefansl/c090b300d00d72cd5c08eeeebcd95c76 to your computer and use it in GitHub Desktop.
Pass Contao filter/search/order settings to your export class
<?php
/**
* Class MyExport
*/
class MyExport
{
/**
* @param \DataContainer $dc
*
* @return \Model\Collection|null|static
*/
public function getRecords(\DataContainer $dc)
{
// Get all filter/search/sorting settings from the session
$filter = $dc->Session->get('filter')[$dc->table];
$search = $dc->Session->get('search')[$dc->table];
$sorting = $dc->Session->get('sorting')[$dc->table];
// I don't need any limit settings
unset($filter['limit']);
$i = 0;
$arrFields = array();
$arrOptions =array();
// add filter settings
if (is_array($filter)) {
foreach ($filter as $k => $v) {
$arrFields[0][$i] = $dc->table . '.' . $k . '=?';
$arrFields[1][$i] = $v;
$i++;
}
}
// add search settings
if (!empty($search['value'])) {
$arrFields[0][$i] = 'LOWER(CAST(' . $dc->table . '.' . $search['field'] . ' AS CHAR)) REGEXP LOWER(?)';
$arrFields[1][$i] = $search['value'];
// alternative: get german umlauts, fe. ä from a
// $arrFields[0][$i] = $dc->table . '.' . $search['field'] . ' LIKE ?';
// $arrFields[1][$i] = '%' . $search['value'] . '%';
}
// add order settings
$arrOptions['order'] = (!empty($sorting)) ? $sorting : 'id ASC';
// fetch data / replace MyModel!
if (!empty($arrFields)) {
$objExport = MyModel::findBy(array_values($arrFields[0]), array_values($arrFields[1]), $arrOptions);
} else {
$objExport = MyModel::findAll($arrOptions);
}
return $objExport;
}
/**
* @param \DataContainer $dc
*
* @return string
*/
public function export(\DataContainer $dc)
{
$objExport = $this->getRecords($dc);
// get your data, fe. $objExport->id
dump($objExport);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment