/**
  * This tests a soft delete trash and restore of a cpt model item.
  * Test prompted by https://events.codebasehq.com/projects/event-espresso/tickets/6625
  *
  * @since 4.4.0
  *
  * @return void
  */
 public function test_soft_trash_restore_cpt_from_model_context()
 {
     //create attendee model object and dependencies.
     $attendee = $this->new_model_obj_with_dependencies('Attendee');
     //verify have an attendeee object
     $this->assertInstanceOf('EE_Attendee', $attendee);
     //use this attendee_id for every model request.
     $att_id = $attendee->ID();
     //reset model
     EE_Registry::instance()->reset_model('Attendee');
     //verify not trashed.
     $status = $attendee->status();
     $this->assertFalse($status == 'trash');
     //k now let's trash it
     EEM_Attendee::instance()->delete_by_ID($att_id);
     //verify
     $trash_attendee = EEM_Attendee::instance()->get_one_by_ID($att_id);
     $this->assertInstanceOf('EE_Attendee', $trash_attendee);
     $status = $trash_attendee->status();
     $this->assertTrue($status == 'trash');
     //reset model again
     EE_Registry::instance()->reset_model('Attendee');
     //now let's try to restore.
     EEM_Attendee::instance()->restore_by_ID($att_id);
     $restore_attendee = EEM_Attendee::instance()->get_one_by_ID($att_id);
     $this->assertInstanceOf('EE_Attendee', $restore_attendee);
     $status = $restore_attendee->status();
     $this->assertFalse($status == 'trash');
 }
 function test_get_attendee_property_answer_value()
 {
     $r = $this->new_model_obj_with_dependencies('Registration');
     global $wpdb;
     $att = EEM_Attendee::reset()->get_one_by_ID($r->attendee_ID());
     $this->assertEquals($r->attendee()->fname(), EEM_Answer::instance()->get_attendee_property_answer_value($r, EEM_Attendee::system_question_fname));
 }
 /**
  * This generates the dummy relation objects for use in a new registration.
  *
  * @since 4.3.0
  *
  * @param array $args
  */
 private function _set_new_relations($args)
 {
     //transaction
     $this->_transaction = empty($args['TXN_ID']) ? $this->factory->transaction->create() : EEM_Transaction::instance()->get_one_by_ID($args['TXN_ID']);
     $this->_transaction = empty($this->_transaction) ? $this->factory->transaction->create() : $this->_transaction;
     //ticket
     $this->_ticket = empty($args['TKT_ID']) ? $this->factory->ticket_chained->create() : EEM_Ticket::instance()->get_one_by_ID($args['TKT_ID']);
     $this->_ticket = empty($this->_ticket) ? $this->factory->ticket_chained->create() : $this->_ticket;
     //attendee
     $this->_attendee = empty($args['ATT_ID']) ? $this->factory->attendee->create() : EEM_Attendee::instance()->get_one_by_ID($args['ATT_ID']);
     $this->_attendee = empty($this->_attendee) ? $this->factory->attendee->create() : $this->_attendee;
     //status
     $this->_status = empty($arg['STS_ID']) ? $this->factory->status->create(array('STS_ID' => EEM_Registration::status_id_pending_payment, 'STS_type' => 'registration', 'STS_code' => 'PENDING_PAYMENT')) : EEM_Status::instance()->get_one_by_ID($args['STS_ID']);
     $this->_status = empty($this->_status) ? $this->factory->status->create(array('STS_ID' => EEM_Registration::status_id_pending_payment, 'STS_type' => 'registration', 'STS_code' => 'PENDING_PAYMENT')) : $this->_status;
 }
 /**
  * verifies that non-logged-in users can't view attendees,
  * but users with contact-reading permissions can read all attendees
  */
 function test_get_all__caps()
 {
     global $current_user;
     $user = $this->factory->user->create_and_get();
     $att = $this->new_model_obj_with_dependencies('Attendee');
     $this->assertEquals(0, EEM_Attendee::instance()->count(array('caps' => EEM_Base::caps_read)));
     //ok now log the user in
     $current_user = $user;
     //and they still hsouldn't be able to ivew the attendee
     $this->assertEquals(0, EEM_Attendee::instance()->count(array('caps' => EEM_Base::caps_read)));
     //ok now give them access to contacts
     $current_user->add_cap('ee_read_contacts');
     //and now they should be able to see it
     $this->assertEquals(1, EEM_Attendee::instance()->count(array('caps' => EEM_Base::caps_read)));
 }
 public function get_csv_data($offset, $limit)
 {
     $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results(array('limit' => array($offset, $limit), 'force_join' => array('State', 'Country')));
     $csv_data = array();
     foreach ($attendee_rows as $attendee_row) {
         $csv_row = array();
         foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) {
             if ($field_name == 'STA_ID') {
                 $state_name_field = \EEM_State::instance()->field_settings_for('STA_name');
                 $csv_row[__('State', 'event_espresso')] = $attendee_row[$state_name_field->get_qualified_column()];
             } elseif ($field_name == 'CNT_ISO') {
                 $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name');
                 $csv_row[__('Country', 'event_espresso')] = $attendee_row[$country_name_field->get_qualified_column()];
             } else {
                 $csv_row[$field_obj->get_nicename()] = $attendee_row[$field_obj->get_qualified_column()];
             }
         }
         $csv_data[] = $csv_row;
     }
     return $csv_data;
 }
 /**
  * Sets the defaults for the billing form according to the attendee's details
  * @param EE_Attendee $attendee
  */
 public function populate_from_attendee($attendee)
 {
     $attendee = EEM_Attendee::instance()->ensure_is_obj($attendee);
     /** @var $attendee EE_Attendee */
     $this->populate_defaults(array('first_name' => $attendee->fname(), 'last_name' => $attendee->lname(), 'email' => $attendee->email(), 'address' => $attendee->address(), 'address2' => $attendee->address2(), 'city' => $attendee->city(), 'state' => $attendee->state_ID(), 'country' => $attendee->country_ID(), 'zip' => $attendee->zip(), 'phone' => $attendee->phone()));
 }
 /**
  * @group 6767
  */
 function test_two_joins()
 {
     EEM_Attendee::instance()->get_all(array(array('Registration.Event.EVT_name' => 'bob')));
     $this->assertTrue(TRUE, 'No exception thrown');
 }
 protected function _get_attendees_count()
 {
     return EEM_Attendee::instance()->count();
 }
 function test_register_mock_addon_success()
 {
     $this->assertFalse($this->_class_has_been_extended());
     $this->assertFalse($this->_model_has_been_extended());
     $this->_pretend_addon_hook_time();
     EE_Register_Model_Extensions::register($this->_model_group, $this->_reg_args);
     $att_model = EE_Registry::instance()->reset_model('Attendee');
     //verify they still haven't been extended
     $this->assertTrue($this->_class_has_been_extended(TRUE));
     $this->assertTrue($this->_model_has_been_extended(TRUE));
     //and that we can still use EEM_Attendee
     $a = EE_Attendee::new_instance();
     EEM_Attendee::instance()->get_all();
     EE_Registry::instance()->load_model('Attendee')->get_all();
     //now deregister it
     EE_Register_Model_Extensions::deregister($this->_model_group);
     $this->assertFalse($this->_class_has_been_extended());
     $this->assertFalse($this->_model_has_been_extended());
     EE_Registry::instance()->reset_model('Attendee');
     //and EEM_Attendee still works right? both ways of instantiating it?
     $a2 = EE_Attendee::new_instance();
     EEM_Attendee::instance()->get_all();
     EE_Registry::instance()->load_model('Attendee')->get_all();
 }
 /**
  * Sets the defaults for the billing form according to the attendee's details
  * @param EE_Attendee $attendee
  */
 public function populate_from_attendee($attendee)
 {
     $attendee = EEM_Attendee::instance()->ensure_is_obj($attendee);
     /** @var $attendee EE_Attendee */
     $this->populate_defaults(apply_filters('FHEE__EE_Billing_Attendee_Info_Form__populate_from_attendee', array('first_name' => $attendee->fname(), 'last_name' => $attendee->lname(), 'email' => $attendee->email(), 'address' => $attendee->address(), 'address2' => $attendee->address2(), 'city' => $attendee->city(), 'state' => $attendee->state_ID(), 'country' => $attendee->country_ID(), 'zip' => $attendee->zip(), 'phone' => $attendee->phone()), $attendee, $this));
 }
 /**
  * @group 7151
  */
 public function test_in_entity_map()
 {
     $att = EE_Attendee::new_instance(array('ATT_fname' => 'mike'));
     $this->assertFalse($att->in_entity_map());
     $att->save();
     $this->assertTrue($att->in_entity_map());
     EE_Registry::instance()->reset_model('Attendee');
     //when we serialized it, it forgot if it was in the entity map or not
     $this->assertFalse($att->in_entity_map());
     try {
         //should throw an exception because we hate saving
         //a model object that's not in the entity mapper
         $att->save();
     } catch (EE_Error $e) {
         $this->assertTrue(TRUE);
     }
     EEM_Attendee::instance()->add_to_entity_map($att);
     //we should all acknowledge it's in the entity map now
     $this->assertTrue($att->in_entity_map());
     //we shouldn't complain at saving it now, it's in the entity map and so we're allowed
     $att->save();
     //also, when we clone an item in the entity map, it shouldn't be considered in the entity map
     $att2 = clone $att;
     $this->assertFalse($att2->in_entity_map());
 }
 /**
  * return the attendee object for a given attendee ID.
  *
  * @since 4.3.0
  *
  * @param int $ATT_ID the attendee id for the attendee to attempt to retrieve
  *
  * @return mixed null|EE_Attendee
  */
 public function get_object_by_id($ATT_ID)
 {
     return EEM_Attendee::instance()->get_one_by_ID($ATT_ID);
 }
 /**
  * @see parent class for phpdocs
  * @param array $attendee_ids
  * @return EE_Attendee[]
  */
 public static function convert_data_from_persistent_storage($attendee_ids)
 {
     $attendee_ids = (array) $attendee_ids;
     $attendees = EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('IN', $attendee_ids))));
     return $attendees;
 }
 /**
  * test if an INT fk doesn't exist -> set it to NULL!
  * if a STRING fk exists -> leave it alone
  */
 public function test_save_data_array_to_db__from_other_site__fks_that_dont_exist()
 {
     //model object with foreign key that's an INT (should be set to 0 or NULL)
     $att = $this->new_model_obj_with_dependencies('Attendee', array('STA_ID' => 99999, 'CNT_ISO' => '77'), false);
     $att_props = $att->model_field_array();
     $att_props['ATT_ID'] = 123;
     $csv_data = array('Attendee' => array($att_props));
     $att_count = EEM_Attendee::instance()->count();
     $mappings = EE_Import::instance()->save_data_rows_to_db($csv_data, true, array());
     $this->assertEquals($att_count + 1, EEM_Attendee::instance()->count());
     //the STA_ID should ahve been set to 0, but teh CNT_ISO should have been left as-is
     $att_id_in_db = $mappings['Attendee'][123];
     $att_in_db = EEM_Attendee::instance()->get_one_by_ID($att_id_in_db);
     //model object with a foreign key that's a STRING (should be left as-is)
     $this->assertEquals(0, $att_in_db->get('STA_ID'));
     $this->assertEquals('77', $att_in_db->get('CNT_ISO'));
 }
