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 if ($since_id != 0) {
104 $inbox->whereAdd('notice_id > ' . $since_id);
108 $inbox->whereAdd('notice_id <= ' . $max_id);
111 $inbox->orderBy('notice_id DESC');
113 if (!is_null($offset)) {
114 $inbox->limit($offset, $limit);
119 if ($inbox->find()) {
120 while ($inbox->fetch()) {
121 $ids[] = $inbox->notice_id;
128 function allowedNickname($nickname)
130 static $blacklist = array('new');
131 return !in_array($nickname, $blacklist);
134 function getMembers($offset=0, $limit=null)
137 'SELECT profile.* ' .
138 'FROM profile JOIN group_member '.
139 'ON profile.id = group_member.profile_id ' .
140 'WHERE group_member.group_id = %d ' .
141 'ORDER BY group_member.created DESC ';
143 if ($limit != null) {
144 if (common_config('db','type') == 'pgsql') {
145 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
147 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
151 $members = new Profile();
153 $members->query(sprintf($qry, $this->id));
157 function getMemberCount()
159 // XXX: WORM cache this
161 $members = $this->getMembers();
164 /** $member->count() doesn't work. */
165 while ($members->fetch()) {
169 return $member_count;
172 function getAdmins($offset=0, $limit=null)
175 'SELECT profile.* ' .
176 'FROM profile JOIN group_member '.
177 'ON profile.id = group_member.profile_id ' .
178 'WHERE group_member.group_id = %d ' .
179 'AND group_member.is_admin = 1 ' .
180 'ORDER BY group_member.modified ASC ';
182 if ($limit != null) {
183 if (common_config('db','type') == 'pgsql') {
184 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
186 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
190 $admins = new Profile();
192 $admins->query(sprintf($qry, $this->id));
196 function getBlocked($offset=0, $limit=null)
199 'SELECT profile.* ' .
200 'FROM profile JOIN group_block '.
201 'ON profile.id = group_block.blocked ' .
202 'WHERE group_block.group_id = %d ' .
203 'ORDER BY group_block.modified DESC ';
205 if ($limit != null) {
206 if (common_config('db','type') == 'pgsql') {
207 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
209 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
213 $blocked = new Profile();
215 $blocked->query(sprintf($qry, $this->id));
219 function setOriginal($filename)
221 $imagefile = new ImageFile($this->id, Avatar::path($filename));
223 $orig = clone($this);
224 $this->original_logo = Avatar::url($filename);
225 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
226 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
227 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
228 common_debug(common_log_objstring($this));
229 return $this->update($orig);
232 function getBestName()
234 return ($this->fullname) ? $this->fullname : $this->nickname;
238 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
239 * if no fullname is provided.
243 function getFancyName()
245 if ($this->fullname) {
246 // TRANS: Full name of a profile or group followed by nickname in parens
247 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
249 return $this->nickname;
253 function getAliases()
259 $alias = new Group_alias();
261 $alias->group_id = $this->id;
263 if ($alias->find()) {
264 while ($alias->fetch()) {
265 $aliases[] = $alias->alias;
274 function setAliases($newaliases) {
276 $newaliases = array_unique($newaliases);
278 $oldaliases = $this->getAliases();
280 # Delete stuff that's old that not in new
282 $to_delete = array_diff($oldaliases, $newaliases);
284 # Insert stuff that's in new and not in old
286 $to_insert = array_diff($newaliases, $oldaliases);
288 $alias = new Group_alias();
290 $alias->group_id = $this->id;
292 foreach ($to_delete as $delalias) {
293 $alias->alias = $delalias;
294 $result = $alias->delete();
296 common_log_db_error($alias, 'DELETE', __FILE__);
301 foreach ($to_insert as $insalias) {
302 $alias->alias = $insalias;
303 $result = $alias->insert();
305 common_log_db_error($alias, 'INSERT', __FILE__);
313 static function getForNickname($nickname, $profile=null)
315 $nickname = common_canonical_nickname($nickname);
317 // Are there any matching remote groups this profile's in?
319 $group = $profile->getGroups();
320 while ($group->fetch()) {
321 if ($group->nickname == $nickname) {
322 // @fixme is this the best way?
323 return clone($group);
328 // If not, check local groups.
330 $group = Local_group::staticGet('nickname', $nickname);
331 if (!empty($group)) {
332 return User_group::staticGet('id', $group->group_id);
334 $alias = Group_alias::staticGet('alias', $nickname);
335 if (!empty($alias)) {
336 return User_group::staticGet('id', $alias->group_id);
343 return Design::staticGet('id', $this->design_id);
346 function getUserMembers()
351 if(common_config('db','quote_identifiers'))
352 $user_table = '"user"';
353 else $user_table = 'user';
357 'FROM '. $user_table .' JOIN group_member '.
358 'ON '. $user_table .'.id = group_member.profile_id ' .
359 'WHERE group_member.group_id = %d ';
361 $user->query(sprintf($qry, $this->id));
365 while ($user->fetch()) {
374 static function maxDescription()
376 $desclimit = common_config('group', 'desclimit');
377 // null => use global limit (distinct from 0!)
378 if (is_null($desclimit)) {
379 $desclimit = common_config('site', 'textlimit');
384 static function descriptionTooLong($desc)
386 $desclimit = self::maxDescription();
387 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
390 function asAtomEntry($namespace=false, $source=false)
392 $xs = new XMLStringer(true);
395 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
396 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
401 $xs->elementStart('entry', $attrs);
404 $xs->elementStart('source');
405 $xs->element('id', null, $this->permalink());
406 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
407 $xs->element('link', array('href' => $this->permalink()));
408 $xs->element('updated', null, $this->modified);
409 $xs->elementEnd('source');
412 $xs->element('title', null, $this->nickname);
413 $xs->element('summary', null, common_xml_safe_str($this->description));
415 $xs->element('link', array('rel' => 'alternate',
416 'href' => $this->permalink()));
418 $xs->element('id', null, $this->permalink());
420 $xs->element('published', null, common_date_w3dtf($this->created));
421 $xs->element('updated', null, common_date_w3dtf($this->modified));
425 array('type' => 'html'),
426 common_xml_safe_str($this->description)
429 $xs->elementEnd('entry');
431 return $xs->getString();
434 function asAtomAuthor()
436 $xs = new XMLStringer(true);
438 $xs->elementStart('author');
439 $xs->element('name', null, $this->nickname);
440 $xs->element('uri', null, $this->permalink());
441 $xs->elementEnd('author');
443 return $xs->getString();
447 * Returns an XML string fragment with group information as an
448 * Activity Streams <activity:subject> element.
450 * Assumes that 'activity' namespace has been previously defined.
454 function asActivitySubject()
456 return $this->asActivityNoun('subject');
460 * Returns an XML string fragment with group information as an
461 * Activity Streams noun object with the given element type.
463 * Assumes that 'activity', 'georss', and 'poco' namespace has been
464 * previously defined.
466 * @param string $element one of 'actor', 'subject', 'object', 'target'
470 function asActivityNoun($element)
472 $noun = ActivityObject::fromGroup($this);
473 return $noun->asString('activity:' . $element);
478 return empty($this->homepage_logo)
479 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
480 : $this->homepage_logo;
483 static function register($fields) {
484 // MAGICALLY put fields into current scope
488 $group = new User_group();
490 $group->query('BEGIN');
497 $group->nickname = $nickname;
498 $group->fullname = $fullname;
499 $group->homepage = $homepage;
500 $group->description = $description;
501 $group->location = $location;
503 $group->mainpage = $mainpage;
504 $group->created = common_sql_now();
506 $result = $group->insert();
509 common_log_db_error($group, 'INSERT', __FILE__);
510 // TRANS: Server exception thrown when creating a group failed.
511 throw new ServerException(_('Could not create group.'));
514 if (!isset($uri) || empty($uri)) {
515 $orig = clone($group);
516 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
517 $result = $group->update($orig);
519 common_log_db_error($group, 'UPDATE', __FILE__);
520 // TRANS: Server exception thrown when updating a group URI failed.
521 throw new ServerException(_('Could not set group URI.'));
525 $result = $group->setAliases($aliases);
528 // TRANS: Server exception thrown when creating group aliases failed.
529 throw new ServerException(_('Could not create aliases.'));
532 $member = new Group_member();
534 $member->group_id = $group->id;
535 $member->profile_id = $userid;
536 $member->is_admin = 1;
537 $member->created = $group->created;
539 $result = $member->insert();
542 common_log_db_error($member, 'INSERT', __FILE__);
543 // TRANS: Server exception thrown when setting group membership failed.
544 throw new ServerException(_('Could not set group membership.'));
548 $local_group = new Local_group();
550 $local_group->group_id = $group->id;
551 $local_group->nickname = $nickname;
552 $local_group->created = common_sql_now();
554 $result = $local_group->insert();
557 common_log_db_error($local_group, 'INSERT', __FILE__);
558 // TRANS: Server exception thrown when saving local group information failed.
559 throw new ServerException(_('Could not save local group info.'));
563 $group->query('COMMIT');
568 * Handle cascading deletion, on the model of notice and profile.
570 * This should handle freeing up cached entries for the group's
571 * id, nickname, URI, and aliases. There may be other areas that
572 * are not de-cached in the UI, including the sidebar lists on
579 // Safe to delete in bulk for now
581 $related = array('Group_inbox',
586 Event::handle('UserGroupDeleteRelated', array($this, &$related));
588 foreach ($related as $cls) {
591 $inst->group_id = $this->id;
594 while ($inst->fetch()) {
601 // And related groups in the other direction...
602 $inst = new Related_group();
603 $inst->related_group_id = $this->id;
606 // Aliases and the local_group entry need to be cleared explicitly
607 // or we'll miss clearing some cache keys; that can make it hard
608 // to create a new group with one of those names or aliases.
609 $this->setAliases(array());
610 $local = Local_group::staticGet('group_id', $this->id);
615 // blow the cached ids
616 self::blow('user_group:notice_ids:%d', $this->id);
619 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");