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)
284 $nickname = common_canonical_nickname($nickname);
285 $group = User_group::staticGet('nickname', $nickname);
286 if (!empty($group)) {
289 $alias = Group_alias::staticGet('alias', $nickname);
290 if (!empty($alias)) {
291 return User_group::staticGet('id', $alias->group_id);
298 return Design::staticGet('id', $this->design_id);
301 function getUserMembers()
306 if(common_config('db','quote_identifiers'))
307 $user_table = '"user"';
308 else $user_table = 'user';
312 'FROM '. $user_table .' JOIN group_member '.
313 'ON '. $user_table .'.id = group_member.profile_id ' .
314 'WHERE group_member.group_id = %d ';
316 $user->query(sprintf($qry, $this->id));
320 while ($user->fetch()) {
329 static function maxDescription()
331 $desclimit = common_config('group', 'desclimit');
332 // null => use global limit (distinct from 0!)
333 if (is_null($desclimit)) {
334 $desclimit = common_config('site', 'textlimit');
339 static function descriptionTooLong($desc)
341 $desclimit = self::maxDescription();
342 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
345 function asAtomEntry($namespace=false, $source=false)
347 $xs = new XMLStringer(true);
350 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
351 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
356 $xs->elementStart('entry', $attrs);
359 $xs->elementStart('source');
360 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
361 $xs->element('link', array('href' => $this->permalink()));
365 $xs->elementEnd('source');
368 $xs->element('title', null, $this->nickname);
369 $xs->element('summary', null, $this->description);
371 $xs->element('link', array('rel' => 'alternate',
372 'href' => $this->permalink()));
374 $xs->element('id', null, $this->permalink());
376 $xs->element('published', null, common_date_w3dtf($this->created));
377 $xs->element('updated', null, common_date_w3dtf($this->modified));
379 $xs->element('content', array('type' => 'html'), $this->description);
381 $xs->elementEnd('entry');
383 return $xs->getString();
386 function asAtomAuthor()
388 $xs = new XMLStringer(true);
390 $xs->elementStart('author');
391 $xs->element('name', null, $this->nickname);
392 $xs->element('uri', null, $this->permalink());
393 $xs->elementEnd('author');
395 return $xs->getString();
399 * Returns an XML string fragment with group information as an
400 * Activity Streams <activity:subject> element.
402 * Assumes that 'activity' namespace has been previously defined.
406 function asActivitySubject()
408 return $this->asActivityNoun('subject');
412 * Returns an XML string fragment with group information as an
413 * Activity Streams noun object with the given element type.
415 * Assumes that 'activity', 'georss', and 'poco' namespace has been
416 * previously defined.
418 * @param string $element one of 'actor', 'subject', 'object', 'target'
422 function asActivityNoun($element)
424 $noun = ActivityObject::fromGroup($this);
425 return $noun->asString('activity:' . $element);
430 return empty($this->homepage_logo)
431 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
432 : $this->homepage_logo;
435 static function register($fields) {
437 // MAGICALLY put fields into current scope
441 $group = new User_group();
443 $group->query('BEGIN');
445 $group->nickname = $nickname;
446 $group->fullname = $fullname;
447 $group->homepage = $homepage;
448 $group->description = $description;
449 $group->location = $location;
451 $group->mainpage = $mainpage;
452 $group->created = common_sql_now();
454 $result = $group->insert();
457 common_log_db_error($group, 'INSERT', __FILE__);
458 throw new ServerException(_('Could not create group.'));
461 if (!isset($uri) || empty($uri)) {
462 $orig = clone($group);
463 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
464 $result = $group->update($orig);
466 common_log_db_error($group, 'UPDATE', __FILE__);
467 throw new ServerException(_('Could not set group uri.'));
471 $result = $group->setAliases($aliases);
474 throw new ServerException(_('Could not create aliases.'));
477 $member = new Group_member();
479 $member->group_id = $group->id;
480 $member->profile_id = $userid;
481 $member->is_admin = 1;
482 $member->created = $group->created;
484 $result = $member->insert();
487 common_log_db_error($member, 'INSERT', __FILE__);
488 throw new ServerException(_('Could not set group membership.'));
492 $local_group = new Local_group();
494 $local_group->group_id = $group->id;
495 $local_group->nickname = $nickname;
496 $local_group->created = common_sql_now();
498 $result = $local_group->insert();
501 common_log_db_error($local_group, 'INSERT', __FILE__);
502 throw new ServerException(_('Could not save local group info.'));
506 $group->query('COMMIT');