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;
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 $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
26 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
27 public $uri; // varchar(255) unique_key
28 public $mainpage; // varchar(255)
29 public $join_policy; // tinyint
30 public $force_scope; // tinyint
33 function staticGet($k,$v=NULL) {
34 return Memcached_DataObject::staticGet('User_group',$k,$v);
37 function multiGet($keyCol, $keyVals, $skipNulls=true)
39 return parent::multiGet('User_group', $keyCol, $keyVals, $skipNulls);
42 /* the code above is auto generated do not remove the tag below */
45 public static function schemaDef()
49 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
51 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname for addressing'),
52 'fullname' => array('type' => 'varchar', 'length' => 255, 'description' => 'display name'),
53 'homepage' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL, cached so we dont regenerate'),
54 'description' => array('type' => 'text', 'description' => 'group description'),
55 'location' => array('type' => 'varchar', 'length' => 255, 'description' => 'related physical location, if any'),
57 'original_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'original size logo'),
58 'homepage_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'homepage (profile) size logo'),
59 'stream_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'stream-sized logo'),
60 'mini_logo' => array('type' => 'varchar', 'length' => 255, 'description' => 'mini logo'),
62 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
63 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
65 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'),
66 'mainpage' => array('type' => 'varchar', 'length' => 255, 'description' => 'page for group info to link to'),
67 'join_policy' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=open; 1=requires admin approval'),
68 'force_scope' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=never,1=sometimes,-1=always'),
70 'primary key' => array('id'),
71 'unique keys' => array(
72 'user_group_uri_key' => array('uri'),
75 'user_group_nickname_idx' => array('nickname'),
80 function defaultLogo($size)
82 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
83 AVATAR_STREAM_SIZE => 'stream',
84 AVATAR_MINI_SIZE => 'mini');
85 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
91 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
92 // normally stored in mainpage, but older ones may be null
93 if (!empty($this->mainpage)) {
94 $url = $this->mainpage;
96 $url = common_local_url('showgroup',
97 array('nickname' => $this->nickname));
100 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
107 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
108 if (!empty($this->uri)) {
111 $uri = common_local_url('groupbyid',
112 array('id' => $this->id));
115 Event::handle('EndUserGroupGetUri', array($this, &$uri));
122 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
123 $url = common_local_url('groupbyid',
124 array('id' => $this->id));
126 Event::handle('EndUserGroupPermalink', array($this, &$url));
130 function getNotices($offset, $limit, $since_id=null, $max_id=null)
132 $stream = new GroupNoticeStream($this);
134 return $stream->getNotices($offset, $limit, $since_id, $max_id);
138 function allowedNickname($nickname)
140 static $blacklist = array('new');
141 return !in_array($nickname, $blacklist);
144 function getMembers($offset=0, $limit=null)
147 'SELECT profile.* ' .
148 'FROM profile JOIN group_member '.
149 'ON profile.id = group_member.profile_id ' .
150 'WHERE group_member.group_id = %d ' .
151 'ORDER BY group_member.created DESC ';
153 if ($limit != null) {
154 if (common_config('db','type') == 'pgsql') {
155 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
157 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
161 $members = new Profile();
163 $members->query(sprintf($qry, $this->id));
168 * Get pending members, who have not yet been approved.
174 function getRequests($offset=0, $limit=null)
177 'SELECT profile.* ' .
178 'FROM profile JOIN group_join_queue '.
179 'ON profile.id = group_join_queue.profile_id ' .
180 'WHERE group_join_queue.group_id = %d ' .
181 'ORDER BY group_join_queue.created DESC ';
183 if ($limit != null) {
184 if (common_config('db','type') == 'pgsql') {
185 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
187 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
191 $members = new Profile();
193 $members->query(sprintf($qry, $this->id));
197 function getMemberCount()
199 // XXX: WORM cache this
201 $members = $this->getMembers();
204 /** $member->count() doesn't work. */
205 while ($members->fetch()) {
209 return $member_count;
212 function getBlockedCount()
214 // XXX: WORM cache this
216 $block = new Group_block();
217 $block->group_id = $this->id;
219 return $block->count();
222 function getQueueCount()
224 // XXX: WORM cache this
226 $queue = new Group_join_queue();
227 $queue->group_id = $this->id;
229 return $queue->count();
232 function getAdmins($offset=0, $limit=null)
235 'SELECT profile.* ' .
236 'FROM profile JOIN group_member '.
237 'ON profile.id = group_member.profile_id ' .
238 'WHERE group_member.group_id = %d ' .
239 'AND group_member.is_admin = 1 ' .
240 'ORDER BY group_member.modified ASC ';
242 if ($limit != null) {
243 if (common_config('db','type') == 'pgsql') {
244 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
246 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
250 $admins = new Profile();
252 $admins->query(sprintf($qry, $this->id));
256 function getBlocked($offset=0, $limit=null)
259 'SELECT profile.* ' .
260 'FROM profile JOIN group_block '.
261 'ON profile.id = group_block.blocked ' .
262 'WHERE group_block.group_id = %d ' .
263 'ORDER BY group_block.modified DESC ';
265 if ($limit != null) {
266 if (common_config('db','type') == 'pgsql') {
267 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
269 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
273 $blocked = new Profile();
275 $blocked->query(sprintf($qry, $this->id));
279 function setOriginal($filename)
281 $imagefile = new ImageFile($this->id, Avatar::path($filename));
283 $orig = clone($this);
284 $this->original_logo = Avatar::url($filename);
285 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
286 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
287 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
288 common_debug(common_log_objstring($this));
289 return $this->update($orig);
292 function getBestName()
294 return ($this->fullname) ? $this->fullname : $this->nickname;
298 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
299 * if no fullname is provided.
303 function getFancyName()
305 if ($this->fullname) {
306 // TRANS: Full name of a profile or group followed by nickname in parens
307 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
309 return $this->nickname;
313 function getAliases()
319 $alias = new Group_alias();
321 $alias->group_id = $this->id;
323 if ($alias->find()) {
324 while ($alias->fetch()) {
325 $aliases[] = $alias->alias;
334 function setAliases($newaliases) {
336 $newaliases = array_unique($newaliases);
338 $oldaliases = $this->getAliases();
340 // Delete stuff that's old that not in new
342 $to_delete = array_diff($oldaliases, $newaliases);
344 // Insert stuff that's in new and not in old
346 $to_insert = array_diff($newaliases, $oldaliases);
348 $alias = new Group_alias();
350 $alias->group_id = $this->id;
352 foreach ($to_delete as $delalias) {
353 $alias->alias = $delalias;
354 $result = $alias->delete();
356 common_log_db_error($alias, 'DELETE', __FILE__);
361 foreach ($to_insert as $insalias) {
362 $alias->alias = $insalias;
363 $result = $alias->insert();
365 common_log_db_error($alias, 'INSERT', __FILE__);
373 static function getForNickname($nickname, $profile=null)
375 $nickname = common_canonical_nickname($nickname);
377 // Are there any matching remote groups this profile's in?
379 $group = $profile->getGroups();
380 while ($group->fetch()) {
381 if ($group->nickname == $nickname) {
382 // @fixme is this the best way?
383 return clone($group);
388 // If not, check local groups.
390 $group = Local_group::staticGet('nickname', $nickname);
391 if (!empty($group)) {
392 return User_group::staticGet('id', $group->group_id);
394 $alias = Group_alias::staticGet('alias', $nickname);
395 if (!empty($alias)) {
396 return User_group::staticGet('id', $alias->group_id);
401 function getUserMembers()
406 if(common_config('db','quote_identifiers'))
407 $user_table = '"user"';
408 else $user_table = 'user';
412 'FROM '. $user_table .' JOIN group_member '.
413 'ON '. $user_table .'.id = group_member.profile_id ' .
414 'WHERE group_member.group_id = %d ';
416 $user->query(sprintf($qry, $this->id));
420 while ($user->fetch()) {
429 static function maxDescription()
431 $desclimit = common_config('group', 'desclimit');
432 // null => use global limit (distinct from 0!)
433 if (is_null($desclimit)) {
434 $desclimit = common_config('site', 'textlimit');
439 static function descriptionTooLong($desc)
441 $desclimit = self::maxDescription();
442 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
445 function asAtomEntry($namespace=false, $source=false)
447 $xs = new XMLStringer(true);
450 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
451 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
456 $xs->elementStart('entry', $attrs);
459 $xs->elementStart('source');
460 $xs->element('id', null, $this->permalink());
461 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
462 $xs->element('link', array('href' => $this->permalink()));
463 $xs->element('updated', null, $this->modified);
464 $xs->elementEnd('source');
467 $xs->element('title', null, $this->nickname);
468 $xs->element('summary', null, common_xml_safe_str($this->description));
470 $xs->element('link', array('rel' => 'alternate',
471 'href' => $this->permalink()));
473 $xs->element('id', null, $this->permalink());
475 $xs->element('published', null, common_date_w3dtf($this->created));
476 $xs->element('updated', null, common_date_w3dtf($this->modified));
480 array('type' => 'html'),
481 common_xml_safe_str($this->description)
484 $xs->elementEnd('entry');
486 return $xs->getString();
489 function asAtomAuthor()
491 $xs = new XMLStringer(true);
493 $xs->elementStart('author');
494 $xs->element('name', null, $this->nickname);
495 $xs->element('uri', null, $this->permalink());
496 $xs->elementEnd('author');
498 return $xs->getString();
502 * Returns an XML string fragment with group information as an
503 * Activity Streams <activity:subject> element.
505 * Assumes that 'activity' namespace has been previously defined.
509 function asActivitySubject()
511 return $this->asActivityNoun('subject');
515 * Returns an XML string fragment with group information as an
516 * Activity Streams noun object with the given element type.
518 * Assumes that 'activity', 'georss', and 'poco' namespace has been
519 * previously defined.
521 * @param string $element one of 'actor', 'subject', 'object', 'target'
525 function asActivityNoun($element)
527 $noun = ActivityObject::fromGroup($this);
528 return $noun->asString('activity:' . $element);
533 return empty($this->homepage_logo)
534 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
535 : $this->homepage_logo;
538 static function register($fields) {
539 if (!empty($fields['userid'])) {
540 $profile = Profile::staticGet('id', $fields['userid']);
541 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
542 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
544 // TRANS: Client exception thrown when a user tries to create a group while banned.
545 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
549 // MAGICALLY put fields into current scope
550 // @fixme kill extract(); it makes debugging absurdly hard
552 $defaults = array('nickname' => null,
555 'description' => null,
559 'aliases' => array(),
562 $fields = array_merge($defaults, $fields);
566 $group = new User_group();
568 $group->query('BEGIN');
574 if (empty($mainpage)) {
575 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
578 $group->nickname = $nickname;
579 $group->fullname = $fullname;
580 $group->homepage = $homepage;
581 $group->description = $description;
582 $group->location = $location;
584 $group->mainpage = $mainpage;
585 $group->created = common_sql_now();
587 if (isset($fields['join_policy'])) {
588 $group->join_policy = intval($fields['join_policy']);
590 $group->join_policy = 0;
593 if (isset($fields['force_scope'])) {
594 $group->force_scope = intval($fields['force_scope']);
596 $group->force_scope = 0;
599 if (Event::handle('StartGroupSave', array(&$group))) {
601 $result = $group->insert();
604 common_log_db_error($group, 'INSERT', __FILE__);
605 // TRANS: Server exception thrown when creating a group failed.
606 throw new ServerException(_('Could not create group.'));
609 if (!isset($uri) || empty($uri)) {
610 $orig = clone($group);
611 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
612 $result = $group->update($orig);
614 common_log_db_error($group, 'UPDATE', __FILE__);
615 // TRANS: Server exception thrown when updating a group URI failed.
616 throw new ServerException(_('Could not set group URI.'));
620 $result = $group->setAliases($aliases);
623 // TRANS: Server exception thrown when creating group aliases failed.
624 throw new ServerException(_('Could not create aliases.'));
627 $member = new Group_member();
629 $member->group_id = $group->id;
630 $member->profile_id = $userid;
631 $member->is_admin = 1;
632 $member->created = $group->created;
634 $result = $member->insert();
637 common_log_db_error($member, 'INSERT', __FILE__);
638 // TRANS: Server exception thrown when setting group membership failed.
639 throw new ServerException(_('Could not set group membership.'));
642 self::blow('profile:groups:%d', $userid);
645 $local_group = new Local_group();
647 $local_group->group_id = $group->id;
648 $local_group->nickname = $nickname;
649 $local_group->created = common_sql_now();
651 $result = $local_group->insert();
654 common_log_db_error($local_group, 'INSERT', __FILE__);
655 // TRANS: Server exception thrown when saving local group information failed.
656 throw new ServerException(_('Could not save local group info.'));
660 $group->query('COMMIT');
662 Event::handle('EndGroupSave', array($group));
669 * Handle cascading deletion, on the model of notice and profile.
671 * This should handle freeing up cached entries for the group's
672 * id, nickname, URI, and aliases. There may be other areas that
673 * are not de-cached in the UI, including the sidebar lists on
680 // Safe to delete in bulk for now
682 $related = array('Group_inbox',
687 Event::handle('UserGroupDeleteRelated', array($this, &$related));
689 foreach ($related as $cls) {
692 $inst->group_id = $this->id;
695 while ($inst->fetch()) {
702 // And related groups in the other direction...
703 $inst = new Related_group();
704 $inst->related_group_id = $this->id;
707 // Aliases and the local_group entry need to be cleared explicitly
708 // or we'll miss clearing some cache keys; that can make it hard
709 // to create a new group with one of those names or aliases.
710 $this->setAliases(array());
711 $local = Local_group::staticGet('group_id', $this->id);
716 // blow the cached ids
717 self::blow('user_group:notice_ids:%d', $this->id);
720 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");
727 return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
728 $this->force_scope == 1);