3 * Table Definition for user_group
6 class User_group extends Memcached_DataObject
9 /* the code below is auto generated do not remove the above tag */
11 public $__table = 'user_group'; // table name
12 public $id; // int(4) primary_key not_null
13 public $nickname; // varchar(64)
14 public $fullname; // varchar(255)
15 public $homepage; // varchar(255)
16 public $description; // text
17 public $location; // varchar(255)
18 public $original_logo; // varchar(255)
19 public $homepage_logo; // varchar(255)
20 public $stream_logo; // varchar(255)
21 public $mini_logo; // varchar(255)
22 public $design_id; // int(4)
23 public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
24 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
25 public $uri; // varchar(255) unique_key
26 public $mainpage; // varchar(255)
29 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
31 /* the code above is auto generated do not remove the tag below */
34 function defaultLogo($size)
36 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
37 AVATAR_STREAM_SIZE => 'stream',
38 AVATAR_MINI_SIZE => 'mini');
39 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
45 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
46 // normally stored in mainpage, but older ones may be null
47 if (!empty($this->mainpage)) {
48 $url = $this->mainpage;
50 $url = common_local_url('showgroup',
51 array('nickname' => $this->nickname));
54 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
61 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
62 if (!empty($this->uri)) {
65 $uri = common_local_url('groupbyid',
66 array('id' => $this->id));
69 Event::handle('EndUserGroupGetUri', array($this, &$uri));
76 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
77 $url = common_local_url('groupbyid',
78 array('id' => $this->id));
80 Event::handle('EndUserGroupPermalink', array($this, &$url));
84 function getNotices($offset, $limit, $since_id=null, $max_id=null)
86 $ids = Notice::stream(array($this, '_streamDirect'),
88 'user_group:notice_ids:' . $this->id,
89 $offset, $limit, $since_id, $max_id);
91 return Notice::getStreamByIds($ids);
94 function _streamDirect($offset, $limit, $since_id, $max_id)
96 $inbox = new Group_inbox();
98 $inbox->group_id = $this->id;
101 $inbox->selectAdd('notice_id');
103 Notice::addWhereSinceId($inbox, $since_id, 'notice_id');
104 Notice::addWhereMaxId($inbox, $max_id, 'notice_id');
106 $inbox->orderBy('created DESC, notice_id DESC');
108 if (!is_null($offset)) {
109 $inbox->limit($offset, $limit);
114 if ($inbox->find()) {
115 while ($inbox->fetch()) {
116 $ids[] = $inbox->notice_id;
123 function allowedNickname($nickname)
125 static $blacklist = array('new');
126 return !in_array($nickname, $blacklist);
129 function getMembers($offset=0, $limit=null)
132 'SELECT profile.* ' .
133 'FROM profile JOIN group_member '.
134 'ON profile.id = group_member.profile_id ' .
135 'WHERE group_member.group_id = %d ' .
136 'ORDER BY group_member.created DESC ';
138 if ($limit != null) {
139 if (common_config('db','type') == 'pgsql') {
140 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
142 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
146 $members = new Profile();
148 $members->query(sprintf($qry, $this->id));
152 function getMemberCount()
154 // XXX: WORM cache this
156 $members = $this->getMembers();
159 /** $member->count() doesn't work. */
160 while ($members->fetch()) {
164 return $member_count;
167 function getAdmins($offset=0, $limit=null)
170 'SELECT profile.* ' .
171 'FROM profile JOIN group_member '.
172 'ON profile.id = group_member.profile_id ' .
173 'WHERE group_member.group_id = %d ' .
174 'AND group_member.is_admin = 1 ' .
175 'ORDER BY group_member.modified ASC ';
177 if ($limit != null) {
178 if (common_config('db','type') == 'pgsql') {
179 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
181 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
185 $admins = new Profile();
187 $admins->query(sprintf($qry, $this->id));
191 function getBlocked($offset=0, $limit=null)
194 'SELECT profile.* ' .
195 'FROM profile JOIN group_block '.
196 'ON profile.id = group_block.blocked ' .
197 'WHERE group_block.group_id = %d ' .
198 'ORDER BY group_block.modified DESC ';
200 if ($limit != null) {
201 if (common_config('db','type') == 'pgsql') {
202 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
204 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
208 $blocked = new Profile();
210 $blocked->query(sprintf($qry, $this->id));
214 function setOriginal($filename)
216 $imagefile = new ImageFile($this->id, Avatar::path($filename));
218 $orig = clone($this);
219 $this->original_logo = Avatar::url($filename);
220 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
221 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
222 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
223 common_debug(common_log_objstring($this));
224 return $this->update($orig);
227 function getBestName()
229 return ($this->fullname) ? $this->fullname : $this->nickname;
233 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
234 * if no fullname is provided.
238 function getFancyName()
240 if ($this->fullname) {
241 // TRANS: Full name of a profile or group followed by nickname in parens
242 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
244 return $this->nickname;
248 function getAliases()
254 $alias = new Group_alias();
256 $alias->group_id = $this->id;
258 if ($alias->find()) {
259 while ($alias->fetch()) {
260 $aliases[] = $alias->alias;
269 function setAliases($newaliases) {
271 $newaliases = array_unique($newaliases);
273 $oldaliases = $this->getAliases();
275 # Delete stuff that's old that not in new
277 $to_delete = array_diff($oldaliases, $newaliases);
279 # Insert stuff that's in new and not in old
281 $to_insert = array_diff($newaliases, $oldaliases);
283 $alias = new Group_alias();
285 $alias->group_id = $this->id;
287 foreach ($to_delete as $delalias) {
288 $alias->alias = $delalias;
289 $result = $alias->delete();
291 common_log_db_error($alias, 'DELETE', __FILE__);
296 foreach ($to_insert as $insalias) {
297 $alias->alias = $insalias;
298 $result = $alias->insert();
300 common_log_db_error($alias, 'INSERT', __FILE__);
308 static function getForNickname($nickname, $profile=null)
310 $nickname = common_canonical_nickname($nickname);
312 // Are there any matching remote groups this profile's in?
314 $group = $profile->getGroups();
315 while ($group->fetch()) {
316 if ($group->nickname == $nickname) {
317 // @fixme is this the best way?
318 return clone($group);
323 // If not, check local groups.
325 $group = Local_group::staticGet('nickname', $nickname);
326 if (!empty($group)) {
327 return User_group::staticGet('id', $group->group_id);
329 $alias = Group_alias::staticGet('alias', $nickname);
330 if (!empty($alias)) {
331 return User_group::staticGet('id', $alias->group_id);
338 return Design::staticGet('id', $this->design_id);
341 function getUserMembers()
346 if(common_config('db','quote_identifiers'))
347 $user_table = '"user"';
348 else $user_table = 'user';
352 'FROM '. $user_table .' JOIN group_member '.
353 'ON '. $user_table .'.id = group_member.profile_id ' .
354 'WHERE group_member.group_id = %d ';
356 $user->query(sprintf($qry, $this->id));
360 while ($user->fetch()) {
369 static function maxDescription()
371 $desclimit = common_config('group', 'desclimit');
372 // null => use global limit (distinct from 0!)
373 if (is_null($desclimit)) {
374 $desclimit = common_config('site', 'textlimit');
379 static function descriptionTooLong($desc)
381 $desclimit = self::maxDescription();
382 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
385 function asAtomEntry($namespace=false, $source=false)
387 $xs = new XMLStringer(true);
390 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
391 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
396 $xs->elementStart('entry', $attrs);
399 $xs->elementStart('source');
400 $xs->element('id', null, $this->permalink());
401 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
402 $xs->element('link', array('href' => $this->permalink()));
403 $xs->element('updated', null, $this->modified);
404 $xs->elementEnd('source');
407 $xs->element('title', null, $this->nickname);
408 $xs->element('summary', null, common_xml_safe_str($this->description));
410 $xs->element('link', array('rel' => 'alternate',
411 'href' => $this->permalink()));
413 $xs->element('id', null, $this->permalink());
415 $xs->element('published', null, common_date_w3dtf($this->created));
416 $xs->element('updated', null, common_date_w3dtf($this->modified));
420 array('type' => 'html'),
421 common_xml_safe_str($this->description)
424 $xs->elementEnd('entry');
426 return $xs->getString();
429 function asAtomAuthor()
431 $xs = new XMLStringer(true);
433 $xs->elementStart('author');
434 $xs->element('name', null, $this->nickname);
435 $xs->element('uri', null, $this->permalink());
436 $xs->elementEnd('author');
438 return $xs->getString();
442 * Returns an XML string fragment with group information as an
443 * Activity Streams <activity:subject> element.
445 * Assumes that 'activity' namespace has been previously defined.
449 function asActivitySubject()
451 return $this->asActivityNoun('subject');
455 * Returns an XML string fragment with group information as an
456 * Activity Streams noun object with the given element type.
458 * Assumes that 'activity', 'georss', and 'poco' namespace has been
459 * previously defined.
461 * @param string $element one of 'actor', 'subject', 'object', 'target'
465 function asActivityNoun($element)
467 $noun = ActivityObject::fromGroup($this);
468 return $noun->asString('activity:' . $element);
473 return empty($this->homepage_logo)
474 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
475 : $this->homepage_logo;
478 static function register($fields) {
479 if (!empty($fields['userid'])) {
480 $profile = Profile::staticGet('id', $fields['userid']);
481 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
482 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
484 // TRANS: Client exception thrown when a user tries to create a group while banned.
485 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
489 // MAGICALLY put fields into current scope
490 // @fixme kill extract(); it makes debugging absurdly hard
494 $group = new User_group();
496 $group->query('BEGIN');
502 if (empty($mainpage)) {
503 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
506 $group->nickname = $nickname;
507 $group->fullname = $fullname;
508 $group->homepage = $homepage;
509 $group->description = $description;
510 $group->location = $location;
512 $group->mainpage = $mainpage;
513 $group->created = common_sql_now();
515 if (Event::handle('StartGroupSave', array(&$group))) {
517 $result = $group->insert();
520 common_log_db_error($group, 'INSERT', __FILE__);
521 // TRANS: Server exception thrown when creating a group failed.
522 throw new ServerException(_('Could not create group.'));
525 if (!isset($uri) || empty($uri)) {
526 $orig = clone($group);
527 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
528 $result = $group->update($orig);
530 common_log_db_error($group, 'UPDATE', __FILE__);
531 // TRANS: Server exception thrown when updating a group URI failed.
532 throw new ServerException(_('Could not set group URI.'));
536 $result = $group->setAliases($aliases);
539 // TRANS: Server exception thrown when creating group aliases failed.
540 throw new ServerException(_('Could not create aliases.'));
543 $member = new Group_member();
545 $member->group_id = $group->id;
546 $member->profile_id = $userid;
547 $member->is_admin = 1;
548 $member->created = $group->created;
550 $result = $member->insert();
553 common_log_db_error($member, 'INSERT', __FILE__);
554 // TRANS: Server exception thrown when setting group membership failed.
555 throw new ServerException(_('Could not set group membership.'));
559 $local_group = new Local_group();
561 $local_group->group_id = $group->id;
562 $local_group->nickname = $nickname;
563 $local_group->created = common_sql_now();
565 $result = $local_group->insert();
568 common_log_db_error($local_group, 'INSERT', __FILE__);
569 // TRANS: Server exception thrown when saving local group information failed.
570 throw new ServerException(_('Could not save local group info.'));
574 $group->query('COMMIT');
576 Event::handle('EndGroupSave', array($group));
583 * Handle cascading deletion, on the model of notice and profile.
585 * This should handle freeing up cached entries for the group's
586 * id, nickname, URI, and aliases. There may be other areas that
587 * are not de-cached in the UI, including the sidebar lists on
594 // Safe to delete in bulk for now
596 $related = array('Group_inbox',
601 Event::handle('UserGroupDeleteRelated', array($this, &$related));
603 foreach ($related as $cls) {
606 $inst->group_id = $this->id;
609 while ($inst->fetch()) {
616 // And related groups in the other direction...
617 $inst = new Related_group();
618 $inst->related_group_id = $this->id;
621 // Aliases and the local_group entry need to be cleared explicitly
622 // or we'll miss clearing some cache keys; that can make it hard
623 // to create a new group with one of those names or aliases.
624 $this->setAliases(array());
625 $local = Local_group::staticGet('group_id', $this->id);
630 // blow the cached ids
631 self::blow('user_group:notice_ids:%d', $this->id);
634 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");