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 getAdmins($offset=0, $limit=null)
160 'SELECT profile.* ' .
161 'FROM profile JOIN group_member '.
162 'ON profile.id = group_member.profile_id ' .
163 'WHERE group_member.group_id = %d ' .
164 'AND group_member.is_admin = 1 ' .
165 'ORDER BY group_member.modified ASC ';
167 if ($limit != null) {
168 if (common_config('db','type') == 'pgsql') {
169 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
171 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
175 $admins = new Profile();
177 $admins->query(sprintf($qry, $this->id));
181 function getBlocked($offset=0, $limit=null)
184 'SELECT profile.* ' .
185 'FROM profile JOIN group_block '.
186 'ON profile.id = group_block.blocked ' .
187 'WHERE group_block.group_id = %d ' .
188 'ORDER BY group_block.modified DESC ';
190 if ($limit != null) {
191 if (common_config('db','type') == 'pgsql') {
192 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
194 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
198 $blocked = new Profile();
200 $blocked->query(sprintf($qry, $this->id));
204 function setOriginal($filename)
206 $imagefile = new ImageFile($this->id, Avatar::path($filename));
208 $orig = clone($this);
209 $this->original_logo = Avatar::url($filename);
210 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
211 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
212 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
213 common_debug(common_log_objstring($this));
214 return $this->update($orig);
217 function getBestName()
219 return ($this->fullname) ? $this->fullname : $this->nickname;
222 function getAliases()
228 $alias = new Group_alias();
230 $alias->group_id = $this->id;
232 if ($alias->find()) {
233 while ($alias->fetch()) {
234 $aliases[] = $alias->alias;
243 function setAliases($newaliases) {
245 $newaliases = array_unique($newaliases);
247 $oldaliases = $this->getAliases();
249 # Delete stuff that's old that not in new
251 $to_delete = array_diff($oldaliases, $newaliases);
253 # Insert stuff that's in new and not in old
255 $to_insert = array_diff($newaliases, $oldaliases);
257 $alias = new Group_alias();
259 $alias->group_id = $this->id;
261 foreach ($to_delete as $delalias) {
262 $alias->alias = $delalias;
263 $result = $alias->delete();
265 common_log_db_error($alias, 'DELETE', __FILE__);
270 foreach ($to_insert as $insalias) {
271 $alias->alias = $insalias;
272 $result = $alias->insert();
274 common_log_db_error($alias, 'INSERT', __FILE__);
282 static function getForNickname($nickname, $profile=null)
284 $nickname = common_canonical_nickname($nickname);
286 // Are there any matching remote groups this profile's in?
288 $group = $profile->getGroups();
289 while ($group->fetch()) {
290 if ($group->nickname == $nickname) {
291 // @fixme is this the best way?
292 return clone($group);
297 // If not, check local groups.
299 $group = Local_group::staticGet('nickname', $nickname);
300 if (!empty($group)) {
301 return User_group::staticGet('id', $group->group_id);
303 $alias = Group_alias::staticGet('alias', $nickname);
304 if (!empty($alias)) {
305 return User_group::staticGet('id', $alias->group_id);
312 return Design::staticGet('id', $this->design_id);
315 function getUserMembers()
320 if(common_config('db','quote_identifiers'))
321 $user_table = '"user"';
322 else $user_table = 'user';
326 'FROM '. $user_table .' JOIN group_member '.
327 'ON '. $user_table .'.id = group_member.profile_id ' .
328 'WHERE group_member.group_id = %d ';
330 $user->query(sprintf($qry, $this->id));
334 while ($user->fetch()) {
343 static function maxDescription()
345 $desclimit = common_config('group', 'desclimit');
346 // null => use global limit (distinct from 0!)
347 if (is_null($desclimit)) {
348 $desclimit = common_config('site', 'textlimit');
353 static function descriptionTooLong($desc)
355 $desclimit = self::maxDescription();
356 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
359 function asAtomEntry($namespace=false, $source=false)
361 $xs = new XMLStringer(true);
364 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
365 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
370 $xs->elementStart('entry', $attrs);
373 $xs->elementStart('source');
374 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
375 $xs->element('link', array('href' => $this->permalink()));
379 $xs->elementEnd('source');
382 $xs->element('title', null, $this->nickname);
383 $xs->element('summary', null, $this->description);
385 $xs->element('link', array('rel' => 'alternate',
386 'href' => $this->permalink()));
388 $xs->element('id', null, $this->permalink());
390 $xs->element('published', null, common_date_w3dtf($this->created));
391 $xs->element('updated', null, common_date_w3dtf($this->modified));
393 $xs->element('content', array('type' => 'html'), $this->description);
395 $xs->elementEnd('entry');
397 return $xs->getString();
400 function asAtomAuthor()
402 $xs = new XMLStringer(true);
404 $xs->elementStart('author');
405 $xs->element('name', null, $this->nickname);
406 $xs->element('uri', null, $this->permalink());
407 $xs->elementEnd('author');
409 return $xs->getString();
413 * Returns an XML string fragment with group information as an
414 * Activity Streams <activity:subject> element.
416 * Assumes that 'activity' namespace has been previously defined.
420 function asActivitySubject()
422 return $this->asActivityNoun('subject');
426 * Returns an XML string fragment with group information as an
427 * Activity Streams noun object with the given element type.
429 * Assumes that 'activity', 'georss', and 'poco' namespace has been
430 * previously defined.
432 * @param string $element one of 'actor', 'subject', 'object', 'target'
436 function asActivityNoun($element)
438 $noun = ActivityObject::fromGroup($this);
439 return $noun->asString('activity:' . $element);
444 return empty($this->homepage_logo)
445 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
446 : $this->homepage_logo;
449 static function register($fields) {
451 // MAGICALLY put fields into current scope
455 $group = new User_group();
457 $group->query('BEGIN');
464 $group->nickname = $nickname;
465 $group->fullname = $fullname;
466 $group->homepage = $homepage;
467 $group->description = $description;
468 $group->location = $location;
470 $group->mainpage = $mainpage;
471 $group->created = common_sql_now();
473 $result = $group->insert();
476 common_log_db_error($group, 'INSERT', __FILE__);
477 throw new ServerException(_('Could not create group.'));
480 if (!isset($uri) || empty($uri)) {
481 $orig = clone($group);
482 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
483 $result = $group->update($orig);
485 common_log_db_error($group, 'UPDATE', __FILE__);
486 throw new ServerException(_('Could not set group uri.'));
490 $result = $group->setAliases($aliases);
493 throw new ServerException(_('Could not create aliases.'));
496 $member = new Group_member();
498 $member->group_id = $group->id;
499 $member->profile_id = $userid;
500 $member->is_admin = 1;
501 $member->created = $group->created;
503 $result = $member->insert();
506 common_log_db_error($member, 'INSERT', __FILE__);
507 throw new ServerException(_('Could not set group membership.'));
511 $local_group = new Local_group();
513 $local_group->group_id = $group->id;
514 $local_group->nickname = $nickname;
515 $local_group->created = common_sql_now();
517 $result = $local_group->insert();
520 common_log_db_error($local_group, 'INSERT', __FILE__);
521 throw new ServerException(_('Could not save local group info.'));
525 $group->query('COMMIT');