| 
									
										
										
										
											2009-01-13 00:35:41 -05:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 00:35:41 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for group_member | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Group_member extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2009-01-13 00:35:41 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     ###START_AUTOCODE
 | 
					
						
							|  |  |  |     /* the code below is auto generated do not remove the above tag */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public $__table = 'group_member';                    // table name
 | 
					
						
							|  |  |  |     public $group_id;                        // int(4)  primary_key not_null
 | 
					
						
							|  |  |  |     public $profile_id;                      // int(4)  primary_key not_null
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public $is_admin;                        // bool    default_false
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |     public $uri;                             // varchar(191)   not 255 because utf8mb4 takes more space
 | 
					
						
							| 
									
										
										
										
											2019-07-25 00:43:25 +01:00
										 |  |  |     public $created;                         // datetime()   not_null default_0000-00-00%2000%3A00%3A00
 | 
					
						
							|  |  |  |     public $modified;                        // datetime()   not_null default_CURRENT_TIMESTAMP
 | 
					
						
							| 
									
										
										
										
											2009-01-13 00:35:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* the code above is auto generated do not remove the tag below */ | 
					
						
							|  |  |  |     ###END_AUTOCODE
 | 
					
						
							| 
									
										
										
										
											2009-01-21 21:52:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 18:02:29 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'), | 
					
						
							|  |  |  |                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'), | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |                 'is_admin' => array('type' => 'bool', 'default' => false, 'description' => 'is this user an admin?'), | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'), | 
					
						
							| 
									
										
										
										
											2019-07-25 00:43:25 +01:00
										 |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'), | 
					
						
							|  |  |  |                 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'), | 
					
						
							| 
									
										
										
										
											2011-08-22 18:02:29 -04:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('group_id', 'profile_id'), | 
					
						
							| 
									
										
										
										
											2011-08-22 18:13:02 -04:00
										 |  |  |             'unique keys' => array( | 
					
						
							|  |  |  |                 'group_member_uri_key' => array('uri'), | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2011-08-22 18:02:29 -04:00
										 |  |  |             'foreign keys' => array( | 
					
						
							|  |  |  |                 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')), | 
					
						
							|  |  |  |                 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							|  |  |  |                 // @fixme probably we want a (profile_id, created) index here?
 | 
					
						
							|  |  |  |                 'group_member_profile_id_idx' => array('profile_id'), | 
					
						
							|  |  |  |                 'group_member_created_idx' => array('created'), | 
					
						
							| 
									
										
										
										
											2011-10-20 12:50:39 -04:00
										 |  |  |                 'group_member_profile_id_created_idx' => array('profile_id', 'created'), | 
					
						
							|  |  |  |                 'group_member_group_id_created_idx' => array('group_id', 'created'), | 
					
						
							| 
									
										
										
										
											2011-08-22 18:02:29 -04:00
										 |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Method to add a user to a group. | 
					
						
							| 
									
										
										
										
											2011-03-21 14:35:29 -07:00
										 |  |  |      * In most cases, you should call Profile->joinGroup() instead. | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param integer $group_id   Group to add to | 
					
						
							|  |  |  |      * @param integer $profile_id Profile being added | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |      * @return Group_member new membership object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public static function join($group_id, $profile_id) | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $member = new Group_member(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $member->group_id   = $group_id; | 
					
						
							|  |  |  |         $member->profile_id = $profile_id; | 
					
						
							|  |  |  |         $member->created    = common_sql_now(); | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |         $member->uri        = self::newUri( | 
					
						
							|  |  |  |             Profile::getByID($profile_id), | 
					
						
							|  |  |  |             User_group::getByID($group_id), | 
					
						
							|  |  |  |             $member->created | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $result = $member->insert(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$result) { | 
					
						
							|  |  |  |             common_log_db_error($member, 'INSERT', __FILE__); | 
					
						
							| 
									
										
										
										
											2010-07-29 13:01:04 +02:00
										 |  |  |             // TRANS: Exception thrown when joining a group fails.
 | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |             throw new Exception(_("Group join failed.")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |         return $member; | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public static function leave($group_id, $profile_id) | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $member = Group_member::pkeyGet(array('group_id' => $group_id, | 
					
						
							|  |  |  |                                               'profile_id' => $profile_id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($member)) { | 
					
						
							| 
									
										
										
										
											2010-07-29 13:01:04 +02:00
										 |  |  |             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
 | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |             throw new Exception(_("Not part of group.")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $member->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$result) { | 
					
						
							|  |  |  |             common_log_db_error($member, 'INSERT', __FILE__); | 
					
						
							| 
									
										
										
										
											2010-07-29 13:01:04 +02:00
										 |  |  |             // TRANS: Exception thrown when trying to leave a group fails.
 | 
					
						
							| 
									
										
										
										
											2010-01-13 02:16:13 -08:00
										 |  |  |             throw new Exception(_("Group leave failed.")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function getMember() | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $member = Profile::getKV('id', $this->profile_id); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (empty($member)) { | 
					
						
							| 
									
										
										
										
											2010-09-28 23:21:09 +02:00
										 |  |  |             // TRANS: Exception thrown providing an invalid profile ID.
 | 
					
						
							|  |  |  |             // TRANS: %s is the invalid profile ID.
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |             throw new Exception(sprintf(_("Profile ID %s is invalid."), $this->profile_id)); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $member; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function getGroup() | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $group  = User_group::getKV('id', $this->group_id); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (empty($group)) { | 
					
						
							| 
									
										
										
										
											2010-09-28 23:21:09 +02:00
										 |  |  |             // TRANS: Exception thrown providing an invalid group ID.
 | 
					
						
							|  |  |  |             // TRANS: %s is the invalid group ID.
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |             throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id)); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $group; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get stream of memberships by member | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param integer $memberId profile ID of the member to fetch for | 
					
						
							|  |  |  |      * @param integer $offset   offset from start of stream to get | 
					
						
							|  |  |  |      * @param integer $limit    number of memberships to get | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Group_member stream of memberships, use fetch() to iterate | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public static function byMember($memberId, $offset = 0, $limit = GROUPS_PER_PAGE) | 
					
						
							| 
									
										
										
										
											2010-12-13 13:50:39 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $membership = new Group_member(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $membership->profile_id = $memberId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $membership->orderBy('created DESC'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $membership->limit($offset, $limit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $membership->find(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $membership; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function asActivity() | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $member = $this->getMember(); | 
					
						
							| 
									
										
										
										
											2013-06-30 12:07:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$member) { | 
					
						
							|  |  |  |             throw new Exception("No such member: " . $this->profile_id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |         $group  = $this->getGroup(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-30 12:07:55 -04:00
										 |  |  |         if (!$group) { | 
					
						
							|  |  |  |             throw new Exception("No such group: " . $this->group_id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |         $act = new Activity(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-10 22:09:51 +02:00
										 |  |  |         $act->id = $this->getUri(); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-02 18:50:28 +02:00
										 |  |  |         $act->actor     = $member->asActivityObject(); | 
					
						
							| 
									
										
										
										
											2010-09-15 07:11:24 -04:00
										 |  |  |         $act->verb      = ActivityVerb::JOIN; | 
					
						
							|  |  |  |         $act->objects[] = ActivityObject::fromGroup($group); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $act->time  = strtotime($this->created); | 
					
						
							| 
									
										
										
										
											2010-09-28 23:21:09 +02:00
										 |  |  |         // TRANS: Activity title.
 | 
					
						
							| 
									
										
										
										
											2010-09-15 07:11:24 -04:00
										 |  |  |         $act->title = _("Join"); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // TRANS: Success message for subscribe to group attempt through OStatus.
 | 
					
						
							|  |  |  |         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |         $act->content = sprintf( | 
					
						
							|  |  |  |             _('%1$s has joined group %2$s.'), | 
					
						
							|  |  |  |             $member->getBestName(), | 
					
						
							|  |  |  |             $group->getBestName() | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |         $url = common_local_url( | 
					
						
							|  |  |  |             'AtomPubShowMembership', | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'profile' => $member->id, | 
					
						
							|  |  |  |                 'group'   => $group->id, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-12-13 12:40:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $act->selfLink = $url; | 
					
						
							|  |  |  |         $act->editLink = $url; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-14 11:01:29 -04:00
										 |  |  |         return $act; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-22 16:26:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Send notifications via email etc to group administrators about | 
					
						
							|  |  |  |      * this exciting new membership! | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function notify() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         mail_notify_group_join($this->getGroup(), $this->getMember()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-22 16:36:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function getUri() | 
					
						
							| 
									
										
										
										
											2011-08-22 16:36:23 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:09:51 +02:00
										 |  |  |         return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created); | 
					
						
							| 
									
										
										
										
											2011-08-22 16:36:23 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-13 00:35:41 -05:00
										 |  |  | } |