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 getAdmins($offset=0, $limit=null)
215 'SELECT profile.* ' .
216 'FROM profile JOIN group_member '.
217 'ON profile.id = group_member.profile_id ' .
218 'WHERE group_member.group_id = %d ' .
219 'AND group_member.is_admin = 1 ' .
220 'ORDER BY group_member.modified ASC ';
222 if ($limit != null) {
223 if (common_config('db','type') == 'pgsql') {
224 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
226 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
230 $admins = new Profile();
232 $admins->query(sprintf($qry, $this->id));
236 function getBlocked($offset=0, $limit=null)
239 'SELECT profile.* ' .
240 'FROM profile JOIN group_block '.
241 'ON profile.id = group_block.blocked ' .
242 'WHERE group_block.group_id = %d ' .
243 'ORDER BY group_block.modified DESC ';
245 if ($limit != null) {
246 if (common_config('db','type') == 'pgsql') {
247 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
249 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
253 $blocked = new Profile();
255 $blocked->query(sprintf($qry, $this->id));
259 function setOriginal($filename)
261 $imagefile = new ImageFile($this->id, Avatar::path($filename));
263 $orig = clone($this);
264 $this->original_logo = Avatar::url($filename);
265 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
266 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
267 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
268 common_debug(common_log_objstring($this));
269 return $this->update($orig);
272 function getBestName()
274 return ($this->fullname) ? $this->fullname : $this->nickname;
278 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
279 * if no fullname is provided.
283 function getFancyName()
285 if ($this->fullname) {
286 // TRANS: Full name of a profile or group followed by nickname in parens
287 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
289 return $this->nickname;
293 function getAliases()
299 $alias = new Group_alias();
301 $alias->group_id = $this->id;
303 if ($alias->find()) {
304 while ($alias->fetch()) {
305 $aliases[] = $alias->alias;
314 function setAliases($newaliases) {
316 $newaliases = array_unique($newaliases);
318 $oldaliases = $this->getAliases();
320 // Delete stuff that's old that not in new
322 $to_delete = array_diff($oldaliases, $newaliases);
324 // Insert stuff that's in new and not in old
326 $to_insert = array_diff($newaliases, $oldaliases);
328 $alias = new Group_alias();
330 $alias->group_id = $this->id;
332 foreach ($to_delete as $delalias) {
333 $alias->alias = $delalias;
334 $result = $alias->delete();
336 common_log_db_error($alias, 'DELETE', __FILE__);
341 foreach ($to_insert as $insalias) {
342 $alias->alias = $insalias;
343 $result = $alias->insert();
345 common_log_db_error($alias, 'INSERT', __FILE__);
353 static function getForNickname($nickname, $profile=null)
355 $nickname = common_canonical_nickname($nickname);
357 // Are there any matching remote groups this profile's in?
359 $group = $profile->getGroups();
360 while ($group->fetch()) {
361 if ($group->nickname == $nickname) {
362 // @fixme is this the best way?
363 return clone($group);
368 // If not, check local groups.
370 $group = Local_group::staticGet('nickname', $nickname);
371 if (!empty($group)) {
372 return User_group::staticGet('id', $group->group_id);
374 $alias = Group_alias::staticGet('alias', $nickname);
375 if (!empty($alias)) {
376 return User_group::staticGet('id', $alias->group_id);
381 function getUserMembers()
386 if(common_config('db','quote_identifiers'))
387 $user_table = '"user"';
388 else $user_table = 'user';
392 'FROM '. $user_table .' JOIN group_member '.
393 'ON '. $user_table .'.id = group_member.profile_id ' .
394 'WHERE group_member.group_id = %d ';
396 $user->query(sprintf($qry, $this->id));
400 while ($user->fetch()) {
409 static function maxDescription()
411 $desclimit = common_config('group', 'desclimit');
412 // null => use global limit (distinct from 0!)
413 if (is_null($desclimit)) {
414 $desclimit = common_config('site', 'textlimit');
419 static function descriptionTooLong($desc)
421 $desclimit = self::maxDescription();
422 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
425 function asAtomEntry($namespace=false, $source=false)
427 $xs = new XMLStringer(true);
430 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
431 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
436 $xs->elementStart('entry', $attrs);
439 $xs->elementStart('source');
440 $xs->element('id', null, $this->permalink());
441 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
442 $xs->element('link', array('href' => $this->permalink()));
443 $xs->element('updated', null, $this->modified);
444 $xs->elementEnd('source');
447 $xs->element('title', null, $this->nickname);
448 $xs->element('summary', null, common_xml_safe_str($this->description));
450 $xs->element('link', array('rel' => 'alternate',
451 'href' => $this->permalink()));
453 $xs->element('id', null, $this->permalink());
455 $xs->element('published', null, common_date_w3dtf($this->created));
456 $xs->element('updated', null, common_date_w3dtf($this->modified));
460 array('type' => 'html'),
461 common_xml_safe_str($this->description)
464 $xs->elementEnd('entry');
466 return $xs->getString();
469 function asAtomAuthor()
471 $xs = new XMLStringer(true);
473 $xs->elementStart('author');
474 $xs->element('name', null, $this->nickname);
475 $xs->element('uri', null, $this->permalink());
476 $xs->elementEnd('author');
478 return $xs->getString();
482 * Returns an XML string fragment with group information as an
483 * Activity Streams <activity:subject> element.
485 * Assumes that 'activity' namespace has been previously defined.
489 function asActivitySubject()
491 return $this->asActivityNoun('subject');
495 * Returns an XML string fragment with group information as an
496 * Activity Streams noun object with the given element type.
498 * Assumes that 'activity', 'georss', and 'poco' namespace has been
499 * previously defined.
501 * @param string $element one of 'actor', 'subject', 'object', 'target'
505 function asActivityNoun($element)
507 $noun = ActivityObject::fromGroup($this);
508 return $noun->asString('activity:' . $element);
513 return empty($this->homepage_logo)
514 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
515 : $this->homepage_logo;
518 static function register($fields) {
519 if (!empty($fields['userid'])) {
520 $profile = Profile::staticGet('id', $fields['userid']);
521 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
522 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
524 // TRANS: Client exception thrown when a user tries to create a group while banned.
525 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
529 // MAGICALLY put fields into current scope
530 // @fixme kill extract(); it makes debugging absurdly hard
534 $group = new User_group();
536 $group->query('BEGIN');
542 if (empty($mainpage)) {
543 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
546 $group->nickname = $nickname;
547 $group->fullname = $fullname;
548 $group->homepage = $homepage;
549 $group->description = $description;
550 $group->location = $location;
552 $group->mainpage = $mainpage;
553 $group->created = common_sql_now();
555 if (isset($fields['join_policy'])) {
556 $group->join_policy = intval($fields['join_policy']);
558 $group->join_policy = 0;
561 if (isset($fields['force_scope'])) {
562 $group->force_scope = intval($fields['force_scope']);
564 $group->force_scope = 0;
567 if (Event::handle('StartGroupSave', array(&$group))) {
569 $result = $group->insert();
572 common_log_db_error($group, 'INSERT', __FILE__);
573 // TRANS: Server exception thrown when creating a group failed.
574 throw new ServerException(_('Could not create group.'));
577 if (!isset($uri) || empty($uri)) {
578 $orig = clone($group);
579 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
580 $result = $group->update($orig);
582 common_log_db_error($group, 'UPDATE', __FILE__);
583 // TRANS: Server exception thrown when updating a group URI failed.
584 throw new ServerException(_('Could not set group URI.'));
588 $result = $group->setAliases($aliases);
591 // TRANS: Server exception thrown when creating group aliases failed.
592 throw new ServerException(_('Could not create aliases.'));
595 $member = new Group_member();
597 $member->group_id = $group->id;
598 $member->profile_id = $userid;
599 $member->is_admin = 1;
600 $member->created = $group->created;
602 $result = $member->insert();
605 common_log_db_error($member, 'INSERT', __FILE__);
606 // TRANS: Server exception thrown when setting group membership failed.
607 throw new ServerException(_('Could not set group membership.'));
610 self::blow('profile:groups:%d', $userid);
613 $local_group = new Local_group();
615 $local_group->group_id = $group->id;
616 $local_group->nickname = $nickname;
617 $local_group->created = common_sql_now();
619 $result = $local_group->insert();
622 common_log_db_error($local_group, 'INSERT', __FILE__);
623 // TRANS: Server exception thrown when saving local group information failed.
624 throw new ServerException(_('Could not save local group info.'));
628 $group->query('COMMIT');
630 Event::handle('EndGroupSave', array($group));
637 * Handle cascading deletion, on the model of notice and profile.
639 * This should handle freeing up cached entries for the group's
640 * id, nickname, URI, and aliases. There may be other areas that
641 * are not de-cached in the UI, including the sidebar lists on
648 // Safe to delete in bulk for now
650 $related = array('Group_inbox',
655 Event::handle('UserGroupDeleteRelated', array($this, &$related));
657 foreach ($related as $cls) {
660 $inst->group_id = $this->id;
663 while ($inst->fetch()) {
670 // And related groups in the other direction...
671 $inst = new Related_group();
672 $inst->related_group_id = $this->id;
675 // Aliases and the local_group entry need to be cleared explicitly
676 // or we'll miss clearing some cache keys; that can make it hard
677 // to create a new group with one of those names or aliases.
678 $this->setAliases(array());
679 $local = Local_group::staticGet('group_id', $this->id);
684 // blow the cached ids
685 self::blow('user_group:notice_ids:%d', $this->id);
688 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");
695 return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
696 $this->force_scope == 1);