2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Inclusive Design Institute */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
15 require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
18 * Class for individual social group
21 var $group_id; //group id
22 var $user_id; //group creator
24 var $name; //group name
25 var $type_id; //the type_id
26 var $privacy; //privacy, 0 for public, 1 for private
27 var $description; //description of this group
28 var $created_date; //sql timestamp
29 var $last_updated; //sql timestamp
30 var $group_members; //group members
31 var $group_activities; //group activities
32 var $is_valid; //set false if this is not a valid group
37 function SocialGroup($group_id){
39 $this->group_id = intval($group_id);
41 if ($this->group_id > 0){
42 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups WHERE id='.$this->group_id;
43 $result = mysql_query($sql, $db);
44 if (mysql_num_rows($result) > 0){
45 $row = mysql_fetch_assoc($result);
46 $this->user_id = $row['member_id'];
47 $this->logo = $row['logo'];
48 $this->name = $row['name'];
49 $this->type_id = $row['type_id'];
50 $this->privacy = $row['privacy'];
51 $this->description = $row['description'];
52 $this->created_date = $row['created_date'];
53 $this->last_updated = $row['last_updated'];
54 $this->group_members = $this->getGroupMembers();
55 $this->group_activities = $this->getGroupActivities();
56 $this->is_valid = true;
58 //group does not exist, most likely deleted
59 $this->is_valid = false;
66 * Retrieve a list of group members
68 * @return mixed array of members object
70 function getGroupMembers(){
72 if (!empty($this->group_members)){
73 return $this->group_members;
76 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
77 $result = mysql_query($sql, $db);
79 while ($row = mysql_fetch_assoc($result)){
80 $members[] = new Member($row['member_id']);
84 //TODO Return empty array or should i return error?
90 * Get the group activities, all the activities that happens in this group.
92 * @return mixed array of activities
94 function getGroupActivities(){
96 if (!empty($this->group_activities)){
97 return $this->group_activities;
99 $activities = array();
100 $sql = 'SELECT a,id AS id, a.title AS title FROM '.TABLE_PREFIX.'social_groups_activities g LEFT JOIN '.TABLE_PREFIX.'social_activities a ON g.activity_id=a.id WHERE g.group_id='.$this->group_id;
101 $result = mysql_query($sql, $db);
103 while($row = mysql_fetch_assoc($result)){
104 $activities[$row['id']] = $row['title'];
111 * Get a specific mesage from the given user.
112 * @param int the message id.
113 * @param int the member id, the member who created this message, or the moderator.
114 * @return the text of the message
116 function getMessage($id, $member_id){
119 $member_id = intval($member_id);
121 $sql = 'SELECT body FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' AND id='.$id;
124 if($member_id!=$this->user_id){
125 $sql .= ' AND member_id='.$member_id;
128 $rs = mysql_query($sql, $db);
130 list($body) = mysql_fetch_array($rs);
131 return htmlentities_utf8($body);
137 * Get message boards message, return a list sorted by date, in descending order
139 function getMessages(){
143 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' ORDER BY created_date DESC';
144 $rs = mysql_query($sql, $db);
147 while ($row = mysql_fetch_assoc($rs)){
148 $row['body'] = htmlentities_utf8($row['body']); //escape xss attack
149 $result [$row['id']] = $row;
157 * Get the group information
160 return $this->group_id;
163 return $this->user_id;
165 function getGroupType(){
167 //or maybe print out the exact type name
168 $sql = 'SELECT title FROM '.TABLE_PREFIX.'social_groups_types WHERE type_id='.$this->type_id;
169 $result = mysql_query($sql, $db);
170 list($type_name) = mysql_fetch_row($result);
171 return _AT($type_name);
174 if (!empty($this->logo)) {
175 $str = '<a href="'.url_rewrite(AT_SOCIAL_BASENAME.'groups/view.php?id='.$this->getID()).'"><img border="0" src="'.AT_SOCIAL_BASENAME.'groups/get_sgroup_logo.php?id='.$this->getID().'" alt="'.$this->getName().'" title="'.$this->getName().'"/></a>';
177 $str = '<img src="'.AT_SOCIAL_BASENAME.'images/placelogo.png" alt="'._AT('placelogo').'" title="'._AT('placelogo').'"/>';
183 return htmlentities_utf8($this->name);
185 //@param boolean change all carrier returns to <br/> if true.
186 function getDescription($use_nl2br=true){
187 return htmlentities_utf8($this->description, $use_nl2br);
189 function getCreatedDate(){
190 return $this->created_date;
192 function getPrivacy(){
193 //0 for public, 1 for private
194 return $this->privacy;
196 function getLastUpdated(){
197 return $this->last_updated;
200 return $this->is_valid;
204 * Add a member to the group
205 * @param int member id
206 * @return boolean true if succeded, false otherwise.
208 function addMember($member_id){
210 $member_id = intval($member_id);
212 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_members (group_id, member_id) VALUES ('.$this->group_id.", $member_id)";
213 $result = mysql_query($sql, $db);
215 //add a record to the activities
216 $act = new Activity();
217 $str1 = _AT('has_joined_group', '<a href="'. url_rewrite(AT_SOCIAL_BASENAME . 'groups/view.php?id='.$this->getID(), AT_PRETTY_URL_IS_HEADER).'">'.htmlentities_utf8($this->getName()).'</a>');
219 $act->addActivity($member_id, $str1);
228 * Sends an invitation to a member
229 * @param int member_id
231 function addInvitation($member_id) {
233 $member_id = intval($member_id);
235 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_invitations (sender_id, member_id, group_id) VALUES ('
236 .$_SESSION['member_id'].', '.$member_id.', '.$this->getID().')';
237 $result = mysql_query($sql, $db);
242 * Sends a request to the group creator
243 * @param int member_id
245 function addRequest(){
248 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_requests (sender_id, member_id, group_id) VALUES ('
249 .$_SESSION['member_id'].', '.$this->getUser().', '.$this->getID().')';
250 $result = mysql_query($sql, $db);
255 * Add message to the message board
256 * @param string the message body
258 function addMessage($body) {
259 global $db, $addslashes;
260 $body = $addslashes($body);
261 $member_id = $_SESSION['member_id'];
262 $group_id = $this->getID();
264 $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_board (member_id, group_id, body, created_date) VALUES ($member_id, $group_id, '$body', NOW())";
265 $result = mysql_query($sql, $db);
272 * @param string filename of the logo. <name.extension>
274 function updateGroupLogo($logo) {
275 global $db, $addslashes;
276 $logo = $addslashes($logo);
277 $sql = 'UPDATE '.TABLE_PREFIX."social_groups SET logo='$logo' WHERE id=".$this->getID();
278 $result = mysql_query($sql, $db);
285 * @param int the id of the message
286 * @param string message body
288 function updateMessage($id, $body){
289 global $db, $addslashes;
291 $body = $addslashes($body);
296 $sql = 'UPDATE '.TABLE_PREFIX."social_groups_board SET body='$body' WHERE id=$id";
297 $result = mysql_query($sql, $db);
303 * Remove a member from the group
304 * @param int member_id
305 * @retrun boolean troe successful and false otherwise.
307 function removeMember($member_id){
309 $member_id = intval($member_id);
311 //quit if member_id = creator id
312 if ($member_id == $this->getUser()){
316 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_members WHERE member_id=$member_id AND group_id=".$this->group_id;
317 $result = mysql_query($sql, $db);
325 * Remove logo from content/social folder
327 function removeGroupLogo(){
328 if ($this->logo!=''){
329 unlink(AT_CONTENT_DIR.'social/'. $this->logo);
331 return file_exists(AT_CONTENT_DIR.'social/'. $this->logo);
337 * Delete all group activities
338 * ToDo: Delete just one?
340 function removeGroupActivities(){
343 $act_obj = new Activity();
345 //First remove groups activities from activity table
346 $allActs = $this->group_activities;
347 foreach ($allActs as $id=>$garbage) {
348 $act_obj->deleteActivity($id);
351 //Then remove the associations from social_groups_activities
352 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_activities WHERE group_id=".$this->group_id;
353 $result = mysql_query($sql, $db);
362 * Delete all group forums
364 function removeGroupForums(){
366 include(AT_INCLUDE_PATH.'../mods/_standard/forums/lib/forums.inc.php');
368 //delete all forums for this social group
369 $sql = 'SELECT forum_id FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
370 $result = mysql_query($sql, $db);
372 while ($row = mysql_fetch_assoc($result)){
373 delete_forum($row['forum_id']);
377 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
378 $result = mysql_query($sql, $db);
387 * Delete all group members
389 function removeGroupMembers(){
391 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
392 $result = mysql_query($sql, $db);
401 * Delete all requests inside this group
403 function removeGroupRequests(){
405 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_requests WHERE group_id='.$this->group_id;
406 $result = mysql_query($sql, $db);
415 * Delete all invitations inside this group
417 function removeGroupInvitations(){
419 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_invitations WHERE group_id='.$this->group_id;
420 $result = mysql_query($sql, $db);
428 * Delete a message from the board
429 * @param int member_id
431 function removeMessage($id, $member_id){
434 $member_id = intval($member_id);
435 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_board WHERE id=$id ";
438 if ($member_id != $this->user_id){
439 $sql .= " AND member_id=$member_id";
441 $result = mysql_query($sql, $db);
447 * Delete all the messages from the board
449 function removeAllMessages(){
452 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID();
453 $result = mysql_query($sql, $db);
460 * TODO: Maybe to make a general search($string, $member_obj_array) that takess any member obj array.
461 * This can be used for friends search as well. Optimize the code and structure a bit.
462 * @param string member name
463 * @return array of Members Object
465 function searchMembers($name){
466 global $db, $addslashes;
468 //break the names by space, then accumulate the query
469 $name = $addslashes($name);
470 $sub_names = explode(' ', $name);
471 foreach($sub_names as $piece){
472 $query .= "(first_name LIKE '%$piece%' OR second_name LIKE '%$piece%' OR last_name LIKE '%$piece%' OR email LIKE '$piece') AND ";
474 //trim back the extra "AND "
475 $query = substr($query, 0, -4);
477 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members g LEFT JOIN '.TABLE_PREFIX.'members m ON g.member_id=m.member_id WHERE g.group_id='.$this->getID().' AND '.$query;
478 $rs = mysql_query($sql, $db);
480 while ($row=mysql_fetch_assoc($rs)) {
481 $result[$row['member_id']] = new Member($row['member_id']);