Esempio n. 15
0
 /**
  * Export a custom CSV of registration info including: A bunch of the reg fields, the time of the event, the price name,
  * and the questions associated with the registrations
  * @param type $event_id
  */
 function report_registrations_for_event()
 {
     $event_id = $this->_req_data['EVT_ID'];
     $reg_fields_to_include = array('REG_ID', 'REG_date', 'REG_code', 'REG_count', 'REG_final_price');
     $att_fields_to_include = array('ATT_fname', 'ATT_lname', 'ATT_email', 'ATT_address', 'ATT_address2', 'ATT_city', 'STA_ID', 'CNT_ISO', 'ATT_zip', 'ATT_phone');
     $registrations_csv_ready_array = array();
     $reg_model = EE_Registry::instance()->load_model('Registration');
     $registrations = $reg_model->get_all(array(array('EVT_ID' => $event_id), 'order_by' => array('Transaction.TXN_ID' => 'asc', 'REG_count' => 'asc')));
     //get all questions which relate to someone in this group
     $registration_ids = array_keys($registrations);
     //		EEM_Question::instance()->show_next_x_db_queries();
     $questions_for_these_registrations = EEM_Question::instance()->get_all(array(array('Answer.REG_ID' => array('IN', $registration_ids))));
     foreach ($registrations as $registration) {
         $reg_csv_array = array();
         /*@var $registration EE_Registration */
         foreach ($reg_fields_to_include as $field_name) {
             $field = $reg_model->field_settings_for($field_name);
             if ($field_name == 'REG_final_price') {
                 $value = $registration->get_pretty($field_name, 'localized_float');
             } else {
                 $value = $registration->get_pretty($field->get_name());
             }
             $reg_csv_array[$this->_get_column_name_for_field($field)] = $value;
             if ($field_name == 'REG_final_price') {
                 //add a column named Currency after the final price
                 $reg_csv_array[__("Currency", "event_espresso")] = EE_Config::instance()->currency->code;
             }
         }
         //get pretty status
         $reg_csv_array[__("Registration Status", 'event_espresso')] = $registration->pretty_status();
         //get pretty trnasaction status
         $reg_csv_array[__("Transaction Status", 'event_espresso')] = $registration->transaction()->pretty_status();
         //get whether or not the user has checked in
         $reg_csv_array[__("Check-Ins", "event_espresso")] = $registration->count_checkins();
         //get ticket of registration and its price
         $ticket_model = EE_Registry::instance()->load_model('Ticket');
         $ticket = $registration->ticket();
         $reg_csv_array[$ticket_model->field_settings_for('TKT_name')->get_nicename()] = $ticket->name();
         //get datetime(s) of registration
         $datetimes_strings = array();
         foreach ($ticket->datetimes() as $datetime) {
             $datetimes_strings[] = $datetime->start_date_and_time();
         }
         $reg_csv_array[__("Datetimes of Ticket", "event_espresso")] = implode(", ", $datetimes_strings);
         //add attendee columns
         $attendee = $registration->attendee();
         foreach ($att_fields_to_include as $att_field_name) {
             if ($attendee) {
                 if ($att_field_name == 'STA_ID') {
                     $state = $attendee->state_obj();
                     if ($state) {
                         $value = $state->name();
                     } else {
                         $value = '';
                     }
                 } elseif ($att_field_name == 'CNT_ISO') {
                     $country = $attendee->country_obj();
                     if ($country) {
                         $value = $country->name();
                     } else {
                         $value = '';
                     }
                 } else {
                     $value = $attendee->get_pretty($att_field_name);
                 }
             } else {
                 $value = '';
             }
             $field_obj = EEM_Attendee::instance()->field_settings_for($att_field_name);
             $reg_csv_array[$this->_get_column_name_for_field($field_obj)] = $value;
         }
         //make sure each registration has the same questions in the same order
         foreach ($questions_for_these_registrations as $question) {
             if (!isset($reg_csv_array[$question->admin_label()])) {
                 $reg_csv_array[$question->admin_label()] = null;
             }
         }
         //now fill out the questions THEY answered
         foreach ($registration->answers() as $answer) {
             /* @var $answer EE_Answer */
             $reg_csv_array[$answer->question()->admin_label()] = $answer->pretty_value();
         }
         $registrations_csv_ready_array[] = $reg_csv_array;
     }
     //if we couldn't export anything, we want to at least show the column headers
     if (empty($registrations_csv_ready_array)) {
         $reg_csv_array = array();
         foreach ($reg_fields_to_include as $model_name => $field_list) {
             $model = EE_Registry::instance()->load_model($model_name);
             foreach ($field_list as $field_name) {
                 $field = $model->field_settings_for($field_name);
                 $reg_csv_array[$this->_get_column_name_for_field($field)] = null;
                 //$registration->get($field->get_name());
             }
         }
         $registrations_csv_ready_array[] = $reg_csv_array;
     }
     $event = EEM_Event::instance()->get_one_by_ID($event_id);
     $filename = sprintf("registrations-for-%s", $event->slug());
     $handle = $this->EE_CSV->begin_sending_csv($filename);
     $this->EE_CSV->write_data_array_to_csv($handle, $registrations_csv_ready_array);
     $this->EE_CSV->end_sending_csv($handle);
 }
 /**
  * 	_process_attendee_information
  *
  * 	@access 	private
  * @return mixed
  */
 private function _process_attendee_information()
 {
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     $success = TRUE;
     // empty container
     $valid_data = array();
     if (EE_Registry::instance()->REQ->is_set('qstn')) {
         $valid_data = apply_filters('FHEE__EE_Single_Page_Checkout__process_attendee_information__REQ', EE_Registry::instance()->REQ->get('qstn'));
         // loop through post data and sanitize all elements
         array_walk_recursive($valid_data, array(EE_Registry::instance()->REQ, 'sanitize_text_field_for_array_walk'));
     }
     // if we don't have any $valid_data then something went TERRIBLY WRONG !!! AHHHHHHHH!!!!!!!
     if (!empty($valid_data)) {
         if (isset($valid_data['custom_questions'])) {
             if (!$this->_reg_url_link) {
                 EE_Registry::instance()->SSN->set_session_data(array('custom_questions' => $valid_data['custom_questions']));
             }
             unset($valid_data['custom_questions']);
         }
         $primary_attendee = array();
         $primary_attendee_obj = NULL;
         $primary_attendee['line_item_id'] = NULL;
         if (isset($valid_data['primary_attendee'])) {
             $primary_attendee['line_item_id'] = !empty($valid_data['primary_attendee']) ? $valid_data['primary_attendee'] : FALSE;
             unset($valid_data['primary_attendee']);
         }
         //			printr( $valid_data, '$valid_data  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
         //			printr( $this->_transaction, '$this->_transaction  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
         // attendee counter
         $att_nmbr = 0;
         if ($this->_continue_reg) {
             if ($this->_transaction instanceof EE_Transaction && $this->_continue_reg) {
                 $registrations = $this->_transaction->registrations(array(), TRUE);
                 if (!empty($registrations)) {
                     EE_Registry::instance()->load_model('Attendee');
                     $primary_attendee_obj = NULL;
                     // grab the saved registrations from the transaction
                     foreach ($registrations as $registration) {
                         // verify EE_Registration object
                         if ($registration instanceof EE_Registration) {
                             // if event is sold out but this is NOT a revisit
                             if ($this->_revisit || !$this->_revisit && !($registration->event()->is_sold_out() || $registration->event()->is_sold_out(TRUE))) {
                                 // EITHER a) first time thru SPCO so process ALL registrations
                                 // OR b) primary registrant is editing info, so process ALL registrations
                                 // OR b) another registrant is editing info, so ONLY process their registration
                                 if (!$this->_revisit || $this->_primary_revisit || $this->_revisit && $this->_reg_url_link == $registration->reg_url_link()) {
                                     // reg_url_link / line item ID exists ?
                                     if ($line_item_id = $registration->reg_url_link()) {
                                         //										echo '<h5 style="color:#2EA2CC;">$line_item_id : <span style="color:#E76700">' . $line_item_id . '</span><br/><span style="font-size:9px;font-weight:normal;color:#666">' . __FILE__ . '</span>    <b style="font-size:10px;color:#333">  ' . __LINE__ . ' </b></h5>';
                                         // Houston, we have a registration!
                                         $att_nmbr++;
                                         // grab related answer objects
                                         $answers = $registration->answers();
                                         //										printr( $answers, '$answers  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                         $attendee_data = array();
                                         // do we need to copy basic info from primary attendee ?
                                         $copy_primary = isset($valid_data[$line_item_id]['additional_attendee_reg_info']) && absint($valid_data[$line_item_id]['additional_attendee_reg_info']) === 0 ? TRUE : FALSE;
                                         //										echo '<h5 style="color:#2EA2CC;">$copy_primary : <span style="color:#E76700">' . $copy_primary . '</span><br/><span style="font-size:9px;font-weight:normal;color:#666">' . __FILE__ . '</span>    <b style="font-size:10px;color:#333">  ' . __LINE__ . ' </b></h5>';
                                         unset($valid_data[$line_item_id]['additional_attendee_reg_info']);
                                         if (isset($valid_data[$line_item_id])) {
                                             // filter form input data for this registration
                                             $valid_data[$line_item_id] = apply_filters('FHEE__EE_Single_Page_Checkout__process_attendee_information__valid_data_line_item', $valid_data[$line_item_id]);
                                             //									printr( $valid_data[ $line_item_id ], '$valid_data[ $line_item_id ]  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                             // now loop through our array of valid post data && process attendee reg forms
                                             foreach ($valid_data[$line_item_id] as $form_input => $input_value) {
                                                 // check for critical inputs
                                                 if (empty($input_value)) {
                                                     switch ($form_input) {
                                                         case 'fname':
                                                             EE_Error::add_error(__('First Name is a required value.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                                                             $success = FALSE;
                                                             break;
                                                         case 'lname':
                                                             EE_Error::add_error(__('Last Name is a required value.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                                                             $success = FALSE;
                                                             break;
                                                         case 'email':
                                                             EE_Error::add_error(__('Email Address is a required value.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                                                             $success = FALSE;
                                                             break;
                                                     }
                                                 } elseif ($form_input == 'email') {
                                                     // clean the email address
                                                     $valid_email = sanitize_email($input_value);
                                                     // check if it matches
                                                     if ($input_value != $valid_email) {
                                                         // whoops!!!
                                                         EE_Error::add_error(__('Please enter a valid email address.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                                                         $success = FALSE;
                                                     }
                                                 }
                                                 // store a bit of data about the primary attendee
                                                 if ($att_nmbr == 1 && $line_item_id == $primary_attendee['line_item_id'] && !empty($input_value)) {
                                                     $primary_attendee[$form_input] = $input_value;
                                                 } else {
                                                     if ($copy_primary && isset($primary_attendee[$form_input]) && $input_value == NULL) {
                                                         $input_value = $primary_attendee[$form_input];
                                                     }
                                                 }
                                                 // $answer_cache_id is the key used to find the EE_Answer we want
                                                 $answer_cache_id = $this->_reg_url_link ? $form_input : $form_input . '-' . $line_item_id;
                                                 $answer_is_obj = isset($answers[$answer_cache_id]) && $answers[$answer_cache_id] instanceof EE_Answer ? TRUE : FALSE;
                                                 //rename a couple of form_inputs
                                                 switch ($form_input) {
                                                     case 'state':
                                                         $form_input = 'STA_ID';
                                                         $attendee_property = TRUE;
                                                         break;
                                                     case 'country':
                                                         $form_input = 'CNT_ISO';
                                                         $attendee_property = TRUE;
                                                         break;
                                                     default:
                                                         $attendee_property = EEM_Attendee::instance()->has_field('ATT_' . $form_input) ? TRUE : FALSE;
                                                         $form_input = $attendee_property ? 'ATT_' . $form_input : $form_input;
                                                 }
                                                 //		echo '<h4>$answer_cache_id : ' . $answer_cache_id . '  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
                                                 //		echo '<h4>attendee_property: ' . $attendee_property . '  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
                                                 //		echo '<h4>$answer_is_obj : ' . $answer_is_obj . '  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
                                                 //		echo '<h4>' . $form_input . ': ' . ( is_array( $input_value ) ? implode( ', ', $input_value ) : $input_value ) . '  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
                                                 $saved = FALSE;
                                                 // if this form input has a corresponding attendee property
                                                 if ($attendee_property) {
                                                     $attendee_data[$form_input] = $input_value;
                                                     if ($answer_is_obj) {
                                                         // and delete the corresponding answer since we won't be storing this data in that object
                                                         $registration->_remove_relation_to($answers[$answer_cache_id], 'Answer');
                                                     }
                                                     $saved = TRUE;
                                                 } elseif ($answer_is_obj) {
                                                     // save this data to the answer object
                                                     $answers[$answer_cache_id]->set_value($input_value);
                                                     $saved = TRUE;
                                                 } else {
                                                     foreach ($answers as $answer) {
                                                         if ($answer instanceof EE_Answer && $answer->question_ID() == $answer_cache_id) {
                                                             $answer->set_value($input_value);
                                                             $saved = TRUE;
                                                         }
                                                     }
                                                 }
                                                 if (!$saved) {
                                                     EE_Error::add_error(sprintf(__('Unable to save registration form data for the form input: %s', 'event_espresso'), $form_input), __FILE__, __FUNCTION__, __LINE__);
                                                     $success = FALSE;
                                                 }
                                             }
                                             // end of foreach ( $valid_data[ $line_item_id ] as $form_input => $input_value )
                                         }
                                         /*else {
                                         			EE_Error::add_error( sprintf( __( 'It appears that no form data, or invalid data, for attendee #%s was received while attempting to process the registration form.', 'event_espresso' ), $att_nmbr ), __FILE__, __FUNCTION__, __LINE__ );
                                         			$success = FALSE;
                                         		}*/
                                         //											printr( $attendee_data, '$attendee_data  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                         //											printr( $answers, '$answers  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                         // this registration does not require additional attendee information ?
                                         if ($copy_primary && $att_nmbr > 1) {
                                             //										echo '<h1>$copy_primary && $att_nmbr > 1  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h1>';
                                             // add relation to new attendee
                                             $registration->_add_relation_to($primary_attendee_obj, 'Attendee');
                                             $registration->set_attendee_id($primary_attendee_obj->ID());
                                             $registration->update_cache_after_object_save('Attendee', $primary_attendee_obj);
                                             //										echo '$copy_primary attendee: '. $primary_attendee_obj->ID() . '<br/>';
                                         } else {
                                             // does this attendee already exist in the db ? we're searching using a combination of first name, last name, AND email address
                                             $ATT_fname = isset($attendee_data['ATT_fname']) && !empty($attendee_data['ATT_fname']) ? $attendee_data['ATT_fname'] : '';
                                             $ATT_lname = isset($attendee_data['ATT_lname']) && !empty($attendee_data['ATT_lname']) ? $attendee_data['ATT_lname'] : '';
                                             $ATT_email = isset($attendee_data['ATT_email']) && !empty($attendee_data['ATT_email']) ? $attendee_data['ATT_email'] : '';
                                             // but only if those have values
                                             if ($ATT_fname && $ATT_lname && $ATT_email) {
                                                 $existing_attendee = EE_Registry::instance()->LIB->EEM_Attendee->find_existing_attendee(array('ATT_fname' => $ATT_fname, 'ATT_lname' => $ATT_lname, 'ATT_email' => $ATT_email));
                                             } else {
                                                 $existing_attendee = NULL;
                                             }
                                             //										printr( $existing_attendee, '$existing_attendee  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                             $existing_attendee = apply_filters('FHEE_EE_Single_Page_Checkout__save_registration_items__find_existing_attendee', $existing_attendee, $registration);
                                             // did we find an already existing record for this attendee ?
                                             if ($existing_attendee instanceof EE_Attendee) {
                                                 //												echo '<h1>$existing_attendee instanceof EE_Attendee  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h1>';
                                                 // update attendee data in case it has changed since last time they registered for an event
                                                 // first remove fname, lname, and email from attendee data
                                                 unset($attendee_data['ATT_fname']);
                                                 unset($attendee_data['ATT_lname']);
                                                 unset($attendee_data['ATT_email']);
                                                 // now loop thru what's left and add to attendee CPT
                                                 foreach ($attendee_data as $property_name => $property_value) {
                                                     if (EEM_Attendee::instance()->has_field($property_name)) {
                                                         $existing_attendee->set($property_name, $property_value);
                                                     }
                                                 }
                                                 // better save that now
                                                 $existing_attendee->save();
                                                 // add relation to existing attendee
                                                 $registration->_add_relation_to($existing_attendee, 'Attendee');
                                                 $registration->set_attendee_id($existing_attendee->ID());
                                                 $registration->update_cache_after_object_save('Attendee', $existing_attendee);
                                             } else {
                                                 // ensure critical details are set for additional attendees
                                                 if ($att_nmbr > 1) {
                                                     $critical_attendee_details = array('ATT_fname', 'ATT_lname', 'ATT_email');
                                                     $critical_attendee_details = apply_filters('FHEE__EE_Single_Page_Checkout__process_attendee_information__critical_attendee_details', $critical_attendee_details);
                                                     foreach ($critical_attendee_details as $critical_attendee_detail) {
                                                         if (!isset($attendee_data[$critical_attendee_detail]) || empty($attendee_data[$critical_attendee_detail])) {
                                                             $attendee_data[$critical_attendee_detail] = $primary_attendee_obj->get($critical_attendee_detail);
                                                         }
                                                     }
                                                 }
                                                 // set author to event creator
                                                 $attendee_data['ATT_author'] = $registration->event()->wp_user();
                                                 // create new attendee object
                                                 $new_attendee = EE_Attendee::new_instance($attendee_data);
                                                 $new_attendee->save();
                                                 //												echo '<h1>$new_attendee  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h1>';
                                                 // add relation to new attendee
                                                 $registration->_add_relation_to($new_attendee, 'Attendee');
                                                 $registration->set_attendee_id($new_attendee->ID());
                                                 $registration->update_cache_after_object_save('Attendee', $new_attendee);
                                             }
                                             // who's the man ?
                                             if ($att_nmbr == 1) {
                                                 $primary_attendee_obj = $registration->get_first_related('Attendee');
                                                 //												printr( $primary_attendee_obj, '$primary_attendee_obj  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                             }
                                         }
                                     } else {
                                         EE_Error::add_error(__('An invalid or missing line item ID was encountered while attempting to process the registration form.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                                         $success = FALSE;
                                         // remove malformed data
                                         unset($valid_data[$line_item_id]);
                                     }
                                     if (!$registration->attendee() instanceof EE_Attendee) {
                                         EE_Error::add_error(sprintf(__('Registration %s has an invalid or missing Attendee object.', 'event_espresso'), $line_item_id), __FILE__, __FUNCTION__, __LINE__);
                                         $success = FALSE;
                                     }
                                     //									printr( $registration->attendee(), '$registration->attendee()  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                                 }
                                 // end of if ( ! $this->_revisit || $this->_primary_revisit || ( $this->_revisit && $this->_reg_url_link == $registration->reg_url_link() )) {
                             } else {
                                 EE_Error::add_error(apply_filters('FHEE__Single_Page_Checkout___process_attendee_information__sold_out_events_msg', __('It appears that the event you are attempting to register for has just sold out. If you have already made a partial payment towards this event, please contact the event administrator for a refund.', 'event_espresso')), __FILE__, __FUNCTION__, __LINE__);
                                 $success = FALSE;
                             }
                             // end of if ( $registration instanceof EE_Registration )
                         } else {
                             EE_Error::add_error(__('An invalid Registration object was discovered when attempting to process your registration information.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                             $success = FALSE;
                         }
                         //							printr( $registration, '$registration  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
                     }
                     // end of foreach ( $this->_transaction->registrations()  as $registration )
                     if (!$this->_reg_url_link) {
                         EE_Registry::instance()->SSN->set_session_data(array('transaction' => $this->_transaction));
                     }
                 } else {
                     EE_Error::add_error(__('Your form data could not be applied to any valid registrations.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                     $success = FALSE;
                 }
             } else {
                 EE_Error::add_error(__('A valid transaction could not be initiated for processing your registrations.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                 $success = FALSE;
             }
         }
     } else {
         EE_Error::add_error(__('No valid question responses were received.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         $success = FALSE;
     }
     // grab any errors
     if ($success) {
         EE_Error::add_success(__('Attendee information submitted successfully.', 'event_espresso'));
     }
     //this might be called while in admin and if it is then we don't want to do our normal steps.
     if (is_admin() && !EE_Registry::instance()->REQ->front_ajax) {
         return $success;
     }
     //do action in case a plugin wants to do something with the data submitted in step 1.
     //passes EE_Single_Page_Checkout, and it's posted data
     do_action('AHEE__EE_Single_Page_Checkout__process_attendee_information__end', $this, $valid_data);
     //		printr( $registration->answers(), '$registration->answers()  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
     //		die();
     $this->go_to_next_step(__FUNCTION__);
     return TRUE;
 }
 /**
  * Determines if the Attendee model has been extended by the mock extension
  * @return boolean
  */
 private function _model_has_been_extended($throw_error = FALSE)
 {
     try {
         $att = EE_Registry::instance()->reset_model('Attendee');
         if (!$att->has_field('ATT_foobar')) {
             if ($throw_error) {
                 throw new EE_Error(sprintf(__('The field ATT_foobar is not on EEM_Attendee, but the extension should have added it. fields are: %s', 'event_espresso'), implode(",", array_keys(EEM_Attendee::instance()->field_settings()))));
             }
             return FALSE;
         }
         if (!$att->has_relation('New_Addon_Thing')) {
             if ($throw_error) {
                 throw new EE_Error(sprintf(__('The relation of type New_Addon_Thing on EEM_Attendee, but the extension should have added it. fields are: %s', 'event_espresso'), implode(",", array_keys(EEM_Attendee::instance()->field_settings()))));
             }
             return FALSE;
         }
         $att->get_all_new_things();
         return TRUE;
     } catch (EE_Error $e) {
         if ($throw_error) {
             throw $e;
         }
         return FALSE;
     }
 }
 /**
  * 		_trash_or_restore_attendee
  *		@param boolean 		$trash - whether to move item to trash (TRUE) or restore it (FALSE)
  *		@access protected
  *		@return void
  */
 protected function _trash_or_restore_attendees($trash = TRUE)
 {
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     $ATT_MDL = EEM_Attendee::instance();
     $success = 1;
     //Checkboxes
     if (!empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
         // if array has more than one element than success message should be plural
         $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
         // cycle thru checkboxes
         while (list($ATT_ID, $value) = each($this->_req_data['checkbox'])) {
             $updated = $trash ? $ATT_MDL->update_by_ID(array('status' => 'trash'), $ATT_ID) : $ATT_MDL->update_by_ID(array('status' => 'publish'), $ATT_ID);
             if (!$updated) {
                 $success = 0;
             }
         }
     } else {
         // grab single id and delete
         $ATT_ID = absint($this->_req_data['ATT_ID']);
         //get attendee
         $att = $ATT_MDL->get_one_by_ID($ATT_ID);
         $updated = $trash ? $att->set_status('trash') : $att->set_status('publish');
         $updated = $att->save();
         if (!$updated) {
             $success = 0;
         }
     }
     $what = $success > 1 ? __('Contacts', 'event_espresso') : __('Contact', 'event_espresso');
     $action_desc = $trash ? __('moved to the trash', 'event_espresso') : __('restored', 'event_espresso');
     $this->_redirect_after_action($success, $what, $action_desc, array('action' => 'contact_list'));
 }
 /**
  * txn_registrant_side_meta_box
  * generates HTML for the Edit Transaction side meta box
  *
  * @access public
  * @throws \EE_Error
  * @return void
  */
 public function txn_registrant_side_meta_box()
 {
     $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->get_first_related('Attendee') : null;
     if (!$primary_att instanceof EE_Attendee) {
         $this->_template_args['no_attendee_message'] = __('There is no attached contact for this transaction.  The transaction either failed due to an error or was abandoned.', 'event_espresso');
         $primary_att = EEM_Attendee::instance()->create_default_object();
     }
     $this->_template_args['ATT_ID'] = $primary_att->ID();
     $this->_template_args['prime_reg_fname'] = $primary_att->fname();
     $this->_template_args['prime_reg_lname'] = $primary_att->lname();
     $this->_template_args['prime_reg_email'] = $primary_att->email();
     $this->_template_args['prime_reg_phone'] = $primary_att->phone();
     $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce(array('action' => 'edit_attendee', 'post' => $primary_att->ID()), REG_ADMIN_URL);
     // get formatted address for registrant
     EE_Registry::instance()->load_helper('Formatter');
     $this->_template_args['formatted_address'] = EEH_Address::format($primary_att);
     echo EEH_Template::display_template(TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', $this->_template_args, TRUE);
 }
 /**
  * 	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);
 }
 /**
  * Gets the string answer to the question for this registration's attendee
  * @param EE_Registration $registration
  * @param int|string $question_system_id if an INT this is understood to be the question's ID; if a string then it should be its QST_system value.
  *	Passing in the QST_system value is more efficient
  * @param boolean $pretty_answer
  * @return string|null (if the registration has no attendee, or the question_system_id is not a QST_ID or QST_system for 
  * a question corresponding to an attendee field, returns null)
  */
 public function get_attendee_property_answer_value(EE_Registration $registration, $question_system_id = NULL, $pretty_answer = FALSE)
 {
     $field_name = NULL;
     $value = NULL;
     //backward compat: we still want to find the question's ID
     if (is_numeric($question_system_id)) {
         //find this question's QST_system value
         $question_id = $question_system_id;
         $question_system_id = EEM_Question::instance()->get_var(array(array('QST_ID' => $question_system_id)), 'QST_system');
     } else {
         $question_id = intval(EEM_Question::instance()->get_var(array(array('QST_system' => $question_system_id)), 'QST_ID'));
     }
     //only bother checking if the registration has an attendee
     if ($registration->attendee() instanceof EE_Attendee) {
         $field_name = EEM_Attendee::instance()->get_attendee_field_for_system_question($question_system_id);
         if ($field_name) {
             if ($pretty_answer) {
                 if ($field_name == 'STA_ID') {
                     $state = $registration->attendee()->state_obj();
                     $value = $state instanceof EE_State ? $state->name() : sprintf(__('Unknown State (%s)', 'event_espresso'), $registration->attendee()->state_ID());
                 } else {
                     if ($field_name == 'CNT_ISO') {
                         $country = $registration->attendee()->country_obj();
                         $value = $country instanceof EE_Country ? $country->name() : sprintf(__('Unknown Country (%s)', "event_espresso"), $registration->attendee()->country_ID());
                     } else {
                         $value = $registration->attendee()->get_pretty($field_name);
                     }
                 }
                 //if field name is blank, leave the value as null too
             } else {
                 $value = $registration->attendee()->get($field_name);
             }
         }
         //if no field was found, leave value blank
     }
     return apply_filters('FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value', $value, $registration, $question_id, $question_system_id);
 }
 /**
  * resets the model and returns it
  * @return EEM_Attendee
  */
 public static function reset()
 {
     self::$_instance = NULL;
     return self::instance();
 }
 /**
  *    _update_existing_attendee_data - in case it has changed since last time they registered for an event
  *
  * @param EE_Attendee     $existing_attendee
  * @param array           $attendee_data
  * @return \EE_Attendee
  */
 private function _update_existing_attendee_data(EE_Attendee $existing_attendee, $attendee_data = array())
 {
     // first remove fname, lname, and email from attendee data
     $dont_set = array('ATT_fname', 'ATT_lname', 'ATT_email');
     // now loop thru what's left and add to attendee CPT
     foreach ($attendee_data as $property_name => $property_value) {
         if (!in_array($property_name, $dont_set) && EEM_Attendee::instance()->has_field($property_name)) {
             $existing_attendee->set($property_name, $property_value);
         }
     }
     // better save that now
     $existing_attendee->save();
     return $existing_attendee;
 }
 /**
  * Handles sending selected registrations/contacts a newsletter.
  *
  * @since  4.3.0
  *
  * @return void
  */
 protected function _newsletter_selected_send()
 {
     $success = TRUE;
     //first we need to make sure we have a GRP_ID so we know what template we're sending and updating!
     if (empty($this->_req_data['newsletter_mtp_selected'])) {
         EE_Error::add_error(__('In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         $success = FALSE;
     }
     if ($success) {
         //update Message template in case there are any changes
         $Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['newsletter_mtp_selected']);
         $Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group ? $Message_Template_Group->context_templates() : array();
         if (empty($Message_Templates)) {
             EE_Error::add_error(__('Unable to retrieve message template fields from the db. Messages not sent.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         }
         //let's just update the specific fields
         foreach ($Message_Templates['attendee'] as $Message_Template) {
             if ($Message_Template instanceof EE_Message_Template) {
                 $field = $Message_Template->get('MTP_template_field');
                 $content = $Message_Template->get('MTP_content');
                 $new_content = $content;
                 switch ($field) {
                     case 'from':
                         $new_content = !empty($this->_req_data['batch_message']['from']) ? $this->_req_data['batch_message']['from'] : $content;
                         break;
                     case 'subject':
                         $new_content = !empty($this->_req_data['batch_message']['subject']) ? $this->_req_data['batch_message']['subject'] : $content;
                         break;
                     case 'content':
                         $new_content = $content;
                         $new_content['newsletter_content'] = !empty($this->_req_data['batch_message']['content']) ? $this->_req_data['batch_message']['content'] : $content['newsletter_content'];
                         break;
                     default:
                         continue;
                         break;
                 }
                 $Message_Template->set('MTP_content', $new_content);
                 $Message_Template->save();
             }
         }
         //great fields are updated!  now let's make sure we just have contact objects (EE_Attendee).
         $id_type = !empty($this->_req_data['batch_message']['id_type']) ? $this->_req_data['batch_message']['id_type'] : 'registration';
         //id_type will affect how we assemble the ids.
         $ids = !empty($this->_req_data['batch_message']['ids']) ? json_decode(stripslashes($this->_req_data['batch_message']['ids'])) : array();
         $registrations_used_for_contact_data = array();
         //using switch because eventually we'll have other contexts that will be used for generating messages.
         switch ($id_type) {
             case 'registration':
                 $registrations_used_for_contact_data = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $ids))));
                 break;
             case 'contact':
                 $registrations_used_for_contact_data = EEM_Registration::instance()->get_latest_registration_for_each_of_given_contacts($ids);
                 break;
         }
         do_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', $registrations_used_for_contact_data, $Message_Template_Group->ID());
         //kept for backward compat, internally we no longer use this action.
         //@deprecated 4.8.36.rc.002
         $contacts = $id_type == 'registration' ? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids) : EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids))));
         do_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send', $contacts, $Message_Template_Group->ID());
     }
     $query_args = array('action' => !empty($this->_req_data['redirect_back_to']) ? $this->_req_data['redirect_back_to'] : 'default');
     $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
 }
 /**
  * Export a custom CSV of registration info including: A bunch of the reg fields, the time of the event, the price name,
  * and the questions associated with the registrations
  * @param int $event_id
  */
 function report_registrations_for_event($event_id = NULL)
 {
     $reg_fields_to_include = array('TXN_ID', 'ATT_ID', 'REG_ID', 'REG_date', 'REG_code', 'REG_count', 'REG_final_price');
     $att_fields_to_include = array('ATT_fname', 'ATT_lname', 'ATT_email', 'ATT_address', 'ATT_address2', 'ATT_city', 'STA_ID', 'CNT_ISO', 'ATT_zip', 'ATT_phone');
     $registrations_csv_ready_array = array();
     $reg_model = EE_Registry::instance()->load_model('Registration');
     $query_params = apply_filters('FHEE__EE_Export__report_registration_for_event', array(array('OR' => array('Transaction.STS_ID' => array('NOT IN', array(EEM_Transaction::failed_status_code, EEM_Transaction::abandoned_status_code)), 'STS_ID' => EEM_Registration::status_id_approved), 'Ticket.TKT_deleted' => array('IN', array(true, false))), 'order_by' => array('Transaction.TXN_ID' => 'asc', 'REG_count' => 'asc'), 'force_join' => array('Transaction', 'Ticket', 'Attendee'), 'caps' => EEM_Base::caps_read_admin), $event_id);
     if ($event_id) {
         $query_params[0]['EVT_ID'] = $event_id;
     } else {
         $query_params['force_join'][] = 'Event';
     }
     $registration_rows = $reg_model->get_all_wpdb_results($query_params);
     //get all questions which relate to someone in this group
     $registration_ids = array();
     foreach ($registration_rows as $reg_row) {
         $registration_ids[] = intval($reg_row['Registration.REG_ID']);
     }
     //		EEM_Question::instance()->show_next_x_db_queries();
     $questions_for_these_regs_rows = EEM_Question::instance()->get_all_wpdb_results(array(array('Answer.REG_ID' => array('IN', $registration_ids))));
     foreach ($registration_rows as $reg_row) {
         if (is_array($reg_row)) {
             $reg_csv_array = array();
             if (!$event_id) {
                 //get the event's name and Id
                 $reg_csv_array[__('Event', 'event_espresso')] = sprintf(__('%1$s (%2$s)', 'event_espresso'), $this->_prepare_value_from_db_for_display(EEM_Event::instance(), 'EVT_name', $reg_row['Event_CPT.post_title']), $reg_row['Event_CPT.ID']);
             }
             $is_primary_reg = $reg_row['Registration.REG_count'] == '1' ? true : false;
             /*@var $reg_row EE_Registration */
             foreach ($reg_fields_to_include as $field_name) {
                 $field = $reg_model->field_settings_for($field_name);
                 if ($field_name == 'REG_final_price') {
                     $value = $this->_prepare_value_from_db_for_display($reg_model, $field_name, $reg_row['Registration.REG_final_price'], 'localized_float');
                 } elseif ($field_name == 'REG_count') {
                     $value = sprintf(__('%s of %s', 'event_espresso'), $this->_prepare_value_from_db_for_display($reg_model, 'REG_count', $reg_row['Registration.REG_count']), $this->_prepare_value_from_db_for_display($reg_model, 'REG_group_size', $reg_row['Registration.REG_group_size']));
                 } elseif ($field_name == 'REG_date') {
                     $value = $this->_prepare_value_from_db_for_display($reg_model, $field_name, $reg_row['Registration.REG_date'], 'no_html');
                 } else {
                     $value = $this->_prepare_value_from_db_for_display($reg_model, $field_name, $reg_row[$field->get_qualified_column()]);
                 }
                 $reg_csv_array[$this->_get_column_name_for_field($field)] = $value;
                 if ($field_name == 'REG_final_price') {
                     //add a column named Currency after the final price
                     $reg_csv_array[__("Currency", "event_espresso")] = EE_Config::instance()->currency->code;
                 }
             }
             //get pretty status
             $stati = EEM_Status::instance()->localized_status(array($reg_row['Registration.STS_ID'] => __('unknown', 'event_espresso'), $reg_row['TransactionTable.STS_ID'] => __('unknown', 'event_espresso')), FALSE, 'sentence');
             $reg_csv_array[__("Registration Status", 'event_espresso')] = $stati[$reg_row['Registration.STS_ID']];
             //get pretty trnasaction status
             $reg_csv_array[__("Transaction Status", 'event_espresso')] = $stati[$reg_row['TransactionTable.STS_ID']];
             $reg_csv_array[__('Transaction Amount Due', 'event_espresso')] = $is_primary_reg ? $this->_prepare_value_from_db_for_display(EEM_Transaction::instance(), 'TXN_total', $reg_row['TransactionTable.TXN_total'], 'localized_float') : '0.00';
             $reg_csv_array[__('Amount Paid', 'event_espresso')] = $is_primary_reg ? $this->_prepare_value_from_db_for_display(EEM_Transaction::instance(), 'TXN_paid', $reg_row['TransactionTable.TXN_paid'], 'localized_float') : '0.00';
             $payment_methods = array();
             $gateway_txn_ids_etc = array();
             $payment_times = array();
             if ($is_primary_reg && $reg_row['TransactionTable.TXN_ID']) {
                 $payments_info = EEM_Payment::instance()->get_all_wpdb_results(array(array('TXN_ID' => $reg_row['TransactionTable.TXN_ID'], 'STS_ID' => EEM_Payment::status_id_approved), 'force_join' => array('Payment_Method')), ARRAY_A, 'Payment_Method.PMD_admin_name as name, Payment.PAY_txn_id_chq_nmbr as gateway_txn_id, Payment.PAY_timestamp as payment_time');
                 foreach ($payments_info as $payment_method_and_gateway_txn_id) {
                     $payment_methods[] = isset($payment_method_and_gateway_txn_id['name']) ? $payment_method_and_gateway_txn_id['name'] : __('Unknown', 'event_espresso');
                     $gateway_txn_ids_etc[] = isset($payment_method_and_gateway_txn_id['gateway_txn_id']) ? $payment_method_and_gateway_txn_id['gateway_txn_id'] : '';
                     $payment_times[] = isset($payment_method_and_gateway_txn_id['payment_time']) ? $payment_method_and_gateway_txn_id['payment_time'] : '';
                 }
             }
             $reg_csv_array[__('Payment Date(s)', 'event_espresso')] = implode(',', $payment_times);
             $reg_csv_array[__('Payment Method(s)', 'event_espresso')] = implode(",", $payment_methods);
             $reg_csv_array[__('Gateway Transaction ID(s)', 'event_espresso')] = implode(',', $gateway_txn_ids_etc);
             //get whether or not the user has checked in
             $reg_csv_array[__("Check-Ins", "event_espresso")] = $reg_model->count_related($reg_row['Registration.REG_ID'], 'Checkin');
             //get ticket of registration and its price
             $ticket_model = EE_Registry::instance()->load_model('Ticket');
             if ($reg_row['Ticket.TKT_ID']) {
                 $ticket_name = $this->_prepare_value_from_db_for_display($ticket_model, 'TKT_name', $reg_row['Ticket.TKT_name']);
                 $datetimes_strings = array();
                 foreach (EEM_Datetime::instance()->get_all_wpdb_results(array(array('Ticket.TKT_ID' => $reg_row['Ticket.TKT_ID']), 'order_by' => array('DTT_EVT_start' => 'ASC'), 'default_where_conditions' => 'none')) as $datetime) {
                     $datetimes_strings[] = $this->_prepare_value_from_db_for_display(EEM_Datetime::instance(), 'DTT_EVT_start', $datetime['Datetime.DTT_EVT_start']);
                 }
             } else {
                 $ticket_name = __('Unknown', 'event_espresso');
                 $datetimes_strings = array(__('Unknown', 'event_espresso'));
             }
             $reg_csv_array[$ticket_model->field_settings_for('TKT_name')->get_nicename()] = $ticket_name;
             $reg_csv_array[__("Datetimes of Ticket", "event_espresso")] = implode(", ", $datetimes_strings);
             //get datetime(s) of registration
             //add attendee columns
             foreach ($att_fields_to_include as $att_field_name) {
                 $field_obj = EEM_Attendee::instance()->field_settings_for($att_field_name);
                 if ($reg_row['Attendee_CPT.ID']) {
                     if ($att_field_name == 'STA_ID') {
                         $value = EEM_State::instance()->get_var(array(array('STA_ID' => $reg_row['Attendee_Meta.STA_ID'])), 'STA_name');
                     } elseif ($att_field_name == 'CNT_ISO') {
                         $value = EEM_Country::instance()->get_var(array(array('CNT_ISO' => $reg_row['Attendee_Meta.CNT_ISO'])), 'CNT_name');
                     } else {
                         $value = $this->_prepare_value_from_db_for_display(EEM_Attendee::instance(), $att_field_name, $reg_row[$field_obj->get_qualified_column()]);
                     }
                 } else {
                     $value = '';
                 }
                 $reg_csv_array[$this->_get_column_name_for_field($field_obj)] = $value;
             }
             //make sure each registration has the same questions in the same order
             foreach ($questions_for_these_regs_rows as $question_row) {
                 if (!isset($reg_csv_array[$question_row['Question.QST_admin_label']])) {
                     $reg_csv_array[$question_row['Question.QST_admin_label']] = null;
                 }
             }
             //now fill out the questions THEY answered
             foreach (EEM_Answer::instance()->get_all_wpdb_results(array(array('REG_ID' => $reg_row['Registration.REG_ID']), 'force_join' => array('Question'))) as $answer_row) {
                 /* @var $answer EE_Answer */
                 if ($answer_row['Question.QST_ID']) {
                     $question_label = $this->_prepare_value_from_db_for_display(EEM_Question::instance(), 'QST_admin_label', $answer_row['Question.QST_admin_label']);
                 } else {
                     $question_label = sprintf(__('Question $s', 'event_espresso'), $answer_row['Answer.QST_ID']);
                 }
                 if (isset($answer_row['Question.QST_type']) && $answer_row['Question.QST_type'] == EEM_Question::QST_type_state) {
                     $reg_csv_array[$question_label] = EEM_State::instance()->get_state_name_by_ID($answer_row['Answer.ANS_value']);
                 } else {
                     $reg_csv_array[$question_label] = $this->_prepare_value_from_db_for_display(EEM_Answer::instance(), 'ANS_value', $answer_row['Answer.ANS_value']);
                 }
             }
             $registrations_csv_ready_array[] = apply_filters('FHEE__EE_Export__report_registrations__reg_csv_array', $reg_csv_array, $reg_row);
         }
     }
     //if we couldn't export anything, we want to at least show the column headers
     if (empty($registrations_csv_ready_array)) {
         $reg_csv_array = array();
         $model_and_fields_to_include = array('Registration' => $reg_fields_to_include, 'Attendee' => $att_fields_to_include);
         foreach ($model_and_fields_to_include as $model_name => $field_list) {
             $model = EE_Registry::instance()->load_model($model_name);
             foreach ($field_list as $field_name) {
                 $field = $model->field_settings_for($field_name);
                 $reg_csv_array[$this->_get_column_name_for_field($field)] = null;
                 //$registration->get($field->get_name());
             }
         }
         $registrations_csv_ready_array[] = $reg_csv_array;
     }
     if ($event_id) {
         $event_slug = EEM_Event::instance()->get_var(array(array('EVT_ID' => $event_id)), 'EVT_slug');
         if (!$event_slug) {
             $event_slug = __('unknown', 'event_espresso');
         }
     } else {
         $event_slug = __('all', 'event_espresso');
     }
     $filename = sprintf("registrations-for-%s", $event_slug);
     $handle = $this->EE_CSV->begin_sending_csv($filename);
     $this->EE_CSV->write_data_array_to_csv($handle, $registrations_csv_ready_array);
     $this->EE_CSV->end_sending_csv($handle);
 }
 /**
  * Returns whether or not a message of that type was sent for a given attendee.
  * @param EE_Attendee|int $attendee
  * @param string $message_type the message type slug
  * @return boolean
  */
 public function message_sent_for_attendee($attendee, $message_type)
 {
     $attendee_ID = EEM_Attendee::instance()->ensure_is_ID($attendee);
     return $this->exists(array(array('Attendee.ATT_ID' => $attendee_ID, 'MSG_message_type' => $message_type, 'STS_ID' => array('IN', $this->stati_indicating_sent()))));
 }