function mpp_group_is_gallery_enabled($group_id = false)
{
    //is groups component enabled?
    if (mpp_is_enabled('groups', $group_id)) {
        $is_enabled = true;
    } else {
        $is_enabled = false;
    }
    //if component is not enabled, return false
    if (!$is_enabled) {
        return false;
    }
    if (!$group_id) {
        $group = groups_get_current_group();
        if (!empty($group)) {
            $group_id = $group->id;
        }
    }
    if (!$group_id) {
        return false;
    }
    //check for group settings
    $is_enabled = groups_get_groupmeta($group_id, '_mpp_is_enabled', true);
    //if current group has no preference set, fallback to global preference
    //this global preference can be set by visting Dashboard->MediaPress->Settings->Groups
    if (empty($is_enabled)) {
        $is_enabled = mpp_get_option('enable_group_galleries_default', 'yes');
    }
    return $is_enabled == 'yes';
}
/**
 * Check if current user can create gallery for the given context
 * 
 * @param type $component
 * @param type $component_id
 * @return boolean
 */
function mpp_user_can_create_gallery($component, $component_id)
{
    if (!is_user_logged_in()) {
        return false;
    }
    $can_do = false;
    $user_id = get_current_user_id();
    if (is_super_admin()) {
        $can_do = true;
    } elseif ($component == 'members' && $component_id == $user_id && mediapress()->is_bp_active()) {
        $can_do = true;
    } elseif ($component == 'groups' && function_exists('groups_is_user_member') && groups_is_user_member($user_id, $component_id)) {
        $can_do = true;
    } elseif ($component == 'sitewide' && mpp_is_active_component('sitewide')) {
        $can_do = true;
    }
    //alright, but is it enabled?
    if (!mpp_is_enabled($component, $component_id)) {
        $can_do = false;
    }
    $can_do = apply_filters('mpp_user_can_create_gallery', $can_do, $component, $component_id);
    return $can_do;
    //apply_filters( "mpp_can_user_upload_to_{$component}", $can_do, $component_id );
}
 /**
  * Set up the Toolbar.
  *
  * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()}
  *        for details.
  */
 public function setup_admin_bar($wp_admin_nav = array())
 {
     $bp = buddypress();
     // Menus for logged in user if the members gallery is enabled
     if (is_user_logged_in() && mpp_is_enabled('members', bp_loggedin_user_id())) {
         $component = 'members';
         $component_id = get_current_user_id();
         $gallery_link = trailingslashit(mpp_get_gallery_base_url($component, $component_id));
         $title = __('Gallery', 'mediapress');
         $my_galleries = __('My Gallery', 'mediapress');
         $create = __('Create', 'mediapress');
         // Add main mediapress menu
         $wp_admin_nav[] = array('parent' => $bp->my_account_menu_id, 'id' => 'my-account-' . $this->id, 'title' => $title, 'href' => trailingslashit($gallery_link));
         // Add main mediapress menu
         $wp_admin_nav[] = array('parent' => 'my-account-' . $this->id, 'id' => 'my-account-' . $this->id . '-my-galleries', 'title' => $my_galleries, 'href' => trailingslashit($gallery_link));
         if (mpp_user_can_create_gallery($component, $component_id)) {
             $wp_admin_nav[] = array('parent' => 'my-account-' . $this->id, 'id' => 'my-account-' . $this->id . '-create', 'title' => $create, 'href' => mpp_get_gallery_create_url($component, $component_id));
         }
     }
     parent::setup_admin_bar($wp_admin_nav);
 }
 /**
  * Setup Query for User profile Galleries
  * 
  */
 public function setup_user_gallery()
 {
     if (mpp_is_enabled('members', bp_displayed_user_id()) && function_exists('bp_is_user') && bp_is_user()) {
         //is User Gallery enabled? and are we on the user section?
         $this->component = 'members';
         //initialize for members component
         $this->init();
         //in this case, we are on the gallery directory, check if we have it enabled?
     } elseif (mpp_has_gallery_directory()) {
         $this->setup_gallery_directory_query();
     }
     //finally setup galleries
     $this->setup_galleries();
 }
/**
 * Create new Gallery
 * @param type $args
 * @return boolean
 */
function mpp_create_gallery($args = '')
{
    $default = array('id' => false, 'creator_id' => get_current_user_id(), 'title' => '', 'description' => '', 'slug' => '', 'status' => '', 'order' => false, 'component' => false, 'component_id' => false, 'type' => '', 'date_created' => '', 'date_updated' => '');
    //if the gallery id is set
    if (isset($args['id']) && !empty($args['id'])) {
        return mpp_update_gallery($args);
    }
    $args = wp_parse_args($args, $default);
    extract($args);
    $post_data = array('post_type' => mpp_get_gallery_post_type(), 'post_status' => 'publish', 'post_author' => $creator_id, 'post_title' => $title, 'post_content' => $description, 'post_name' => $slug);
    //if no component id is given, assume to be user gallery
    if (!$component_id) {
        $component_id = get_current_user_id();
    }
    //if a component is not given, assume it to be user gallery
    if (!$component) {
        $component = 'members';
    }
    if (!mpp_is_enabled($component, $component_id)) {
        return false;
        // not enabled for this, can not be created
    }
    if (!$type) {
        $type = 'photo';
    }
    $gallery_status = mpp_get_default_status();
    if (!empty($status)) {
        if (mpp_is_registered_status($status)) {
            $gallery_status = $status;
        }
    }
    //hierarchical tax should always pass ids
    $tax = array(mpp_get_component_taxname() => mpp_underscore_it($component), mpp_get_type_taxname() => mpp_underscore_it($type), mpp_get_status_taxname() => mpp_underscore_it($gallery_status));
    $post_data['tax_input'] = $tax;
    if (!empty($date_created)) {
        $post_data['post_date'] = $date_created;
    }
    if (!empty($date_updated)) {
        $post_data['post_modified'] = $date_updated;
    }
    $gallery_id = wp_insert_post($post_data);
    if (is_wp_error($gallery_id)) {
        return false;
        //unable to add gallery
    }
    //otherwise update the meta with component id
    mpp_update_gallery_meta($gallery_id, '_mpp_component_id', $component_id);
    //fire the gallery created action
    do_action('mpp_gallery_created', $gallery_id);
    return $gallery_id;
}
/**
 * Filter Main MPp Gallery Query to add support for "my-gallery" view on group component
 * @param type $args
 * @return type
 */
function mpp_group_filter_gallery_query($args)
{
    if (mpp_group_is_my_galleries_enabled() && bp_is_active('groups') && bp_is_group() && mpp_is_enabled(mpp_get_current_component(), mpp_get_current_component_id())) {
        //check if the current av0 is 'my-gallery';
        if (is_user_logged_in() && bp_is_action_variable('my-gallery', 0)) {
            $args['user_id'] = bp_loggedin_user_id();
        }
    }
    return $args;
}
/**
 * Record Media Activity
 * 
 * It does not actually records activity, simply simulates the activity update and rest are done by the actions.php functions
 * 
 * It will be removed in future for a better record_activity method
 * @param type $args
 * @return boolean
 */
