/**
  * Constructor
  *
  * The Achievements template tag loop. It will retrieve results per the criteria passed from the database, apply pagination (if required)
  * and populate the class variables required for the template tag loop to work in the page templates.
  *
  * @access public
  * @global object $bp BuddyPress global settings
  * @param integer $user_id = '0'. User ID
  * @param string $type = 'all'. Either: all | active | inactive | unlocked | locked | single | active_by_action | eventcount
  * @param integer $page = '1'. Which page of results are we on, for pagination? Also, $page = 1 without a per_page will result in no pagination being applied to the results
  * @param integer $per_page = '20'. How many Achievements per page, for pagination?
  * @param integer $max = '0'. How many Achievements to return as a maximum?
  * @param string $slug = ''. The slug of an Achievement. Only required when $type is 'single'
  * @param string $action = ''. Only get Achievements with this action. Only required when $type is 'active_by_action' and $user_id != 0 and $populate_extras is true
  * @param string $search_terms = ''. Search query
  * @param bool $populate_extras = true. If true, fetch additional information about when or if $user_id had unlocked each Achievement
  * @param bool $skip_detail_page_result = true. On a single Achievement page, dpa_setup_nav() will setup the Achievement template objects appropiately. Set this to skip that prefetched value.
  * @see DPA_Achievement::get()
  * @see dpa_setup_nav()
  * @since 2.0
  * @todo Wrap all these arguments into an array, $args. Change bools to strings to better describe what variable is used for.
  * @uses DPA_Achievement_Template
  */
 function DPA_Achievement_Template($user_id, $type, $page, $per_page, $max, $slug, $action, $search_terms, $populate_extras, $skip_detail_page_result)
 {
     global $bp;
     $this->pag_page = !empty($_REQUEST['xpage']) ? intval($_REQUEST['xpage']) : $page;
     $this->pag_num = !empty($_GET['num']) ? intval($_GET['num']) : $per_page;
     $args = array('limit' => $max, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'populate_extras' => $populate_extras, 'search_terms' => $search_terms, 'skip_detail_page_result' => $skip_detail_page_result, 'slug' => $slug, 'action' => $action, 'type' => $type, 'user_id' => $user_id);
     $this->achievements = DPA_Achievement::get($args);
     // Item Requests
     if (!$max || $max >= (int) $this->achievements['total']) {
         $this->total_achievement_count = (int) $this->achievements['total'];
     } else {
         $this->total_achievement_count = (int) $max;
     }
     $this->achievements = $this->achievements['achievements'];
     if ($max) {
         if ($max >= count($this->achievements)) {
             $this->achievement_count = count($this->achievements);
         } else {
             $this->achievement_count = (int) $max;
         }
     } else {
         $this->achievement_count = count($this->achievements);
     }
     if ((int) $this->total_achievement_count && (int) $this->pag_num) {
         $this->pag_links = paginate_links(array('base' => add_query_arg(array('xpage' => '%#%', 'num' => $this->pag_num, 's' => $search_terms)), 'format' => '', 'total' => ceil((int) $this->total_achievement_count / (int) $this->pag_num), 'current' => (int) $this->pag_page, 'prev_text' => '←', 'next_text' => '→', 'mid_size' => 1));
     }
 }
 /**
  * Validates class variables and saves to database
  *
  * @access public
  * @global object $bp BuddyPress global settings
  * @global nxtdb $nxtdb NXTClass database object
  * @param DPA_Achievement $old_achievement A copy of the Achievement which is about to be saved, for comparision purposes
  * @return bool Whether the achievement was saved successfully or not.
  * @since 2.0
  * @uses nxt_Error
  */
 function save($old_achievement = null)
 {
     global $bp, $nxtdb;
     $errors = new nxt_Error();
     if ($this->id) {
         $this->id = apply_filters('dpa_achievement_id_before_save', (int) $this->id, $this);
     }
     $this->action_id = apply_filters('dpa_achievement_action_id_before_save', (int) $this->action_id, $this);
     $this->picture_id = apply_filters('dpa_achievement_picture_id_before_save', (int) $this->picture_id, $this);
     $this->action_count = apply_filters('dpa_achievement_action_count_before_save', (int) $this->action_count, $this);
     $this->name = stripslashes(apply_filters('dpa_achievement_name_before_save', $this->name, $this));
     $this->description = stripslashes(apply_filters('dpa_achievement_description_before_save', $this->description, $this));
     $this->points = apply_filters('dpa_achievement_points_before_save', (int) $this->points, $this);
     $this->is_active = apply_filters('dpa_achievement_is_active_before_save', (int) $this->is_active, $this);
     $this->slug = apply_filters('dpa_achievement_slug_before_save', $this->slug, $this);
     $this->site_id = apply_filters('dpa_achievement_site_id_before_save', (int) $this->site_id, $this);
     $this->group_id = apply_filters('dpa_achievement_group_id_before_save', (int) $this->group_id, $this);
     DPA_Achievement::validate_achievement_details($this, $old_achievement, $errors);
     do_action('dpa_achievement_before_save', $this, $old_achievement, $errors);
     if ($errors->get_error_code()) {
         return $errors;
     }
     if ($this->id) {
         $result = $nxtdb->query($nxtdb->prepare("UPDATE {$bp->achievements->table_achievements} SET action_id = %d, picture_id = %d, action_count = %d, name = %s, description = %s, points = %d, is_active = %d, slug = %s, site_id = %d, group_id = %d WHERE id = %d LIMIT 1", $this->action_id, $this->picture_id, $this->action_count, $this->name, $this->description, $this->points, $this->is_active, $this->slug, $this->site_id, $this->group_id, $this->id));
     } else {
         // Save
         $result = $nxtdb->insert($bp->achievements->table_achievements, array('action_id' => $this->action_id, 'picture_id' => $this->picture_id, 'action_count' => $this->action_count, 'name' => $this->name, 'description' => $this->description, 'points' => $this->points, 'is_active' => $this->is_active, 'slug' => $this->slug, 'site_id' => $this->site_id, 'group_id' => $this->group_id), array('%d', '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d', '%d'));
         $this->id = $nxtdb->insert_id;
     }
     // Remove active actions cache
     nxt_cache_delete('dpa_active_actions', 'dpa');
     do_action('dpa_achievement_after_save', $this, $old_achievement);
     return $result;
 }
