/**
 * Create the Achievements admin page.
 *
 * @global object $bp BuddyPress global settings
 * @global int $screen_layout_columns Number of columns shown on this admin page
 * @see dpa_admin_screen_layout_columns()
 * @since 2.0
 */
function dpa_admin_screen()
{
    global $bp, $screen_layout_columns;
    if (!($settings = get_blog_option(BP_ROOT_BLOG, 'achievements'))) {
        update_blog_option(BP_ROOT_BLOG, 'achievements', array());
    }
    $is_support_tab = false;
    if (!empty($_GET['tab']) && DPA_SLUG_ADMIN_SUPPORT == stripslashes($_GET['tab'])) {
        $is_support_tab = true;
    }
    // Email contact form
    if (!empty($_POST['contact_body']) && !empty($_POST['contact_type']) && !empty($_POST['contact_email'])) {
        $body = force_balance_tags(nxt_filter_kses(stripslashes($_POST['contact_body'])));
        $type = force_balance_tags(nxt_filter_kses(stripslashes($_POST['contact_type'])));
        $email = sanitize_email(force_balance_tags(nxt_filter_kses(stripslashes($_POST['contact_email']))));
        if ($body && $type && $email && is_email($email)) {
            $email_sent = nxt_mail(array('*****@*****.**', $email), "Achievements support request: " . $type, $body);
        }
    }
    ?>
	<div id="bp-admin">
		<div id="dpa-admin-metaboxes-general" class="wrap">

			<div id="bp-admin-header">
				<h3><?php 
    _e('BuddyPress', 'dpa');
    ?>
</h3>
				<h4><?php 
    _e('Achievements', 'dpa');
    ?>
</h4>
			</div>

			<div id="bp-admin-nav">
				<ol>
					<li <?php 
    if (!$is_support_tab) {
        echo 'class="current"';
    }
    ?>
><a href="<?php 
    echo site_url('nxt-admin/admin.php?page=' . $bp->achievements->id, 'admin');
    ?>
"><?php 
    _e('Configure', 'dpa');
    ?>
</a></li>
					<li <?php 
    if ($is_support_tab) {
        echo 'class="current"';
    }
    ?>
><a href="<?php 
    echo site_url('nxt-admin/admin.php?page=' . $bp->achievements->id . '&amp;tab=' . DPA_SLUG_ADMIN_SUPPORT, 'admin');
    ?>
"><?php 
    _e('Support', 'dpa');
    ?>
</a></li>
				</ol>
			</div>

			<?php 
    if (!empty($_GET['updated'])) {
        ?>
				<div id="message" class="updated">
					<p><?php 
        _e('Your Achievements settings have been saved.', 'dpa');
        ?>
</p>
				</div>
			<?php 
    }
    ?>

			<?php 
    if (isset($email_sent)) {
        ?>
				<div id="message" class="updated">
					<p><?php 
        _e("Thanks, we've recieved your message and have emailed you a copy for your records. We'll be in touch soon!", 'dpa');
        ?>
</p>
				</div>
			<?php 
    }
    ?>

			<div class="dpa-spacer">
				<?php 
    if (!$is_support_tab) {
        ?>
					<p><?php 
        _e("Achievements gives your BuddyPress community fresh impetus by promoting and rewarding social interaction with challenges, badges and points. For information, support, premium enhancements and developer documentation, visit <a href='http://achievementsapp.nxtclass.com/'>our website</a>.", 'dpa');
        ?>
</p>
					<p><?php 
        printf(__("To create and manage Achievements, visit the <a href='%s'>Achievements Directory</a>.", 'dpa'), dpa_get_achievements_permalink());
        ?>
</p>
				<?php 
    } else {
        ?>
					<p><?php 
        printf(__("Have you found a bug or do you have a great idea for the next release? Please make a report on <a href='%s'>BuddyPress.org</a>, or use the form below to get in contact. We're listening.", 'dpa'), 'http://buddypress.org/community/groups/achievements/forum/');
        ?>
</p>
				<?php 
    }
    ?>
			</div>

			<?php 
    if (!$is_support_tab) {
        ?>
				<form method="post" action="options.php" id="achievements">
				<?php 
        nxt_nonce_field('closedpostboxes', 'closedpostboxesnonce', false);
        ?>
				<?php 
        nxt_nonce_field('meta-box-order', 'meta-box-order-nonce', false);
        ?>
				<?php 
        settings_fields('dpa-settings-group');
        ?>
			<?php 
    }
    ?>

				<div id="poststuff" class="metabox-holder<?php 
    echo 2 == $screen_layout_columns ? ' has-right-sidebar' : '';
    ?>
">
					<div id="side-info-column" class="inner-sidebar">
						<?php 
    if ($is_support_tab) {
        do_meta_boxes('buddypress_page_achievements-support', 'side', $settings);
    } else {
        do_meta_boxes('buddypress_page_achievements', 'side', $settings);
    }
    ?>
					</div>
					<div id="post-body" class="has-sidebar">
						<div id="post-body-content" class="has-sidebar-content">
							<?php 
    if ($is_support_tab) {
        do_meta_boxes('buddypress_page_achievements-support', 'normal', $settings);
    } else {
        do_meta_boxes('buddypress_page_achievements', 'normal', $settings);
    }
    ?>
						</div>

						<?php 
    if (!$is_support_tab) {
        ?>
							<p><input type="submit" class="button-primary" value="<?php 
        _e('Save Settings', 'dpa');
        ?>
" /></p>
						<?php 
    }
    ?>
					</div>
				</div>

			<?php 
    if (!$is_support_tab) {
        ?>
			</form>
			<?php 
    }
    ?>

		</div><!-- #dpa-admin-metaboxes-general -->
	</div><!-- #bp-admin -->
<?php 
}
/**
 * Template tag version of dpa_get_achievements_permalink()
 *
 * @uses dpa_get_achievements_permalink()
 * @since 2.0
 */
