$num_regis = 0;
 $counts_day_key = $event_id . "_time_{$date_id}";
 $counts_time_key = $event_id . "_time_{$date_id}_{$time_id}";
 if (isset($counts['_total_att_' . $counts_time_key])) {
     $num_regis = epl_get_element('_total_att_' . $counts_time_key, $counts, 0);
 }
 $money_total = epl_get_formatted_curr(epl_get_element('_money_total_' . $counts_time_key, $money_totals, 0.0));
 $avail -= $num_regis;
 if ($avail <= 0) {
     $open_spots = "<span class='spots_closed'>Sold Out</span>";
 } else {
     $open_spots = "<span class='spots_open'>" . $avail . " Spots</span>";
 }
 if ($avail <= 0) {
     $register_button_text = "<span class='button_closed'>Sold<strong>Out</strong></span>";
     if (epl_is_ok_for_waitlist() && ($wl_spaces_left = epl_waitlist_spaces_open()) !== false) {
         $register_button_url = array('_date_id' => $date_id, 'button_text' => "Waiting List", 'class' => 'epl_button button_waitlist');
     }
 }
 $table_link_arr = array('epl_action' => 'view_names', 'epl_download_trigger' => 1, 'table_view' => 1, 'epl_controller' => 'epl_report_manager', 'event_id' => $event_id);
 $dt_array = array('date_id' => $date_id, 'time_id' => $time_id, 'event_id' => $event_id);
 $table_link_arr = array_merge($table_link_arr, $dt_array);
 $load_att_link = add_query_arg(array_merge($table_link_arr, $dt_array) + array('names_only' => 1), $base_url);
 $load_att_name_button = '<a href="' . $load_att_link . '" class="btn load_attendees list_action_button" title="Attendees" data-label="Att" href="#">Att</a>';
 $table_link_arr[''] = 'view_names';
 $load_att_link = add_query_arg(array_merge($table_link_arr, $dt_array), $base_url);
 $load_att_full_data_button = '<a href="' . $load_att_link . '" class="btn load_attendees list_action_button" title="Full Data" data-label="Full" href="#">Full</a>';
 $load_att_link = add_query_arg(array_merge($table_link_arr, $dt_array) + array('combined' => 1), $base_url);
 $load_att_comb_data_button = '<a href="' . $load_att_link . '" class="btn load_attendees list_action_button" title="Combined" data-label="Comb." href="#">Comb.</a>';
 $table_link_arr['epl_action'] = 'get_the_email_form';
 $load_att_link = add_query_arg(array_merge($table_link_arr, $dt_array) + array('names_only' => 1), $base_url);
 /**
  * This function will create a global variable called $event_snapshot, which holds all the following information about the event
  * -Availability for each date, each time inside each date, each price inside each date and time.
  * -Availability errors
  *
  * Uses global vars $event_details, $current_att_count
  *
  * @since 1.0.0
  * @param event_id
  * @param refresh
  * @return  Sets the global $event_snapshot variable
  */
 function event_snapshot($event_id = null, $refresh = false)
 {
     global $event_details, $capacity, $current_att_count, $event_snapshot, $epl_error, $event_totals;
     $event_id = is_null($event_id) ? $this->get_current_event_id() : $event_id;
     setup_event_details($event_id);
     $meta = $this->current_data[$this->regis_id];
     $cart_selected_dates = epl_get_element($event_id, epl_get_element('_epl_start_date', $this->get_cart_values('_dates')));
     $cart_selected_times = (array) epl_get_element($event_id, epl_get_element('_epl_start_time', $this->get_cart_values('_dates')));
     $cart_selected_quantities = (array) epl_get_element($event_id, epl_get_element('_att_quantity', $this->get_cart_values('_dates')));
     static $_cache = array();
     $_is_cached = epl_get_element($event_id, $_cache);
     if ($_is_cached) {
         return $_cache[$event_id];
     }
     if (empty($cart_selected_dates) && epl_get_element('cart_action', $_REQUEST) != 'add') {
         $epl_error[] = array('', epl__('Please select at least one date.'));
     }
     $current_att_count = EPL_report_model::get_instance()->get_attendee_counts($event_id, true);
     $sold_out_text = apply_filters('merm__event_snapshot__sold_out_text', epl__('Sold Out.'));
     //get the attendee and money totals
     //$_totals = $this->calculate_cart_totals();
     setup_event_details($event_id);
     $grand_total = epl_get_element_m('grand_total', 'money_totals', $event_totals);
     $grand_total_key = "_grand_total";
     //this will hold the snapshot
     $event_snapshot = array();
     $qty_meta_key = "_total_att_" . $event_id;
     //$total_att = array_sum( ( array ) $meta[$this->regis_id]['_dates']['_att_quantity'][$event_details['ID']] );
     $total_att = epl_get_element_m($event_id, 'total', epl_get_element('_att_quantity', $event_totals));
     //event dates, times and prices
     $dates = epl_get_element('_epl_start_date', $event_details);
     $times = epl_get_element('_epl_start_time', $event_details);
     $prices = epl_get_element('_epl_price_name', $event_details);
     $rolling_regis = epl_get_element('_epl_rolling_regis', $event_details) == 10;
     if (epl_is_empty_array($dates)) {
         return;
     }
     //foreach event date
     foreach ($dates as $_date_key => $date_timestamp) {
         $date_timestamp = epl_get_date_timestamp($date_timestamp);
         //number registered for the date
         $date_total_att = 0;
         $_date = epl_formatted_date($event_details['_epl_start_date'][$_date_key], 'Y-m-d', 'date');
         //the date to display
         $_displ_date = epl_formatted_date($_date);
         $qty_meta_key = "_total_att_" . $event_details['ID'] . '_date_' . $_date_key;
         //find the capacity for this date.
         $cap = $event_details['_epl_date_capacity'][$_date_key];
         //find the number of people regitered for this date
         $num_att = epl_get_element($qty_meta_key, $current_att_count, 0);
         //find the available spcaes.  If there is no capacity, always available
         $date_avail = $this->avail_spaces($cap, $num_att);
         $_past = epl_compare_dates(EPL_TIME, $_date . ' 23:59:59', ">");
         $_date_avail_display = epl_is_ok_to_register($event_details, $_date_key);
         $_date_avail_display = $_date_avail_display === true ? epl__('Available') : $_date_avail_display;
         //snapshot template
         $_t = array('timestamp' => $date_timestamp, 'disp' => $_displ_date, 'avail' => $date_avail, 'avail_display' => $_date_avail_display, 'regis' => $num_att, 'db_key' => $qty_meta_key, 'cart' => 0, 'past' => $_past, 'hide' => $date_avail != '' && $date_avail <= 0);
         //Set the snapshot for this date
         $event_snapshot[$event_id][$_date_key]['date'] = $_t;
         $rolling_regis_time_avail = 0;
         //foreach time available for the event
         foreach ($times as $_time_key => $_time_id) {
             $time_total_att = 0;
             $_time = $event_details['_epl_start_time'][$_time_key];
             $qty_meta_key = "_total_att_" . $event_details['ID'] . '_time_' . $_date_key . '_' . $_time_key;
             //$cap = $capacity['time'][$_time_key];
             $cap = epl_get_element($_time_key, epl_get_element('_epl_time_capacity', $event_details), '');
             if ($rolling_regis && $cap == '') {
                 $cap = epl_get_element_m($_date_key, '_epl_date_per_time_capacity', $event_details);
             }
             $num_att = epl_nz(epl_get_element($qty_meta_key, $current_att_count), 0);
             $time_avail = $this->avail_spaces($cap, $num_att);
             $rolling_regis_time_avail += $cap == '' ? 999 : epl_nz($time_avail, 0);
             $_comp_time = epl_get_element($_time_key, $event_details['_epl_regis_endtime']);
             $_comp_time = !$_comp_time ? $_time : $_comp_time;
             //Is this time for this date in the past and not available any more?
             $_past = epl_compare_dates(EPL_TIME, $_date . ' ' . $_comp_time, ">");
             if ($rolling_regis && $_past) {
                 $event_snapshot[$event_id][$_date_key]['date']['hide'] = true;
             }
             $_t = array('timestamp' => strtotime($times[$_time_key], $date_timestamp), 'disp' => $times[$_time_key], 'avail' => $time_avail, 'avail_display' => $_date_avail_display, 'regis' => $num_att, 'db_key' => $qty_meta_key, 'past' => $_past);
             //Set the snapsot for this time for this date
             $event_snapshot[$event_id][$_date_key]['time'][$_time_key] = $_t;
             foreach ($prices as $_price_key => $_price_id) {
                 $_price = $event_details['_epl_price_name'][$_price_key];
                 $price_avail = 0;
                 $do_count = true;
                 $price_att = 0;
                 $price_type = epl_get_element($_price_key, epl_get_element('_epl_price_type', $event_details), 'att');
                 if (isset($meta['_dates']['_att_quantity'][$event_details['ID']])) {
                     if (is_array(epl_get_element($_price_key, $meta['_dates']['_att_quantity'][$event_details['ID']]))) {
                         $price_att = array_sum((array) epl_get_element($_price_key, $meta['_dates']['_att_quantity'][$event_details['ID']]));
                     } else {
                         $price_att = epl_get_element($_price_key, $meta['_dates']['_att_quantity'][$event_details['ID']]);
                     }
                 }
                 $qty_meta_key = "_total_att_" . $event_details['ID'] . '_price_' . $_date_key . '_' . $_time_key . '_' . $_price_key;
                 if (epl_is_date_level_price()) {
                     $price_att = epl_get_element_m($_date_key, $_price_key, $cart_selected_quantities);
                 }
                 $cap = epl_get_element($_price_key, epl_get_element('_epl_price_capacity', $event_details), '');
                 $num_att = epl_nz(epl_get_element($qty_meta_key, $current_att_count), 0);
                 $price_avail = $this->avail_spaces($cap, $num_att);
                 if (!epl_is_empty_array($offset = $this->is_offsetter_price($_price_key)) && $cap !== '' && $price_avail > 0) {
                     //check to make sure users are not using the price as offset against itself
                     if ($offset['offset_key'] != $_price_key) {
                         // see if available count of the offseter is > capacity for this price
                         $offset_avail = $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$offset['offset_key']]['avail'];
                         $offset_count = $offset['offset_count'];
                         if ((int) $offset_count > (int) $offset_avail) {
                             $price_avail = 0;
                         } elseif ($offset_avail >= $offset_count) {
                             $price_avail = intval($offset_avail / $offset_count);
                         }
                     }
                 }
                 //echo "<pre class='prettyprint'>" . __LINE__ . "> $event_id >>>> " . print_r($price_avail, true). "</pre>";
                 $time_total_att += $price_att;
                 $_t = array('disp' => $prices[$_price_key], 'avail' => $price_avail, 'avail_display' => $_date_avail_display, 'regis' => $num_att, 'db_key' => $qty_meta_key, 'cart' => $price_att, 'past' => $_past);
                 $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$_price_key] = $_t;
                 if (epl_is_date_level_time() && epl_get_element($_date_key, $cart_selected_times) != $_time_key) {
                     $do_count = false;
                 }
                 if (epl_is_date_level_price() && !in_array($_date_key, (array) $cart_selected_dates)) {
                     //if ( $_date_key != $_price_key ) {
                     $do_count = false;
                     //}
                 }
                 if (epl_is_time_specific_price($_price_key)) {
                     if ($event_details['_epl_price_parent_time_id'][$_price_key] != $_time_key) {
                         unset($event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$_price_key]);
                         $do_count = false;
                     }
                 }
                 if (!epl_is_time_optonal() && !in_array($_time_key, $cart_selected_times) || !in_array($_date_key, (array) $cart_selected_dates)) {
                     $do_count = false;
                 }
                 if (!$do_count || $price_type != 'att') {
                     $time_total_att -= $price_att;
                     if (isset($event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$_price_key]['cart'])) {
                         $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$_price_key]['cart'] = 0;
                     }
                 } else {
                     //check for a price availability for each date/time
                     if ($price_att > 0 && $price_avail !== '') {
                         $_error = array();
                         if ($price_avail === 0 || $price_avail < 0) {
                             $_error = array($_displ_date . '<br />' . $_time . '<br />' . $_price, $sold_out_text);
                         } elseif ($price_att > epl_nz($price_avail, 1000)) {
                             $_error = array($_displ_date . '<br />' . $_time . '<br />' . $_price, sprintf(epl__(' Only %d spaces left.'), $price_avail));
                         }
                         if (!empty($_error)) {
                             $epl_error[] = $_error;
                             $event_snapshot[$event_id]['error'][] = $_error;
                         }
                         $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['price'][$_price_key]['avail_display'] = epl_get_element(1, $_error);
                     }
                 }
                 /* if( $price_type != 'att' )
                    $time_total_att -= $price_att; */
             }
             $date_total_att += $time_total_att;
             $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['cart'] = $time_total_att;
             //echo "<pre class='prettyprint'>" . __LINE__ . "> $_date "  . print_r($time_total_att, true). "</pre>";
             //check for time availablility for each date
             if ($this->flow_mode == 'n' && !epl_is_time_optonal() && $time_total_att > 0 && ($time_avail !== '' || $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['past'] === true)) {
                 $_error = array();
                 if (!epl_is_ongoing_event() && $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['past'] == 1) {
                     $_error = array($_displ_date . '<br />' . $_time, epl__('This time has passed.'));
                 }
                 if ($time_avail === 0 || $time_avail < 0) {
                     $_error = array($_displ_date . '<br />' . $_time, $sold_out_text);
                 } elseif ($time_total_att > epl_nz($time_avail, 1000)) {
                     $_error = array($_displ_date . '<br />' . $_time, sprintf(epl__('Only %d spaces left.'), $time_avail));
                 }
                 if (!empty($_error)) {
                     $epl_error[] = $_error;
                     $event_snapshot[$event_id]['error'][] = $_error;
                 }
                 $event_snapshot[$event_id][$_date_key]['time'][$_time_key]['avail_display'] = epl_get_element(1, $_error);
             }
         }
         if ($rolling_regis && $rolling_regis_time_avail == 0 && $event_snapshot[$event_id][$_date_key]['date'] == '') {
             $event_snapshot[$event_id][$_date_key]['date']['hide'] = true;
         }
         //check for the date availability
         $event_snapshot[$event_id][$_date_key]['date']['cart'] = $date_total_att;
         if ($date_total_att > 0 && ($date_avail !== '' || $event_snapshot[$event_id][$_date_key]['date']['past'] === true) && (!epl_is_waitlist_approved() && !epl_is_waitlist_session_approved())) {
             $_error = array();
             if ($this->flow_mode == 'n' && ($date_avail === 0 || $date_avail < 0)) {
                 $_error = array($_displ_date, $sold_out_text);
                 if (epl_is_ok_for_waitlist() && ($wl_spaces_left = epl_waitlist_spaces_open()) !== false) {
                     $_error[1] .= '<br />' . epl__('If you would like to be added to the waiting list, please click on the button below.  You will not be charged at this time.');
                     if ($wl_spaces_left !== true) {
                         $_error[1] .= '<br />' . sprintf('Spaces available on the waiting list: %d', $wl_spaces_left);
                     }
                     if (epl_waitlist_enough_spaces($event_id) == false) {
                         $_error[1] .= '<br />' . sprintf('To continue, please select only %d.', $wl_spaces_left);
                     } else {
                         $_error[1] .= '<br />' . epl_anchor('#', epl__('Click here to add your name to the waitlist'), null, " class='open_add_to_waitlist_form epl_button' data-event_id='{$event_id}'");
                     }
                     $this->epl->epl_util->set_response_param('waitlist_form', '');
                 }
             } elseif ($this->flow_mode == 'n' && !epl_is_ongoing_event() && $event_snapshot[$event_id][$_date_key]['date']['past'] == 1) {
                 $_error = array($_displ_date, epl__('This date has passed.'));
             } elseif ($this->flow_mode == 'n' && $date_total_att > epl_nz($date_avail, 1000)) {
                 $_error = array($_displ_date, sprintf(epl__('Only %d spaces left.'), $date_avail));
             }
             $event_snapshot[$event_id][$_date_key]['date']['avail_display'] = epl_get_element(1, $_error);
             if (!empty($_error)) {
                 $event_snapshot[$event_id]['error'][] = $_error;
                 $epl_error[] = $_error;
             }
         }
         //$this->epl_table->add_row( '', $event_details['_epl_start_date'][$_date_key], $avail );
     }
     $_cache[$event_id] = $event_snapshot;
     return $event_snapshot;
 }
 function regis_form()
 {
     global $epl_error, $event_details, $epl_is_waitlist_flow;
     if ($this->erm->is_empty_cart()) {
         //return $this->epl_util->epl_invoke_error( 20, null, false );
     }
     //$this->ecm->setup_event_details( intval( $_REQUEST['event_id'] ) );
     $_ok = $this->erm->ok_to_proceed();
     if ($_ok !== '') {
         if (!epl_is_waitlist_flow() && (epl_is_ok_for_waitlist() && epl_waitlist_enough_spaces())) {
             epl_waitlist_flow_trigger();
         } else {
             return $_ok;
         }
     }
     $this->erm->_process_session_cart();
     //$this->erm->_set_relevant_data(null, null, true);
     $data['content'] = '';
     $this->regis_flow = epl_regis_flow();
     // if ( $this->regis_flow <= 2 && $_POST ) {
     /* if ( !epl_has_all_req_user_fields() ) {
        $event_id = intval( epl_get_element( 'event_id', $_REQUEST ) );
        $url = get_the_register_button( $event_id, true );
        wp_redirect( $url );
        die();
        } */
     //from the shopping cart
     //}
     $data['mode'] = 'edit';
     if ($this->regis_flow == 2 || $this->regis_flow == 10) {
         if ($this->regis_flow == 2) {
             $data['mode'] = 'overview';
         }
         $this->erm->set_mode($data['mode']);
         $data['cart_data'] = $this->erm->show_cart();
         $data['content'] = $this->epl->load_view('front/cart/cart', $data, true);
     }
     $data = array_merge($data, $this->set_next_step_vars('regis_form'));
     $this->erm->set_mode('edit');
     $data['mode'] = 'edit';
     $data['content'] .= $this->erm->regis_form();
     /* if ( empty( $epl_error ) )
         $data['next_step_label'] = apply_filters( 'epl_regis_form_next_step_label', epl__( 'Next' ) );
        */
     return $this->epl->load_view('front/cart-container', $data, true);
 }