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
31 public $force_scope; // tinyint
34 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
36 /* the code above is auto generated do not remove the tag below */
39 function defaultLogo($size)
41 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
42 AVATAR_STREAM_SIZE => 'stream',
43 AVATAR_MINI_SIZE => 'mini');
44 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
50 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
51 // normally stored in mainpage, but older ones may be null
52 if (!empty($this->mainpage)) {
53 $url = $this->mainpage;
55 $url = common_local_url('showgroup',
56 array('nickname' => $this->nickname));
59 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
66 if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
67 if (!empty($this->uri)) {
70 $uri = common_local_url('groupbyid',
71 array('id' => $this->id));
74 Event::handle('EndUserGroupGetUri', array($this, &$uri));
81 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
82 $url = common_local_url('groupbyid',
83 array('id' => $this->id));
85 Event::handle('EndUserGroupPermalink', array($this, &$url));
89 function getNotices($offset, $limit, $since_id=null, $max_id=null)
91 $stream = new GroupNoticeStream($this);
93 return $stream->getNotices($offset, $limit, $since_id, $max_id);
97 function allowedNickname($nickname)
99 static $blacklist = array('new');
100 return !in_array($nickname, $blacklist);
103 function getMembers($offset=0, $limit=null)
106 'SELECT profile.* ' .
107 'FROM profile JOIN group_member '.
108 'ON profile.id = group_member.profile_id ' .
109 'WHERE group_member.group_id = %d ' .
110 'ORDER BY group_member.created DESC ';
112 if ($limit != null) {
113 if (common_config('db','type') == 'pgsql') {
114 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
116 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
120 $members = new Profile();
122 $members->query(sprintf($qry, $this->id));
127 * Get pending members, who have not yet been approved.
133 function getRequests($offset=0, $limit=null)
136 'SELECT profile.* ' .
137 'FROM profile JOIN group_join_queue '.
138 'ON profile.id = group_join_queue.profile_id ' .
139 'WHERE group_join_queue.group_id = %d ' .
140 'ORDER BY group_join_queue.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();
519 if (isset($fields['join_policy'])) {
520 $group->join_policy = intval($fields['join_policy']);
522 $group->join_policy = 0;
525 if (isset($fields['force_scope'])) {
526 $group->force_scope = intval($fields['force_scope']);
528 $group->force_scope = 0;
531 if (Event::handle('StartGroupSave', array(&$group))) {
533 $result = $group->insert();
536 common_log_db_error($group, 'INSERT', __FILE__);
537 // TRANS: Server exception thrown when creating a group failed.
538 throw new ServerException(_('Could not create group.'));
541 if (!isset($uri) || empty($uri)) {
542 $orig = clone($group);
543 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
544 $result = $group->update($orig);
546 common_log_db_error($group, 'UPDATE', __FILE__);
547 // TRANS: Server exception thrown when updating a group URI failed.
548 throw new ServerException(_('Could not set group URI.'));
552 $result = $group->setAliases($aliases);
555 // TRANS: Server exception thrown when creating group aliases failed.
556 throw new ServerException(_('Could not create aliases.'));
559 $member = new Group_member();
561 $member->group_id = $group->id;
562 $member->profile_id = $userid;
563 $member->is_admin = 1;
564 $member->created = $group->created;
566 $result = $member->insert();
569 common_log_db_error($member, 'INSERT', __FILE__);
570 // TRANS: Server exception thrown when setting group membership failed.
571 throw new ServerException(_('Could not set group membership.'));
575 $local_group = new Local_group();
577 $local_group->group_id = $group->id;
578 $local_group->nickname = $nickname;
579 $local_group->created = common_sql_now();
581 $result = $local_group->insert();
584 common_log_db_error($local_group, 'INSERT', __FILE__);
585 // TRANS: Server exception thrown when saving local group information failed.
586 throw new ServerException(_('Could not save local group info.'));
590 $group->query('COMMIT');
592 Event::handle('EndGroupSave', array($group));
599 * Handle cascading deletion, on the model of notice and profile.
601 * This should handle freeing up cached entries for the group's
602 * id, nickname, URI, and aliases. There may be other areas that
603 * are not de-cached in the UI, including the sidebar lists on
610 // Safe to delete in bulk for now
612 $related = array('Group_inbox',
617 Event::handle('UserGroupDeleteRelated', array($this, &$related));
619 foreach ($related as $cls) {
622 $inst->group_id = $this->id;
625 while ($inst->fetch()) {
632 // And related groups in the other direction...
633 $inst = new Related_group();
634 $inst->related_group_id = $this->id;
637 // Aliases and the local_group entry need to be cleared explicitly
638 // or we'll miss clearing some cache keys; that can make it hard
639 // to create a new group with one of those names or aliases.
640 $this->setAliases(array());
641 $local = Local_group::staticGet('group_id', $this->id);
646 // blow the cached ids
647 self::blow('user_group:notice_ids:%d', $this->id);
650 common_log(LOG_WARN, "Ambiguous user_group->delete(); skipping related tables.");
657 return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
658 $this->force_scope == 1);