Skip to:
Content

BuddyPress.org

Ticket #31: bp-groups-class.php

File bp-groups-class.php, 42.9 KB (added by talk2manoj, 18 years ago)
Line 
1<?php
2/**************************************************************************
3 PLUGIN CLASS
4 --------------------------------------------------------------------------
5 - BP_Groups -- All group functions
6 --------------------------------------------------------------------------
7 **************************************************************************/
8
9include_once ( 'bp-groups-functions.php' );
10
11class BP_Groups
12{
13
14 // set parameters
15 var $message;
16 var $options;
17 var $enabled;
18 var $slug;
19 var $id;
20 var $current_group;
21 var $featuretitle;
22 var $itemtitle;
23 var $groups_table;
24 var $group_members_table;
25
26 // the BP_Groups object
27 function BP_Groups()
28 {
29
30 // set the options as an array of arrays, each option has
31 // array('Option description', 'Option name', 'Default value', 'Options type [text|boolean])
32 $this->options = array (
33 array("Application enabled", "bp_groups_enabled", "true", "boolean"),
34 array("Groups application title", "bp_groups_title", "Groups", "text"),
35 array("Group title", "bp_group_title", "group", "text"),
36 array("Groups application description", "bp_groups_description", "Welcome to the groups.", "textarea"),
37 array("No group members", "bp_groups_no_members", "There are no members of this group. Why not be the first?", "text"),
38 array("Already group member", "bp_groups_already_member", "You can't join this group as you are already a member.", "text"),
39 array("Member not invited", "bp_groups_not_invited", "You can't join this group as you have not been invited.", "text"),
40 array("Joined group", "bp_groups_joined", "Congratulations, you've just joined this group.", "text"),
41 array("Join group message", "bp_groups_join_message", "Click here to join this group", "text"),
42 array("Action denied error", "bp_groups_action_denied", "You cannot perform this action", "text"),
43 array("Error saving image", "bp_error_saving_image", "There has been a problem saving your group image. Please see the errors below for more information.", "text"),
44 array("Error duplicate group name", "bp_error_duplicate_group_name", "Sorry, you will have to call your group something different as that name has already been used. Click back and try again. ", "text")
45 );
46
47 $this->featuretitle = get_site_option("bp_groups_title");
48 $this->itemtitle = get_site_option("bp_group_title");
49
50 //if (get_site_option("bp_groups_enabled") == "")
51 //{
52
53 $this->groups_table = $wpdb->base_prefix."bp_groups";
54 $this->group_members_table = $wpdb->base_prefix."bp_group_members";
55
56 // check the options exist
57 $this->Check_Options();
58
59 // check the tables exist
60 $this->Tables_Action();
61
62 // add the menu options
63 $this->Menu_Action();
64
65 //}
66 }
67
68 function Menu_Action()
69 {
70 // add the menu options
71 add_action('admin_menu', array(&$this, 'Build_Menu'));
72 }
73
74 function Tables_Action()
75 {
76 // check the tables exist
77 add_action('admin_head', array(&$this, 'Check_Tables'));
78 }
79
80 function Build_Menu()
81 {
82 global $userdata, $wpdb;
83
84 if($wpdb->blogid == $userdata->primary_blog)
85 {
86 add_menu_page("Groups", "Groups", 1, basename(__FILE__), array(&$this, 'Groups'));
87 add_submenu_page(basename(__FILE__), "Your Groups", "Your Groups", 1, basename(__FILE__), array(&$this, 'Groups'));
88 add_submenu_page(basename(__FILE__), "Group Invites", "Group Invites", 1, "groups.invites", array(&$this, 'Invites'));
89 add_submenu_page(basename(__FILE__), "Join Group", "Join Group", 1, "groups.join", array(&$this, 'Join'));
90 add_submenu_page(basename(__FILE__), "Create Group", "Create Group", 1, "groups.create", array(&$this, 'Create'));
91
92 if ($this->Has_Group_Admin_Rights())
93 {
94 add_submenu_page(basename(__FILE__), "Administer Groups", "Administer Groups", 1, "groups.edit", array(&$this, 'Edit'));
95 }
96 }
97 }
98
99 // feature a group
100 function Feature_Group($groupid)
101 {
102
103 $featured_group = (int)$groupid;
104 update_site_option("bp_featured_group", $featured_group);
105 $this->message = "<p class=\"success\">The featured group has been successfully saved.</p>";
106
107 }
108
109 // check if the current group exists, and get the details for it
110 function Get_Current_Group()
111 {
112 // get the globals
113 global $wpdb;
114 global $myjournal_members;
115 global $current_user;
116
117 // get the group details
118 if ($this->slug)
119 {
120 $sql = "select n.id, n.name, n.slug, n.description, n.private, n.open, n.type,
121 count(m.id) as members
122 from ".$this->groups_table." n
123 inner join ".$this->group_members_table." m on m.group_id = n.id and m.status_id = 1
124 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
125 where n.slug = '".$wpdb->escape($this->slug)."'
126 group by n.id;";
127 }
128 if ($this->id)
129 {
130 $sql = "select n.id, n.name, n.slug, n.description, n.private, n.open, n.type,
131 count(m.id) as members
132 from ".$this->groups_table." n
133 inner join ".$this->group_members_table." m on m.group_id = n.id and m.status_id = 1
134 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
135 where n.id = ".$wpdb->escape($this->id)."
136 group by n.id;";
137 }
138 $details = $wpdb->get_row($sql);
139
140 if ($details->private == "1")
141 {
142 $sql = "select id from ".$this->group_members_table."
143 where group_id = ".$details->id."
144 and user_id = ".$current_user->ID."
145 and status_id in (1,2);";
146
147 $valid_member = $wpdb->get_var($sql);
148 } else {
149 $valid_member = 1;
150 }
151
152 // if the group was not found, or it is private and the user isn't a member
153 if (!$details || !$valid_member || get_site_option("bp_groups_enabled") != "true")
154 {
155 $this->current_group->name = "Not found";
156 $this->current_group->description = "Sorry, the group you are looking for cannot be found.";
157 } else {
158 $this->current_group = $details;
159 };
160 }
161
162 // get basic details of a group
163 function Group_Details($groupid)
164 {
165 // get the globals
166 global $wpdb;
167 $sql = "select name, slug from ".$this->groups_table." where id = ".$wpdb->escape($groupid).";";
168 return $wpdb->get_row($sql);
169 }
170
171 // search public groups
172 function Search_Groups($q, $start=0, $num=10)
173 {
174 // get the globals
175 global $wpdb;
176
177 $sql = "select SQL_CALC_FOUND_ROWS id, name, description, slug
178 from ".$this->groups_table."
179 where (name like '%".$wpdb->escape($q)."%' or description like '%".$wpdb->escape($q)."%')
180 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
181
182 $groups = $wpdb->get_results($sql);
183 if (is_array($groups) && count($groups) > 0)
184 {
185 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
186 for ($i = 0; $i < count($groups); $i++)
187 {
188 $image = $this->Get_Group_Image($groups[$i]->id, "m");
189 $groups[$i]->image = $image;
190 $groups[$i]->rows = $rows;
191 }
192 }
193 return $groups;
194 }
195
196 // get the latest groups
197 function Latest_Groups($limit=6, $start=0)
198 {
199 // get the globals
200 global $wpdb;
201
202 $groups = $wpdb->get_results("select id, slug, name, description, open
203 from ".$this->groups_table."
204 where status_id = 1
205 and private = 0
206 order by timestamp desc
207 limit ".$start.", ".$limit.";");
208
209 return $groups;
210 }
211
212 // get the latest groups
213 function Popular_Groups($limit=6, $start=0)
214 {
215 // get the globals
216 global $wpdb;
217
218 $groups = $wpdb->get_results("select n.id, n.slug, n.name, n.description, n.open, count(m.id) as members
219 from ".$this->groups_table." n
220 left outer join ".$this->group_members_table." m on m.group_id = n.id and m.status_id = 1
221 where n.status_id = 1
222 and n.private = 0
223 group by n.id
224 order by members desc
225 limit ".$start.", ".$limit.";");
226
227 return $groups;
228 }
229
230 // get the groups the current user is in
231 function User_Groups($start = 0, $num = 10, $all = false)
232 {
233 // get the globals
234 global $wpdb;
235 global $current_user;
236
237 if ($all)
238 {
239
240 $sql = "select SQL_CALC_FOUND_ROWS n.id, n.name, n.slug, n.description, n.private,
241 n.open, n.type, m.group_admin,
242 (select count(id) from ".$this->group_members_table." where group_id = n.id and status_id = 1) as members
243 from ".$this->groups_table." n
244 inner join ".$this->group_members_table." m on m.group_id = n.id
245 where m.user_id = ".$current_user->ID."
246 and m.status_id = 1
247 and (n.private = 0
248 or m.user_id = ".$current_user->ID.")
249 group by n.id
250 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
251
252 } else {
253
254 $sql = "select SQL_CALC_FOUND_ROWS n.id, n.name, n.slug, n.description, n.private,
255 n.open, n.type, m.group_admin, Group_Members
256 (select count(id) from ".$this->group_members_table." where group_id = n.id and status_id = 1) as members
257 from ".$this->groups_table." n
258 inner join ".$this->group_members_table." m on m.group_id = n.id
259 where m.user_id = ".$current_user->ID."
260 and m.status_id = 1
261 group by n.id
262 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
263
264 }
265 //print $sql."<br />";
266 $groups = $wpdb->get_results($sql);
267
268 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
269 $groups[0]->rows = $rows;
270 return $groups;
271 }
272
273 // get the number of group invitations
274 function User_Invites_Num()
275 {
276 // get the globals
277 global $wpdb;
278 global $current_user;
279
280 $sql = "select count(n.id)
281 from ".$this->groups_table." n
282 inner join ".$this->group_members_table." m on m.group_id = n.id
283 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
284 where m.user_id = ".$current_user->user_id."
285 and m.status_id = 2
286 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
287 $invites = $wpdb->get_var($sql);
288 if ($invites > 0)
289 {
290 return " (".$invites.")";
291 }
292 }
293
294 // get the groups the current user has been invited to join
295 function User_Invites($start = 0, $num = 10)
296 {
297 // get the globals
298 global $wpdb;
299 global $current_user;
300
301 $sql = "select SQL_CALC_FOUND_ROWS n.id, n.name, n.slug, n.description, n.private, n.open, n.type, m.group_admin, m.inviter_id
302 from ".$this->groups_table." n
303 inner join ".$this->group_members_table." m on m.group_id = n.id
304 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
305 where m.user_id = ".$current_user->user_id."
306 and m.status_id = 2
307 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
308 $invites = $wpdb->get_results($sql);
309 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
310 if (is_array($invites) && count($invites) > 0)
311 {
312 for ($i = 0; $i < count($invites); $i++)
313 {
314 $user_details = get_user_details($invites[$i]->inviter_id);
315 $inviter = get_userdata($invites[$i]->inviter_id);
316 $invites[$i]->inviter = $inviter->user_nicename;
317 $invites[$i]->inviter_url = "/members/" . $inviter->user_login . "/";
318 $invites[$i]->siteurl = $user_details->siteurl;
319 $invites[$i]->username = $user_details->username;
320 $invites[$i]->rows = $rows;
321 }
322 }
323 return $invites;
324 }
325
326 // get the invites for the current group
327 function Group_Invites($start = 0, $num = 10)
328 {
329 // get the globals
330 global $wpdb;
331
332 $sql = "select SQL_CALC_FOUND_ROWS b.user_id, i.user_id as inviter_id, m.group_admin
333 from ".$this->group_members_table." m
334 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
335 inner join ".$wpdb->base_prefix."users i on i.id = m.inviter_id
336 where m.group_id = ".$this->current_group->id."
337 and m.status_id = 2
338 order by m.timestamp desc
339 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
340 $invites = $wpdb->get_results($sql);
341 if (is_array($invites) && count($invites) > 0)
342 {
343 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
344 for ($i = 0; $i < count($invites); $i++)
345 {
346
347 $user = get_user_details($invites[$i]->user_id);
348 $owner = get_userdata($invites[$i]->user_id);
349
350 $inviter_user = get_user_details($invites[$i]->inviter_id);
351 $inviter = get_userdata($invites[$i]->inviter_id);
352
353 $invites[$i]->inviter = $inviter->user_nicename;
354 $invites[$i]->inviter_siteurl = $inviter->user_url;
355 $invites[$i]->inviter_username = $inviter_user->username;
356 $invites[$i]->inviter_userurl = $inviter_user->path;
357
358 $invites[$i]->name = $owner->user_nicename;
359 $invites[$i]->siteurl = $owner->user_url;
360 $invites[$i]->username = $user->username;
361 $invites[$i]->userurl = $user->path;
362
363 $invites[$i]->rows = $rows;
364
365 }
366 return $invites;
367 } else {
368 return false;
369 }
370 }
371
372 // get the members in the current group
373 function Group_Members($start = 0, $num = 10)
374 {
375 // get the globals
376 global $wpdb;
377
378 $sql = "select SQL_CALC_FOUND_ROWS b.id as user_id, b.user_login, m.group_admin
379 from ".$this->group_members_table." m
380 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
381 where m.group_id = ".$this->current_group->id."
382 and m.status_id = 1
383 order by m.timestamp desc
384 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
385 $users = $wpdb->get_results($sql);
386 if (is_array($users) && count($users) > 0)
387 {
388 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
389 $users_out = array();
390 foreach ($users as $user)
391 {
392
393 $user_details = get_user_details($user->user_login);
394 $user_details->admin = $user->group_admin;
395 $user_details->rows = $rows;
396
397 $users_out[] = $user_details;
398
399 }
400 return $users_out;
401 } else {
402 return false;
403 }
404 }
405
406 // get the deleted members in the current group
407 function Deleted_Members($start = 0, $num = 10)
408 {
409 // get the globals
410 global $wpdb;
411
412 $sql = "select SQL_CALC_FOUND_ROWS b.id as user_id, b.user_login, m.group_admin
413 from ".$this->group_members_table." m
414 inner join ".$wpdb->base_prefix."users b on b.id = m.user_id
415 where m.group_id = ".$this->current_group->id."
416 and m.status_id = 0
417 order by m.timestamp desc
418 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
419 $users = $wpdb->get_results($sql);
420 if (is_array($users) && count($users) > 0)
421 {
422 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
423 $users_out = array();
424 foreach ($users as $user)
425 {
426
427 $user_details = get_user_details($user->user_login);
428 $user_details->admin = $user->group_admin;
429 $user_details->rows = $rows;
430
431 $users_out[] = $user_details;
432
433 }
434 return $users_out;
435 } else {
436 return false;
437 }
438 }
439
440 // is the current user a member of the current group
441 function Is_Group_Member($userid = 0)
442 {
443 global $wpdb;
444
445 if ($userid == 0)
446 {
447 global $current_user;
448 $userid = $current_user->ID;
449 }
450
451 $sql = "select count(id)
452 from ".$this->group_members_table."
453 where group_id = ".$this->current_group->id."
454 and status_id = 1
455 and user_id = ".$userid.";";
456
457 if ($wpdb->get_var($sql) == 0)
458 {
459 return false;
460 } else {
461 return true;
462 }
463 }
464
465 // is the current user an administrator of the current group
466 function Is_Group_Admin($userid = 0)
467 {
468 global $wpdb;
469
470 if ($userid == 0)
471 {
472 global $current_user;
473 $userid = $current_user->ID;
474 }
475
476 $sql = "select count(id)
477 from ".$this->group_members_table."
478 where group_id = ".$this->current_group->id."
479 and status_id = 1
480 and user_id = ".$userid."
481 and group_admin = 1;";
482
483 if ($wpdb->get_var($sql) == 0)
484 {
485 return false;
486 } else {
487 return true;
488 }
489 }
490
491 // is the current user an administrator of the current group
492 function User_Is_Group_Admin()
493 {
494 global $wpdb;
495 global $current_user;
496
497 $sql = "select count(id)
498 from ".$this->group_members_table."
499 where group_id = ".$this->current_group->id."
500 and status_id = 1
501 and user_id = ".$current_user->user_id."
502 and group_admin = 1;";
503
504 if ($wpdb->get_var($sql) == 0)
505 {
506 return false;
507 } else {
508 return true;
509 }
510 }
511
512 // is the current user an administrator of any groups
513 function Has_Group_Admin_Rights()
514 {
515 global $wpdb;
516 global $current_user;
517
518 $sql = "select count(id)
519 from ".$this->group_members_table."
520 where status_id = 1
521 and user_id = ".$current_user->ID."
522 and group_admin = 1;";
523
524 if ($wpdb->get_var($sql) == 0)
525 {
526 return false;
527 } else {
528 return true;
529 }
530 }
531
532 // get the groups the current user is an administrator of
533 function Admin_Groups($start = 0, $num = 10)
534 {
535 // get the globals
536 global $wpdb;
537 global $current_user;
538
539 $sql = "select SQL_CALC_FOUND_ROWS n.id, n.name, n.slug, n.description, n.private, n.open, n.type, m.group_admin
540 from ".$this->groups_table." n
541 left outer join ".$this->group_members_table." m on m.group_id = n.id
542 where m.user_id = ".$current_user->ID."
543 and m.status_id = 1
544 and m.group_admin = 1
545 limit ".$wpdb->escape($start).", ".$wpdb->escape($num).";";
546
547 $groups = $wpdb->get_results($sql);
548 $rows = $wpdb->get_var("SELECT found_rows() AS found_rows");
549 if ($rows > 0)
550 {
551 $groups[0]->rows = $rows;
552 }
553 return $groups;
554 }
555
556 // is the current user invited to join this group
557 function Is_Invited()
558 {
559 global $current_user;
560 global $wpdb;
561
562 $sql = "select id
563 from ".$this->group_members_table."
564 where group_id = ".$this->current_group->id."
565 and status_id = 2
566 and user_id = ".$current_user->user_ID.";";
567
568 if ($wpdb->get_var($sql) == "")
569 {
570 return false;
571 } else {
572 return true;
573 }
574 }
575
576 // show a form to join this group, if allowed
577 function Join_Group_Form($message="")
578 {
579 if ($message == ""){ $message = get_site_option("bp_groups_join_message"); }
580
581 if ($this->current_group->open == "1")
582 {
583
584 print "
585 <a href=\"".$user->siteurl."/wp-admin/admin.php?page=groups.join&amp;join=".$this->current_group->id."\">Click here to join this group</a>
586 ";
587
588 } else {
589
590 print "
591 You can only join this group by being invited by a member.
592 ";
593
594 }
595 }
596
597 // get the featured group
598 function Featured_Group($imagesize="m")
599 {
600 // if the wall application is enabled
601 if (get_site_option("bp_groups_enabled") == "true")
602 {
603
604 // get the globals
605 global $wpdb;
606 global $current_site;
607 global $current_user;
608
609 $featured_group = get_site_option("bp_featured_group");
610
611 if ($featured_group != "" && $featured_group != "0")
612 {
613
614 // get the featured group
615 $group = $wpdb->get_row("select id, name, slug, description
616 from ".$this->groups_table."
617 where id = ".$wpdb->escape($featured_group).";");
618
619 $group->image = $this->Get_Group_Image($group->id, $imagesize);
620
621 return $group;
622
623 } else {
624
625 return false;
626
627 }
628
629 }
630
631 }
632
633 // try to join a group
634 function Join_Group()
635 {
636 global $wpdb;
637 global $current_user;
638 global $current_user;
639
640 $this->id = $_GET["join"];
641 $this->Get_Current_Group();
642
643 // if the current user is already a member of this group
644 if ($this->Is_Group_Member())
645 {
646
647 $this->message = "<p class=\"error\">".get_site_option("bp_groups_already_member")."</p>";
648
649 } else {
650
651 $invited = $this->Is_Invited();
652
653 // if the group is private and the current user doesn't have an invite
654 if (($this->current_group->private || !$this->current_group->open) && !invited)
655 {
656
657 $this->message = "<p class=\"error\">".get_site_option("bp_groups_not_invited")."</p>";
658
659 } else {
660
661 if ($wpdb->query("insert into ".$this->group_members_table."
662 (timestamp, status_id, user_id, group_id)
663 values
664 (UNIX_TIMESTAMP(), 1, ".$current_user->user_id.", ".$wpdb->escape($_GET["join"]).");"))
665 {
666
667 $this->id = (int)$_GET["join"];
668 $this->Get_Current_Group();
669
670 $this->message = "<p class=\"success\">".get_site_option("bp_groups_joined")."</p>";
671 } else {
672 $this->message = "<p class=\"error\">You could not join this group because of a system error</p>";
673 }
674 }
675
676 }
677 }
678
679 // get an image for a group
680 function Get_Group_Image($groupid, $size, $fallback=true, $qs = "")
681 {
682 global $current_site;
683 global $myjournal_config;
684
685 if ($qs != "")
686 {
687 $qs = "?".$qs;
688 }
689
690 $image = "";
691
692 // check if the image exists
693 if (file_exists(ABSPATH."wp-content/groups.dir/".$groupid."/group_image_".$size.".jpg"))
694 {
695 $image = $current_site->path."wp-content/groups.dir/".$groupid."/group_image_".$size.".jpg".$qs;
696 } else {
697 if ($fallback)
698 {
699 $image = $current_site->path."wp-content/groups.dir/0/group_image_".$size.".jpg";
700 } else {
701 $image = "";
702 }
703 }
704
705 return $image;
706 }
707
708 // check the tables exist, if not create them
709 function Check_Tables()
710 {
711 require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
712 global $wpdb;
713
714 if($wpdb->get_var("SHOW TABLES LIKE '".$this->groups_table."'") != $this->groups_table)
715 {
716 /* schema:
717 id: auto-incrementing identifier
718 timestamp: time this group was created
719 status_id: status of this group (0 = dead, 1 = live)
720 name: name of this group
721 slug: URL-safe version of the name (lowercase, alphanumeric, spaces replaced with -)
722 description: description of this group
723 private: 1 if this group is private, i.e. can only be seen by members
724 open: 0 if this group is private, i.e. requires members to be invited
725 type: 1 if this group is a business group, 2 if this is group is a leisure group
726 created_by: the ID of the user who created this group
727 */
728 $sql = "CREATE TABLE " . $this->groups_table . " (
729 id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
730 timestamp int DEFAULT '0' NOT NULL,
731 status_id tinyint(2) DEFAULT '1' NOT NULL,
732 name varchar(100) NOT NULL,
733 slug varchar(100) NOT NULL,
734 description varchar(300) NOT NULL,
735 private tinyint(1) DEFAULT '0' NOT NULL,
736 open tinyint(1) DEFAULT '1' NOT NULL,
737 type tinyint(1) DEFAULT '1' NOT NULL,
738 created_by int NOT NULL
739 );";
740 dbDelta($sql);
741 }
742
743 if($wpdb->get_var("SHOW TABLES LIKE '".$this->group_members_table."'") != $this->group_members_table)
744 {
745 /* schema:
746 id: auto-incrementing identifier
747 timestamp: time this group member was created
748 status_id: status of this group (0 = dead, 1 = live, 2 = invited)
749 user_id: id of this member user
750 group_admin: 1 if this member is a group administrator
751 group_id: id of the group
752 inviter_id: id of the user of the person who invited this member
753 */
754 $sql = "CREATE TABLE " . $this->group_members_table . " (
755 id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
756 timestamp int DEFAULT '0' NOT NULL,
757 status_id tinyint(2) DEFAULT '1' NOT NULL,
758 user_id int NOT NULL,
759 group_admin tinyint(1) DEFAULT '0' NOT NULL,
760 group_id int NOT NULL,
761 inviter_id int DEFAULT '0' NOT NULL
762 );";
763 dbDelta($sql);
764 }
765 return true;
766 }
767
768 // check the options exist, if not create them
769 function Check_Options()
770 {
771 foreach ($this->options as $option)
772 {
773 if (get_site_option($option[1]) == "")
774 {
775 add_site_option($option[1], $option[2]);
776 }
777 }
778 return true;
779 }
780
781 // show the groups list
782 function Groups()
783 {
784 include(ABSPATH."wp-content/mu-plugins/bp-groups/bp-groups-list.php");
785 }
786
787 // show the create group form
788 function Create()
789 {
790 include(ABSPATH."wp-content/mu-plugins/bp-groups/bp-groups-create.php");
791 }
792
793 // show the edit group form
794 function Edit()
795 {
796 include(ABSPATH."wp-content/mu-plugins/bp-groups/bp-groups-edit.php");
797 }
798
799 // show the invitations
800 function Invites()
801 {
802 include(ABSPATH."wp-content/mu-plugins/bp-groups/bp-groups-invites.php");
803 }
804
805 // show the group join search form
806 function Join()
807 {
808 include(ABSPATH."wp-content/mu-plugins/bp-groups/bp-groups-join.php");
809 }
810
811 // confirm leaving a group
812 function Confirm_Leave()
813 {
814 $this->message = "<p class=\"warning\">Are you sure you want to leave this group? <a href=\"admin.php?page=groups.class.php&amp;leave=".$_GET["leave"]."&amp;confirm=leave\" class=\"del\">Click here to confirm you want to leave this group</a> <a href=\"admin.php?page=groups.class.php\" class=\"cancel\">Click here to stay in this group</a></p>";
815 }
816
817 // invite a member
818 function Invite_Member()
819 {
820 // get the globals
821 global $wpdb;
822 global $bp_groups;
823 global $current_user;
824 global $current_user;
825
826 // is this a deleted member
827 $deleted_member = $wpdb->get_var("select id from ".$this->group_members_table." where user_id = ".$wpdb->escape($_GET["invite"])." and group_id = ".$wpdb->escape($_GET["group"])." and status_id = 0;");
828
829 // is this an existing member
830 $existing_member = $wpdb->get_var("select id from ".$this->group_members_table." where user_id = ".$wpdb->escape($_GET["invite"])." and group_id = ".$wpdb->escape($_GET["group"])." and status_id = 1;");
831
832 // is this an already invited member
833 $invited_member = $wpdb->get_var("select id from ".$this->group_members_table." where user_id = ".$wpdb->escape($_GET["invite"])." and group_id = ".$wpdb->escape($_GET["group"])." and status_id = 2;");
834
835 if ($deleted_member > 0)
836 {
837 $this->message = "<p class=\"error\">You could not invite this member because they are a deleted member of this group. Only a group administrator can invite them back.</p>";
838 } else if ($existing_member > 0) {
839 $this->message = "<p class=\"error\">You could not invite this member because they are already a member of this group.</p>";
840 } else if ($invited_member > 0) {
841 $this->message = "<p class=\"error\">You could not invite this member because they have already been invited to join this group.</p>";
842 } else {
843
844 // get the members email address
845 $invited_member = get_userdata($_GET["invite"]);
846 $invited_email = $invited_member->user_email;
847 $inviter = $current_user->user_email;
848 $group = $this->Group_Details($_GET["group"]);
849
850 $message_headers = "MIME-Version: 1.0\n" . "From: \"".get_site_option("myjournal_system_name")."\" <support@".$_SERVER["SERVER_NAME"].">\n" . "Content-Type: text/plain; charset=\"" . get_option('user_charset') . "\"\n";
851
852 $message = $current_user->user_nicename . " has invited you to join the '" . $group->name . "' group. Please log in to your dashboard to accept or reject this invitation.\n\nRegards, ".get_site_option("myjournal_system_name")." Administrator";
853
854 if ($wpdb->query("insert into ".$this->group_members_table." (timestamp, status_id, user_id, group_id, inviter_id) values (UNIX_TIMESTAMP(), 2, ".$wpdb->escape($_GET["invite"]).", ".$wpdb->escape($_GET["group"]).", ".$current_user->user_id.");") !== false && wp_mail($invited_email, get_site_option("myjournal_system_name")." group invitation", $message, $message_headers))
855 {
856 $this->message = "<p class=\"success\">Congratulations, you have invited this member</p>";
857 } else {
858 $this->message = "<p class=\"error\">You could not invite this member because of a system error</p>";
859 }
860 }
861 }
862
863 // leave a group
864 function Leave_Group()
865 {
866 // get the globals
867 global $wpdb;
868 global $current_user;
869 if ($wpdb->query("update ".$this->group_members_table." set status_id = 0 where user_id=".$current_user->user_id." and group_id = ".$wpdb->escape($_GET["leave"]).";") !== false)
870 {
871 $this->message = "<p class=\"success\">You have left the group <a href=\"admin.php?page=groups.class.php&amp;undoleave=".$_GET["leave"]."\" class=\"undo\">Undo?</a></p>";
872 } else {
873 $this->message = "<p class=\"error\">You could not leave this group because of a system error</p>";
874 }
875 }
876
877 // undo leaving a group
878 function Undo_Leave_Group()
879 {
880 // get the globals
881 global $wpdb;
882 global $current_user;
883 if ($wpdb->query("update ".$this->group_members_table." set status_id = 1 where user_id=".$current_user->user_id." and group_id = ".$wpdb->escape($_GET["undoleave"]).";") !== false)
884 {
885 $this->message = "<p class=\"success\">You have rejoined this group</p>";
886 } else {
887 $this->message = "<p class=\"error\">You could not rejoin this group because of a system error</p>";
888 }
889 }
890
891 // confirm removing a group member
892 function Confirm_Remove()
893 {
894 if ($this->User_Is_Group_Admin())
895 {
896 $this->message = "<p class=\"warning\">Are you sure you want to remove this member from this group? <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;remove=".$_GET["remove"]."&amp;confirm=remove&amp;view=members\" class=\"del\">Click here to confirm you want to remove this member</a> <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;view=members\" class=\"cancel\">Click here to keep this group member</a></p>";
897 } else {
898 $this->message = "<p class=\"error\">You cannot perform this action</p>";
899 }
900 }
901
902 // remove a group member
903 function Remove_Member()
904 {
905 // get the globals
906 global $wpdb;
907 global $current_user;
908 if ($this->User_Is_Group_Admin())
909 {
910 if ($wpdb->query("update ".$this->group_members_table." set status_id = 0 where user_id=".$wpdb->escape($_GET["remove"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
911 {
912 $this->message = "<p class=\"success\">You have removed this member <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;undoremove=".$_GET["remove"]."&amp;view=members\" class=\"undo\">Undo?</a></p>";
913 } else {
914 $this->message = "<p class=\"error\">This member could not be removed because of a system error</p>";
915 }
916 } else {
917 $this->message = "<p class=\"error\">You cannot perform this action</p>";
918 }
919 }
920
921 // undo removing a group member
922 function Undo_Remove_Member()
923 {
924 // get the globals
925 global $wpdb;
926 global $current_user;
927 if ($this->User_Is_Group_Admin())
928 {
929 if ($wpdb->query("update ".$this->group_members_table." set status_id = 1 where user_id=".$wpdb->escape($_GET["undoremove"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
930 {
931 $this->message = "<p class=\"success\">You have reinstated this member</p>";
932 } else {
933 $this->message = "<p class=\"error\">This member could not be reinstated because of a system error</p>";
934 }
935 } else {
936 $this->message = "<p class=\"error\">You cannot perform this action</p>";
937 }
938 }
939
940 // confirm cancelling an invite
941 function Confirm_Cancel()
942 {
943 if ($this->User_Is_Group_Admin())
944 {
945 $this->message = "<p class=\"warning\">Are you sure you want to cancel this group invitation? <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;cancel=".$_GET["cancel"]."&amp;confirm=cancel&amp;view=invites\" class=\"del\">Click here to confirm you want to cancel this invitation</a> <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;view=invites\" class=\"cancel\">Click here to keep this invitation</a></p>";
946 } else {
947 $this->message = "<p class=\"error\">You cannot perform this action</p>";
948 }
949 }
950
951 // cancel an invite
952 function Cancel_Invite()
953 {
954 // get the globals
955 global $wpdb;
956 global $current_user;
957 if ($this->User_Is_Group_Admin())
958 {
959 if ($wpdb->query("update ".$this->group_members_table." set status_id = 0 where user_id=".$wpdb->escape($_GET["cancel"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
960 {
961 $this->message = "<p class=\"success\">You have cancelled this invitation <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;undocancel=".$_GET["cancel"]."&amp;view=invites\" class=\"undo\">Undo?</a></p>";
962 } else {
963 $this->message = "<p class=\"error\">This invitation could not be cancelled because of a system error</p>";
964 }
965 } else {
966 $this->message = "<p class=\"error\">You cannot perform this action</p>";
967 }
968 }
969
970 // confirm reinviting a member
971 function Confirm_Reinvite()
972 {
973 if ($this->User_Is_Group_Admin())
974 {
975 $this->message = "<p class=\"warning\">Are you sure you want to invite this member? <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;reinvite=".$_GET["reinvite"]."&amp;confirm=reinvite&amp;view=deleted\" class=\"del\">Click here to confirm you want to reinvite this member</a> <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;view=deleted\" class=\"cancel\">Click here to keep this member deleted</a></p>";
976 } else {
977 $this->message = "<p class=\"error\">You cannot perform this action</p>";
978 }
979 }
980
981 // reinvite a member
982 function Reinvite_Member()
983 {
984 // get the globals
985 global $wpdb;
986 global $current_user;
987 if ($this->User_Is_Group_Admin())
988 {
989 if ($wpdb->query("update ".$this->group_members_table." set status_id = 2 where user_id=".$wpdb->escape($_GET["reinvite"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
990 {
991 $this->message = "<p class=\"success\">You have reinvited this member</p>";
992 } else {
993 $this->message = "<p class=\"error\">This member could not be invited because of a system error</p>";
994 }
995 } else {
996 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
997 }
998 }
999
1000 // confirm joining a group
1001 function Confirm_Join()
1002 {
1003 $this->message = "<p class=\"warning\">Are you sure you want to join this group? <a href=\"admin.php?page=groups.join&amp;join=".$_GET["join"]."&amp;confirm=join\" class=\"del\">Click here to confirm you want to join this group</a> <a href=\"admin.php?page=groups.join\" class=\"cancel\">Click here to cancel joining this group</a></p>";
1004 }
1005
1006 // confirm promoting a member
1007 function Confirm_Promote()
1008 {
1009 if ($this->User_Is_Group_Admin())
1010 {
1011 $this->message = "<p class=\"warning\">Are you sure you want to promote this member to be an administrator? <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;promote=".$_GET["promote"]."&amp;confirm=promote&amp;view=members\" class=\"del\">Click here to confirm you want to promote this member</a> <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;view=members\" class=\"cancel\">Click here to cancel</a></p>";
1012 } else {
1013 $this->message = "<p class=\"error\">You cannot perform this action</p>";
1014 }
1015 }
1016
1017 // promote a member
1018 function Promote_Member()
1019 {
1020 // get the globals
1021 global $wpdb;
1022 global $current_user;
1023 if ($this->User_Is_Group_Admin())
1024 {
1025 if ($wpdb->query("update ".$this->group_members_table." set group_admin = 1 where user_id=".$wpdb->escape($_GET["promote"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
1026 {
1027 $this->message = "<p class=\"success\">You have promoted this member <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;undopromote=".$_GET["promote"]."&amp;view=members\" class=\"undo\">Undo?</a></p>";
1028 } else {
1029 $this->message = "<p class=\"error\">This member could not be promoted because of a system error</p>";
1030 }
1031 } else {
1032 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1033 }
1034 }
1035
1036 // undo promote a member
1037 function Undo_Promote_Member()
1038 {
1039 // get the globals
1040 global $wpdb;
1041 global $current_user;
1042 if ($this->User_Is_Group_Admin())
1043 {
1044 if ($wpdb->query("update ".$this->group_members_table." set group_admin = 0 where user_id=".$wpdb->escape($_GET["undopromote"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
1045 {
1046 $this->message = "<p class=\"success\">You have undone promoting this member</p>";
1047 } else {
1048 $this->message = "<p class=\"error\">You could not undo promoting this member because of a system error</p>";
1049 }
1050 } else {
1051 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1052 }
1053 }
1054
1055 // confirm reinstating a member
1056 function Confirm_Reinstate()
1057 {
1058 if ($this->User_Is_Group_Admin())
1059 {
1060 $this->message = "<p class=\"warning\">Are you sure you want to reinstate this member? <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;reinstate=".$_GET["reinstate"]."&amp;confirm=reinstate&amp;view=deleted\" class=\"del\">Click here to confirm you want to reinstate this member</a> <a href=\"admin.php?page=groups.edit&amp;group=".$_GET["group"]."&amp;view=deleted\" class=\"cancel\">Click here to keep this member deleted</a></p>";
1061 } else {
1062 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1063 }
1064 }
1065
1066 // reinstate a member
1067 function Reinstate_Member()
1068 {
1069 // get the globals
1070 global $wpdb;
1071 global $current_user;
1072 if ($this->User_Is_Group_Admin())
1073 {
1074 if ($wpdb->query("update ".$this->group_members_table." set status_id = 1 where user_id=".$wpdb->escape($_GET["reinstate"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
1075 {
1076 $this->message = "<p class=\"success\">You have reinstated this member</p>";
1077 } else {
1078 $this->message = "<p class=\"error\">This member could not be reinstated because of a system error</p>";
1079 }
1080 } else {
1081 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1082 }
1083 }
1084
1085 // undo cancelling an invite
1086 function Undo_Cancel_Invite()
1087 {
1088 // get the globals
1089 global $wpdb;
1090 global $current_user;
1091 if ($this->User_Is_Group_Admin())
1092 {
1093 if ($wpdb->query("update ".$this->group_members_table." set status_id = 2 where user_id=".$wpdb->escape($_GET["undocancel"])." and group_id = ".$wpdb->escape($_GET["group"]).";") !== false)
1094 {
1095 $this->message = "<p class=\"success\">You have reinstated this invitation</p>";
1096 } else {
1097 $this->message = "<p class=\"error\">This invitation could not be reinstated because of a system error</p>";
1098 }
1099 } else {
1100 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1101 }
1102 }
1103
1104 // accept an invitation
1105 function Accept_Invite()
1106 {
1107 // get the globals
1108 global $wpdb;
1109 global $current_user;
1110 global $current_user;
1111
1112 if ($wpdb->query("update ".$this->group_members_table." set status_id = 1 where user_id=".$current_user->user_id." and group_id = ".$wpdb->escape($_GET["accept"]).";") !== false)
1113 {
1114 $this->id = (int)$_GET["accept"];
1115 $this->Get_Current_Group();
1116
1117 $this->message = "<p class=\"success\">Congratulations, you are now a member of the group.</p>";
1118 } else {
1119 $this->message = "<p class=\"error\">You could not accept this invitation because of a system error</p>";
1120 }
1121 }
1122
1123 // decline an invitation
1124 function Decline_Invite()
1125 {
1126 // get the globals
1127 global $wpdb;
1128 global $current_user;
1129
1130 if ($wpdb->query("update ".$this->group_members_table." set status_id = 0 where user_id=".$current_user->user_id." and group_id = ".$wpdb->escape($_GET["decline"]).";") !== false)
1131 {
1132 $this->message = "<p class=\"success\">You have declined this invitation. You will not be able to join the group unless someone invites you again.</p>";
1133 } else {
1134 $this->message = "<p class=\"error\">You could not decline this invitation because of a system error</p>";
1135 }
1136 }
1137
1138 // edit a group
1139 function Edit_Group()
1140 {
1141 global $wpdb;
1142
1143 $this->message = "";
1144
1145 if ($this->User_Is_Group_Admin())
1146 {
1147 $private = bp_boolean(@$_POST["private"]);
1148 $open = bp_boolean(@$_POST["open"]);
1149 if ($private == 1)
1150 {
1151 $open = 0;
1152 }
1153
1154 if ($wpdb->query("update ".$this->groups_table." set name = '".substr($wpdb->escape(stripslashes(@$_POST["name"])), 0, 100)."', description = '".substr($wpdb->escape(stripslashes(@$_POST["description"])), 0, 300)."', open = ".$open.$private." where id = ".$wpdb->escape($_GET["group"]).";") !== false && $this->Update_Group_Image())
1155 {
1156 $this->message = "<p class=\"success\">This group has been updated</p>";
1157 } else {
1158 $this->message .= "<p class=\"error\">You could not update this group because of a system error</p>";
1159 }
1160
1161 } else {
1162 $this->message = "<p class=\"error\">" . get_site_option("bp_groups_action_denied") . "</p>";
1163 }
1164 }
1165
1166 // create a group
1167 function Create_Group()
1168 {
1169 global $wpdb;
1170 global $current_user;
1171 global $current_site;
1172
1173 $this->message = "";
1174
1175 $open = bp_boolean(@$_POST["open"]);
1176 $private = bp_boolean(@$_POST["private"]);
1177 $type = (int)@$_POST["type"];
1178
1179 if ($private == 1)
1180 {
1181 $open = 0;
1182 }
1183
1184 $name = substr(stripslashes(@$_POST["name"]), 0, 100);
1185
1186 $slug = sanitize_title($name);
1187
1188 if ($wpdb->get_var("select count(id) from ".$this->groups_table." where slug = '".$slug."';") == 0)
1189 {
1190
1191 $sql = "insert into ".$this->groups_table."
1192 (timestamp, name, slug, description, open, private, type, status_id, created_by)
1193 values
1194 (UNIX_TIMESTAMP(), '".$wpdb->escape($name)."', '".$slug."', '".substr($wpdb->escape(stripslashes(@$_POST["description"])), 0, 300)."', ".$open.", ".$private.", ".$type.", 1, ".$current_user->ID.");";
1195 //print $sql."<br />";
1196 $create_group = $wpdb->query($sql);
1197
1198 $group_id = $wpdb->get_var("select id from ".$this->groups_table." where slug = '".$slug."' order by timestamp desc limit 1;");
1199
1200 $sql = "insert into ".$this->group_members_table."
1201 (timestamp, status_id, user_id, group_id, group_admin)
1202 values
1203 (UNIX_TIMESTAMP(), 1, ".$current_user->id.", ".$group_id.", 1);";
1204 //print $sql."<br />";
1205 $insert_member = $wpdb->query($sql);
1206
1207 $this->id = $group_id;
1208 $this->Get_Current_Group();
1209
1210 if ($create_group !== false && $insert_member !== false)
1211 {
1212 if ($this->Update_Group_Image())
1213 {
1214 $this->message = "<p class=\"success\">Congratulations, your new group has been created. <a href=\"".$current_site->path."groups/".$slug."/\">Click here to see it</a>.</p>";
1215 } else {
1216 $this->message = "<p class=\"success\">Congratulations, your new group has been created, however the image you have chosen could not be saved. You can try to set the image for this group any time you want. <a href=\"".$current_site->path."groups/".$slug."/\">Click here to see your new group</a>.</p>";
1217 }
1218 } else {
1219 $this->message .= "<p class=\"error\">Sorry, this group could not be created because of a system error (".$wpdb->print_error().")</p>";
1220 }
1221 } else {
1222
1223 $this->message .= "<p class=\"error\">".get_site_option("bp_error_duplicate_group_name")."<a href=\"".$current_site->path."groups/".$slug."/\">Click here to see the group with that name</a></p>";
1224
1225 }
1226 }
1227
1228 // update an image for the current group
1229 function Update_Group_Image()
1230 {
1231 // if there is an image
1232 if (is_array($_FILES["image"]) && $_FILES["image"]["name"] != "")
1233 {
1234
1235 // Upload the image using the built in WP upload function.
1236 $image = wp_handle_upload($_FILES["image"],
1237 array("action" => "save",
1238 "upload_error_strings" => $upload_error_strings,
1239 "uploads" => $uploads)
1240 );
1241
1242 global $wpdb;
1243 global $current_site;
1244 global $current_user;
1245 $image = new Image();
1246
1247 $sizes = array(
1248 array("suffix"=>"f", "width"=>200, "width_max"=>true, "width_fixed"=>false, "height"=>200, "height_max"=>true, "height_fixed"=>false),
1249 array("suffix"=>"l", "width"=>193, "width_max"=>false, "width_fixed"=>true, "height"=>260, "height_max"=>true, "height_fixed"=>false),
1250 array("suffix"=>"m", "width"=>120, "width_max"=>false, "width_fixed"=>true, "height"=>90, "height_max"=>true, "height_fixed"=>false),
1251 array("suffix"=>"t", "width"=>89, "width_max"=>false, "width_fixed"=>true, "height"=>62, "height_max"=>false, "height_fixed"=>true),
1252 array("suffix"=>"s", "width"=>36, "width_max"=>false, "width_fixed"=>true, "height"=>36, "height_max"=>false, "height_fixed"=>true),
1253 );
1254
1255 // upload and resize the new image
1256 $errors = $image->uploadAndResize($_FILES["image"], "myjournal_group_image", $sizes, "groups.dir/".$this->current_group->id, "jpg");
1257
1258 if ($errors == "")
1259 {
1260 $value = $option[1];
1261 $image->delete("groups.dir/".$this->current_group->id."_original.jpg");
1262
1263 return true;
1264 } else {
1265 $this->message .= "<p class=\"error\">".get_site_option("bp_error_saving_image")."</p><ul>" . $errors . "</ul>";
1266 $value = null;
1267 return false;
1268 }
1269
1270 } else {
1271 // no image supplied
1272 return true;
1273 }
1274 }
1275
1276 // show the current message
1277 function Show_Message()
1278 {
1279 if ($this->message != "")
1280 {
1281 print $this->message;
1282 }
1283 }
1284}
1285// create the new BP_Groups object
1286global $bp_groups;
1287$bp_groups = new BP_Groups();
1288?>