/**
         * Displays various panels for the admin registration. If there is an ID set
         * in the URL, it will display a single registration record based on that ID.
         * Otherwise, it will display all the registration records in a table.
         */
        function registrations_display()
        {
            // if id is set in the URL, we need to view the registration with that ID.
            if ($id = @$_GET['id']) {
                $registration = thatcamp_registrations_get_registration_by_id($id);
                $applicant = thatcamp_registrations_get_applicant_info($registration);
                $applicantUser = 0;
                if (($userId = email_exists($applicant->user_email)) && is_user_member_of_blog($userId)) {
                    $applicantUser = 1;
                }
                if (isset($_POST['update_status'])) {
                    thatcamp_registrations_process_registration($_GET['id'], $_POST['status']);
                    // If this is an approval, and if the user_account flag is set to 1,
                    // attempt to create a new WP user (or associate an existing one)
                    // with this registration
                    if (isset($_POST['user_account']) && $_POST['user_account'] == 1 && 'approved' == $_POST['status']) {
                        thatcamp_registrations_process_user($id);
                    }
                    wp_redirect(get_admin_url() . 'admin.php?page=thatcamp-registrations&applicant_saved=1');
                }
            }
            ?>
    <style type="text/css" media="screen">
        #thatcamp-registrations-panel {
            background: #fff;
        	margin: 25px 15px 25px 15px;
        	padding: 20px;
        	-moz-border-radius: 6px;
        	-webkit-border-radius: 6px;
        	border-radius: 6px;
        	-moz-box-shadow: #ddd 0 -1px 10px;
        	-webkit-box-shadow: #ddd 0 -1px 10px;
        	-khtml-box-shadow: #ddd 0 -1px 10px;
        	box-shadow: #ddd 0 -1px 10px;
        	color: #555;
        	overflow: hidden;

        }

        #thatcamp-registrations-applicant-info th,
        #thatcamp-registrations-applicant-info td {
            border-bottom: 1px dotted #ddd;
            line-height: 2em;
        }
        #thatcamp-registrations-applicant-info th {
            width: 20%;
        }

        #thatcamp-registrations-list-link {
            display:block;
            float:right;
            width: 20%;
            background: #eee;
            color: #333;
            text-decoration:none;
            text-align:center;
            padding: 10px 20px;
            border:1px solid #ddd;
            -moz-border-radius: 6px;
        	-webkit-border-radius: 6px;
        	border-radius: 6px;
        }
        #thatcamp-registrations-list-link:link,
        #thatcamp-registrations-list-link:visited {
            color: #21759B;
        }
        #thatcamp-registrations-list-link:hover,
        #thatcamp-registrations-list-link:active {
            color: #D54E21;
            background: #f9f9f9;
        }

        h2, h3 {
        margin-bottom: .25em;
        }
    </style>
        <div class="wrap">
            <h2><?php 
            echo _e('THATCamp Registrations');
            ?>