function dpa_achievements_permalink()
{
    echo dpa_get_achievements_permalink();
}
/**
 * Loads the create Achievement page. Also implements controller logic.
 *
 * @global DPA_Achievement_Template $achievements_template Achievements template tag object
 * @global nxt_Error $achievements_errors Achievement creation error object
 * @global object $bp BuddyPress global settings
 * @since 2.0
 * @uses DPA_Achievement
 */
function dpa_screen_achievement_create()
{
    global $achievements_template, $achievements_errors, $bp, $current_blog;
    if (!bp_is_current_component($bp->achievements->slug) || DPA_SLUG_CREATE != $bp->current_action || !dpa_permission_can_user_create()) {
        return;
    }
    $bp->achievements->current_achievement = new DPA_Achievement();
    $achievement =& $bp->achievements->current_achievement;
    // Has form been submitted?
    if (empty($_POST['achievement-create'])) {
        $achievement->points = '';
        $achievement->action_count = 1;
        $achievement->is_active = 1;
        do_action('dpa_screen_achievement_create', $achievement);
        bp_core_load_template(apply_filters('dpa_screen_achievement_create_template', 'achievements/create'));
        return;
    }
    if (!nxt_verify_nonce($_POST['_nxtnonce'], 'achievement-create')) {
        nxt_nonce_ays('');
        die;
    }
    /* We can't use template tags because if the new details fail validation and do not save, the template loop will fetch the old version. */
    if ('badge' == stripslashes($_POST['achievement_type'])) {
        $achievement->action_count = 1;
        $achievement->action_id = -1;
    } else {
        $achievement->action_count = (int) $_POST['action_count'];
        $achievement->action_id = (int) $_POST['action_id'];
    }
    if (is_multisite() && bp_is_active('blogs')) {
        $achievement->site_id = (int) $_POST['site_id'];
    } else {
        $achievement->site_id = BP_ROOT_BLOG;
    }
    if (bp_is_active('groups')) {
        $achievement->group_id = (int) $_POST['group_id'];
    } else {
        $achievement->group_id = -1;
    }
    if (!empty($_POST['is_hidden'])) {
        $achievement->is_active = 2;
    } elseif (!empty($_POST['is_active'])) {
        $achievement->is_active = 1;
    } else {
        $achievement->is_active = 0;
    }
    $achievement->name = stripslashes($_POST['name']);
    $achievement->description = stripslashes($_POST['description']);
    $achievement->points = (int) $_POST['points'];
    $achievement->slug = stripslashes($_POST['slug']);
    $achievement->picture_id = -1;
    // A pictures is chosen on its own page, after creation.
    $achievements_errors = $achievement->save();
    if (!is_nxt_error($achievements_errors)) {
        $achievements_template->achievement = $achievement;
        // Required for dpa_record_activity()
        if (1 == $achievement->is_active) {
            dpa_record_activity($bp->loggedin_user->id, dpa_format_activity($bp->loggedin_user->id, $achievement->id), $achievement->id, 'achievement_created');
        }
        bp_core_add_message(__("Achievement created succesfully!", 'dpa'));
        do_action('dpa_screen_achievement_create_success', $achievement);
        if (dpa_permission_can_user_change_picture()) {
            bp_core_redirect(dpa_get_achievements_permalink() . '/' . $achievement->slug . '/' . DPA_SLUG_ACHIEVEMENT_CHANGE_PICTURE);
        } else {
            bp_core_redirect(dpa_get_achievements_permalink() . '/' . $achievement->slug);
        }
    } else {
        if (!$achievement->points) {
            $achievement->points = '';
        }
        if (!$achievement->action_count) {
            $achievement->action_count = '';
        }
        do_action('dpa_screen_achievement_create_fail', $achievement, $achievements_errors);
        bp_core_add_message(__('An error has occurred and the Achievement has not been created. See below for details.', 'dpa'), 'error');
        bp_core_load_template(apply_filters('dpa_screen_achievement_create_template', 'achievements/create'));
    }
}