3 * Table Definition for user_group
6 class User_group extends Memcached_DataObject
8 const JOIN_POLICY_OPEN = 0;
9 const JOIN_POLICY_MODERATE = 1;
12 /* the code below is auto generated do not remove the above tag */
14 public $__table = 'user_group'; // table name
15 public $id; // int(4) primary_key not_null
16 public $nickname; // varchar(64)
17 public $fullname; // varchar(255)
18 public $homepage; // varchar(255)
19 public $description; // text
20 public $location; // varchar(255)
21 public $original_logo; // varchar(255)
22 public $homepage_logo; // varchar(255)
23 public $stream_logo; // varchar(255)
24 public $mini_logo; // varchar(255)
25 public $design_id; // int(4)
26 public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
27 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
28 public $uri; // varchar(255) unique_key
29 public $mainpage; // varchar(255)
30 public $join_policy; // tinyint
33 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
35 /* the code above is auto generated do not remove the tag below */
38 function defaultLogo($size)
40 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
41 AVATAR_STREAM_SIZE => 'stream',
42 AVATAR_MINI_SIZE => 'mini');
43 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
49 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
50 // normally stored in mainpage, but older ones may be null
51 if (!empty($this->mainpage)) {
52 $url = $this->mainpage;
54 $url = common_local_url('showgroup',
55 array('nickname' => $this->nickname));
58 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
65 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
66 if (!empty($this->uri)) {
69 $uri = common_local_url('groupbyid',
70 array('id' => $this->id));
73 Event::handle('EndUserGroupGetUri', array($this, &$uri));
80 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
81 $url = common_local_url('groupbyid',
82 array('id' => $this->id));
84 Event::handle('EndUserGroupPermalink', array($this, &$url));
88 function getNotices($offset, $limit, $since_id=null, $max_id=null)
90 $ids = Notice::stream(array($this, '_streamDirect'),
92 'user_group:notice_ids:' . $this->id,
93 $offset, $limit, $since_id, $max_id);
95 return Notice::getStreamByIds($ids);
98 function _streamDirect($offset, $limit, $since_id, $max_id)
100 $inbox = new Group_inbox();
102 $inbox->group_id = $this->id;
105 $inbox->selectAdd('notice_id');
107 Notice::addWhereSinceId($inbox, $since_id, 'notice_id');
108 Notice::addWhereMaxId($inbox, $max_id, 'notice_id');
110 $inbox->orderBy('created DESC, notice_id DESC');
112 if (!is_null($offset)) {
113 $inbox->limit($offset, $limit);
118 if ($inbox->find()) {
119 while ($inbox->fetch()) {
120 $ids[] = $inbox->notice_id;
127 function allowedNickname($nickname)
129 static $blacklist = array('new');
130 return !in_array($nickname, $blacklist);
133 function getMembers($offset=0, $limit=null)
136 'SELECT profile.* ' .
137 'FROM profile JOIN group_member '.
138 'ON profile.id = group_member.profile_id ' .
139 'WHERE group_member.group_id = %d ' .
140 'ORDER BY group_member.created DESC ';
142 if ($limit != null) {
143 if (common_config('db','type') == 'pgsql') {
144 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
146 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
150 $members = new Profile();
152 $members->query(sprintf($qry, $this->id));
157 * Get pending members, who have not yet been approved.
163 function getRequests($offset=0, $limit=null)
166 'SELECT profile.* ' .
167 'FROM profile JOIN group_join_queue '.
168 'ON profile.id = group_join_queue.profile_id ' .
169 'WHERE group_join_queue.group_id = %d ' .
170 'ORDER BY group_join_queue.created DESC ';
172 if ($limit != null) {
173 if (common_config('db','type') == 'pgsql') {
174 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
176 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
180 $members = new Profile();
182 $members->query(sprintf($qry, $this->id));
186 function getMemberCount()
188 // XXX: WORM cache this
190 $members = $this->getMembers();
193 /** $member->count() doesn't work. */
194 while ($members->fetch()) {
198 return $member_count;
201 function getAdmins($offset=0, $limit=null)
204 'SELECT profile.* ' .
205 'FROM profile JOIN group_member '.
206 'ON profile.id = group_member.profile_id ' .
207 'WHERE group_member.group_id = %d ' .
208 'AND group_member.is_admin = 1 ' .
209 'ORDER BY group_member.modified ASC ';
211 if ($limit != null) {
212 if (common_config('db','type') == 'pgsql') {
213 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
215 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
219 $admins = new Profile();
221 $admins->query(sprintf($qry, $this->id));
225 function getBlocked($offset=0, $limit=null)
228 'SELECT profile.* ' .
229 'FROM profile JOIN group_block '.
230 'ON profile.id = group_block.blocked ' .
231 'WHERE group_block.group_id = %d ' .
232 'ORDER BY group_block.modified DESC ';
234 if ($limit != null) {
235 if (common_config('db','type') == 'pgsql') {
236 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
238 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
242 $blocked = new Profile();
244 $blocked->query(sprintf($qry, $this->id));
248 function setOriginal($filename)
250 $imagefile = new ImageFile($this->id, Avatar::path($filename));
252 $orig = clone($this);
253 $this->original_logo = Avatar::url($filename);
254 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
255 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
256 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
257 common_debug(common_log_objstring($this));
258 return $this->update($orig);
261 function getBestName()
263 return ($this->fullname) ? $this->fullname : $this->nickname;
267 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
268 * if no fullname is provided.
272 function getFancyName()
274 if ($this->fullname) {
275 // TRANS: Full name of a profile or group followed by nickname in parens
276 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
278 return $this->nickname;
282 function getAliases()
288 $alias = new Group_alias();
290 $alias->group_id = $this->id;
292 if ($alias->find()) {
293 while ($alias->fetch()) {
294 $aliases[] = $alias->alias;
303 function setAliases($newaliases) {
305 $newaliases = array_unique($newaliases);
307 $oldaliases = $this->getAliases();
309 # Delete stuff that's old that not in new
311 $to_delete = array_diff($oldaliases, $newaliases);
313 # Insert stuff that's in new and not in old
315 $to_insert = array_diff($newaliases, $oldaliases);
317 $alias = new Group_alias();
319 $alias->group_id = $this->id;
321 foreach ($to_delete as $delalias) {
322 $alias->alias = $delalias;
323 $result = $alias->delete();
325 common_log_db_error($alias, 'DELETE', __FILE__);
330 foreach ($to_insert as $insalias) {
331 $alias->alias = $insalias;
332 $result = $alias->insert();
334 common_log_db_error($alias, 'INSERT', __FILE__);
342 static function getForNickname($nickname, $profile=null)
344 $nickname = common_canonical_nickname($nickname);
346 // Are there any matching remote groups this profile's in?
348 $group = $profile->getGroups();
349 while ($group->fetch()) {
350 if ($group->nickname == $nickname) {
351 // @fixme is this the best way?
352 return clone($group);
357 // If not, check local groups.
359 $group = Local_group::staticGet('nickname', $nickname);
360 if (!empty($group)) {
361 return User_group::staticGet('id', $group->group_id);
363 $alias = Group_alias::staticGet('alias', $nickname);
364 if (!empty($alias)) {
365 return User_group::staticGet('id', $alias->group_id);
372 return Design::staticGet('id', $this->design_id);
375 function getUserMembers()
380 if(common_config('db','quote_identifiers'))
381 $user_table = '"user"';
382 else $user_table = 'user';
386 'FROM '. $user_table .' JOIN group_member '.
387 'ON '. $user_table .'.id = group_member.profile_id ' .
388 'WHERE group_member.group_id = %d ';
390 $user->query(sprintf($qry, $this->id));
394 while ($user->fetch()) {
403 static function maxDescription()
405 $desclimit = common_config('group', 'desclimit');
406 // null => use global limit (distinct from 0!)
407 if (is_null($desclimit)) {
408 $desclimit = common_config('site', 'textlimit');
413 static function descriptionTooLong($desc)
415 $desclimit = self::maxDescription();
416 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
419 function asAtomEntry($namespace=false, $source=false)
421 $xs = new XMLStringer(true);
424 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
425 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
430 $xs->elementStart('entry', $attrs);
433 $xs->elementStart('source');
434 $xs->element('id', null, $this->permalink());
435 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
436 $xs->element('link', array('href' => $this->permalink()));
437 $xs->element('updated', null, $this->modified);
438 $xs->elementEnd('source');
441 $xs->element('title', null, $this->nickname);
442 $xs->element('summary', null, common_xml_safe_str($this->description));
444 $xs->element('link', array('rel' => 'alternate',
445 'href' => $this->permalink()));
447 $xs->element('id', null, $this->permalink());
449 $xs->element('published', null, common_date_w3dtf($this->created));
450 $xs->element('updated', null, common_date_w3dtf($this->modified));
454 array('type' => 'html'),
455 common_xml_safe_str($this->description)
458 $xs->elementEnd('entry');
460 return $xs->getString();
463 function asAtomAuthor()
465 $xs = new XMLStringer(true);
467 $xs->elementStart('author');
468 $xs->element('name', null, $this->nickname);
469 $xs->element('uri', null, $this->permalink());
470 $xs->elementEnd('author');
472 return $xs->getString();
476 * Returns an XML string fragment with group information as an
477 * Activity Streams <activity:subject> element.
479 * Assumes that 'activity' namespace has been previously defined.
483 function asActivitySubject()
485 return $this->asActivityNoun('subject');
489 * Returns an XML string fragment with group information as an
490 * Activity Streams noun object with the given element type.
492 * Assumes that 'activity', 'georss', and 'poco' namespace has been
493 * previously defined.
495 * @param string $element one of 'actor', 'subject', 'object', 'target'
499 function asActivityNoun($element)
501 $noun = ActivityObject::fromGroup($this);
502 return $noun->asString('activity:' . $element);
507 return empty($this->homepage_logo)
508 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
509 : $this->homepage_logo;
512 static function register($fields) {
513 if (!empty($fields['userid'])) {
514 $profile = Profile::staticGet('id', $fields['userid']);
515 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
516 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
518 // TRANS: Client exception thrown when a user tries to create a group while banned.
519 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
523 // MAGICALLY put fields into current scope
524 // @fixme kill extract(); it makes debugging absurdly hard
528 $group = new User_group();
530 $group->query('BEGIN');
536 if (empty($mainpage)) {
537 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
540 $group->nickname = $nickname;
541 $group->fullname = $fullname;
542 $group->homepage = $homepage;
543 $group->description = $description;
544 $group->location = $location;
546 $group->mainpage = $mainpage;
547 $group->created = common_sql_now();
548 if (isset($fields['join_policy'])) {
549 $group->join_policy = intval($fields['join_policy']);
551 $group->join_policy = 0;
554 if (Event::handle('StartGroupSave', array(&$group))) {
556 $result = $group->insert();
559 common_log_db_error($group, 'INSERT', __FILE__);
560 // TRANS: Server exception thrown when creating a group failed.
561 throw new ServerException(_('Could not create group.'));
564 if (!isset($uri) || empty($uri)) {
565 $orig = clone($group);
566 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
567 $result = $group->update($orig);
569 common_log_db_error($group, 'UPDATE', __FILE__);
570 // TRANS: Server exception thrown when updating a group URI failed.
571 throw new ServerException(_('Could not set group URI.'));
575 $result = $group->setAliases($aliases);
578 // TRANS: Server exception thrown when creating group aliases failed.
579 throw new ServerException(_('Could not create aliases.'));
582 $member = new Group_member();
584 $member->group_id = $group->id;
585 $member->profile_id = $userid;
586 $member->is_admin = 1;
587 $member->created = $group->created;
589 $result = $member->insert();
592 common_log_db_error($member, 'INSERT', __FILE__);
593 // TRANS: Server exception thrown when setting group membership failed.
594 throw new ServerException(_('Could not set group membership.'));
598 $local_group = new Local_group();
600 $local_group->group_id = $group->id;
601 $local_group->nickname = $nickname;
602 $local_group->created = common_sql_now();
604 $result = $local_group->insert();
607 common_log_db_error($local_group, 'INSERT', __FILE__);
608 // TRANS: Server exception thrown when saving local group information failed.
609 throw new ServerException(_('Could not save local group info.'));
613 $group->query('COMMIT');
615 Event::handle('EndGroupSave', array($group));
622 * Handle cascading deletion, on the model of notice and profile.
624 * This should handle freeing up cached entries for the group's
625 * id, nickname, URI, and aliases. There may be other areas that
626 * are not de-cached in the UI, including the sidebar lists on
633 // Safe to delete in bulk for now
635 $related = array('Group_inbox',
640 Event::handle('UserGroupDeleteRelated', array($this, &$related));
642 foreach ($related as $cls) {
645 $inst->group_id = $this->id;
648 while ($inst->fetch()) {
655 // And related groups in the other direction...
656 $inst = new Related_group();
657 $inst->related_group_id = $this->id;
660 // Aliases and the local_group entry need to be cleared explicitly
661 // or we'll miss clearing some cache keys; that can make it hard
662 // to create a new group with one of those names or aliases.
663 $this->setAliases(array());
664 $local = Local_group::staticGet('group_id', $this->id);
669 // blow the cached ids
670 self::blow('user_group:notice_ids:%d', $this->id);
673 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");