Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created April 1, 2014 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/9918378 to your computer and use it in GitHub Desktop.
Save joshfeck/9918378 to your computer and use it in GitHub Desktop.
Compatibility fix for servers running PHP5.2. Fixes an issue with exporting an Event Espresso attendee list if using ticketing on a server running PHP 5.2. Upload this file to /wp-content/uploads/espresso/
<?php
class EEDateTime extends DateTime
{
public static function createFromFormat($format, $time, $timezone = null)
{
if(!$timezone) $timezone = new DateTimeZone(date_default_timezone_get());
$version = explode('.', phpversion());
if(((int)$version[0] >= 5 && (int)$version[1] >= 2 && (int)$version[2] > 17)){
return parent::createFromFormat($format, $time, $timezone);
}
return new DateTime(date($format, strtotime($time)), $timezone);
}
}
function espresso_export_stuff() {
global $wpdb, $ticketing_installed;
$today = date("Y-m-d-Hi", time());
$export_all_events = isset($_REQUEST['all_events']) && $_REQUEST['all_events'] == "true" ? TRUE : FALSE;
//Export data to Excel file
if (isset($_REQUEST['export'])) {
switch ($_REQUEST['export']) {
case "report":
$event_id = isset($_REQUEST['event_id']) ? $_REQUEST['event_id'] : FALSE;
// export for one event only ?
if ($event_id) {
$SQL = "SELECT event_name, event_desc, event_identifier, question_groups, event_meta FROM " . EVENTS_DETAIL_TABLE;
$SQL .= " WHERE id = %d";
if ($results = $wpdb->get_row($wpdb->prepare($SQL, $event_id), ARRAY_N)) {
list( $event_name, $event_description, $event_identifier, $question_groups, $event_meta) = $results;
$question_groups = maybe_unserialize($question_groups);
$event_meta = maybe_unserialize($event_meta);
if (!empty($event_meta['add_attendee_question_groups'])) {
$question_groups = array_unique(array_merge((array) $question_groups, (array) $event_meta['add_attendee_question_groups']));
}
}
} else {
// export for ALL EVENTS
$question_groups = array();
$event_meta = array();
$SQL = "SELECT event_name, event_desc, event_identifier, question_groups, event_meta FROM " . EVENTS_DETAIL_TABLE;
if ($results = $wpdb->get_results($SQL, ARRAY_N)) {
foreach ($results as $result) {
list( $event_name, $event_description, $event_identifier, $q_groups, $e_meta) = $result;
$question_groups = array_unique(array_merge($question_groups, (array) maybe_unserialize($q_groups)));
$e_meta = (array) maybe_unserialize($e_meta);
$event_meta = array_unique(array_merge($event_meta, (array) $e_meta['add_attendee_question_groups']));
}
}
}
$basic_header = array(
__('Group', 'event_espresso'), // column # A
__('ID', 'event_espresso'), // B
__('Reg ID', 'event_espresso'), // C
__('Payment Method', 'event_espresso'), // D
__('Reg Date', 'event_espresso'), // E
__('Pay Status', 'event_espresso'), // F
__('Type of Payment', 'event_espresso'), // G
__('Transaction ID', 'event_espresso'), // H
__('Price', 'event_espresso'), // I
__('Coupon Code', 'event_espresso'), // J
__('# Attendees', 'event_espresso'), // K
__('Amount Paid', 'event_espresso'), // L
__('Date Paid', 'event_espresso'), // M
__('Event Name', 'event_espresso'), // N
__('Price Option', 'event_espresso'), // O
__('Event Date', 'event_espresso'), // P
__('Event Time', 'event_espresso'), // Q
);
if ( $ticketing_installed ) {
$basic_header[] = __('Website Check-in', 'event_espresso'); // R
$basic_header[] = __('Tickets Scanned', 'event_espresso'); // S
$basic_header[] = __('Check-in Date', 'event_espresso'); // T
}
$basic_header[] = __('Seat Tag', 'event_espresso'); // R or U
$basic_header[] = __('First Name', 'event_espresso'); // S or V
$basic_header[] = __('Last Name', 'event_espresso'); // T or W
$basic_header[] = __('Email', 'event_espresso'); // U or X
$question_groups = maybe_unserialize($question_groups);
$event_meta = maybe_unserialize($event_meta);
if (isset($event_meta['add_attendee_question_groups'])) {
// if ( is_serialized( $event_meta['add_attendee_question_groups'] ) ){
// $add_attendee_question_groups = unserialize($event_meta['add_attendee_question_groups']);
// } else {
// $add_attendee_question_groups = $event_meta['add_attendee_question_groups'];
// }
if (!empty($add_attendee_question_groups)) {
$question_groups = array_unique(array_merge((array) $question_groups, (array) $event_meta['add_attendee_question_groups']));
}
}
switch ($_REQUEST['action']) {
case "event":
espresso_event_export($event_name);
break;
case "payment":
$question_list = array(); //will be used to associate questions with correct answers
$question_filter = array(); //will be used to keep track of newly added and deleted questions
if (count($question_groups) > 0) {
$question_sequence = array();
$questions_in = '';
foreach ($question_groups as $g_id) {
$questions_in .= $g_id . ',';
}
$questions_in = substr($questions_in, 0, -1);
$group_name = '';
$counter = 0;
$quest_sql = "SELECT q.id, q.question FROM " . EVENTS_QUESTION_TABLE . " q ";
$quest_sql .= " JOIN " . EVENTS_QST_GROUP_REL_TABLE . " qgr on q.id = qgr.question_id ";
$quest_sql .= " JOIN " . EVENTS_QST_GROUP_TABLE . " qg on qg.id = qgr.group_id ";
$quest_sql .= " WHERE qgr.group_id in ( $questions_in ) ";
if (function_exists('espresso_member_data') && ( espresso_member_data('role') == 'espresso_event_manager')) {
$quest_sql .= " AND qg.wp_user = '" . espresso_member_data('id') . "' ";
}
//Fix from Jesse in the forums (http://eventespresso.com/forums/2010/10/form-questions-appearing-in-wrong-columns-in-excel-export/)
//$quest_sql .= " AND q.system_name is null ORDER BY qg.id, q.id ASC ";
//$quest_sql .= " AND q.system_name is null ";
$quest_sql .= " ORDER BY q.sequence, q.id ASC ";
$questions = $wpdb->get_results($quest_sql);
$ignore = array('1'=>1, '2'=>2, '3'=>3);
$num_rows = $wpdb->num_rows;
if ($num_rows > 0) {
foreach ($questions as $question) {
if (!isset($ignore[$question->id])) {
$question_list[$question->id] = $question->question;
$question_filter[$question->id] = $question->id;
$question_text = escape_csv_val( stripslashes( $question->question ));
if ( ! in_array( $question_text, $basic_header )) {
array_push( $basic_header, $question_text );
}
}
}
}
}
if (count($question_filter) > 0) {
$question_filter = implode(",", $question_filter);
}
//$question_filter = str_replace( array( '1,','2,','3,' ), '', $question_filter );
$sql = '';
$espresso_member = function_exists('espresso_member_data') && espresso_member_data('role') == 'espresso_group_admin' ? TRUE : FALSE;
if ($espresso_member) {
$group = get_user_meta(espresso_member_data('id'), "espresso_group", true);
$group = maybe_unserialize($group);
$group = implode(",", $group);
$sql .= "(SELECT ed.event_name, ed.start_date, a.id AS att_id, a.registration_id, a.payment, a.date, a.payment_status, a.txn_type, a.txn_id";
$sql .= ", a.amount_pd, a.quantity, a.coupon_code";
$sql .= $ticketing_installed == true ? ", a.checked_in, a.checked_in_quantity, ac.date_scanned" : '';
$sql .= ", a.payment_date, a.event_time, a.price_option, a.final_price a_final_price, a.quantity a_quantity, a.fname, a.lname, a.email";
$sql .= " FROM " . EVENTS_ATTENDEE_TABLE . " a ";
$sql .= " JOIN " . EVENTS_DETAIL_TABLE . " ed ON ed.id=a.event_id ";
$sql .= $ticketing_installed == true ? " LEFT JOIN " . $wpdb->prefix . "events_attendee_checkin ac ON a.id=ac.attendee_id " : '';
if ($group != '') {
$sql .= " JOIN " . EVENTS_VENUE_REL_TABLE . " r ON r.event_id = ed.id ";
$sql .= " JOIN " . EVENTS_LOCALE_REL_TABLE . " l ON l.venue_id = r.venue_id ";
}
$sql .= $event_id ? " WHERE ed.id = '" . $event_id . "' " : '';
$sql .= $group != '' ? " AND l.locale_id IN (" . $group . ") " : '';
$sql .= ") UNION (";
}
$sql .= "SELECT ed.event_name, ed.start_date, a.id AS att_id, a.registration_id, a.payment, a.date, a.payment_status, a.txn_type, a.txn_id";
$sql .= ", a.quantity, a.coupon_code, a.final_price a_final_price, a.amount_pd, a.quantity a_quantity";
$sql .= $ticketing_installed == true ? ", a.checked_in, a.checked_in_quantity, ac.date_scanned" : '';
$sql .= ", a.payment_date, a.event_time, a.price_option, a.fname, a.lname, a.email";
$sql .= " FROM " . EVENTS_ATTENDEE_TABLE . " a ";
$sql .= " JOIN " . EVENTS_DETAIL_TABLE . " ed ON ed.id=a.event_id ";
$sql .= $ticketing_installed == true ? " LEFT JOIN " . $wpdb->prefix . "events_attendee_checkin ac ON a.id=ac.attendee_id " : '';
//$sql .= " JOIN " . EVENTS_ATTENDEE_COST_TABLE . " ac ON a.id=ac.attendee_id ";
$sql .= $event_id ? " WHERE ed.id = '" . $event_id . "' " : '';
$sql .= apply_filters('filter_hook_espresso_export_payments_query_where', '');
if (function_exists('espresso_member_data') && ( espresso_member_data('role') == 'espresso_event_manager' || espresso_member_data('role') == 'espresso_group_admin')) {
$sql .= " AND ed.wp_user = '" . espresso_member_data('id') . "' ";
}
$sql .= $espresso_member ? ") ORDER BY att_id " : " ORDER BY a.id ";
$participants = $wpdb->get_results($sql);
$rebuild_participants = array();
foreach ($participants as $participant) {
$rebuild_participants[$participant->att_id] = $participant;
}
$participants = $rebuild_participants;
$filename = ( isset($_REQUEST['all_events']) && $_REQUEST['all_events'] == "true" ) ? __('all-events', 'event_espresso') : $event_name;
$filename = sanitize_title_with_dashes($filename) . "-" . $today;
switch ($_REQUEST['type']) {
case "csv" :
$st = "";
$et = ",";
$s = $et . $st;
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=" . $filename . ".csv");
//header("Content-Disposition: attachment; filename='" .$filename .".csv'");
header("Pragma: no-cache");
header("Expires: 0");
//echo header
echo implode($s, $basic_header) . "\r\n";
break;
default :
$st = "";
$et = "\t";
$s = $et . $st;
header("Content-Disposition: attachment; filename=" . $filename . ".xls");
//header("Content-Disposition: attachment; filename='" .$filename .".xls'");
header("Content-Type: application/vnd.ms-excel");
header("Pragma: no-cache");
header("Expires: 0");
//echo header
echo implode($s, $basic_header) . $et . "\r\n";
break;
}
if ($participants) {
$temp_reg_id = ''; //will temporarily hold the registration id for checking with the next row
$attendees_group = ''; //will hold the names of the group members
$group_counter = 1;
$amount_pd = 0;
foreach ($participants as $participant) {
if ($temp_reg_id == '') {
$temp_reg_id = $participant->registration_id;
$amount_pd = $participant->amount_pd;
}
if ($temp_reg_id == $participant->registration_id) {
//Do nothing
} else {
$group_counter++;
$temp_reg_id = $participant->registration_id;
}
$attendees_group = "Group $group_counter";
//Build the seating assignment
$seatingchart_tag = '';
if (defined("ESPRESSO_SEATING_CHART")) {
if (class_exists("seating_chart")) {
if (seating_chart::check_event_has_seating_chart($event_id)) {
$rs = $wpdb->get_row("select scs.* from " . EVENTS_SEATING_CHART_EVENT_SEAT_TABLE . " sces inner join " . EVENTS_SEATING_CHART_SEAT_TABLE . " scs on sces.seat_id = scs.id where sces.attendee_id = " . $participant->att_id);
if ($rs !== NULL) {
$participant->seatingchart_tag = $rs->custom_tag . " " . $rs->seat . " " . $rs->row;
}
}
}
} else {
$participant->seatingchart_tag = '';
}
if(!empty($participant->date_scanned)) {
$scanned_date_object = EEDateTime::createFromFormat('Y-m-d H:i:s', $participant->date_scanned);
$scanned_date = $scanned_date_object->format(get_option('date_format') . ' ' . get_option('time_format'));
} else {
$scanned_date = "";
}
echo $attendees_group // column # A
. $s . escape_csv_val($participant->att_id) // B
. $s . escape_csv_val($participant->registration_id) // C
. $s . escape_csv_val(stripslashes($participant->payment)) // D
. $s . escape_csv_val(stripslashes(event_date_display($participant->date, get_option('date_format')))) //E
. $s . escape_csv_val(stripslashes($participant->payment_status)) // F
. $s . escape_csv_val(stripslashes($participant->txn_type)) // G
. $s . escape_csv_val(stripslashes($participant->txn_id)) // H
. $s . escape_csv_val($participant->a_final_price * $participant->a_quantity) // I
. $s . escape_csv_val($participant->coupon_code) // J
. $s . escape_csv_val($participant->quantity) // K
. $s . escape_csv_val($participant->amount_pd) // L
. $s . escape_csv_val(event_date_display($participant->payment_date, get_option('date_format'))) // M
. $s . escape_csv_val($participant->event_name) // N
. $s . escape_csv_val($participant->price_option) // O
. $s . escape_csv_val(event_date_display($participant->start_date, get_option('date_format'))) // P
. $s . escape_csv_val(event_date_display($participant->event_time, get_option('time_format'))) // Q
;
if ( $ticketing_installed == true ) {
echo $s . escape_csv_val($participant->checked_in ? "Yes" : "No") // R
. $s . escape_csv_val($participant->checked_in_quantity) // S
. $s . escape_csv_val($scanned_date); // T
}
echo $s . escape_csv_val($participant->seatingchart_tag) // R or U
. $s . escape_csv_val($participant->fname) // S or V
. $s . escape_csv_val($participant->lname) // T or W
. $s . escape_csv_val($participant->email) // U or X
;
if ( ! empty( $question_filter )) {
$SQL = "SELECT question_id, answer FROM " . EVENTS_ANSWER_TABLE . " ";
$SQL .= "WHERE question_id IN ($question_filter) AND attendee_id = %d";
$answers = $wpdb->get_results($wpdb->prepare($SQL, $participant->att_id), OBJECT_K);
} else {
$answers = array();
}
foreach ($question_list as $k => $v) {
// in case the event organizer removes a question from a question group,
// the orphaned answers will remian in the answers table. This check will make sure they don't get exported.
$search = array("\r", "\n", "\t");
if (isset($answers[$k])) {
$clean_answer = str_replace($search, " ", $answers[$k]->answer);
$clean_answer = stripslashes(str_replace("&#039;", "'", trim($clean_answer)));
$clean_answer = escape_csv_val($clean_answer);
echo $s . $clean_answer;
} else {
echo $s;
}
}
switch ($_REQUEST['type']) {
case "csv" :
echo "\r\n";
break;
default :
echo $et . "\r\n";
break;
}
}
} else {
echo __('No participant data has been collected.', 'event_espresso');
}
exit;
break;
default:
echo '<p>' . __('This Is Not A Valid Selection!', 'event_espresso') . '</p>';
break;
}
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment