<?php

function bp_dedication_add_js() {
    global $bp;
    if (bp_is_dedication_component() && bp_is_current_action('new-dedication')) {
        wp_enqueue_script('jquery-validate-js', plugins_url("includes/js/jquery.validate.min.js", __FILE__), array('jquery'));
        wp_enqueue_script('jquery-autocomplete-js', plugins_url("includes/js/jquery.autocomplete-min.js", __FILE__), array('jquery'));
        wp_enqueue_script('jquery-jyoutube-js', plugins_url("includes/js/jquery.jyoutube.js", __FILE__), array('jquery'));
        wp_register_script('dedication-js', plugins_url("includes/js/dedication-script.js", __FILE__), array('jquery-autocomplete-js', 'jquery-jyoutube-js'));

        wp_localize_script('dedication-js', 'script_vars', array(
            'source_url' => plugins_url("includes/bp-dedication-search-members.php", __FILE__)
                )
        );

        wp_enqueue_script('dedication-js');
    }
}

add_action('wp_head', 'bp_dedication_add_js', 1);

function bp_dedication_add_css() {
    global $bp;
    if (bp_is_dedication_component() && bp_is_current_action('new-dedication')) {
        wp_register_style('bp-dedication-style', plugins_url("includes/css/bp-dedication.css", __FILE__));
        wp_enqueue_style('bp-dedication-style');
    }
}

add_action('wp_print_styles', 'bp_dedication_add_css');

class BP_Dedication extends BP_Component {
    /* var $enable_nav_item = true;
      var $enable_create_step = true;
      var $enable_edit_item = false; */

    /**
     * Constructor method
     *
     * You can do all sorts of stuff in your constructor, but it's recommended that, at the
     * very least, you call the parent::start() function. This tells the parent BP_Component
     * to begin its setup routine.
     *
     * BP_Component::start() takes three parameters:
     *   (1) $id   - A unique identifier for the component. Letters, numbers, and underscores
     * 		 only.
     *   (2) $name - This is a translatable name for your component, which will be used in
     *               various places through the BuddyPress admin screens to identify it.
     *   (3) $path - The path to your plugin directory. Primarily, this is used by
     * 		 BP_Component::includes(), to include your plugin's files. See loader.php
     * 		 to see how BP_EXAMPLE_PLUGIN_DIR was defined.
     *
     * @package BuddyPress_Skeleton_Component
     * @since 1.6
     */
    function __construct() {
        global $bp;

        parent::start(
                BP_DEDICATION_SLUG, __('Dedications', 'bp-dedication'), BP_DEDICATION_PLUGIN_DIR
        );

        $this->includes();

        /**
         * Put your component into the active components array, so that
         *   bp_is_active( 'example' );
         * returns true when appropriate. We have to do this manually, because non-core
         * components are not saved as active components in the database.
         */
        $bp->active_components[$this->id] = '1';

        /**
         * Hook the register_post_types() method. If you're using custom post types to store
         * data (which is recommended), you will need to hook your function manually to
         * 'init'.
         */
        //add_action( 'init', array( &$this, 'register_post_types' ) );
    }

    function includes() {

        // Files to include
        $includes = array(
            'includes/bp-dedication-create-dedication.php',
            'includes/bp-dedication-screens.php',
            'includes/bp-dedication-activity.php',
            'includes/bp-dedication-functions.php',
            'includes/bp-dedication-classes.php',
            'includes/bp-dedication-ajax.php',
                /* 'includes/bp-example-filters.php',


                  'includes/bp-example-template.php',

                  'includes/bp-example-notifications.php',
                  'includes/bp-example-widgets.php',
                  'includes/bp-example-cssjs.php',
                  'includes/bp-example-ajax.php' */
        );

        parent::includes($includes);

        /* if ( is_admin() || is_network_admin() ) {
          include( BP_EXAMPLE_PLUGIN_DIR . '/includes/bp-example-admin.php' );
          } */
    }

    function get_count_dedications_from_user($user_id) {
        $dedications = bp_dedication_get_dedications_from_user($user_id);
        return count($dedications); 
    }
    
