Skip to:
Content

BuddyPress.org

Ticket #3060: bp-groups-loader.php

File bp-groups-loader.php, 12.2 KB (added by jasonnathan, 14 years ago)
Line 
1<?php
2
3
4/**
5 * BuddyPress Groups Loader
6 *
7 * A groups component, for users to group themselves together. Includes a
8 * robust sub-component API that allows Groups to be extended.
9 * Comes preconfigured with an activity stream, discussion forums, and settings.
10 *
11 * @package BuddyPress
12 * @subpackage Groups Core
13 */
14
15class BP_Groups_Component extends BP_Component {
16
17        /**
18         * Start the groups component creation process
19         *
20         * @since BuddyPress {unknown}
21         */
22        function BP_Groups_Component() {
23                parent::start(
24                        'groups',
25                        __( 'User Groups', 'buddypress' ),
26                        BP_PLUGIN_DIR
27                );
28        }
29
30        /**
31         * Include files
32         */
33        function _includes() {
34                $includes = array(
35                        'cache',
36                        'forums',
37                        'actions',
38                        'filters',
39                        'screens',
40                        'classes',
41                        'widgets',
42                        'activity',
43                        'template',
44                        'buddybar',
45                        'functions'
46                );
47                parent::_includes( $includes );
48        }
49
50        /**
51         * Setup globals
52         *
53         * The BP_GROUPS_SLUG constant is deprecated, and only used here for
54         * backwards compatibility.
55         *
56         * @since BuddyPress {unknown}
57         * @global obj $bp
58         */
59        function _setup_globals() {
60                global $bp;
61
62                // Define a slug, if necessary
63                if ( !defined( 'BP_GROUPS_SLUG' ) )
64                        define( 'BP_GROUPS_SLUG', $this->id );
65
66                // Global tables for messaging component
67                $global_tables = array(
68                        'table_name'           => $bp->table_prefix . 'bp_groups',
69                        'table_name_members'   => $bp->table_prefix . 'bp_groups_members',
70                        'table_name_groupmeta' => $bp->table_prefix . 'bp_groups_groupmeta'
71                );
72
73                // All globals for messaging component.
74                // Note that global_tables is included in this array.
75                $globals = array(
76                        'slug'                  => BP_GROUPS_SLUG,
77                        'root_slug'             => isset( $bp->pages->groups->slug ) ? $bp->pages->groups->slug : BP_GROUPS_SLUG,
78                        'notification_callback' => 'groups_format_notifications',
79                        'search_string'         => __( 'Search Groups...', 'buddypress' ),
80                        'global_tables'         => $global_tables,
81                );
82
83                parent::_setup_globals( $globals );
84
85                /** Single Group Globals **********************************************/
86
87                // Are we viewing a single group?
88                if ( bp_is_groups_component() && $group_id = BP_Groups_Group::group_exists( bp_current_action() ) ) {
89
90                        $bp->is_single_item  = true;
91                        $this->current_group = new BP_Groups_Group( $group_id );
92
93                        // When in a single group, the first action is bumped down one because of the
94                        // group name, so we need to adjust this and set the group name to current_item.
95                        $bp->current_item   = isset( $bp->current_action )      ? $bp->current_action      : false;
96                        $bp->current_action = isset( $bp->action_variables[0] ) ? $bp->action_variables[0] : 'home';
97                        array_shift( $bp->action_variables );
98
99                        // Using "item" not "group" for generic support in other components.
100                        if ( is_super_admin() )
101                                $bp->is_item_admin = 1;
102                        else
103                                $bp->is_item_admin = groups_is_user_admin( $bp->loggedin_user->id, $this->current_group->id );
104
105                        // If the user is not an admin, check if they are a moderator
106                        if ( empty( $bp->is_item_admin ) )
107                                $bp->is_item_mod = groups_is_user_mod( $bp->loggedin_user->id, $this->current_group->id );
108
109                        // Is the logged in user a member of the group?
110                        if ( ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $this->current_group->id ) ) )
111                                $this->current_group->is_user_member = true;
112                        else
113                                $this->current_group->is_user_member = false;
114
115                        // Should this group be visible to the logged in user?
116                        if ( 'public' == $this->current_group->status || $this->current_group->is_user_member )
117                                $this->current_group->is_visible = true;
118                        else
119                                $this->current_group->is_visible = false;
120
121                        // If this is a private or hidden group, does the user have access?
122                        if ( 'private' == $this->current_group->status || 'hidden' == $this->current_group->status ) {
123                                if ( $this->current_group->is_user_member && is_user_logged_in() || is_super_admin() )
124                                        $this->current_group->user_has_access = true;
125                                else
126                                        $this->current_group->user_has_access = false;
127                        } else {
128                                $this->current_group->user_has_access = true;
129                        }
130
131                // Set current_group to 0 to prevent debug errors
132                } else {
133                        $this->current_group = 0;
134                }
135
136                // Illegal group names/slugs
137                $this->forbidden_names = apply_filters( 'groups_forbidden_names', array(
138                        'my-groups',
139                        'create',
140                        'invites',
141                        'send-invites',
142                        'forum',
143                        'delete',
144                        'add',
145                        'admin',
146                        'request-membership',
147                        'members',
148                        'settings',
149                        'avatar',
150                        $this->slug,
151                        $this->root_slug,
152                ) );
153
154                // Preconfigured group creation steps
155                $this->group_creation_steps = apply_filters( 'groups_create_group_steps', array(
156                        'group-details'  => array(
157                                'name'       => __( 'Details',  'buddypress' ),
158                                'position'   => 0
159                        ),
160                        'group-settings' => array(
161                                'name'       => __( 'Settings', 'buddypress' ),
162                                'position'   => 10
163                        ),
164                        'group-avatar'   => array(
165                                'name'       => __( 'Avatar',   'buddypress' ),
166                                'position'   => 20 ),
167                ) );
168
169                // If friends component is active, add invitations
170                if ( bp_is_active( 'friends' ) ) {
171                        $this->group_creation_steps['group-invites'] = array(
172                                'name'     => __( 'Invites', 'buddypress' ),
173                                'position' => 30
174                        );
175                }
176
177                // Groups statuses
178                $this->valid_status = apply_filters( 'groups_valid_status', array(
179                        'public',
180                        'private',
181                        'hidden'
182                ) );
183
184                // Auto join group when non group member performs group activity
185                $this->auto_join = defined( 'BP_DISABLE_AUTO_GROUP_JOIN' );
186        }
187
188        /**
189         * Setup BuddyBar navigation
190         *
191         * @global obj $bp
192         */
193        function _setup_nav() {
194                global $bp;
195
196                // Add 'Groups' to the main navigation
197                $main_nav = array(
198                        'name'                => sprintf( __( 'Groups <span>(%d)</span>', 'buddypress' ), groups_total_groups_for_user() ),
199                        'slug'                => $this->slug,
200                        'position'            => 70,
201                        'screen_function'     => 'groups_screen_my_groups',
202                        'default_subnav_slug' => 'my-groups',
203                        'item_css_id'         => $this->id
204                );
205
206                $groups_link = trailingslashit( $bp->loggedin_user->domain . $this->slug );
207
208                // Add the My Groups nav item
209                $sub_nav[] = array(
210                        'name'            => __( 'My Groups', 'buddypress' ),
211                        'slug'            => 'my-groups',
212                        'parent_url'      => $groups_link,
213                        'parent_slug'     => $this->slug,
214                        'screen_function' => 'groups_screen_my_groups',
215                        'position'        => 10,
216                        'item_css_id'     => 'groups-my-groups',
217            'user_has_access' => bp_is_my_profile()
218                );
219
220                // Add the Group Invites nav item
221                $sub_nav[] = array(
222                        'name'            => __( 'Invitations',   'buddypress' ),
223                        'slug'            => 'invites',
224                        'parent_url'      => $groups_link,
225                        'parent_slug'     => $this->slug,
226                        'screen_function' => 'groups_screen_group_invites',
227                        'position'        => 30,
228                        'user_has_access' => bp_is_my_profile()
229                );
230
231                parent::_setup_nav( $main_nav, $sub_nav );
232
233                if ( bp_is_groups_component() && bp_is_single_item() ) {
234
235                        // Add 'Groups' to the main navigation
236                        $main_nav = array(
237                                'name'                => __( 'Groups', 'buddypress' ),
238                                'slug'                => $this->root_slug,
239                                'position'            => -1, // Do not show in BuddyBar
240                                'screen_function'     => 'groups_screen_group_home',
241                                'default_subnav_slug' => 'home',
242                                'item_css_id'         => $this->id
243                        );
244
245                        $group_link = trailingslashit( bp_get_root_domain() . '/' . $this->root_slug . '/' . $this->current_group->slug );
246
247                        // Add the "Home" subnav item, as this will always be present
248                        $sub_nav[] = array(
249                                'name'            => __( 'Home', 'buddypress' ),
250                                'slug'            => 'home',
251                                'parent_url'      => $group_link,
252                                'parent_slug'     => $this->root_slug,
253                                'screen_function' => 'groups_screen_group_home',
254                                'position'        => 10,
255                                'item_css_id'     => 'home'
256                        );
257
258                        // If the user is a group mod or more, then show the group admin nav item
259                        if ( bp_is_item_admin() || bp_is_item_mod() ) {
260                                $sub_nav[] = array(
261                                        'name'            => __( 'Admin', 'buddypress' ),
262                                        'slug'            => 'admin',
263                                        'parent_url'      => $group_link,
264                                        'parent_slug'     => $this->root_slug,
265                                        'screen_function' => 'groups_screen_group_admin',
266                                        'position'        => 20,
267                                        'user_has_access' => ( $bp->is_item_admin + (int)$bp->is_item_mod ),
268                                        'item_css_id'     => 'admin'
269                                );
270                        }
271
272                        // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
273                        if ( is_user_logged_in() &&
274                                 !is_super_admin() &&
275                                 !$this->current_group->is_user_member &&
276                                 !groups_check_for_membership_request( $bp->loggedin_user->id, $this->current_group->id ) &&
277                                 $this->current_group->status == 'private'
278                                ) {
279                                $sub_nav[] = array(
280                                        'name'               => __( 'Request Membership', 'buddypress' ),
281                                        'slug'               => 'request-membership',
282                                        'parent_url'         => $group_link,
283                                        'parent_slug'        => $this->root_slug,
284                                        'screen_function'    => 'groups_screen_group_request_membership',
285                                        'position'           => 30
286                                );
287                        }
288
289                        // Forums are enabled and turned on
290                        if ( $this->current_group->enable_forum && bp_is_active( 'forums' ) ) {
291                                $sub_nav[] = array(
292                                        'name'            => __( 'Forum', 'buddypress' ),
293                                        'slug'            => 'forum',
294                                        'parent_url'      => $group_link,
295                                        'parent_slug'     => $this->root_slug,
296                                        'screen_function' => 'groups_screen_group_forum',
297                                        'position'        => 40,
298                                        'user_has_access' => $this->current_group->user_has_access,
299                                        'item_css_id'     => 'forums'
300                                );
301                        }
302
303                        $sub_nav[] = array(
304                                'name'            => sprintf( __( 'Members (%s)', 'buddypress' ), number_format( $this->current_group->total_member_count ) ),
305                                'slug'            => 'members',
306                                'parent_url'      => $group_link,
307                                'parent_slug'     => $this->root_slug,
308                                'screen_function' => 'groups_screen_group_members',
309                                'position'        => 60,
310                                'user_has_access' => $this->current_group->user_has_access,
311                                'item_css_id'     => 'members'
312                        );
313
314                        if ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $this->current_group->id ) ) {
315                                if ( bp_is_active( 'friends' ) ) {
316                                        $sub_nav[] = array(
317                                                'name'            => __( 'Send Invites', 'buddypress' ),
318                                                'slug'            => 'send-invites',
319                                                'parent_url'      => $group_link,
320                                                'parent_slug'     => $this->root_slug,
321                                                'screen_function' => 'groups_screen_group_invite',
322                                                'item_css_id'     => 'invite',
323                                                'position'        => 70,
324                                                'user_has_access' => $this->current_group->user_has_access
325                                        );
326                                }
327                        }
328
329                        parent::_setup_nav( $main_nav, $sub_nav );
330                }
331
332                if ( isset( $this->current_group->user_has_access ) )
333                        do_action( 'groups_setup_nav', $this->current_group->user_has_access );
334                else
335                        do_action( 'groups_setup_nav');
336        }
337
338        /**
339         * Sets up the title for pages and <title>
340         *
341         * @global obj $bp
342         */
343        function _setup_title() {
344                global $bp;
345
346                if ( bp_is_groups_component() ) {
347
348                        if ( bp_is_my_profile() && !bp_is_single_item() ) {
349
350                                $bp->bp_options_title = __( 'My Groups', 'buddypress' );
351
352                        } else if ( !bp_is_my_profile() && !bp_is_single_item() ) {
353
354                                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
355                                        'item_id' => $bp->displayed_user->id,
356                                        'type'    => 'thumb'
357                                ) );
358                                $bp->bp_options_title  = $bp->displayed_user->fullname;
359
360                        // We are viewing a single group, so set up the
361                        // group navigation menu using the $this->current_group global.
362                        } else if ( bp_is_single_item() ) {
363                                $bp->bp_options_title  = $this->current_group->name;
364                                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
365                                        'item_id'    => $this->current_group->id,
366                                        'object'     => 'group',
367                                        'type'       => 'thumb',
368                                        'avatar_dir' => 'group-avatars',
369                                        'alt'        => __( 'Group Avatar', 'buddypress' )
370                                ) );
371                                if ( empty( $bp->bp_options_avatar ) )
372                                        $bp->bp_options_avatar = '<img src="' . esc_attr( $group->avatar_full ) . '" class="avatar" alt="' . esc_attr( $group->name ) . '" />';
373                        }
374                }
375
376                parent::_setup_title();
377        }
378}
379// Create the groups component
380$bp->groups = new BP_Groups_Component();
381
382?>