Group_member.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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(255)
  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' => 255, '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_id, $group_id, $member->created);
  62. $result = $member->insert();
  63. if (!$result) {
  64. common_log_db_error($member, 'INSERT', __FILE__);
  65. // TRANS: Exception thrown when joining a group fails.
  66. throw new Exception(_("Group join failed."));
  67. }
  68. return $member;
  69. }
  70. static function leave($group_id, $profile_id)
  71. {
  72. $member = Group_member::pkeyGet(array('group_id' => $group_id,
  73. 'profile_id' => $profile_id));
  74. if (empty($member)) {
  75. // TRANS: Exception thrown when trying to leave a group the user is not a member of.
  76. throw new Exception(_("Not part of group."));
  77. }
  78. $result = $member->delete();
  79. if (!$result) {
  80. common_log_db_error($member, 'INSERT', __FILE__);
  81. // TRANS: Exception thrown when trying to leave a group fails.
  82. throw new Exception(_("Group leave failed."));
  83. }
  84. return true;
  85. }
  86. function getMember()
  87. {
  88. $member = Profile::getKV('id', $this->profile_id);
  89. if (empty($member)) {
  90. // TRANS: Exception thrown providing an invalid profile ID.
  91. // TRANS: %s is the invalid profile ID.
  92. throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
  93. }
  94. return $member;
  95. }
  96. function getGroup()
  97. {
  98. $group = User_group::getKV('id', $this->group_id);
  99. if (empty($group)) {
  100. // TRANS: Exception thrown providing an invalid group ID.
  101. // TRANS: %s is the invalid group ID.
  102. throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
  103. }
  104. return $group;
  105. }
  106. /**
  107. * Get stream of memberships by member
  108. *
  109. * @param integer $memberId profile ID of the member to fetch for
  110. * @param integer $offset offset from start of stream to get
  111. * @param integer $limit number of memberships to get
  112. *
  113. * @return Group_member stream of memberships, use fetch() to iterate
  114. */
  115. static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
  116. {
  117. $membership = new Group_member();
  118. $membership->profile_id = $memberId;
  119. $membership->orderBy('created DESC');
  120. $membership->limit($offset, $limit);
  121. $membership->find();
  122. return $membership;
  123. }
  124. function asActivity()
  125. {
  126. $member = $this->getMember();
  127. if (!$member) {
  128. throw new Exception("No such member: " . $this->profile_id);
  129. }
  130. $group = $this->getGroup();
  131. if (!$group) {
  132. throw new Exception("No such group: " . $this->group_id);
  133. }
  134. $act = new Activity();
  135. $act->id = $this->getURI();
  136. $act->actor = $member->asActivityObject();
  137. $act->verb = ActivityVerb::JOIN;
  138. $act->objects[] = ActivityObject::fromGroup($group);
  139. $act->time = strtotime($this->created);
  140. // TRANS: Activity title.
  141. $act->title = _("Join");
  142. // TRANS: Success message for subscribe to group attempt through OStatus.
  143. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
  144. $act->content = sprintf(_('%1$s has joined group %2$s.'),
  145. $member->getBestName(),
  146. $group->getBestName());
  147. $url = common_local_url('AtomPubShowMembership',
  148. array('profile' => $member->id,
  149. 'group' => $group->id));
  150. $act->selfLink = $url;
  151. $act->editLink = $url;
  152. return $act;
  153. }
  154. /**
  155. * Send notifications via email etc to group administrators about
  156. * this exciting new membership!
  157. */
  158. public function notify()
  159. {
  160. mail_notify_group_join($this->getGroup(), $this->getMember());
  161. }
  162. function getURI()
  163. {
  164. if (!empty($this->uri)) {
  165. return $this->uri;
  166. } else {
  167. return self::newURI($this->profile_id, $this->group_id, $this->created);
  168. }
  169. }
  170. static function newURI($profile_id, $group_id, $created)
  171. {
  172. return TagURI::mint('join:%d:%d:%s',
  173. $profile_id,
  174. $group_id,
  175. common_date_iso8601($created));
  176. }
  177. }