So nach ewigen probiere habsch einfach mal das aus dem Isotobe genommen.
PHP-Code:
'onload_callback' => array
(
array('tl_wt_travel', 'applyAdvancedFilters'),
)
PHP-Code:
'panelLayout' => 'iso_filter,filter;sort,search,limit',
'panel_callback' => array
(
'iso_filter' => array('tl_wt_travel', 'generateAdvancedFilters'),
)
PHP-Code:
*/
public function generateAdvancedFilters()
{
if (\Input::get('id') > 0) {
return '';
}
$arrOptions = array();
$objPages = $this->Database->prepare("SELECT c.id,c.pid,c.country,k.continent FROM tl_wt_countries c
LEFT JOIN tl_wt_continent k ON c.pid = k.id
ORDER BY c.country ASC")->execute();
while($objPages->next()){
$arrOptions[$objPages->continent][$objPages->id] = "$objPages->country";
}
$session = \Session::getInstance()->getData();
// Filters
$arrFilters = array
(
'travelCountries' => array
(
'name' => 'travelCountries',
'label' => $GLOBALS['TL_LANG']['tl_wt_travel']['filter_travelCountries'],
'options' => $arrOptions,
),
);
$strBuffer = '<div class="tl_filter iso_filter tl_subpanel">
<strong>' . $GLOBALS['TL_LANG']['tl_wt_travel']['filter'] . '</strong>' . "\n";
// Generate filters
foreach ($arrFilters as $arrFilter) {
$strOptions = '
<option value="' . $arrFilter['name'] . '">' . $arrFilter['label'] . '</option>
<option value="' . $arrFilter['name'] . '">---</option>' . "\n";
// Generate options
foreach ($arrFilter['options'] as $k => $v) {
if(is_array($v)) {
$strOptions .= '<optgroup label="'.$k.'">';
foreach ($v as $id => $name) {
$strOptions .= ' <option value="' . $id . '"' . (($session['filter']['tl_wt_travel'][$arrFilter['name']] === (string) $id) ? ' selected' : '') . '>' . $name . '</option>' . "\n";
}
$strOptions .= '</optgroup>';
} else {
$strOptions .= ' <option value="' . $k . '"' . (($session['filter']['tl_wt_travel'][$arrFilter['name']] === (string) $k) ? ' selected' : '') . '>' . $v . '</option>' . "\n";
}
}
$strBuffer .= '<select name="' . $arrFilter['name'] . '" id="' . $arrFilter['name'] . '" class="tl_select' . (isset($session['filter']['tl_wt_travel'][$arrFilter['name']]) ? ' active' : '') . '">
' . $strOptions . '
</select>' . "\n";
}
return $strBuffer . '</div>';
}
public function applyAdvancedFilters()
{
$session = $this->Session->getData();
// Store filter values in the session
foreach ($_POST as $k => $v) {
// Reset the filter
if ($k == \Input::post($k)) {
unset($session['filter']['tl_wt_travel'][$k]);
} // Apply the filter
else {
$session['filter']['tl_wt_travel'][$k] = \Input::post($k);
}
}
$this->Session->setData($session);
if (\Input::get('id') > 0 || !isset($session['filter']['tl_wt_travel'])) {
return;
}
$arrProducts = null;
// Filter the products
foreach ($session['filter']['tl_wt_travel'] as $k => $v) {
switch ($k) {
// Show products with or without images
case 'travelCountries':
$objProducts = \Database::getInstance()->prepare("SELECT id FROM tl_wt_travel WHERE travelCountries LIKE ?")->execute('%'.serialize($v).'%');
$arrProducts = is_array($arrProducts) ? array_intersect($arrProducts, $objProducts->fetchEach('id')) : $objProducts->fetchEach('id');
break;
default:
// !HOOK: add custom advanced filters
if (isset($GLOBALS['ISO_HOOKS']['applyAdvancedFilters']) && is_array($GLOBALS['ISO_HOOKS']['applyAdvancedFilters'])) {
foreach ($GLOBALS['ISO_HOOKS']['applyAdvancedFilters'] as $callback) {
$objCallback = \System::importStatic($callback[0]);
$arrReturn = $objCallback->$callback[1]($k);
if (is_array($arrReturn)) {
$arrProducts = is_array($arrProducts) ? array_intersect($arrProducts, $arrReturn) : $arrReturn;
break;
}
}
}
\System::log('Advanced product filter "' . $k . '" not found.', __METHOD__, TL_ERROR);
break;
}
}
if (is_array($arrProducts) && empty($arrProducts)) {
$arrProducts = array(0);
}
$GLOBALS['TL_DCA']['tl_wt_travel']['list']['sorting']['root'] = $arrProducts;
}
Also funktiniert jetzt echt super und wir können nun die Reisen 1a nach Länder anzeigen lassen
Meine Frage ist nun, gehts eventuell auch einfacher?
EDIT: kleines Problem,
wenn man einen Datensatz bearbeitet, werden automotsch die anderen Felder die man Filtern kann in der Listenansicht übernommen und es werden die Einträge gefiltet.
Beispiel:
Custom Filter: LAnd 1 Contao Filter Tage ---
jetzt bearbeite ich einen Datensatz -> Speichern und Schließen
Custom Filter: LAnd 1 Contao Filter Tage 7