3 * Table Definition for user_group
6 class User_group extends Memcached_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 $design_id; // int(4)
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
33 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
35 /* the code above is auto generated do not remove the tag below */
38 function defaultLogo($size)
40 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
41 AVATAR_STREAM_SIZE => 'stream',
42 AVATAR_MINI_SIZE => 'mini');
43 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
49 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
50 // normally stored in mainpage, but older ones may be null
51 if (!empty($this->mainpage)) {
52 $url = $this->mainpage;
54 $url = common_local_url('showgroup',
55 array('nickname' => $this->nickname));
58 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
65 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
66 if (!empty($this->uri)) {
69 $uri = common_local_url('groupbyid',
70 array('id' => $this->id));
73 Event::handle('EndUserGroupGetUri', array($this, &$uri));
80 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
81 $url = common_local_url('groupbyid',
82 array('id' => $this->id));
84 Event::handle('EndUserGroupPermalink', array($this, &$url));
88 function getNotices($offset, $limit, $since_id=null, $max_id=null)
90 $stream = new GroupNoticeStream($this);
92 return $stream->getNotices($offset, $limit, $since_id, $max_id);
96 function allowedNickname($nickname)
98 static $blacklist = array('new');
99 return !in_array($nickname, $blacklist);
102 function getMembers($offset=0, $limit=null)
105 'SELECT profile.* ' .
106 'FROM profile JOIN group_member '.
107 'ON profile.id = group_member.profile_id ' .
108 'WHERE group_member.group_id = %d ' .
109 'ORDER BY group_member.created DESC ';
111 if ($limit != null) {
112 if (common_config('db','type') == 'pgsql') {
113 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
115 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
119 $members = new Profile();
121 $members->query(sprintf($qry, $this->id));
126 * Get pending members, who have not yet been approved.
132 function getRequests($offset=0, $limit=null)
135 'SELECT profile.* ' .
136 'FROM profile JOIN group_join_queue '.
137 'ON profile.id = group_join_queue.profile_id ' .
138 'WHERE group_join_queue.group_id = %d ' .
139 'ORDER BY group_join_queue.created DESC ';
141 if ($limit != null) {
142 if (common_config('db','type') == 'pgsql') {
143 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
145 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
149 $members = new Profile();
151 $members->query(sprintf($qry, $this->id));
155 function getMemberCount()
157 // XXX: WORM cache this
159 $members = $this->getMembers();
162 /** $member->count() doesn't work. */
163 while ($members->fetch()) {
167 return $member_count;
170 function getAdmins($offset=0, $limit=null)
173 'SELECT profile.* ' .
174 'FROM profile JOIN group_member '.
175 'ON profile.id = group_member.profile_id ' .
176 'WHERE group_member.group_id = %d ' .
177 'AND group_member.is_admin = 1 ' .
178 'ORDER BY group_member.modified ASC ';
180 if ($limit != null) {
181 if (common_config('db','type') == 'pgsql') {
182 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
184 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
188 $admins = new Profile();
190 $admins->query(sprintf($qry, $this->id));
194 function getBlocked($offset=0, $limit=null)
197 'SELECT profile.* ' .
198 'FROM profile JOIN group_block '.
199 'ON profile.id = group_block.blocked ' .
200 'WHERE group_block.group_id = %d ' .
201 'ORDER BY group_block.modified DESC ';
203 if ($limit != null) {
204 if (common_config('db','type') == 'pgsql') {
205 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
207 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
211 $blocked = new Profile();
213 $blocked->query(sprintf($qry, $this->id));
217 function setOriginal($filename)
219 $imagefile = new ImageFile($this->id, Avatar::path($filename));
221 $orig = clone($this);
222 $this->original_logo = Avatar::url($filename);
223 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
224 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
225 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
226 common_debug(common_log_objstring($this));
227 return $this->update($orig);
230 function getBestName()
232 return ($this->fullname) ? $this->fullname : $this->nickname;
236 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
237 * if no fullname is provided.
241 function getFancyName()
243 if ($this->fullname) {
244 // TRANS: Full name of a profile or group followed by nickname in parens
245 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
247 return $this->nickname;
251 function getAliases()
257 $alias = new Group_alias();
259 $alias->group_id = $this->id;
261 if ($alias->find()) {
262 while ($alias->fetch()) {
263 $aliases[] = $alias->alias;
272 function setAliases($newaliases) {
274 $newaliases = array_unique($newaliases);
276 $oldaliases = $this->getAliases();
278 // Delete stuff that's old that not in new
280 $to_delete = array_diff($oldaliases, $newaliases);
282 // Insert stuff that's in new and not in old
284 $to_insert = array_diff($newaliases, $oldaliases);
286 $alias = new Group_alias();
288 $alias->group_id = $this->id;
290 foreach ($to_delete as $delalias) {
291 $alias->alias = $delalias;
292 $result = $alias->delete();
294 common_log_db_error($alias, 'DELETE', __FILE__);
299 foreach ($to_insert as $insalias) {
300 $alias->alias = $insalias;
301 $result = $alias->insert();
303 common_log_db_error($alias, 'INSERT', __FILE__);
311 static function getForNickname($nickname, $profile=null)
313 $nickname = common_canonical_nickname($nickname);
315 // Are there any matching remote groups this profile's in?
317 $group = $profile->getGroups();
318 while ($group->fetch()) {
319 if ($group->nickname == $nickname) {
320 // @fixme is this the best way?
321 return clone($group);
326 // If not, check local groups.
328 $group = Local_group::staticGet('nickname', $nickname);
329 if (!empty($group)) {
330 return User_group::staticGet('id', $group->group_id);
332 $alias = Group_alias::staticGet('alias', $nickname);
333 if (!empty($alias)) {
334 return User_group::staticGet('id', $alias->group_id);
341 return Design::staticGet('id', $this->design_id);
344 function getUserMembers()
349 if(common_config('db','quote_identifiers'))
350 $user_table = '"user"';
351 else $user_table = 'user';
355 'FROM '. $user_table .' JOIN group_member '.
356 'ON '. $user_table .'.id = group_member.profile_id ' .
357 'WHERE group_member.group_id = %d ';
359 $user->query(sprintf($qry, $this->id));
363 while ($user->fetch()) {
372 static function maxDescription()
374 $desclimit = common_config('group', 'desclimit');
375 // null => use global limit (distinct from 0!)
376 if (is_null($desclimit)) {
377 $desclimit = common_config('site', 'textlimit');
382 static function descriptionTooLong($desc)
384 $desclimit = self::maxDescription();
385 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
388 function asAtomEntry($namespace=false, $source=false)
390 $xs = new XMLStringer(true);
393 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
394 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
399 $xs->elementStart('entry', $attrs);
402 $xs->elementStart('source');
403 $xs->element('id', null, $this->permalink());
404 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
405 $xs->element('link', array('href' => $this->permalink()));
406 $xs->element('updated', null, $this->modified);
407 $xs->elementEnd('source');
410 $xs->element('title', null, $this->nickname);
411 $xs->element('summary', null, common_xml_safe_str($this->description));
413 $xs->element('link', array('rel' => 'alternate',
414 'href' => $this->permalink()));
416 $xs->element('id', null, $this->permalink());
418 $xs->element('published', null, common_date_w3dtf($this->created));
419 $xs->element('updated', null, common_date_w3dtf($this->modified));
423 array('type' => 'html'),
424 common_xml_safe_str($this->description)
427 $xs->elementEnd('entry');
429 return $xs->getString();
432 function asAtomAuthor()
434 $xs = new XMLStringer(true);
436 $xs->elementStart('author');
437 $xs->element('name', null, $this->nickname);
438 $xs->element('uri', null, $this->permalink());
439 $xs->elementEnd('author');
441 return $xs->getString();
445 * Returns an XML string fragment with group information as an
446 * Activity Streams <activity:subject> element.
448 * Assumes that 'activity' namespace has been previously defined.
452 function asActivitySubject()
454 return $this->asActivityNoun('subject');
458 * Returns an XML string fragment with group information as an
459 * Activity Streams noun object with the given element type.
461 * Assumes that 'activity', 'georss', and 'poco' namespace has been
462 * previously defined.
464 * @param string $element one of 'actor', 'subject', 'object', 'target'
468 function asActivityNoun($element)
470 $noun = ActivityObject::fromGroup($this);
471 return $noun->asString('activity:' . $element);
476 return empty($this->homepage_logo)
477 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
478 : $this->homepage_logo;
481 static function register($fields) {
482 if (!empty($fields['userid'])) {
483 $profile = Profile::staticGet('id', $fields['userid']);
484 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
485 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
487 // TRANS: Client exception thrown when a user tries to create a group while banned.
488 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
492 // MAGICALLY put fields into current scope
493 // @fixme kill extract(); it makes debugging absurdly hard
497 $group = new User_group();
499 $group->query('BEGIN');
505 if (empty($mainpage)) {
506 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
509 $group->nickname = $nickname;
510 $group->fullname = $fullname;
511 $group->homepage = $homepage;
512 $group->description = $description;
513 $group->location = $location;
515 $group->mainpage = $mainpage;
516 $group->created = common_sql_now();
517 if (isset($fields['join_policy'])) {
518 $group->join_policy = intval($fields['join_policy']);
520 $group->join_policy = 0;
523 if (Event::handle('StartGroupSave', array(&$group))) {
525 $result = $group->insert();
528 common_log_db_error($group, 'INSERT', __FILE__);
529 // TRANS: Server exception thrown when creating a group failed.
530 throw new ServerException(_('Could not create group.'));
533 if (!isset($uri) || empty($uri)) {
534 $orig = clone($group);
535 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
536 $result = $group->update($orig);
538 common_log_db_error($group, 'UPDATE', __FILE__);
539 // TRANS: Server exception thrown when updating a group URI failed.
540 throw new ServerException(_('Could not set group URI.'));
544 $result = $group->setAliases($aliases);
547 // TRANS: Server exception thrown when creating group aliases failed.
548 throw new ServerException(_('Could not create aliases.'));
551 $member = new Group_member();
553 $member->group_id = $group->id;
554 $member->profile_id = $userid;
555 $member->is_admin = 1;
556 $member->created = $group->created;
558 $result = $member->insert();
561 common_log_db_error($member, 'INSERT', __FILE__);
562 // TRANS: Server exception thrown when setting group membership failed.
563 throw new ServerException(_('Could not set group membership.'));
567 $local_group = new Local_group();
569 $local_group->group_id = $group->id;
570 $local_group->nickname = $nickname;
571 $local_group->created = common_sql_now();
573 $result = $local_group->insert();
576 common_log_db_error($local_group, 'INSERT', __FILE__);
577 // TRANS: Server exception thrown when saving local group information failed.
578 throw new ServerException(_('Could not save local group info.'));
582 $group->query('COMMIT');
584 Event::handle('EndGroupSave', array($group));
591 * Handle cascading deletion, on the model of notice and profile.
593 * This should handle freeing up cached entries for the group's
594 * id, nickname, URI, and aliases. There may be other areas that
595 * are not de-cached in the UI, including the sidebar lists on
602 // Safe to delete in bulk for now
604 $related = array('Group_inbox',
609 Event::handle('UserGroupDeleteRelated', array($this, &$related));
611 foreach ($related as $cls) {
614 $inst->group_id = $this->id;
617 while ($inst->fetch()) {
624 // And related groups in the other direction...
625 $inst = new Related_group();
626 $inst->related_group_id = $this->id;
629 // Aliases and the local_group entry need to be cleared explicitly
630 // or we'll miss clearing some cache keys; that can make it hard
631 // to create a new group with one of those names or aliases.
632 $this->setAliases(array());
633 $local = Local_group::staticGet('group_id', $this->id);
638 // blow the cached ids
639 self::blow('user_group:notice_ids:%d', $this->id);
642 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");