Jump to:

10842 Posts in 2512 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » Event Calendar Module: Filtering based on a Many_Many Rship

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1106 Views
  • dizzystuff
    Avatar
    Community Member
    93 Posts

    Event Calendar Module: Filtering based on a Many_Many Rship Link to this post

    Hey Guys

    I have extended the Event Calendar in similar ways to the recipe in the docs http://doc.silverstripe.org/doku.php?id=recipes:extending_the_event_calendar 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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Event Calendar Module: Filtering based on a Many_Many Rship Link to this post

    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.

  • dizzystuff
    Avatar
    Community Member
    93 Posts

    Re: Event Calendar Module: Filtering based on a Many_Many Rship Link to this post

    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?

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Event Calendar Module: Filtering based on a Many_Many Rship Link to this post

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

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