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 $ids = Notice::stream(array($this, '_streamDirect'),
92 'user_group:notice_ids:' . $this->id,
93 $offset, $limit, $since_id, $max_id);
95 return Notice::getStreamByIds($ids);
98 function _streamDirect($offset, $limit, $since_id, $max_id)
100 $inbox = new Group_inbox();
102 $inbox->group_id = $this->id;
105 $inbox->selectAdd('notice_id');
107 Notice::addWhereSinceId($inbox, $since_id, 'notice_id');
108 Notice::addWhereMaxId($inbox, $max_id, 'notice_id');
110 $inbox->orderBy('created DESC, notice_id DESC');
112 if (!is_null($offset)) {
113 $inbox->limit($offset, $limit);
118 if ($inbox->find()) {
119 while ($inbox->fetch()) {
120 $ids[] = $inbox->notice_id;
127 function allowedNickname($nickname)
129 static $blacklist = array('new');
130 return !in_array($nickname, $blacklist);
133 function getMembers($offset=0, $limit=null)
136 'SELECT profile.* ' .
137 'FROM profile JOIN group_member '.
138 'ON profile.id = group_member.profile_id ' .
139 'WHERE group_member.group_id = %d ' .
140 'ORDER BY group_member.created DESC ';
142 if ($limit != null) {
143 if (common_config('db','type') == 'pgsql') {
144 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
146 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
150 $members = new Profile();
152 $members->query(sprintf($qry, $this->id));
156 function getMemberCount()
158 // XXX: WORM cache this
160 $members = $this->getMembers();
163 /** $member->count() doesn't work. */
164 while ($members->fetch()) {
168 return $member_count;
171 function getAdmins($offset=0, $limit=null)
174 'SELECT profile.* ' .
175 'FROM profile JOIN group_member '.
176 'ON profile.id = group_member.profile_id ' .
177 'WHERE group_member.group_id = %d ' .
178 'AND group_member.is_admin = 1 ' .
179 'ORDER BY group_member.modified ASC ';
181 if ($limit != null) {
182 if (common_config('db','type') == 'pgsql') {
183 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
185 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
189 $admins = new Profile();
191 $admins->query(sprintf($qry, $this->id));
195 function getBlocked($offset=0, $limit=null)
198 'SELECT profile.* ' .
199 'FROM profile JOIN group_block '.
200 'ON profile.id = group_block.blocked ' .
201 'WHERE group_block.group_id = %d ' .
202 'ORDER BY group_block.modified DESC ';
204 if ($limit != null) {
205 if (common_config('db','type') == 'pgsql') {
206 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
208 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
212 $blocked = new Profile();
214 $blocked->query(sprintf($qry, $this->id));
218 function setOriginal($filename)
220 $imagefile = new ImageFile($this->id, Avatar::path($filename));
222 $orig = clone($this);
223 $this->original_logo = Avatar::url($filename);
224 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
225 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
226 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
227 common_debug(common_log_objstring($this));
228 return $this->update($orig);
231 function getBestName()
233 return ($this->fullname) ? $this->fullname : $this->nickname;
237 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
238 * if no fullname is provided.
242 function getFancyName()
244 if ($this->fullname) {
245 // TRANS: Full name of a profile or group followed by nickname in parens
246 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
248 return $this->nickname;
252 function getAliases()
258 $alias = new Group_alias();
260 $alias->group_id = $this->id;
262 if ($alias->find()) {
263 while ($alias->fetch()) {
264 $aliases[] = $alias->alias;
273 function setAliases($newaliases) {
275 $newaliases = array_unique($newaliases);
277 $oldaliases = $this->getAliases();
279 # Delete stuff that's old that not in new
281 $to_delete = array_diff($oldaliases, $newaliases);
283 # Insert stuff that's in new and not in old
285 $to_insert = array_diff($newaliases, $oldaliases);
287 $alias = new Group_alias();
289 $alias->group_id = $this->id;
291 foreach ($to_delete as $delalias) {
292 $alias->alias = $delalias;
293 $result = $alias->delete();
295 common_log_db_error($alias, 'DELETE', __FILE__);
300 foreach ($to_insert as $insalias) {
301 $alias->alias = $insalias;
302 $result = $alias->insert();
304 common_log_db_error($alias, 'INSERT', __FILE__);
312 static function getForNickname($nickname, $profile=null)
314 $nickname = common_canonical_nickname($nickname);
316 // Are there any matching remote groups this profile's in?
318 $group = $profile->getGroups();
319 while ($group->fetch()) {
320 if ($group->nickname == $nickname) {
321 // @fixme is this the best way?
322 return clone($group);
327 // If not, check local groups.
329 $group = Local_group::staticGet('nickname', $nickname);
330 if (!empty($group)) {
331 return User_group::staticGet('id', $group->group_id);
333 $alias = Group_alias::staticGet('alias', $nickname);
334 if (!empty($alias)) {
335 return User_group::staticGet('id', $alias->group_id);
342 return Design::staticGet('id', $this->design_id);
345 function getUserMembers()
350 if(common_config('db','quote_identifiers'))
351 $user_table = '"user"';
352 else $user_table = 'user';
356 'FROM '. $user_table .' JOIN group_member '.
357 'ON '. $user_table .'.id = group_member.profile_id ' .
358 'WHERE group_member.group_id = %d ';
360 $user->query(sprintf($qry, $this->id));
364 while ($user->fetch()) {
373 static function maxDescription()
375 $desclimit = common_config('group', 'desclimit');
376 // null => use global limit (distinct from 0!)
377 if (is_null($desclimit)) {
378 $desclimit = common_config('site', 'textlimit');
383 static function descriptionTooLong($desc)
385 $desclimit = self::maxDescription();
386 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
389 function asAtomEntry($namespace=false, $source=false)
391 $xs = new XMLStringer(true);
394 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
395 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
400 $xs->elementStart('entry', $attrs);
403 $xs->elementStart('source');
404 $xs->element('id', null, $this->permalink());
405 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
406 $xs->element('link', array('href' => $this->permalink()));
407 $xs->element('updated', null, $this->modified);
408 $xs->elementEnd('source');
411 $xs->element('title', null, $this->nickname);
412 $xs->element('summary', null, common_xml_safe_str($this->description));
414 $xs->element('link', array('rel' => 'alternate',
415 'href' => $this->permalink()));
417 $xs->element('id', null, $this->permalink());
419 $xs->element('published', null, common_date_w3dtf($this->created));
420 $xs->element('updated', null, common_date_w3dtf($this->modified));
424 array('type' => 'html'),
425 common_xml_safe_str($this->description)
428 $xs->elementEnd('entry');
430 return $xs->getString();
433 function asAtomAuthor()
435 $xs = new XMLStringer(true);
437 $xs->elementStart('author');
438 $xs->element('name', null, $this->nickname);
439 $xs->element('uri', null, $this->permalink());
440 $xs->elementEnd('author');
442 return $xs->getString();
446 * Returns an XML string fragment with group information as an
447 * Activity Streams <activity:subject> element.
449 * Assumes that 'activity' namespace has been previously defined.
453 function asActivitySubject()
455 return $this->asActivityNoun('subject');
459 * Returns an XML string fragment with group information as an
460 * Activity Streams noun object with the given element type.
462 * Assumes that 'activity', 'georss', and 'poco' namespace has been
463 * previously defined.
465 * @param string $element one of 'actor', 'subject', 'object', 'target'
469 function asActivityNoun($element)
471 $noun = ActivityObject::fromGroup($this);
472 return $noun->asString('activity:' . $element);
477 return empty($this->homepage_logo)
478 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
479 : $this->homepage_logo;
482 static function register($fields) {
483 if (!empty($fields['userid'])) {
484 $profile = Profile::staticGet('id', $fields['userid']);
485 if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
486 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
488 // TRANS: Client exception thrown when a user tries to create a group while banned.
489 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
493 // MAGICALLY put fields into current scope
494 // @fixme kill extract(); it makes debugging absurdly hard
498 $group = new User_group();
500 $group->query('BEGIN');
506 if (empty($mainpage)) {
507 $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
510 $group->nickname = $nickname;
511 $group->fullname = $fullname;
512 $group->homepage = $homepage;
513 $group->description = $description;
514 $group->location = $location;
516 $group->mainpage = $mainpage;
517 $group->created = common_sql_now();
518 if (isset($fields['join_policy'])) {
519 $group->join_policy = intval($fields['join_policy']);
521 $group->join_policy = 0;
524 if (Event::handle('StartGroupSave', array(&$group))) {
526 $result = $group->insert();
529 common_log_db_error($group, 'INSERT', __FILE__);
530 // TRANS: Server exception thrown when creating a group failed.
531 throw new ServerException(_('Could not create group.'));
534 if (!isset($uri) || empty($uri)) {
535 $orig = clone($group);
536 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
537 $result = $group->update($orig);
539 common_log_db_error($group, 'UPDATE', __FILE__);
540 // TRANS: Server exception thrown when updating a group URI failed.
541 throw new ServerException(_('Could not set group URI.'));
545 $result = $group->setAliases($aliases);
548 // TRANS: Server exception thrown when creating group aliases failed.
549 throw new ServerException(_('Could not create aliases.'));
552 $member = new Group_member();
554 $member->group_id = $group->id;
555 $member->profile_id = $userid;
556 $member->is_admin = 1;
557 $member->created = $group->created;
559 $result = $member->insert();
562 common_log_db_error($member, 'INSERT', __FILE__);
563 // TRANS: Server exception thrown when setting group membership failed.
564 throw new ServerException(_('Could not set group membership.'));
568 $local_group = new Local_group();
570 $local_group->group_id = $group->id;
571 $local_group->nickname = $nickname;
572 $local_group->created = common_sql_now();
574 $result = $local_group->insert();
577 common_log_db_error($local_group, 'INSERT', __FILE__);
578 // TRANS: Server exception thrown when saving local group information failed.
579 throw new ServerException(_('Could not save local group info.'));
583 $group->query('COMMIT');
585 Event::handle('EndGroupSave', array($group));
592 * Handle cascading deletion, on the model of notice and profile.
594 * This should handle freeing up cached entries for the group's
595 * id, nickname, URI, and aliases. There may be other areas that
596 * are not de-cached in the UI, including the sidebar lists on
603 // Safe to delete in bulk for now
605 $related = array('Group_inbox',
610 Event::handle('UserGroupDeleteRelated', array($this, &$related));
612 foreach ($related as $cls) {
615 $inst->group_id = $this->id;
618 while ($inst->fetch()) {
625 // And related groups in the other direction...
626 $inst = new Related_group();
627 $inst->related_group_id = $this->id;
630 // Aliases and the local_group entry need to be cleared explicitly
631 // or we'll miss clearing some cache keys; that can make it hard
632 // to create a new group with one of those names or aliases.
633 $this->setAliases(array());
634 $local = Local_group::staticGet('group_id', $this->id);
639 // blow the cached ids
640 self::blow('user_group:notice_ids:%d', $this->id);
643 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");