Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

filter in ModelAdmin for DATE-Field


Reply


2 Posts   1477 Views

Avatar
lx

Community Member, 83 Posts

4 September 2010 at 2:56am

Hi,

i have a class with a DATE-Field...

public static $db = array(
'paymentdate' => 'Date',
...
...
...
);

If the invoice is payed this field will save the date of payment.
If it is NULL it means the invoice hasnt been payed already.

In ModelAdmin I get a Searchfilter as an inputfield for filtering.
For me, it doesnt make sense to have an inputfield for filtering, because i dont want to search for invoices that are payed on a particular day.

I would like to have a dropdownbox with "any", "not payed", "payed" for filtering.

I can make a Dropdownfield for this, but i dont know how to define the filter, that searches for "paymentdate IS NULL"

public function getDefaultSearchContext() {

$context = parent::getDefaultSearchContext();
$context->removeFieldByName('paymentdate');
$paymentMap = array("" => "alle",
//"IS NULL" => "not payed",
"2000-01-01" => "payed");
$paymentField = new DropdownField('paymentdate', 'payed', $paymentMap);
$context->addField($paymentField);
$context->setFilters(array('paymentdate' => new GreaterThanFilter('paymentdate')));

return $context;
}

Avatar
lx

Community Member, 83 Posts

9 September 2010 at 8:26am

Ok, so i had to solve it myself.

Here is my new Filter Class HaveValueFilter for anyone who needs this too.

In your function getDefaultSearchContext() write this:

$zahlungMap = array("" => "any",
FALSE => "unpayed",
TRUE => "payed");

$zahlungseingangField = new DropdownField('Zahlungseingang', 'Zahlung', $zahlungMap);
$context->addField($zahlungseingangField);
$context->setFilters(array('Zahlungseingang' => new HaveValueFilter('Zahlungseingang')));

Make a new File "mysite/code/HaveValueFilter.php"

<?php
class HaveValueFilter extends SearchFilter {
   
   /**
    * @return $query
    */
   public function apply(SQLQuery $query) {
      
$query = $this->applyRelation($query);
      
if ($this->value) {
return $query->where(sprintf(
"%s IS NOT NULL",
$this->getDbName()
));
}
else {
return $query->where(sprintf(
"%s IS NULL",
$this->getDbName()
));
}

   }
   
   public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';
   }
}
?>