Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » filter in ModelAdmin for DATE-Field

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1400 Views
  • lx
    Avatar
    Community Member
    82 Posts

    filter in ModelAdmin for DATE-Field Link to this post

    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;
    }

  • lx
    Avatar
    Community Member
    82 Posts

    Re: filter in ModelAdmin for DATE-Field Link to this post

    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() == '';
       }
    }
    ?>

    1400 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.