/**
 * Loads an Achievement's delete page. Also implements controller logic.
 *
 * @global object $bp BuddyPress global settings
 * @since 2.0
 * @uses DPA_Achievement
 */
function dpa_screen_achievement_delete()
{
    global $bp;
    if (empty($_REQUEST['delete-achievement-button']) || empty($_REQUEST['delete-achievement-understand'])) {
        do_action('dpa_screen_achievement_delete');
        bp_core_load_template(apply_filters('dpa_screen_achievement_delete_template', 'achievements/single/home'));
        return;
    }
    if (!nxt_verify_nonce($_REQUEST['_nxtnonce'], 'achievements-delete-achievement-' . $bp->current_item)) {
        nxt_nonce_ays('');
        die;
    }
    // Delete Achievement
    $slug = $bp->current_item;
    if (!($achievement_id = DPA_Achievement::delete($slug))) {
        do_action('dpa_screen_achievement_delete_fail', $slug);
        bp_core_add_message(__('There was an error deleting the Achievement, please try again.', 'dpa'), 'error');
        bp_core_redirect(dpa_get_achievements_permalink() . '/' . $slug);
    } else {
        do_action('dpa_screen_achievement_delete_success', $slug, $achievement_id);
        // Remove all notifications for any user relating to this Achievement
        bp_core_delete_all_notifications_by_type($achievement_id, $bp->achievements->id, 'new_achievement');
        bp_core_add_message(__('The Achievement was deleted successfully.', 'dpa'));
        bp_core_redirect(dpa_get_achievements_permalink());
    }
}