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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Event Calendar - Filtering based on Boolean Checkboxes


Go to End


2 Posts   2242 Views

Avatar
entercow

Community Member, 13 Posts

19 August 2009 at 10:49pm

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!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

20 August 2009 at 1:36am

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.