Advertisement
eventsmanager

Prevent Creating Overlapping Events at a Location

May 3rd, 2018
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Events Manager - Time Clash
  4. Version: 1.0
  5. Plugin URI: http://wp-events-plugin.com
  6. Description: Checks if a location already has an event during these times, preventing double-bookings of locations.
  7. Author: Marcus Sykes
  8. Author URI: http://wp-events-plugin.com
  9. */
  10.  
  11. /*
  12. Copyright (c) 2012, Marcus Sykes
  13.  
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. as published by the Free Software Foundation; either version 2
  17. of the License, or (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  27. */
  28.  
  29. /**
  30.  * @param boolean $result
  31.  * @param EM_Event $EM_Event
  32.  */
  33. function my_em_event_validate_time_clash( $result, $EM_Event ){
  34.     if( $result && !$EM_Event->is_recurring() && !$EM_Event->is_recurrence() ){
  35.         if( $EM_Event->location_id > 0 ){ //location defined, let's check events at that location
  36.             $events = EM_Events::get( array('location'=>$EM_Event->location_id, 'scope'=> array($EM_Event->event_start_date,$EM_Event->event_end_date)) );
  37.             if( count($events) > 0 ){
  38.                 //check each event for any one that has start/end times within the ones in this new event
  39.                 foreach($events as $event){
  40.                     /* @var $event EM_Event */
  41.                     if( $event->event_id != $EM_Event->event_id ){ //we can't double-book the same event
  42.                         if(
  43.                             ($event->start >= $EM_Event->start && $event->start <= $EM_Event->end) ||
  44.                             ($event->end >= $EM_Event->start && $event->end <= $EM_Event->end) ||
  45.                             ($event->start <= $EM_Event->end && $event->end >= $EM_Event->start) ||
  46.                             (($event->event_start_date == $EM_Event->event_start_date || $event->event_end_date >= $EM_Event->event_end_date) && ($EM_Event->event_all_day || $event->event_all_day))
  47.                         ){
  48.                             //we have a clash!
  49.                             $result = false;
  50.                             $EM_Event->add_error('Your event clashes with another event at this location - '.$event->output('#_EVENTNAME - #_EVENTDATES @ #_EVENTTIMES'));
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     return $result;
  58. }
  59. add_filter('em_event_validate_meta', 'my_em_event_validate_time_clash', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement