SHARE
TWEET

WP-BookIt sample

ulfben Dec 3rd, 2011 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. WP-BookIt is a WordPress plugin I developed for our internal use at the GAME-dept.
  3.  
  4. It provides a shortcode making any post an "asset" (staff, tech, rooms etc) that could be reserved directly from the site. It was fairly nifty, providing:
  5.  
  6. - LDAP-authentication
  7. - iCalendar / Google Calendar integration
  8. - SMS & email notifications
  9. - realtime status updates
  10. - dependencies (like; reserving room for meetings, deny if none available)
  11.  
  12. The plugin saw heavy use during a couple of years, but the code is my usual style
  13. of horrible so I never released the plugin publicly. This function is fairly
  14. representative and - to quote a famous developer - "it's the devils testicles". :)
  15. */
  16.  
  17. //returns associative array of dates[yyyy-mm-dd] => reason code
  18. function wpbkt_get_fully_booked_dates($asset_id){      
  19.         $last_updated = wpbkt_get_asset_meta('cached_in_rev', $asset_id);
  20.         if($last_updated == get_option('wpbkt_revision')){
  21.                 $cache = wpbkt_get_asset_meta('fully_booked_dates', $asset_id);                
  22.                 return maybe_unserialize($cache);
  23.         }      
  24.         global $wpdb;
  25.         /*first, let's get all bookings that overlap the timespan*/
  26.         $from = date('Y-m-d', strtotime(get_option('wpbkt_calendar_start'))); //H:i:s
  27.         $to = date('Y-m-d', strtotime(get_option('wpbkt_calendar_end')));
  28.         $full_dates = array();
  29.         $table = WPBKT_TABLE;  
  30.         $wpdb->show_errors();                          
  31.         $reservations = $wpdb->get_results("SELECT * FROM $table                       
  32.                         WHERE $table.asset_id = $asset_id
  33.                         AND ($table.status = ".WPBKT_PENDING." OR $table.status = ".WPBKT_CONFIRMED." OR $table.status = ".WPBKT_AWAITING_RETURN.")
  34.                         AND NOT(        ($table.to < CAST('$from' as DATETIME))
  35.                                                 OR      ($table.from > CAST('$to' as DATETIME)))
  36.                         ORDER BY $table.from");        
  37.                        
  38.         /*now go through all reservations, and count the number of overlapping reservations per date*/
  39.         $bookings_per_day = array();                                   
  40.         foreach($reservations as $reservation){
  41.                 $start = strtotime( date('Y-m-d', strtotime($reservation->from)) ); //throw away h:m:s
  42.                 $end = strtotime( date('Y-m-d', strtotime($reservation->to)) );                        
  43.                 for($i = $start; $i < $end; $i += WPBKT_A_DAY){        
  44.                         $date = date("Y-m-d", $i);                     
  45.                         $bookings_per_day[$date] = !isset($bookings_per_day[$date]) ? 1 : $bookings_per_day[$date]+1;                                                                  
  46.                 }      
  47.         }
  48.  
  49.         /*compare those numbers to our stock (or available timeslots)*/
  50.         $meta = wpbkt_get_asset_meta('all', $asset_id);
  51.         if($meta['by_daterange']){
  52.                 $total_stock = $meta['total_stock'];
  53.                 foreach($bookings_per_day as $date => $reservation_count){
  54.                         if($reservation_count >= $total_stock){
  55.                                 $full_dates[$date] = WPBKT_OUT_OF_STOCK;
  56.                         }                      
  57.                 }      
  58.         }else{//else, timeslots
  59.                 foreach($bookings_per_day as $date => $reservation_count){                     
  60.                         $day = strtolower(date('D', strtotime($date)));
  61.                         $slots = explode(' ', $meta["$day_timeslots"]);
  62.                         if($reservation_count >= count($slots)){
  63.                                 $full_dates[$date] = WPBKT_OUT_OF_STOCK;
  64.                         }
  65.                 }
  66.         }              
  67.        
  68.         /*next, lets find all invalid weekdays for the entire datespan */
  69.         $start = strtotime( $from ); //throw away h:m:s
  70.         $end = strtotime( $to );       
  71.         if($end < $start){ wp_die('get_fully_booked: invalid dates'); }
  72.         $valid_days = $meta['weekdays'];
  73.         for($i = $start; $i < $end; $i += WPBKT_A_DAY){
  74.                 $date = date("Y-m-d", $i);
  75.                 $day = date('D', strtotime($date));
  76.                 if(stripos($valid_days, $day) === false){
  77.                         $full_dates[$date] = WPBKT_BAD_DAY; //this day is unavailable
  78.                 }                      
  79.         }              
  80.        
  81.         /* now to find all days where *all* requirements are fully booked too */       
  82.         $required_resource = wpbkt_get_asset_meta('requires_resources', $asset_id);    
  83.         if($required_resource){        
  84.                 $cats = wpbkt_get_subcategories_id($required_resource); //category id might be 'room' and you've got 'classrooms', 'office' etc. as subcats.
  85.                 $potential_assets = wpbkt_get_all_assets_ids($cats);//these are all assets that might satisfy out requirements                         
  86.                 $full = array();
  87.                 foreach($potential_assets as $resource_id){
  88.                         $fulldays = wpbkt_get_fully_booked_dates_between($resource_id, $from, $to);
  89.                         foreach($fulldays as $date){
  90.                                 $full[$date] = !isset($full[$date]) ? 1 : $full[$date]+1; //count how many of these assets are fully booked on this date
  91.                         }                              
  92.                 }
  93.                 $max = count($potential_assets); //we have $max potential assets. if any date was fully booked more than max times, it's definetly unavailable.
  94.                 foreach($full as $date => $count){
  95.                         if($count >= $max){
  96.                                 $full_dates[$date] = WPBKT_NO_RESOURCES; //ALL potential required resources are unavailable this date.  
  97.                         }                      
  98.                 }
  99.         }                              
  100.         update_post_meta($asset_id, 'fully_booked_dates', $full_dates);
  101.         update_post_meta($asset_id, 'cached_in_rev', get_option('wpbkt_revision'));                    
  102.         return $full_dates;
  103. }
  104.  
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top