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.

All other Modules

Discuss all other Modules here.

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

Event Calendar Module: Filtering based on a Many_Many Rship


Reply

4 Posts   1178 Views

Avatar
dizzystuff

12 April 2010 at 3:00am Community Member, 93 Posts

Hey Guys

I have extended the Event Calendar in similar ways to the recipe in the docs [url]http://doc.silverstripe.org/doku.php?id=recipes:extending_the_event_calendar[/url] but I'm having an issue adding an extra Filter Dropdown field (under the Calendar Widget) and getting it to work.

Basically, in the context of the tutorial/recipe I linked to, I want to be able to filter by Instructor.

That is, I have a many_many relationship between CalendarDateTime.php and Instructor.php and I would like to add a drop down field of Instructors under the existing From and To Calendar Filter dropdowns.

I got to here:

   public function getFilterFields() {
      $fields = parent::getFilterFields(); // returns a CalendarFilterFieldSet
      $instructors = DataObject::get('Instructor');
      $map = $instructors ? $instructors->toDropdownMap('ID','Name') : array();
      $fields->addFilterField(new DropdownField('CalendarDateTime_Instructors','Instructor', $map));
      return $fields;
   }

but as I expected it's looking in the db for an "Instructors" column in the CalendarDateTime table (which it doesn't find and so errors) rather than matching it up via a joint CalendarDateTime_Instructors table.

Is what I'm trying to do possible?

Avatar
UncleCheese

15 April 2010 at 1:50am 4085 Posts

Sorry, it's a one-to-one filter at the moment. If you want to try to modify the filter functionality to support many_many or has_many, that would be awesome. I imagine it's a lot of work, especially to do it cleanly.

Ultimately, I think we just need to have the ability to hand off the filtering to a custom callback function.

Avatar
dizzystuff

16 April 2010 at 2:28am Community Member, 93 Posts

Yeah fair enough. I think might be a little beyond me, time-wise but also mad-skillz-wise atm lol. If I do end up tweaking I'll post what I come up with here. Any starting points/suggestions?

Avatar
Gene

21 September 2010 at 6:46pm Community Member, 41 Posts

It's a little sloppy but you can get a many_many join going by overriding 3 Calendar methods: Events, getEventJoin, and getDateJoin

Here's a quick example...

   public function getEventJoin() {
      $join = parent::getEventJoin();
      
      if (isset($_GET['center']) && is_numeric($_GET['center']) && (int)$_GET['center'] > 0) {
         $center = (int)$_GET['center'];
         $join .= " LEFT JOIN `EventDateTime_Centers` ON `EventDateTime_Centers`.`EventDateTimeID` = `EventDateTime`.`ID`";
      }
      
      return $join;
   }
   
   public function getDateJoin() {
      $join = parent::getDateJoin();
      
      if (isset($_GET['center']) && is_numeric($_GET['center']) && (int)$_GET['center'] > 0) {
         $center = (int)$_GET['center'];
         $join .= " LEFT JOIN `EventDateTime_Centers` ON `EventDateTime_Centers`.`EventDateTimeID` = `EventDateTime`.`ID`";
      }
      
      return $join;
   }

   public function Events($filter = null, $start_date = null, $end_date = null, $default_view = false, $limit = null, $announcement_filter = null) {
      if (isset($_GET['center']) && is_numeric($_GET['center']) && (int)$_GET['center'] > 0) {
         $center = (int)$_GET['center'];

         if ($filter) {
            $filter .= ' AND ';
         }
         $filter .= "`EventDateTime_Centers`.`CenterPageID` = '$center'";
      }
      
      return parent::Events($filter, $start_date, $end_date, $default_view, $limit, $announcement_filter);
   }