</h2>
            <?php 
            if ($id) {
                ?>
            <div id="thatcamp-registrations-panel">
                <a id="thatcamp-registrations-list-link" href="admin.php?page=thatcamp-registrations">Back to registrations list</a>

                <h2>Registration from <?php 
                echo $applicant->first_name;
                ?>
 <?php 
                echo $applicant->last_name;
                ?>
 (<?php 
                echo $applicant->user_email;
                ?>
)</h2>

               <form action="admin.php?page=thatcamp-registrations&amp;id=<?php 
                echo $id;
                ?>
&amp;noheader=true" method="post">
                    <h3>Registration Status</h3>

                    <select name="status">
                        <option name="pending" id="pending" value="pending"<?php 
                if ($registration->status == "pending") {
                    echo ' selected="selected"';
                }
                ?>
><?php 
                _e('Pending', 'thatcamp-registrations');
                ?>
 </option>
                        <option name="approved" id="approved" value="approved"<?php 
                if ($registration->status == "approved") {
                    echo ' selected="selected"';
                }
                ?>
><?php 
                _e('Approved', 'thatcamp-registrations');
                ?>
 </option>
                        <option name="rejected" id="rejected" value="rejected"<?php 
                if ($registration->status == "rejected") {
                    echo ' selected="selected"';
                }
                ?>
><?php 
                _e('Rejected', 'thatcamp-registrations');
                ?>
 </option>
                    </select>

                    <input type="submit" name="update_status" value="Update Status">

                    <span style="display:none;">
                    <select name="user_account">
                        <option value="0">No</option>
                        <option value="1"<?php 
                if ($applicantUser == 1) {
                    echo ' selected="selected"';
                }
                ?>
Yes</option>
                    </select>
                    </span>

                    <h3>Applicant Site User Account</h3>
                    <p class="description"><?php 
                if ($applicantUser == 1) {
                    echo 'Applicant has a user account on this site.';
                }
                ?>
</p>
                    <p class="description"><?php 
                if ($applicantUser == 0) {
                    echo 'Applicant does not have a user account on this site. If your THATCamp Registrations settings allow it, changing the applicant\'s status to Approved will add the applicant to this site as a user.';
                }
                ?>
</p>
                </form>

                	<h3>Registration Text</h3>
                	<?php 
                echo $registration->application_text;
                ?>

                	<?php 
                if (!empty($registration->additional_information)) {
                    echo $registration->additional_information;
                }
                ?>

        <h3>Biography</h3>
		<?php 
                echo $applicant->description;
                ?>

		<h3>Website</h3>
		<?php 
                echo $applicant->user_url;
                ?>

		<h3>Twitter</h3>
		<?php 
                echo $applicant->user_twitter;
                ?>

		<h3>Title</h3>
		<?php 
                echo $applicant->user_title;
                ?>

		<h3>Organization</h3>
		<?php 
                echo $applicant->user_organization;
                ?>

		<h3><?php 
                _e('Discipline', 'thatcamp-registrations');
                ?>
</h3>
		<?php 
                echo $applicant->discipline;
                ?>
		   	
 		<h3><?php 
                _e('Days Attending', 'thatcamp-registrations');
                ?>
</h3>
		<?php 
                echo $applicant->days_attending;
                ?>

		<h3><?php 
                _e('Technology Skill Level', 'thatcamp-registrations');
                ?>
</h3>
		<?php 
                echo $applicant->technology_skill_level;
                ?>

		<h3><?php 
                _e('T-shirt Size', 'thatcamp-registrations');
                ?>
</h3>
		<?php 
                echo $applicant->tshirt_size;
                ?>

    	<h3><?php 
                _e('Children', 'thatcamp-registrations');
                ?>
</h3>
    	<?php 
                if ($children = $applicant->children) {
                    echo $children;
                } else {
                    echo '0';
                }
                ?>

		<h3><?php 
                _e('Particular Needs', 'thatcamp-registrations');
                ?>
</h3>
    	<?php 
                echo $applicant->particular_needs;
                ?>
    	
    

    
</div>
            <?php 
                // Otherwise, we need to view the list of registrations.
            } else {
                ?>

            <?php 
                /*
                            Get list of registrations. Sort by:
                1. All registrations
                            2. Pending registrations
                            3. Approved registrations
                            4. Rejected registrations
                List needs a bulk action to change status of checked registrations.
                */
                ?>

            <?php 
                $options = get_option('thatcamp_registrations_options');
                if (empty($options)) {
                    ?>
            <div class="updated">
                <p><?php 
                    _e('You have not updated your THATCamp Registrations settings.');
                    ?>
 <a href="admin.php?page=thatcamp-registrations-settings"><?php 
                    _e('Update your settings.');
                    ?>
</a></p>
            </div>
            <?php 
                }
                ?>

	    <?php 
                if (!empty($_GET['success'])) {
                    ?>
		<div class="updated">
		<?php 
                    switch ($_GET['success']) {
                        case 'approved':
                            $message = __('Successfully approved!', 'thatcamp-registrations');
                            break;
                        case 'pending':
                            $message = __('Successfully marked as pending!', 'thatcamp-registrations');
                            break;
                        case 'rejected':
                            $message = __('Successfully rejected!', 'thatcamp-registrations');
                            break;
                        case 'spammed':
                            $message = __('Successfully spammed!', 'thatcamp-registrations');
                            break;
                    }
                    ?>

		<p><?php 
                    echo $message;
                    ?>
</p>
		</div>
	    <?php 
                }
                ?>

            <?php 
                $bootcampRegistrations = thatcamp_registrations_get_registrations(array('bootcamp' => '1'));
                $registrations = thatcamp_registrations_get_registrations();
                if ($registrations) {
                    ?>

                <p>There are <?php 
                    echo count($registrations);
                    ?>
 total registrations.</p>
                <form action="" method="post">

		<div class="tablenav top">
			<div class="alignleft actions">
				<select name="tcr_bulk_action">
					<option selected="selected" value=""><?php 
                    _e('Bulk Actions', 'thatcamp-registrations');
                    ?>
</option>
					<option value="mark_approved"><?php 
                    _e('Mark Approved', 'thatcamp-registrations');
                    ?>
</option>
					<option value="mark_pending"><?php 
                    _e('Mark Pending', 'thatcamp-registrations');
                    ?>
</option>
					<option value="mark_rejected"><?php 
                    _e('Mark Rejected', 'thatcamp-registrations');
                    ?>
</option>
					<option value="mark_spam"><?php 
                    _e('Mark Spam', 'thatcamp-registrations');
                    ?>
</option>
				</select>

				<input type="submit" value="Apply" class="button-secondary action" id="doaction" name="">
			</div>

			<div class="alignright actions">
				<a class="button-secondary action" href="<?php 
                    echo add_query_arg('trc_csv', '1');
                    ?>
"><?php 
                    _e('Export to CSV');
                    ?>
</a>
			</div>
		</div>

                <table class="widefat fixed" cellspacing="0">
                <thead>
                <tr class="thead">
		    <th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th>Applicant Name</th>
                    <th>Applicant Email</th>
                    <th>Status</th>
                    <th>View</th>
                    <th>Mark Spam</th>
                </tr>
                </thead>

                <tfoot>
                <tr class="thead">
		    <th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th>Applicant Name</th>
                    <th>Applicant Email</th>
                    <th>Status</th>
                    <th>View</th>
                    <th>Mark Spam</th>
                </tr>
                </tfoot>

                <tbody id="users" class="list:user user-list">
                <?php 
                    foreach ($registrations as $registration) {
                        ?>
                    <tr>
                        <?php 
                        $applicant = thatcamp_registrations_get_applicant_info($registration);
                        ?>
			<th class="check-column"><input type="checkbox" name="registration_ids[]" value="<?php 
                        echo intval($registration->id);
                        ?>
" /></th>
                        <td><?php 
                        echo $applicant->first_name;
                        ?>
 <?php 
                        echo $applicant->last_name;
                        ?>
</td>
                        <td><?php 
                        echo $applicant->user_email;
                        ?>
</td>
                        <td><?php 
                        echo ucwords($registration->status);
                        ?>
</td>
                        <td><a href="admin.php?page=thatcamp-registrations&amp;id=<?php 
                        echo $registration->id;
                        ?>
">View Full Registration</a></td>
                        <td><a href="<?php 
                        echo wp_nonce_url(add_query_arg(array('id' => $registration->id, 'page' => 'thatcamp-registrations', 'action' => 'spam'), 'admin.php'), 'tcspam');
                        ?>
" class="button-secondary action" onclick="return confirm('Are you sure you want to delete this registration as spam? There is no undo.');">Mark Spam</a></td>
                    </tr>
                <?php 
                    }
                    ?>
                </tbody>
                </table>

		<?php 
                    wp_nonce_field('tcr_bulk_action');
                    ?>
                </form>
                <?php 
                } else {
                    ?>
                    <p>You don't have any registrations yet.</p>
                <?php 
                }
                ?>
            <?php 
            }
            ?>
        </div>
    <?php 
        }
