I like the admin generator very much, but want to extend its filtering capabilities. In a partial I want to give to fields from and to. And then I want to filter all objects which begins OR ends in between.
I think I need a method, where I can add a Criterion, so I added 3 lines in sfPropelAdmin/default/template/actions/actions.class.php:
$this->addFiltersCriteria($c);
<?php if ($this->getParameterValue('list.filter_method')): ?>
<?php echo $this->getPeerClassName() ?>::<?php echo $this->getParameterValue('list.filter_method') ?>($c, $this->filters);
<?php endif ?>
$this->pager->setCriteria($c);
Example
add filter_method in config/generator.yml:
list:
filters: [_beginns_or_ends, Soll]
filter_method: filterRange
the partial contains the select fields for the date range, in my case whole months, or the whole year.
echo select_tag('filters[range][monat]', options_for_select($array_monate, isset($filters['range']['monat']) ? $filters['range']['monat'] : $monat));
echo select_tag('filters[range][jahr]', options_for_select($array_jahre, isset($filters['range']['jahr']) ? $filters['range']['jahr'] : $jahr));
Then define a method in your PeerClass? with two parameters, the Criteria (to be added to) and the filter array with your request parameters:
public static function filterRange($c, $filter)
{
if (!isset($filter['range'])) return;
$monat = $filter['range']['monat'];
$jahr = $filter['range']['jahr'];
if ($jahr) {
if ($monat) {
$von = mktime(0, 0, 0, $monat, 1, $jahr);
$bis = mktime(0, 0, 0, $monat, date('t', $von), $jahr);
} else {
//ganzes Jahr
$von = mktime(0, 0, 0, 1, 1, $jahr);
$bis = mktime(0, 0, 0, 12, 31, $jahr);
}
$c1 = $c->getNewCriterion(self::VON, $von, Criteria::GREATER_EQUAL);
$c2 = $c->getNewCriterion(self::VON, $bis, Criteria::LESS_EQUAL);
$c1->AddAnd($c2);
$c3 = $c->getNewCriterion(self::BIS, $von, Criteria::GREATER_EQUAL);
$c4 = $c->getNewCriterion(self::BIS, $bis, Criteria::LESS_EQUAL);
$c3->AddAnd($c4);
$c1->addOr($c3);
$c->add($c1);
}
}
And you got a flexible way of filtering in admin generator.