3 * Table Definition for user_group
6 class User_group extends Managed_DataObject
8 const JOIN_POLICY_OPEN = 0;
9 const JOIN_POLICY_MODERATE = 1;
10 const CACHE_WINDOW = 201;
13 /* the code below is auto generated do not remove the above tag */
15 public $__table = 'user_group'; // table name
16 public $id; // int(4) primary_key not_null
17 public $nickname; // varchar(64)
18 public $fullname; // varchar(255)
19 public $homepage; // varchar(255)
20 public $description; // text
21 public $location; // varchar(255)
22 public $original_logo; // varchar(255)
23 public $homepage_logo; // varchar(255)
24 public $stream_logo; // varchar(255)
25 public $mini_logo; // varchar(255)
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
33 /* the code above is auto generated do not remove the tag below */
36 public static function schemaDef()
40 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
42 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname for addressing'),
43 'fullname' => array('type' => 'varchar', 'length' => 255, 'description' => 'display name'),
44 'homepage' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL, cached so we dont regenerate'),
45 'description' => array('type' => 'text', 'description' => 'group description'),
46 'location' => array('type' => 'varchar', 'length' => 255, 'description' => 'related physical location, if any'),
48 'original_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'original size logo'),
49 'homepage_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'homepage (profile) size logo'),
50 'stream_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'stream-sized logo'),
51 'mini_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'mini logo'),
53 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
54 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
56 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'),
57 'mainpage' => array('type' => 'varchar', 'length' => 255, 'description' => 'page for group info to link to'),
58 'join_policy' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=open; 1=requires admin approval'),
59 'force_scope' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=never,1=sometimes,-1=always'),
61 'primary key' => array('id'),
62 'unique keys' => array(
63 'user_group_uri_key' => array('uri'),
66 'user_group_nickname_idx' => array('nickname'),
71 function defaultLogo($size)
73 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
74 AVATAR_STREAM_SIZE => 'stream',
75 AVATAR_MINI_SIZE => 'mini');
76 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
82 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
83 // normally stored in mainpage, but older ones may be null
84 if (!empty($this->mainpage)) {
85 $url = $this->mainpage;
87 $url = common_local_url('showgroup',
88 array('nickname' => $this->nickname));
91 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
98 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
99 if (!empty($this->uri)) {
102 $uri = common_local_url('groupbyid',
103 array('id' => $this->id));
106 Event::handle('EndUserGroupGetUri', array($this, &$uri));
113 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
114 $url = common_local_url('groupbyid',
115 array('id' => $this->id));
117 Event::handle('EndUserGroupPermalink', array($this, &$url));
121 function getNotices($offset, $limit, $since_id=null, $max_id=null)
123 $stream = new GroupNoticeStream($this);
125 return $stream->getNotices($offset, $limit, $since_id, $max_id);
129 function allowedNickname($nickname)
131 static $blacklist = array('new');
132 return !in_array($nickname, $blacklist);
135 function getMembers($offset=0, $limit=null) {
137 if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
138 $ids = $this->getMemberIDs($offset,
141 $key = sprintf('group:member_ids:%d', $this->id);
142 $window = self::cacheGet($key);
143 if ($window === false) {
144 $window = $this->getMemberIDs(0,
145 User_group::CACHE_WINDOW);
146 self::cacheSet($key, $window);
149 $ids = array_slice($window,
154 return Profile::multiGet('id', $ids);
157 function getMemberIDs($offset=0, $limit=null)
159 $gm = new Group_member();
162 $gm->selectAdd('profile_id');
164 $gm->group_id = $this->id;
166 $gm->orderBy('created DESC');
168 if (!is_null($limit)) {
169 $gm->limit($offset, $limit);
175 while ($gm->fetch()) {
176 $ids[] = $gm->profile_id;
184 * Get pending members, who have not yet been approved.
190 function getRequests($offset=0, $limit=null)
193 'SELECT profile.* ' .
194 'FROM profile JOIN group_join_queue '.
195 'ON profile.id = group_join_queue.profile_id ' .
196 'WHERE group_join_queue.group_id = %d ' .
197 'ORDER BY group_join_queue.created DESC ';
199 if ($limit != null) {
200 if (common_config('db','type') == 'pgsql') {
201 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
203 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
207 $members = new Profile();
209 $members->query(sprintf($qry, $this->id));
213 function getMemberCount()
215 $key = sprintf("group:member_count:%d", $this->id);
217 $cnt = self::cacheGet($key);
219 if (is_integer($cnt)) {
223 $mem = new Group_member();
224 $mem->group_id = $this->id;
226 // XXX: why 'distinct'?
228 $cnt = (int) $mem->count('distinct profile_id');
230 self::cacheSet($key, $cnt);
235 function getBlockedCount()
237 // XXX: WORM cache this
239 $block = new Group_block();
240 $block->group_id = $this->id;
242 return $block->count();
245 function getQueueCount()
247 // XXX: WORM cache this
249 $queue = new Group_join_queue();
250 $queue->group_id = $this->id;
252 return $queue->count();
255 function getAdmins($offset=0, $limit=null)
258 'SELECT profile.* ' .
259 'FROM profile JOIN group_member '.
260 'ON profile.id = group_member.profile_id ' .
261 'WHERE group_member.group_id = %d ' .
262 'AND group_member.is_admin = 1 ' .
263 'ORDER BY group_member.modified ASC ';
265 if ($limit != null) {
266 if (common_config('db','type') == 'pgsql') {
267 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
269 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
273 $admins = new Profile();
275 $admins->query(sprintf($qry, $this->id));
279 function getBlocked($offset=0, $limit=null)
282 'SELECT profile.* ' .
283 'FROM profile JOIN group_block '.
284 'ON profile.id = group_block.blocked ' .
285 'WHERE group_block.group_id = %d ' .
286 'ORDER BY group_block.modified DESC ';
288 if ($limit != null) {
289 if (common_config('db','type') == 'pgsql') {
290 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
292 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
296 $blocked = new Profile();
298 $blocked->query(sprintf($qry, $this->id));
302 function setOriginal($filename)
304 $imagefile = new ImageFile($this->id, Avatar::path($filename));
306 $orig = clone($this);
307 $this->original_logo = Avatar::url($filename);
308 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
309 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
310 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
311 common_debug(common_log_objstring($this));
312 return $this->update($orig);
315 function getBestName()
317 return ($this->fullname) ? $this->fullname : $this->nickname;
321 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
322 * if no fullname is provided.
326 function getFancyName()
328 if ($this->fullname) {
329 // TRANS: Full name of a profile or group followed by nickname in parens
330 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
332 return $this->nickname;
336 function getAliases()
342 $alias = new Group_alias();
344 $alias->group_id = $this->id;
346 if ($alias->find()) {
347 while ($alias->fetch()) {
348 $aliases[] = $alias->alias;
357 function setAliases($newaliases) {
359 $newaliases = array_unique($newaliases);
361 $oldaliases = $this->getAliases();
363 // Delete stuff that's old that not in new
365 $to_delete = array_diff($oldaliases, $newaliases);
367 // Insert stuff that's in new and not in old
369 $to_insert = array_diff($newaliases, $oldaliases);
371 $alias = new Group_alias();
373 $alias->group_id = $this->id;
375 foreach ($to_delete as $delalias) {
376 $alias->alias = $delalias;
377 $result = $alias->delete();
379 common_log_db_error($alias, 'DELETE', __FILE__);
384 foreach ($to_insert as $insalias) {
385 $alias->alias = $insalias;
386 $result = $alias->insert();
388 common_log_db_error($alias, 'INSERT', __FILE__);
396 static function getForNickname($nickname, $profile=null)
398 $nickname = common_canonical_nickname($nickname);
400 // Are there any matching remote groups this profile's in?
402 $group = $profile->getGroups(0, null);
403 while ($group->fetch()) {
404 if ($group->nickname == $nickname) {
405 // @fixme is this the best way?
406 return clone($group);
411 // If not, check local groups.
413 $group = Local_group::getKV('nickname', $nickname);
414 if (!empty($group)) {
415 return User_group::getKV('id', $group->group_id);
417 $alias = Group_alias::getKV('alias', $nickname);
418 if (!empty($alias)) {
419 return User_group::getKV('id', $alias->group_id);
424 function getUserMembers()
429 if(common_config('db','quote_identifiers'))
430 $user_table = '"user"';
431 else $user_table = 'user';
435 'FROM '. $user_table .' JOIN group_member '.
436 'ON '. $user_table .'.id = group_member.profile_id ' .
437 'WHERE group_member.group_id = %d ';
439 $user->query(sprintf($qry, $this->id));
443 while ($user->fetch()) {
452 static function maxDescription()
454 $desclimit = common_config('group', 'desclimit');
455 // null => use global limit (distinct from 0!)
456 if (is_null($desclimit)) {
457 $desclimit = common_config('site', 'textlimit');
462 static function descriptionTooLong($desc)
464 $desclimit = self::maxDescription();
465 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
468 function asAtomEntry($namespace=false, $source=false)
470 $xs = new XMLStringer(true);
473 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
474 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
479 $xs->elementStart('entry', $attrs);
482 $xs->elementStart('source');
483 $xs->element('id', null, $this->permalink());
484 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
485 $xs->element('link', array('href' => $this->permalink()));
486 $xs->element('updated', null, $this->modified);
487 $xs->elementEnd('source');
490 $xs->element('title', null, $this->nickname);
491 $xs->element('summary', null, common_xml_safe_str($this->description));
493 $xs->element('link', array('rel' => 'alternate',
494 'href' => $this->permalink()));
496 $xs->element('id', null, $this->permalink());
498 $xs->element('published', null, common_date_w3dtf($this->created));
499 $xs->element('updated', null, common_date_w3dtf($this->modified));
503 array('type' => 'html'),
504 common_xml_safe_str($this->description)
507 $xs->elementEnd('entry');
509 return $xs->getString();
512 function asAtomAuthor()
514 $xs = new XMLStringer(true);
516 $xs->elementStart('author');
517 $xs->element('name', null, $this->nickname);
518 $xs->element('uri', null, $this->permalink());
519 $xs->elementEnd('author');
521 return $xs->getString();
525 * Returns an XML string fragment with group information as an
526 * Activity Streams <activity:subject> element.
528 * Assumes that 'activity' namespace has been previously defined.
532 function asActivitySubject()
534 return $this->asActivityNoun('subject');
538 * Returns an XML string fragment with group information as an
539 * Activity Streams noun object with the given element type.
541 * Assumes that 'activity', 'georss', and 'poco' namespace has been
542 * previously defined.
544 * @param string $element one of 'actor', 'subject', 'object', 'target'
548 function asActivityNoun($element)
550 $noun = ActivityObject::fromGroup($this);
551 return $noun->asString('activity:' . $element);
556 return empty($this->homepage_logo)
557 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
558 : $this->homepage_logo;
561 static function register($fields) {
562 if (!empty($fields['userid'])) {
563 $profile = Profile::getKV('id', $fields['userid']);
564 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
565 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
567 // TRANS: Client exception thrown when a user tries to create a group while banned.
568 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
572 // MAGICALLY put fields into current scope
573 // @fixme kill extract(); it makes debugging absurdly hard
575 $defaults = array('nickname' => null,
578 'description' => null,
582 'aliases' => array(),
585 $fields = array_merge($defaults, $fields);
589 $group = new User_group();
591 $group->query('BEGIN');
597 if (empty($mainpage)) {
598 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
601 $group->nickname = $nickname;
602 $group->fullname = $fullname;
603 $group->homepage = $homepage;
604 $group->description = $description;
605 $group->location = $location;
607 $group->mainpage = $mainpage;
608 $group->created = common_sql_now();
610 if (isset($fields['join_policy'])) {
611 $group->join_policy = intval($fields['join_policy']);
613 $group->join_policy = 0;
616 if (isset($fields['force_scope'])) {
617 $group->force_scope = intval($fields['force_scope']);
619 $group->force_scope = 0;
622 if (Event::handle('StartGroupSave', array(&$group))) {
624 $result = $group->insert();
627 common_log_db_error($group, 'INSERT', __FILE__);
628 // TRANS: Server exception thrown when creating a group failed.
629 throw new ServerException(_('Could not create group.'));
632 if (!isset($uri) || empty($uri)) {
633 $orig = clone($group);
634 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
635 $result = $group->update($orig);
637 common_log_db_error($group, 'UPDATE', __FILE__);
638 // TRANS: Server exception thrown when updating a group URI failed.
639 throw new ServerException(_('Could not set group URI.'));
643 $result = $group->setAliases($aliases);
646 // TRANS: Server exception thrown when creating group aliases failed.
647 throw new ServerException(_('Could not create aliases.'));
650 $member = new Group_member();
652 $member->group_id = $group->id;
653 $member->profile_id = $userid;
654 $member->is_admin = 1;
655 $member->created = $group->created;
657 $result = $member->insert();
660 common_log_db_error($member, 'INSERT', __FILE__);
661 // TRANS: Server exception thrown when setting group membership failed.
662 throw new ServerException(_('Could not set group membership.'));
665 self::blow('profile:groups:%d', $userid);
668 $local_group = new Local_group();
670 $local_group->group_id = $group->id;
671 $local_group->nickname = $nickname;
672 $local_group->created = common_sql_now();
674 $result = $local_group->insert();
677 common_log_db_error($local_group, 'INSERT', __FILE__);
678 // TRANS: Server exception thrown when saving local group information failed.
679 throw new ServerException(_('Could not save local group info.'));
683 $group->query('COMMIT');
685 Event::handle('EndGroupSave', array($group));
692 * Handle cascading deletion, on the model of notice and profile.
694 * This should handle freeing up cached entries for the group's
695 * id, nickname, URI, and aliases. There may be other areas that
696 * are not de-cached in the UI, including the sidebar lists on
703 // Safe to delete in bulk for now
705 $related = array('Group_inbox',
710 Event::handle('UserGroupDeleteRelated', array($this, &$related));
712 foreach ($related as $cls) {
715 $inst->group_id = $this->id;
718 while ($inst->fetch()) {
725 // And related groups in the other direction...
726 $inst = new Related_group();
727 $inst->related_group_id = $this->id;
730 // Aliases and the local_group entry need to be cleared explicitly
731 // or we'll miss clearing some cache keys; that can make it hard
732 // to create a new group with one of those names or aliases.
733 $this->setAliases(array());
734 $local = Local_group::getKV('group_id', $this->id);
739 // blow the cached ids
740 self::blow('user_group:notice_ids:%d', $this->id);
743 common_log(LOG_WARNING, "Ambiguous user_group->delete(); skipping related tables.");
750 return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
751 $this->force_scope == 1);