function mpp_record_activity($args = null)
{
    //if activity module is not active, why bother
    if (!bp_is_active('activity')) {
        return false;
    }
    $default = array('id' => false, 'gallery_id' => 0, 'media_id' => 0, 'media_ids' => null, 'action' => '', 'content' => '', 'type' => '', 'component' => mpp_get_current_component(), 'component_id' => mpp_get_current_component_id(), 'user_id' => get_current_user_id(), 'status' => '');
    $args = wp_parse_args($args, $default);
    //atleast a gallery id or a media id should be given
    if (!$args['gallery_id'] && !$args['media_id'] || !mpp_is_enabled($args['component'], $args['component_id']) || !$args['component_id']) {
        return false;
    }
    $gallery_id = absint($args['gallery_id']);
    $media_id = absint($args['media_id']);
    $type = $args['type'];
    //should we validate type too?
    $hide_sitewide = 0;
    $status_object = null;
    if ($args['status']) {
        $status_object = mpp_get_status_object($args['status']);
        if ($status_object && ($status_object->activity_privacy == 'hidden' || $status_object->activity_privacy == 'onlyme')) {
            $hide_sitewide = 1;
        }
    }
    $media_ids = $args['media_ids'];
    if (!empty($media_ids) && !is_array($media_ids)) {
        $media_ids = explode(',', $media_ids);
    }
    $component = $args['component'];
    if ($component == buddypress()->members->id) {
        $component = buddypress()->activity->id;
        //for user gallery updates, let it be simple activity , do not set the component to 'members'
    }
    $activity_args = array('id' => $args['id'], 'user_id' => $args['user_id'], 'action' => $args['action'], 'content' => $args['content'], 'component' => $component, 'type' => 'mpp_media_upload', 'item_id' => absint($args['component_id']), 'secondary_item_id' => false, 'hide_sitewide' => $hide_sitewide);
    //only update record time if this is a new activity
    if (empty($args['id'])) {
        $activity_args['recorded_time'] = bp_core_current_time();
    }
    $activity_id = bp_activity_add($activity_args);
    if (!$activity_id) {
        return false;
        //there was a problem
    }
    //store the type of gallery activity in meta
    if ($type) {
        mpp_activity_update_activity_type($activity_id, $type);
    }
    if ($media_ids) {
        $media_ids = wp_parse_id_list($media_ids);
        mpp_activity_update_attached_media_ids($activity_id, $media_ids);
    }
    if ($gallery_id) {
        mpp_activity_update_gallery_id($activity_id, $gallery_id);
    }
    if ($media_id) {
        mpp_activity_update_media_id($activity_id, $media_id);
    }
    mpp_activity_update_context($activity_id, 'gallery');
    //save activity privacy
    if ($status_object) {
        bp_activity_update_meta($activity_id, 'activity-privacy', $status_object->activity_privacy);
    }
    return $activity_id;
}
/**
 * Handles Gallery creation on the front end in non ajax case
 * 
 * @return type
 */