    function get_count_dedications_to_user($user_id) {
        $dedications = bp_dedication_get_dedications_to_user($user_id);
        return count($dedications);
    }
    
    function setup_nav() {
        $main_nav = array(
            'name' => __('Dedications', 'bp-dedication'),
            'slug' => bp_get_dedication_slug(),
            'position' => 80,
            'screen_function' => 'bp_dedication_index',
            'default_subnav_slug' => 'dedication-profile'
        );

        $dedication_link = trailingslashit(bp_displayed_user_domain() . bp_get_dedication_slug());

        $sub_nav[] = array(
            'name' => __('Profile', 'bp-dedication'),
            'slug' => 'dedication-profile',
            'parent_url' => $dedication_link,
            'parent_slug' => bp_get_dedication_slug(),
            'screen_function' => 'bp_dedication_index',
            'position' => 10
        );
        
        $sub_nav[] = array(
            'name' => __('My dedications', 'bp-dedication'),
            'slug' => 'my-dedications',
            'parent_url' => $dedication_link,
            'parent_slug' => bp_get_dedication_slug(),
            'screen_function' => 'bp_dedication_my_dedications',
            'position' => 20
        );

        $sub_nav[] = array(
            'name' => __('Dedicated 2u', 'bp-dedication'),
            'slug' => 'dedicated-2u',
            'parent_url' => $dedication_link,
            'parent_slug' => bp_get_dedication_slug(),
            'screen_function' => 'bp_dedication_dedications_to_me',
            'position' => 30
        );

        if (bp_displayed_user_id() == bp_loggedin_user_id()) {
            $sub_nav[] = array(
                'name' => __('New dedication', 'bp-dedication'),
                'slug' => 'new-dedication',
                'parent_url' => $dedication_link,
                'parent_slug' => bp_get_dedication_slug(),
                'screen_function' => 'bp_dedication_new_dedication',
                'position' => 40
            );
        }

        parent::setup_nav($main_nav, $sub_nav);
        //parent::setup_nav( $main_nav );
        // If your component needs additional navigation menus that are not handled by
        // BP_Component::setup_nav(), you can register them manually here. For example,
        // if your component needs a subsection under a user's Settings menu, add
        // it like this. See bp_example_screen_settings_menu() for more info
        /* bp_core_new_subnav_item( array(
          'name' 		  => __( 'Example', 'bp-example' ),
          'slug' 		  => 'example-admin',
          'parent_slug'     => bp_get_settings_slug(),
          'parent_url' 	  => trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ),
          'screen_function' => 'bp_example_screen_settings_menu',
          'position' 	  => 40,
          'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
          ) ); */
    }

}

function bp_load_dedication_component() {
    global $bp;

    $bp->dedications = new BP_Dedication;
}

add_action('bp_loaded', 'bp_load_dedication_component');

//function bp_example_var_dump() {
//   	  global $bp;
//	  var_dump( $bp->dedications );
//   }
//add_action( 'bp_init', 'bp_example_var_dump' );

/**
 * Extends the WP_User_Query class to make it easier for us to search across different fields
 *
 * @package Invite Anyone
 * @since 1.0.4
 */
class User_Dedication_Query extends WP_User_Query {

    function __construct($query = null) {
        add_action('pre_user_query', array(&$this, 'filter_registered_users_only'));
        parent::__construct($query);
    }

    /**
     * BuddyPress has different user statuses.  We need to filter the user list so only registered users are shown.
     */
    function filter_registered_users_only($query) {
        $query->query_where .= ' AND user_status = 0';
    }

    /**
     * @see WP_User_Query::get_search_sql()
     */
    function get_search_sql($string, $cols, $wild = false) {
        $string = esc_sql($string);

        // Always search all columns
        $cols = array(
            'user_email',
            'user_login',
            'user_nicename',
            'user_url',
            'display_name'
        );

        // Always do 'both' for trailing_wild
        $wild = 'both';

        $searches = array();
        $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
        $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
        foreach ($cols as $col) {
            if ('ID' == $col)
                $searches[] = "$col = '$string'";
            else
                $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'";
        }

        return ' AND (' . implode(' OR ', $searches) . ') AND user_status = 0';
    }

}

?>
