Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WP-BookIt is a WordPress plugin I developed for our internal use at the GAME-dept.
- 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:
- - LDAP-authentication
- - iCalendar / Google Calendar integration
- - SMS & email notifications
- - realtime status updates
- - dependencies (like; reserving room for meetings, deny if none available)
- The plugin saw heavy use during a couple of years, but the code is my usual style
- of horrible so I never released the plugin publicly. This function is fairly
- representative and - to quote a famous developer - "it's the devils testicles". :)
- */
- //returns associative array of dates[yyyy-mm-dd] => reason code
- function wpbkt_get_fully_booked_dates($asset_id){
- $last_updated = wpbkt_get_asset_meta('cached_in_rev', $asset_id);
- if($last_updated == get_option('wpbkt_revision')){
- $cache = wpbkt_get_asset_meta('fully_booked_dates', $asset_id);
- return maybe_unserialize($cache);
- }
- global $wpdb;
- /*first, let's get all bookings that overlap the timespan*/
- $from = date('Y-m-d', strtotime(get_option('wpbkt_calendar_start'))); //H:i:s
- $to = date('Y-m-d', strtotime(get_option('wpbkt_calendar_end')));
- $full_dates = array();
- $table = WPBKT_TABLE;
- $wpdb->show_errors();
- $reservations = $wpdb->get_results("SELECT * FROM $table
- WHERE $table.asset_id = $asset_id
- AND ($table.status = ".WPBKT_PENDING." OR $table.status = ".WPBKT_CONFIRMED." OR $table.status = ".WPBKT_AWAITING_RETURN.")
- AND NOT( ($table.to < CAST('$from' as DATETIME))
- OR ($table.from > CAST('$to' as DATETIME)))
- ORDER BY $table.from");
- /*now go through all reservations, and count the number of overlapping reservations per date*/
- $bookings_per_day = array();
- foreach($reservations as $reservation){
- $start = strtotime( date('Y-m-d', strtotime($reservation->from)) ); //throw away h:m:s
- $end = strtotime( date('Y-m-d', strtotime($reservation->to)) );
- for($i = $start; $i < $end; $i += WPBKT_A_DAY){
- $date = date("Y-m-d", $i);
- $bookings_per_day[$date] = !isset($bookings_per_day[$date]) ? 1 : $bookings_per_day[$date]+1;
- }
- }
- /*compare those numbers to our stock (or available timeslots)*/
- $meta = wpbkt_get_asset_meta('all', $asset_id);
- if($meta['by_daterange']){
- $total_stock = $meta['total_stock'];
- foreach($bookings_per_day as $date => $reservation_count){
- if($reservation_count >= $total_stock){
- $full_dates[$date] = WPBKT_OUT_OF_STOCK;
- }
- }
- }else{//else, timeslots
- foreach($bookings_per_day as $date => $reservation_count){
- $day = strtolower(date('D', strtotime($date)));
- $slots = explode(' ', $meta["$day_timeslots"]);
- if($reservation_count >= count($slots)){
- $full_dates[$date] = WPBKT_OUT_OF_STOCK;
- }
- }
- }
- /*next, lets find all invalid weekdays for the entire datespan */
- $start = strtotime( $from ); //throw away h:m:s
- $end = strtotime( $to );
- if($end < $start){ wp_die('get_fully_booked: invalid dates'); }
- $valid_days = $meta['weekdays'];
- for($i = $start; $i < $end; $i += WPBKT_A_DAY){
- $date = date("Y-m-d", $i);
- $day = date('D', strtotime($date));
- if(stripos($valid_days, $day) === false){
- $full_dates[$date] = WPBKT_BAD_DAY; //this day is unavailable
- }
- }
- /* now to find all days where *all* requirements are fully booked too */
- $required_resource = wpbkt_get_asset_meta('requires_resources', $asset_id);
- if($required_resource){
- $cats = wpbkt_get_subcategories_id($required_resource); //category id might be 'room' and you've got 'classrooms', 'office' etc. as subcats.
- $potential_assets = wpbkt_get_all_assets_ids($cats);//these are all assets that might satisfy out requirements
- $full = array();
- foreach($potential_assets as $resource_id){
- $fulldays = wpbkt_get_fully_booked_dates_between($resource_id, $from, $to);
- foreach($fulldays as $date){
- $full[$date] = !isset($full[$date]) ? 1 : $full[$date]+1; //count how many of these assets are fully booked on this date
- }
- }
- $max = count($potential_assets); //we have $max potential assets. if any date was fully booked more than max times, it's definetly unavailable.
- foreach($full as $date => $count){
- if($count >= $max){
- $full_dates[$date] = WPBKT_NO_RESOURCES; //ALL potential required resources are unavailable this date.
- }
- }
- }
- update_post_meta($asset_id, 'fully_booked_dates', $full_dates);
- update_post_meta($asset_id, 'cached_in_rev', get_option('wpbkt_revision'));
- return $full_dates;
- }
Advertisement
Add Comment
Please, Sign In to add comment