function mpp_action_create_gallery()
{
    //allow gallery to be created from anywhere
    //the form must have mpp-action set and It should be set to 'create-gallery'
    if (empty($_POST['mpp-action']) || $_POST['mpp-action'] != 'create-gallery') {
        return;
    }
    $referer = wp_get_referer();
    //if we are here, It is gallery create action
    if (!wp_verify_nonce($_POST['mpp-nonce'], 'mpp-create-gallery')) {
        //add error message and return back to the old page
        mpp_add_feedback(__('Action not authorized!', 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    //update it to allow passing component/id from the form
    $component = isset($_POST['mpp-gallery-component']) ? $_POST['mpp-gallery-component'] : mpp_get_current_component();
    $component_id = isset($_POST['mpp-gallery-component-id']) ? $_POST['mpp-gallery-component-id'] : mpp_get_current_component_id();
    //check for permission
    //we may want to allow passing of component from the form in future!
    if (!mpp_user_can_create_gallery($component, $component_id)) {
        mpp_add_feedback(__("You don't have permission to create gallery!", 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    //if we are here, validate the data and let us see if we can create
    $title = $_POST['mpp-gallery-title'];
    $description = $_POST['mpp-gallery-description'];
    $type = $_POST['mpp-gallery-type'];
    $status = $_POST['mpp-gallery-status'];
    $errors = array();
    if (!mpp_is_active_status($status)) {
        $errors['status'] = __('Invalid Gallery status!', 'mediapress');
    }
    if (!mpp_is_active_type($type)) {
        $errors['type'] = __('Invalid gallery type!', 'mediapress');
    }
    //check for current component
    if (!mpp_is_enabled($component, $component_id)) {
        $errors['component'] = __('Invalid action!', 'mediapress');
    }
    if (empty($title)) {
        $errors['title'] = __('Title can not be empty', 'mediapress');
    }
    //give opportunity to other plugins to add their own validation errors
    $validation_errors = apply_filters('mpp-create-gallery-field-validation', $errors, $_POST);
    if (!empty($validation_errors)) {
        //let us add the validation error and return back to the earlier page
        $message = join('\\r\\n', $validation_errors);
        mpp_add_feedback($message, 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    //let us create gallery
    $gallery_id = mpp_create_gallery(array('title' => $title, 'description' => $description, 'type' => $type, 'status' => $status, 'creator_id' => get_current_user_id(), 'component' => $component, 'component_id' => $component_id));
    if (!$gallery_id) {
        mpp_add_feedback(__('Unable to create gallery!', 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    //if we are here, the gallery was created successfully,
    //let us redirect to the gallery_slug/manage/upload page
    $redirect_url = mpp_get_gallery_add_media_url($gallery_id);
    mpp_add_feedback(__('Gallery created successfully!', 'mediapress'));
    mpp_redirect($redirect_url);
}
/**
 * Add various upload icons to activity post form
 * @return type
 */
function mpp_activity_upload_buttons()
{
    $component = mpp_get_current_component();
    if (!mpp_is_activity_upload_enabled($component)) {
        return;
    }
    //if we are here, the gallery activity stream upload is enabled,
    //let us see if we are on user profile and gallery is enabled
    if (!mpp_is_enabled($component, mpp_get_current_component_id())) {
        return;
    }
    //if we are on group page and either the group component is not enabled or gallery is not enabled for current group, do not show the icons
    if (function_exists('bp_is_group') && bp_is_group() && (!mpp_is_active_component('groups') || !(function_exists('mpp_group_is_gallery_enabled') && mpp_group_is_gallery_enabled()))) {
        return;
    }
    //for now, avoid showing it on single gallery/media activity stream
    if (mpp_is_single_gallery() || mpp_is_single_media()) {
        return;
    }
    ?>
	<div id="mpp-activity-upload-buttons" class="mpp-upload-buttons">
	<?php 
    do_action("mpp_before_activity_upload_buttons");
    //allow to add more type
    ?>

		<?php 
    if (mpp_is_active_type('photo') && mpp_component_supports_type($component, 'photo')) {
        ?>
			<a href="#" id="mpp-photo-upload" data-media-type="photo"><img src="<?php 
        echo mediapress()->get_url() . 'assets/images/media-button-image.gif';
        ?>
"/></a>
		<?php 
    }
    ?>

		<?php 
    if (mpp_is_active_type('audio') && mpp_component_supports_type($component, 'audio')) {
        ?>
			<a href="#" id="mpp-audio-upload" data-media-type="audio"><img src="<?php 
        echo mediapress()->get_url() . 'assets/images/media-button-music.gif';
        ?>
"/></a>
		<?php 
    }
    ?>

		<?php 
    if (mpp_is_active_type('video') && mpp_component_supports_type($component, 'video')) {
        ?>
			<a href="#" id="mpp-video-upload"  data-media-type="video"><img src="<?php 
        echo mediapress()->get_url() . 'assets/images/media-button-video.gif';
        ?>
"/></a>
		<?php 
    }
    ?>

		<?php 
    if (mpp_is_active_type('doc') && mpp_component_supports_type($component, 'doc')) {
        ?>
			<a href="#" id="mpp-doc-upload"  data-media-type="doc"><img src="<?php 
        echo mediapress()->get_url() . 'assets/images/media-button-doc.png';
        ?>
" /></a>
		<?php 
    }
    ?>

		<?php 
    //someone please provide me doc icon and some better icons
    ?>
 

		<?php 
    do_action('mpp_after_activity_upload_buttons');
    //allow to add more type
    ?>

	</div>
		<?php 
}