Group_member.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Table Definition for group_member
  4. */
  5. class Group_member extends Managed_DataObject
  6. {
  7. ###START_AUTOCODE
  8. /* the code below is auto generated do not remove the above tag */
  9. public $__table = 'group_member'; // table name
  10. public $group_id; // int(4) primary_key not_null
  11. public $profile_id; // int(4) primary_key not_null
  12. public $is_admin; // tinyint(1)
  13. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  14. public $created; // datetime() not_null
  15. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  16. /* the code above is auto generated do not remove the tag below */
  17. ###END_AUTOCODE
  18. public static function schemaDef()
  19. {
  20. return array(
  21. 'fields' => array(
  22. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
  23. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  24. 'is_admin' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this user an admin?'),
  25. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
  26. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  27. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  28. ),
  29. 'primary key' => array('group_id', 'profile_id'),
  30. 'unique keys' => array(
  31. 'group_member_uri_key' => array('uri'),
  32. ),
  33. 'foreign keys' => array(
  34. 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  35. 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  36. ),
  37. 'indexes' => array(
  38. // @fixme probably we want a (profile_id, created) index here?
  39. 'group_member_profile_id_idx' => array('profile_id'),
  40. 'group_member_created_idx' => array('created'),
  41. 'group_member_profile_id_created_idx' => array('profile_id', 'created'),
  42. 'group_member_group_id_created_idx' => array('group_id', 'created'),
  43. ),
  44. );
  45. }
  46. /**
  47. * Method to add a user to a group.
  48. * In most cases, you should call Profile->joinGroup() instead.
  49. *
  50. * @param integer $group_id Group to add to
  51. * @param integer $profile_id Profile being added
  52. *
  53. * @return Group_member new membership object
  54. */
  55. static function join($group_id, $profile_id)
  56. {
  57. $member = new Group_member();
  58. $member->group_id = $group_id;
  59. $member->profile_id = $profile_id;
  60. $member->created = common_sql_now();
  61. $member->uri = self::newUri(Profile::getByID($profile_id),
  62. User_group::getByID($group_id),
  63. $member->created);
  64. $result = $member->insert();
  65. if (!$result) {
  66. common_log_db_error($member, 'INSERT', __FILE__);
  67. // TRANS: Exception thrown when joining a group fails.
  68. throw new Exception(_("Group join failed."));
  69. }
  70. return $member;
  71. }
  72. static function leave($group_id, $profile_id)
  73. {
  74. $member = Group_member::pkeyGet(array('group_id' => $group_id,
  75. 'profile_id' => $profile_id));
  76. if (empty($member)) {
  77. // TRANS: Exception thrown when trying to leave a group the user is not a member of.
  78. throw new Exception(_("Not part of group."));
  79. }
  80. $result = $member->delete();
  81. if (!$result) {
  82. common_log_db_error($member, 'INSERT', __FILE__);
  83. // TRANS: Exception thrown when trying to leave a group fails.
  84. throw new Exception(_("Group leave failed."));
  85. }
  86. return true;
  87. }
  88. function getMember()
  89. {
  90. $member = Profile::getKV('id', $this->profile_id);
  91. if (empty($member)) {
  92. // TRANS: Exception thrown providing an invalid profile ID.
  93. // TRANS: %s is the invalid profile ID.
  94. throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
  95. }
  96. return $member;
  97. }
  98. function getGroup()
  99. {
  100. $group = User_group::getKV('id', $this->group_id);
  101. if (empty($group)) {
  102. // TRANS: Exception thrown providing an invalid group ID.
  103. // TRANS: %s is the invalid group ID.
  104. throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
  105. }
  106. return $group;
  107. }
  108. /**
  109. * Get stream of memberships by member
  110. *
  111. * @param integer $memberId profile ID of the member to fetch for
  112. * @param integer $offset offset from start of stream to get
  113. * @param integer $limit number of memberships to get
  114. *
  115. * @return Group_member stream of memberships, use fetch() to iterate
  116. */
  117. static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
  118. {
  119. $membership = new Group_member();
  120. $membership->profile_id = $memberId;
  121. $membership->orderBy('created DESC');
  122. $membership->limit($offset, $limit);
  123. $membership->find();
  124. return $membership;
  125. }
  126. function asActivity()
  127. {
  128. $member = $this->getMember();
  129. if (!$member) {
  130. throw new Exception("No such member: " . $this->profile_id);
  131. }
  132. $group = $this->getGroup();
  133. if (!$group) {
  134. throw new Exception("No such group: " . $this->group_id);
  135. }
  136. $act = new Activity();
  137. $act->id = $this->getUri();
  138. $act->actor = $member->asActivityObject();
  139. $act->verb = ActivityVerb::JOIN;
  140. $act->objects[] = ActivityObject::fromGroup($group);
  141. $act->time = strtotime($this->created);
  142. // TRANS: Activity title.
  143. $act->title = _("Join");
  144. // TRANS: Success message for subscribe to group attempt through OStatus.
  145. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
  146. $act->content = sprintf(_('%1$s has joined group %2$s.'),
  147. $member->getBestName(),
  148. $group->getBestName());
  149. $url = common_local_url('AtomPubShowMembership',
  150. array('profile' => $member->id,
  151. 'group' => $group->id));
  152. $act->selfLink = $url;
  153. $act->editLink = $url;
  154. return $act;
  155. }
  156. /**
  157. * Send notifications via email etc to group administrators about
  158. * this exciting new membership!
  159. */
  160. public function notify()
  161. {
  162. mail_notify_group_join($this->getGroup(), $this->getMember());
  163. }
  164. function getUri()
  165. {
  166. return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
  167. }
  168. }