/**
  * Adds a relationship to Term_Taxonomy for each CPT_Base
  * @param string $timezone
  * @return EEM_Event
  */
 protected function __construct($timezone = null)
 {
     EE_Registry::instance()->load_model('Registration');
     $this->singular_item = __('Event', 'event_espresso');
     $this->plural_item = __('Events', 'event_espresso');
     // to remove Cancelled events from the frontend, copy the following filter to your functions.php file
     // add_filter( 'AFEE__EEM_Event__construct___custom_stati__cancelled__Public', '__return_false' );
     // to remove Postponed events from the frontend, copy the following filter to your functions.php file
     // add_filter( 'AFEE__EEM_Event__construct___custom_stati__postponed__Public', '__return_false' );
     // to remove Sold Out events from the frontend, copy the following filter to your functions.php file
     //	add_filter( 'AFEE__EEM_Event__construct___custom_stati__sold_out__Public', '__return_false' );
     $this->_custom_stati = apply_filters('AFEE__EEM_Event__construct___custom_stati', array(EEM_Event::cancelled => array('label' => __('Cancelled', 'event_espresso'), 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__cancelled__Public', TRUE)), EEM_Event::postponed => array('label' => __('Postponed', 'event_espresso'), 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__postponed__Public', TRUE)), EEM_Event::sold_out => array('label' => __('Sold Out', 'event_espresso'), 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__sold_out__Public', TRUE))));
     $this->_tables = array('Event_CPT' => new EE_Primary_Table('posts', 'ID'), 'Event_Meta' => new EE_Secondary_Table('esp_event_meta', 'EVTM_ID', 'EVT_ID'));
     $this->_fields = array('Event_CPT' => array('EVT_ID' => new EE_Primary_Key_Int_Field('ID', __('Post ID for Event', 'event_espresso')), 'EVT_name' => new EE_Plain_Text_Field('post_title', __('Event Name', 'event_espresso'), FALSE, ''), 'EVT_desc' => new EE_Post_Content_Field('post_content', __('Event Description', 'event_espresso'), FALSE, ''), 'EVT_slug' => new EE_Slug_Field('post_name', __('Event Slug', 'event_espresso'), FALSE, ''), 'EVT_created' => new EE_Datetime_Field('post_date', __('Date/Time Event Created', 'event_espresso'), FALSE, current_time('timestamp')), 'EVT_short_desc' => new EE_Simple_HTML_Field('post_excerpt', __('Event Short Description', 'event_espresso'), FALSE, ''), 'EVT_modified' => new EE_Datetime_Field('post_modified', __('Date/Time Event Modified', 'event_espresso'), TRUE, current_time('timestamp')), 'EVT_wp_user' => new EE_Integer_Field('post_author', __('Wordpress User ID', 'event_espresso'), FALSE, 1), 'parent' => new EE_Integer_Field('post_parent', __('Event Parent ID', 'event_espresso'), TRUE), 'EVT_order' => new EE_Integer_Field('menu_order', __('Event Menu Order', 'event_espresso'), FALSE, 1), 'post_type' => new EE_WP_Post_Type_Field('espresso_events'), 'status' => new EE_WP_Post_Status_Field('post_status', __('Event Status', 'event_espresso'), FALSE, 'draft', $this->_custom_stati)), 'Event_Meta' => array('EVTM_ID' => new EE_DB_Only_Float_Field('EVTM_ID', __('Event Meta Row ID', 'event_espresso'), FALSE), 'EVT_ID_fk' => new EE_DB_Only_Int_Field('EVT_ID', __('Foreign key to Event ID from Event Meta table', 'event_espresso'), FALSE), 'EVT_display_desc' => new EE_Boolean_Field('EVT_display_desc', __('Display Description Flag', 'event_espresso'), FALSE, 1), 'EVT_display_ticket_selector' => new EE_Boolean_Field('EVT_display_ticket_selector', __('Display Ticket Selector Flag', 'event_espresso'), FALSE, 1), 'EVT_visible_on' => new EE_Datetime_Field('EVT_visible_on', __('Event Visible Date', 'event_espresso'), TRUE, current_time('timestamp')), 'EVT_additional_limit' => new EE_Integer_Field('EVT_additional_limit', __('Limit of Additional Registrations on Same Transaction', 'event_espresso'), TRUE, 10), 'EVT_default_registration_status' => new EE_Enum_Text_Field('EVT_default_registration_status', __('Default Registration Status on this Event', 'event_espresso'), FALSE, EEM_Registration::status_id_pending_payment, EEM_Registration::reg_status_array()), 'EVT_member_only' => new EE_Boolean_Field('EVT_member_only', __('Member-Only Event Flag', 'event_espresso'), FALSE, FALSE), 'EVT_phone' => new EE_Plain_Text_Field('EVT_phone', __('Event Phone Number', 'event_espresso'), FALSE), 'EVT_allow_overflow' => new EE_Boolean_Field('EVT_allow_overflow', __('Allow Overflow on Event', 'event_espresso'), FALSE, FALSE), 'EVT_timezone_string' => new EE_Plain_Text_Field('EVT_timezone_string', __('Timezone (name) for Event times', 'event_espresso'), FALSE), 'EVT_external_URL' => new EE_Plain_Text_Field('EVT_external_URL', __('URL of Event Page if hosted elsewhere', 'event_espresso'), TRUE), 'EVT_donations' => new EE_Boolean_Field('EVT_donations', __('Accept Donations?', 'event_espresso'), FALSE, FALSE)));
     $this->_model_relations = array('Registration' => new EE_Has_Many_Relation(), 'Datetime' => new EE_Has_Many_Relation(), 'Question_Group' => new EE_HABTM_Relation('Event_Question_Group'), 'Venue' => new EE_HABTM_Relation('Event_Venue'), 'Term_Taxonomy' => new EE_HABTM_Relation('Term_Relationship'), 'Message_Template_Group' => new EE_HABTM_Relation('Event_Message_Template'), 'Attendee' => new EE_HABTM_Relation('Registration'));
     $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions('espresso_events', 'EVTM_ID');
     parent::__construct($timezone);
 }
 /**
  * @param string $default_reg_status
  */
 public static function set_default_reg_status($default_reg_status)
 {
     self::$_default_reg_status = $default_reg_status;
     //if EEM_Event has already been instantiated, then we need to reset the `EVT_default_reg_status` field to use the new default.
     if (self::$_instance instanceof EEM_Event) {
         self::$_instance->_fields['Event_Meta']['EVT_default_registration_status'] = new EE_Enum_Text_Field('EVT_default_registration_status', __('Default Registration Status on this Event', 'event_espresso'), false, $default_reg_status, EEM_Registration::reg_status_array());
         self::$_instance->_fields['Event_Meta']['EVT_default_registration_status']->_construct_finalize('Event_Meta', 'EVT_default_registration_status', 'EEM_Event');
     }
 }
 /**
  * 	process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event.
  *
  *
  *
  * 	[ESPRESSO_EVENT_ATTENDEES] - defaults to attendees for earliest active event, or earliest upcoming event.
  * 	[ESPRESSO_EVENT_ATTENDEES event_id=123] - attendees for specific event.
  * 	[ESPRESSO_EVENT_ATTENDEES datetime_id=245] - attendees for a specific datetime.
  * 	[ESPRESSO_EVENT_ATTENDEES ticket_id=123] - attendees for a specific ticket.
  * 	[ESPRESSO_EVENT_ATTENDEES status=all] - specific registration status (use status id) or all for all attendees
  *                                          regardless of status.  Note default is to only return approved attendees
  * 	[ESPRESSO_EVENT_ATTENDEES show_gravatar=true] - default is to not return gravatar.  Otherwise if this is set
  *                                                  then return gravatar for email address given.
  *
  *  Note: because of the relationship between event_id, ticket_id, and datetime_id. If more than one of those params
  *  is included then preference is given to the following:
  *  - event_id is used whenever its present and any others are ignored.
  *  - if no event_id then datetime is used whenever its present and any others are ignored.
  *  - otherwise ticket_id is used if present.
  *
  *  @access 	public
  *  @param 	    array 	$attributes
  *  @return 	string
  */
 public function process_shortcode($attributes = array())
 {
     //load helpers
     EE_Registry::instance()->load_helper('Event_View');
     EE_Registry::instance()->load_helper('Template');
     // merge in any attributes passed via fallback shortcode processor
     $attributes = array_merge((array) $attributes, (array) $this->_attributes);
     //set default attributes
     $default_shortcode_attributes = array('event_id' => null, 'datetime_id' => null, 'ticket_id' => null, 'status' => EEM_Registration::status_id_approved, 'show_gravatar' => false);
     // allow the defaults to be filtered
     $default_shortcode_attributes = apply_filters('EES_Espresso_Event_Attendees__process_shortcode__default_shortcode_atts', $default_shortcode_attributes);
     // grab attributes and merge with defaults, then extract
     $attributes = array_merge($default_shortcode_attributes, $attributes);
     $template_args = array('contacts' => array(), 'event' => null, 'datetime' => null, 'ticket' => null, 'show_gravatar' => $attributes['show_gravatar']);
     //start setting up the query for the contacts
     $query = array();
     $error = false;
     //what event?
     if (empty($attributes['event_id']) && empty($attributes['datetime_id']) && empty($attributes['ticket_id'])) {
         //seems like is_espresso_event_single() isn't working as expected. So using alternate method.
         if (is_single() && is_espresso_event()) {
             $event = EEH_Event_View::get_event();
             if ($event instanceof EE_Event) {
                 $template_args['event'] = $event;
                 $query[0]['Registration.EVT_ID'] = $event->ID();
             }
         } else {
             //try getting the earliest active event if none then get the
             $events = EEM_Event::instance()->get_active_events(array('limit' => 1, 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC')));
             $events = empty($events) ? EEM_Event::instance()->get_upcoming_events(array('limit' => 1, 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC'))) : $events;
             $event = reset($events);
             if ($event instanceof EE_Event) {
                 $query[0]['Registration.EVT_ID'] = $event->ID();
                 $template_args['event'] = $event;
             }
         }
     } elseif (!empty($attributes['event_id'])) {
         $event = EEM_Event::instance()->get_one_by_ID($attributes['event_id']);
         if ($event instanceof EE_Event) {
             $query[0]['Registration.EVT_ID'] = $attributes['event_id'];
             $template_args['event'] = $event;
         } else {
             $error = true;
         }
     }
     //datetime?
     if (!empty($attributes['datetime_id']) && empty($attributes['event_id'])) {
         $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']);
         if ($datetime instanceof EE_Datetime) {
             $query[0]['Registration.Ticket.Datetime.DTT_ID'] = $attributes['datetime_id'];
             $query['default_where_conditions'] = 'this_model_only';
             $template_args['datetime'] = $datetime;
             $template_args['event'] = $datetime->event();
         } else {
             $error = true;
         }
     }
     //ticket?just
     if (!empty($attributes['ticket_id']) && empty($attributes['event_id']) && empty($attributes['datetime_id'])) {
         $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']);
         if ($ticket instanceof EE_Ticket) {
             $query[0]['Registration.TKT_ID'] = $attributes['ticket_id'];
             $template_args['ticket'] = $ticket;
             $template_args['event'] = $ticket->first_datetime() instanceof EE_Datetime ? $ticket->first_datetime()->event() : null;
         } else {
             $error = true;
         }
     }
     //status
     $reg_status_array = EEM_Registration::reg_status_array();
     if ($attributes['status'] != 'all' && isset($reg_status_array[$attributes['status']])) {
         $query[0]['Registration.STS_ID'] = $attributes['status'];
     }
     $query['group_by'] = array('ATT_ID');
     $query['order_by'] = apply_filters('FHEE__EES_Espresso_Event_Attendees__process_shortcode__order_by', array('ATT_lname' => 'ASC', 'ATT_fname' => 'ASC'));
     //if we have NO query where conditions, then there was an invalid parameter or the shortcode was used incorrectly
     //so when WP_DEBUG is set and true, we'll show a message, otherwise we'll just return an empty string.
     if (!isset($query[0]) || !is_array($query[0]) || $error) {
         if (WP_DEBUG) {
             return '<div class="important-notice ee-attention">' . __('The [ESPRESSO_EVENT_ATTENDEES] shortcode has been used incorrectly.  Please double check the arguments you used for any typos.  In the case of ID type arguments, its possible the given ID does not correspond to existing data in the database.', 'event_espresso') . '</div>';
         } else {
             return '';
         }
     }
     //get contacts!
     $template_args['contacts'] = EEM_Attendee::instance()->get_all($query);
     //all set let's load up the template and return.
     return EEH_Template::locate_template('loop-espresso_event_attendees.php', $template_args, true, true);
 }
 /**
  * 		get list of registration statuses
  *		@access private
  *		@return void
  */
 private function _get_registration_status_array()
 {
     self::$_reg_status = EEM_Registration::reg_status_array(array(), TRUE);
 }
 /**
  * _get_reg_status_selection
  *
  * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets instead of events.
  *	@access protected
  * @return void
  */
 protected function _get_reg_status_selection()
 {
     //first get all possible statuses
     $statuses = EEM_Registration::reg_status_array(array(), TRUE);
     //let's add a "don't change" option.
     $status_array['NAN'] = __('Leave the Same', 'event_espresso');
     $status_array = array_merge($status_array, $statuses);
     $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input('txn_reg_status_change[reg_status]', $status_array, 'NAN', 'id="txn-admin-payment-reg-status-inp"', 'txn-reg-status-change-reg-status');
     $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input('delete_txn_reg_status_change[reg_status]', $status_array, 'NAN', 'delete-txn-admin-payment-reg-status-inp', 'delete-txn-reg-status-change-reg-status');
 }
 /**
  * 	_default_event_settings
  *
  * 	This generates the Default Settings Tab
  *
  * 	@return string html for the settings page
  */
 protected function _default_event_settings()
 {
     $this->_template_args['values'] = $this->_yes_no_values;
     $this->_template_args['reg_status_array'] = EEM_Registration::reg_status_array(array(EEM_Registration::status_id_cancelled, EEM_Registration::status_id_declined, EEM_Registration::status_id_incomplete), TRUE);
     $this->_template_args['default_reg_status'] = isset(EE_Registry::instance()->CFG->registration->default_STS_ID) ? sanitize_text_field(EE_Registry::instance()->CFG->registration->default_STS_ID) : EEM_Registration::status_id_pending_payment;
     $this->_set_add_edit_form_tags('update_default_event_settings');
     $this->_set_publish_post_box_vars(NULL, FALSE, FALSE, NULL, FALSE);
     $this->_template_args['admin_page_content'] = EEH_Template::display_template(EVENTS_TEMPLATE_PATH . 'event_settings.template.php', $this->_template_args, TRUE);
     $this->display_admin_page_with_sidebar();
 }
 /**
  * override caf metabox
  * @return string html contents
  */
 public function registration_options_meta_box()
 {
     $yes_no_values = array(array('id' => true, 'text' => __('Yes', 'event_espresso')), array('id' => false, 'text' => __('No', 'event_espresso')));
     $default_reg_status_values = EEM_Registration::reg_status_array(array(EEM_Registration::status_id_cancelled, EEM_Registration::status_id_declined, EEM_Registration::status_id_incomplete), TRUE);
     $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(FALSE);
     $template_args['_event'] = $this->_cpt_model_obj;
     $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit();
     $template_args['default_registration_status'] = EEH_Form_Fields::select_input('default_reg_status', $default_reg_status_values, $this->_cpt_model_obj->default_registration_status());
     $template_args['display_description'] = EEH_Form_Fields::select_input('display_desc', $yes_no_values, $this->_cpt_model_obj->display_description());
     $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input('display_ticket_selector', $yes_no_values, $this->_cpt_model_obj->display_ticket_selector(), '', '', false);
     $template_args['EVT_default_registration_status'] = EEH_Form_Fields::select_input('EVT_default_registration_status', $default_reg_status_values, $this->_cpt_model_obj->default_registration_status());
     $template_args['additional_registration_options'] = apply_filters('FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options', '', $template_args, $yes_no_values, $default_reg_status_values);
     $templatepath = EVENTS_CAF_TEMPLATE_PATH . 'event_registration_options.template.php';
     EEH_Template::display_template($templatepath, $template_args);
 }
 /**
  * Get the number of registrations per event grouped by registration status.
  * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results.
  *
  * @param string $period
  *
  * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID
  *                    (i.e. RAP)
  */
 public function get_registrations_per_event_and_per_status_report($period = '-1 month')
 {
     global $wpdb;
     $registration_table = $wpdb->prefix . 'esp_registration';
     $event_table = $wpdb->posts;
     $sql_date = date("Y-m-d H:i:s", strtotime($period));
     //inner date query
     $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from {$registration_table} ";
     $inner_where = " WHERE";
     //exclude events not authored by user if permissions in effect
     if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
         $inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
         $inner_where .= " post_author = " . get_current_user_id() . " AND";
     }
     $inner_where .= " REG_date >= '{$sql_date}'";
     $inner_date_query .= $inner_where;
     //build main query
     $select = "SELECT Event.post_title as Registration_Event, ";
     $join = '';
     $join_parts = array();
     $select_parts = array();
     //loop through registration stati to do parts for each status.
     foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) {
         if ($STS_ID === EEM_Registration::status_id_incomplete) {
             continue;
         }
         $select_parts[] = "COUNT({$STS_code}.REG_ID) as {$STS_ID}";
         $join_parts[] = "{$registration_table} AS {$STS_code} ON {$STS_code}.EVT_ID = dates.EVT_ID AND {$STS_code}.STS_ID = '{$STS_ID}' AND {$STS_code}.REG_date = dates.REG_date";
     }
     //setup the selects
     $select .= implode(', ', $select_parts);
     $select .= " FROM ({$inner_date_query}) AS dates LEFT JOIN {$event_table} as Event ON Event.ID = dates.EVT_ID LEFT JOIN ";
     //setup remaining joins
     $join .= implode(" LEFT JOIN ", $join_parts);
     //now put it all together
     $query = $select . $join . ' GROUP BY Registration_Event';
     //and execute
     $results = $wpdb->get_results($query, ARRAY_A);
     return $results;
 }
				<p class="description">
					<?php 
_e('Will list the attendees for a specific ticket.', 'event_espresso');
?>
				</p>
				<br>
			</li>
			<li>
				<strong>[ESPRESSO_EVENT_ATTENDEES status=RAP]</strong><br>
				<p class="description">
					<?php 
_e('You can list attendees that have a specific registration status (use status code) or use "all" to return all attendees regardless of status.  Default when you don\'t have this parameter set is to only return attendees attached to approved contacts.', 'event_espresso');
?>
				</p>
				<p>
					<?php 
_e('The statuses you can use are:', 'event_espresso');
?>
					<br>
					<?php 
foreach (EEM_Registration::reg_status_array(array(), true) as $status_code => $status_label) {
    echo '<strong>' . $status_code . ':</strong>' . ' ' . $status_label . '<br>';
}
?>

				</p>
				<br>
			</li>
		</ul>
</div>