/**
 * Get event attendees, returns null on error
 */
function ikit_event_get_attendees($event_id, $event_service)
{
    if ($event_service == IKIT_EVENT_SERVICE_ETOUCHES) {
        $attendees = ikit_event_etouches_remote_fetch_attendees($event_id);
        // XXX, etouches treats no attendees as an error, so in that specific case do not raise exception.
        if (isset($attendees->error) && $attendees->error->data != 'No attendees found.') {
            return null;
        } else {
            $attendees = $attendees->item;
        }
        $valid_attendees = array();
        // Remove guest and duplicate attendees from the list
        foreach ($attendees as $attendee) {
            if (empty($attendee->name) == false) {
                $attendee_name = (string) $attendee->name;
                if (in_array($attendee_name, $valid_attendees) == false) {
                    // Only include confirmed attendees
                    if ($attendee->status == 'Confirmed' || $attendee->status == 'Attended' || $attendee->status == 'No Show' || $attendee->status == 'Activated') {
                        array_push($valid_attendees, $attendee_name);
                    }
                }
            }
        }
        // Sort thte attendees based on last name, because etouches
        // just returns a name field, we need to parse the names
        $valid_attendees_for_sort = array();
        foreach ($valid_attendees as $valid_attendee) {
            $valid_attendee_exploded = explode(" ", $valid_attendee);
            if (count($valid_attendee_exploded) >= 2) {
                // Valid names are space separated
                // Find last valid exploded item, this accounts for empty exploded items
                $attendee_last_name = "";
                foreach ($valid_attendee_exploded as $valid_attendee_exploded_item) {
                    if (empty($valid_attendee_exploded_item) == false) {
                        $attendee_last_name = $valid_attendee_exploded_item;
                    }
                }
                $valid_attendees_for_sort[$attendee_last_name . ' ' . $valid_attendee] = $valid_attendee;
            }
        }
        ksort($valid_attendees_for_sort);
        $valid_attendees = array();
        foreach ($valid_attendees_for_sort as $key => $value) {
            array_push($valid_attendees, $valid_attendees_for_sort[$key]);
        }
        return $valid_attendees;
    } else {
        if ($event_service == IKIT_EVENT_SERVICE_EVENTBRITE) {
            $attendees = ikit_event_eventbrite_remote_fetch_attendees(ikit_event_eventbrite_get_client(), $event_id);
            $valid_attendees = array();
            foreach ($attendees as $attendee) {
                array_push($valid_attendees, $attendee->attendee->first_name . ' ' . $attendee->attendee->last_name);
            }
            return $valid_attendees;
        }
    }
}
/**
 * Redirect to Eventbrite given the SSO key, used for auto-generated tickets
 */
function ikit_event_eventbrite_sso_redirect($ikit_event_id, $sso_key)
{
    if (empty($sso_key) == false) {
        // Get event meta
        $ikit_event_meta = ikit_event_get_meta($ikit_event_id);
        // Pull member type
        $xml = ikit_sso_get_user($sso_key);
        // Use access code
        $sync_data = unserialize(get_post_meta($ikit_event_id, IKIT_CUSTOM_FIELD_IKIT_EVENT_EVENTBRITE_SYNC_DATA, true));
        // Determine member type
        $member_type = trim((string) $xml->Name->MEMBER_TYPE);
        $attribute1 = (string) $xml->Name->ATTRIBUTE1;
        // Students use a special attribute
        if ($attribute1 == '1STUDENT' && empty($sync_data[IKIT_MEMBER_TYPE_STUDENT]) == false) {
            // Determine if student ticket still exists
            $student_eventbrite_ticket_id = $sync_data[IKIT_MEMBER_TYPE_STUDENT]['id'];
            $student_eventbrite_ticket_exists = false;
            $eb_client = ikit_event_eventbrite_get_client();
            if ($eb_client != null) {
                $ikit_event_meta = ikit_event_get_meta($ikit_event_id);
                $event_id = $ikit_event_meta->id;
                $eventbrite_event = ikit_event_eventbrite_get_event($eb_client, $ikit_event_meta->id);
                $eventbrite_tickets = $eventbrite_event->tickets;
                foreach ($eventbrite_tickets as $eventbrite_ticket) {
                    $eventbrite_ticket_id = $eventbrite_ticket->ticket->id;
                    if ($student_eventbrite_ticket_id == $eventbrite_ticket_id) {
                        $student_eventbrite_ticket_exists = true;
                        break;
                    }
                }
            }
            // If the student ticket still exists, we show the student the student ticket
            // otherwise just show their member ticket
            if ($student_eventbrite_ticket_exists) {
                $member_type = 'STUDENT';
            }
        }
        $ticket = $sync_data[$member_type];
        // XXX Legacy events pre-20131125 used the missing constant name, TODO fix via update
        if (empty($ticket) && $member_type == 'SUP') {
            $ticket = $sync_data['IKIT_MEMBER_TYPE_SUPPORTER'];
        }
        $code = "";
        if (empty($ticket) == false) {
            $code = $ticket['access_code']['code'];
        }
        $eventbrite_registration_url = $ikit_event_meta->url . '&discount=' . $code;
        // Do a server redirect so users do not see a page load
        wp_redirect($eventbrite_registration_url);
        exit;
    }
}