Jump to:

10845 Posts in 2513 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » Event Calendar - Filtering based on Boolean Checkboxes

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1402 Views
  • entercow
    Avatar
    Community Member
    13 Posts

    Event Calendar - Filtering based on Boolean Checkboxes Link to this post

    This isn't a question - just some useful information in case others need it.
    ------------------------------------------

    I have my Calendar Events categories setup as checkboxes rather than a dropdown, so each event can be assigned to more than one category.

    So when I wanted to add the filter to the calendar I had to add each of the checkboxes to the filterFields list like so:

    $this->addFilterField(
       new CheckboxField('CalendarEvent_AcademicCalendar', 'Academic Calendar Events')
    );

    ... etc.

    No problems there. However, when filtering, the filter string gets built like: WHERE "AcademicCalendar = 'on'", but boolean values aren't stored as 'on' or 'off' in the db, they are stored as '1' and '0'. So a quick fix in the getFiltersForID() function was needed:

    //Line below added by Me
    if(strcmp($value,'on')==0){ $value = 1;}
    //Line below is for place reference
    $for_db[] = "$db_field = '$value'";

    The other problem I found was that when I filtered by selecting multiple checkboxes the filter string gets built like: WHERE "AcademicCalendar = '1'" AND "LibraryEvents = '1'" which is exclusive rather than inclusive. This called for another quick fix in the Events() function:

    //Just replace " AND " with " OR "
    $filter = (sizeof($filter_list > 1)) ? implode(" AND ", $filter_list) : $filter_list;

    ----------------------------------

    I will admit that this isn't the slickest way of doing this - but it is quick, easy, and works.

    Cheers!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Event Calendar - Filtering based on Boolean Checkboxes Link to this post

    This is a great fix:

    //Line below added by Me
    if(strcmp($value,'on')==0){ $value = 1;}
    //Line below is for place reference
    $for_db[] = "$db_field = '$value'";

    I had forgotten that checkboxes post as "on". Nice catch!

    Not sure about doing inclusive filtering. I think this only matters when using checkboxes, right? You're saying that if I check off one category, the one that is unchecked doesn't necessarily have to be false in the result set. It can be either one.

    Yeah, the filter definitely needs a lot of work. It's not really relation-aware right now, either. And it requires a start/end date. Lots to improve on there.

    1402 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.