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
31 public $force_scope; // tinyint
34 function staticGet($k,$v=NULL) {
35 return Memcached_DataObject::staticGet('User_group',$k,$v);
38 /* the code above is auto generated do not remove the tag below */
41 function defaultLogo($size)
43 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
44 AVATAR_STREAM_SIZE => 'stream',
45 AVATAR_MINI_SIZE => 'mini');
46 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
52 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
53 // normally stored in mainpage, but older ones may be null
54 if (!empty($this->mainpage)) {
55 $url = $this->mainpage;
57 $url = common_local_url('showgroup',
58 array('nickname' => $this->nickname));
61 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
68 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
69 if (!empty($this->uri)) {
72 $uri = common_local_url('groupbyid',
73 array('id' => $this->id));
76 Event::handle('EndUserGroupGetUri', array($this, &$uri));
83 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
84 $url = common_local_url('groupbyid',
85 array('id' => $this->id));
87 Event::handle('EndUserGroupPermalink', array($this, &$url));
91 function getNotices($offset, $limit, $since_id=null, $max_id=null)
93 $stream = new GroupNoticeStream($this);
95 return $stream->getNotices($offset, $limit, $since_id, $max_id);
99 function allowedNickname($nickname)
101 static $blacklist = array('new');
102 return !in_array($nickname, $blacklist);
105 function getMembers($offset=0, $limit=null)
108 'SELECT profile.* ' .
109 'FROM profile JOIN group_member '.
110 'ON profile.id = group_member.profile_id ' .
111 'WHERE group_member.group_id = %d ' .
112 'ORDER BY group_member.created DESC ';
114 if ($limit != null) {
115 if (common_config('db','type') == 'pgsql') {
116 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
118 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
122 $members = new Profile();
124 $members->query(sprintf($qry, $this->id));
129 * Get pending members, who have not yet been approved.
135 function getRequests($offset=0, $limit=null)
138 'SELECT profile.* ' .
139 'FROM profile JOIN group_join_queue '.
140 'ON profile.id = group_join_queue.profile_id ' .
141 'WHERE group_join_queue.group_id = %d ' .
142 'ORDER BY group_join_queue.created DESC ';
144 if ($limit != null) {
145 if (common_config('db','type') == 'pgsql') {
146 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
148 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
152 $members = new Profile();
154 $members->query(sprintf($qry, $this->id));
158 function getMemberCount()
160 // XXX: WORM cache this
162 $members = $this->getMembers();
165 /** $member->count() doesn't work. */
166 while ($members->fetch()) {
170 return $member_count;
173 function getAdmins($offset=0, $limit=null)
176 'SELECT profile.* ' .
177 'FROM profile JOIN group_member '.
178 'ON profile.id = group_member.profile_id ' .
179 'WHERE group_member.group_id = %d ' .
180 'AND group_member.is_admin = 1 ' .
181 'ORDER BY group_member.modified ASC ';
183 if ($limit != null) {
184 if (common_config('db','type') == 'pgsql') {
185 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
187 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
191 $admins = new Profile();
193 $admins->query(sprintf($qry, $this->id));
197 function getBlocked($offset=0, $limit=null)
200 'SELECT profile.* ' .
201 'FROM profile JOIN group_block '.
202 'ON profile.id = group_block.blocked ' .
203 'WHERE group_block.group_id = %d ' .
204 'ORDER BY group_block.modified DESC ';
206 if ($limit != null) {
207 if (common_config('db','type') == 'pgsql') {
208 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
210 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
214 $blocked = new Profile();
216 $blocked->query(sprintf($qry, $this->id));
220 function setOriginal($filename)
222 $imagefile = new ImageFile($this->id, Avatar::path($filename));
224 $orig = clone($this);
225 $this->original_logo = Avatar::url($filename);
226 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
227 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
228 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
229 common_debug(common_log_objstring($this));
230 return $this->update($orig);
233 function getBestName()
235 return ($this->fullname) ? $this->fullname : $this->nickname;
239 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
240 * if no fullname is provided.
244 function getFancyName()
246 if ($this->fullname) {
247 // TRANS: Full name of a profile or group followed by nickname in parens
248 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
250 return $this->nickname;
254 function getAliases()
260 $alias = new Group_alias();
262 $alias->group_id = $this->id;
264 if ($alias->find()) {
265 while ($alias->fetch()) {
266 $aliases[] = $alias->alias;
275 function setAliases($newaliases) {
277 $newaliases = array_unique($newaliases);
279 $oldaliases = $this->getAliases();
281 // Delete stuff that's old that not in new
283 $to_delete = array_diff($oldaliases, $newaliases);
285 // Insert stuff that's in new and not in old
287 $to_insert = array_diff($newaliases, $oldaliases);
289 $alias = new Group_alias();
291 $alias->group_id = $this->id;
293 foreach ($to_delete as $delalias) {
294 $alias->alias = $delalias;
295 $result = $alias->delete();
297 common_log_db_error($alias, 'DELETE', __FILE__);
302 foreach ($to_insert as $insalias) {
303 $alias->alias = $insalias;
304 $result = $alias->insert();
306 common_log_db_error($alias, 'INSERT', __FILE__);
314 static function getForNickname($nickname, $profile=null)
316 $nickname = common_canonical_nickname($nickname);
318 // Are there any matching remote groups this profile's in?
320 $group = $profile->getGroups();
321 while ($group->fetch()) {
322 if ($group->nickname == $nickname) {
323 // @fixme is this the best way?
324 return clone($group);
329 // If not, check local groups.
331 $group = Local_group::staticGet('nickname', $nickname);
332 if (!empty($group)) {
333 return User_group::staticGet('id', $group->group_id);
335 $alias = Group_alias::staticGet('alias', $nickname);
336 if (!empty($alias)) {
337 return User_group::staticGet('id', $alias->group_id);
344 return Design::staticGet('id', $this->design_id);
347 function getUserMembers()
352 if(common_config('db','quote_identifiers'))
353 $user_table = '"user"';
354 else $user_table = 'user';
358 'FROM '. $user_table .' JOIN group_member '.
359 'ON '. $user_table .'.id = group_member.profile_id ' .
360 'WHERE group_member.group_id = %d ';
362 $user->query(sprintf($qry, $this->id));
366 while ($user->fetch()) {
375 static function maxDescription()
377 $desclimit = common_config('group', 'desclimit');
378 // null => use global limit (distinct from 0!)
379 if (is_null($desclimit)) {
380 $desclimit = common_config('site', 'textlimit');
385 static function descriptionTooLong($desc)
387 $desclimit = self::maxDescription();
388 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
391 function asAtomEntry($namespace=false, $source=false)
393 $xs = new XMLStringer(true);
396 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
397 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
402 $xs->elementStart('entry', $attrs);
405 $xs->elementStart('source');
406 $xs->element('id', null, $this->permalink());
407 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
408 $xs->element('link', array('href' => $this->permalink()));
409 $xs->element('updated', null, $this->modified);
410 $xs->elementEnd('source');
413 $xs->element('title', null, $this->nickname);
414 $xs->element('summary', null, common_xml_safe_str($this->description));
416 $xs->element('link', array('rel' => 'alternate',
417 'href' => $this->permalink()));
419 $xs->element('id', null, $this->permalink());
421 $xs->element('published', null, common_date_w3dtf($this->created));
422 $xs->element('updated', null, common_date_w3dtf($this->modified));
426 array('type' => 'html'),
427 common_xml_safe_str($this->description)
430 $xs->elementEnd('entry');
432 return $xs->getString();
435 function asAtomAuthor()
437 $xs = new XMLStringer(true);
439 $xs->elementStart('author');
440 $xs->element('name', null, $this->nickname);
441 $xs->element('uri', null, $this->permalink());
442 $xs->elementEnd('author');
444 return $xs->getString();
448 * Returns an XML string fragment with group information as an
449 * Activity Streams <activity:subject> element.
451 * Assumes that 'activity' namespace has been previously defined.
455 function asActivitySubject()
457 return $this->asActivityNoun('subject');
461 * Returns an XML string fragment with group information as an
462 * Activity Streams noun object with the given element type.
464 * Assumes that 'activity', 'georss', and 'poco' namespace has been
465 * previously defined.
467 * @param string $element one of 'actor', 'subject', 'object', 'target'
471 function asActivityNoun($element)
473 $noun = ActivityObject::fromGroup($this);
474 return $noun->asString('activity:' . $element);
479 return empty($this->homepage_logo)
480 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
481 : $this->homepage_logo;
484 static function register($fields) {
485 if (!empty($fields['userid'])) {
486 $profile = Profile::staticGet('id', $fields['userid']);
487 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
488 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
490 // TRANS: Client exception thrown when a user tries to create a group while banned.
491 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
495 // MAGICALLY put fields into current scope
496 // @fixme kill extract(); it makes debugging absurdly hard
500 $group = new User_group();
502 $group->query('BEGIN');
508 if (empty($mainpage)) {
509 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
512 $group->nickname = $nickname;
513 $group->fullname = $fullname;
514 $group->homepage = $homepage;
515 $group->description = $description;
516 $group->location = $location;
518 $group->mainpage = $mainpage;
519 $group->created = common_sql_now();
521 if (isset($fields['join_policy'])) {
522 $group->join_policy = intval($fields['join_policy']);
524 $group->join_policy = 0;
527 if (isset($fields['force_scope'])) {
528 $group->force_scope = intval($fields['force_scope']);
530 $group->force_scope = 0;
533 if (Event::handle('StartGroupSave', array(&$group))) {
535 $result = $group->insert();
538 common_log_db_error($group, 'INSERT', __FILE__);
539 // TRANS: Server exception thrown when creating a group failed.
540 throw new ServerException(_('Could not create group.'));
543 if (!isset($uri) || empty($uri)) {
544 $orig = clone($group);
545 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
546 $result = $group->update($orig);
548 common_log_db_error($group, 'UPDATE', __FILE__);
549 // TRANS: Server exception thrown when updating a group URI failed.
550 throw new ServerException(_('Could not set group URI.'));
554 $result = $group->setAliases($aliases);
557 // TRANS: Server exception thrown when creating group aliases failed.
558 throw new ServerException(_('Could not create aliases.'));
561 $member = new Group_member();
563 $member->group_id = $group->id;
564 $member->profile_id = $userid;
565 $member->is_admin = 1;
566 $member->created = $group->created;
568 $result = $member->insert();
571 common_log_db_error($member, 'INSERT', __FILE__);
572 // TRANS: Server exception thrown when setting group membership failed.
573 throw new ServerException(_('Could not set group membership.'));
577 $local_group = new Local_group();
579 $local_group->group_id = $group->id;
580 $local_group->nickname = $nickname;
581 $local_group->created = common_sql_now();
583 $result = $local_group->insert();
586 common_log_db_error($local_group, 'INSERT', __FILE__);
587 // TRANS: Server exception thrown when saving local group information failed.
588 throw new ServerException(_('Could not save local group info.'));
592 $group->query('COMMIT');
594 Event::handle('EndGroupSave', array($group));
601 * Handle cascading deletion, on the model of notice and profile.
603 * This should handle freeing up cached entries for the group's
604 * id, nickname, URI, and aliases. There may be other areas that
605 * are not de-cached in the UI, including the sidebar lists on
612 // Safe to delete in bulk for now
614 $related = array('Group_inbox',
619 Event::handle('UserGroupDeleteRelated', array($this, &$related));
621 foreach ($related as $cls) {
624 $inst->group_id = $this->id;
627 while ($inst->fetch()) {
634 // And related groups in the other direction...
635 $inst = new Related_group();
636 $inst->related_group_id = $this->id;
639 // Aliases and the local_group entry need to be cleared explicitly
640 // or we'll miss clearing some cache keys; that can make it hard
641 // to create a new group with one of those names or aliases.
642 $this->setAliases(array());
643 $local = Local_group::staticGet('group_id', $this->id);
648 // blow the cached ids
649 self::blow('user_group:notice_ids:%d', $this->id);
652 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");
659 return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
660 $this->force_scope == 1);