/**
 * Processes an array of registrations based on ID. Uses mainly in the admin.
 *
 * @param array The IDs of the registration records.
 * @param string The status for the registration records.
 **/
function thatcamp_registrations_process_registrations($ids = array(), $status)
{
    global $wpdb;
    $table = $wpdb->prefix . "thatcamp_registrations";
    $idArray = array();
    foreach ($ids as $id) {
        $idArray['id'] = $id;
    }
    if ($status && !empty($idArray)) {
        // Update the database entry
        $wpdb->update($table, array('status' => $status), $idArray);
        // Maybe create/associate WP accounts with the registration
        if ($status == 'approved') {
            foreach ($ids as $id) {
                thatcamp_registrations_process_user($id);
            }
        } else {
            if ($status == 'rejected') {
                foreach ($ids as $id) {
                    thatcamp_registrations_maybe_remove_wp_user($id);
                }
            }
        }
        // Notify the user of the change
        if ('approved' == $status || 'rejected' == $status) {
            // Don't send 'pending' emails - they are send by the registration process
            // @see thatcamp_registrations_add_registration()
            $registration_data = thatcamp_registrations_get_registration_by_id($id);
            if (!empty($registration_data->applicant_email)) {
                thatcamp_registrations_send_applicant_email($registration_data->applicant_email, $status);
            }
        }
    }
    return;
}