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('StartUserGroupPermalink', array($this, &$url))) {
62 $url = common_local_url('groupbyid',
63 array('id' => $this->id));
65 Event::handle('EndUserGroupPermalink', array($this, &$url));
69 function getNotices($offset, $limit, $since_id=null, $max_id=null)
71 $ids = Notice::stream(array($this, '_streamDirect'),
73 'user_group:notice_ids:' . $this->id,
74 $offset, $limit, $since_id, $max_id);
76 return Notice::getStreamByIds($ids);
79 function _streamDirect($offset, $limit, $since_id, $max_id, $since)
81 $inbox = new Group_inbox();
83 $inbox->group_id = $this->id;
86 $inbox->selectAdd('notice_id');
89 $inbox->whereAdd('notice_id > ' . $since_id);
93 $inbox->whereAdd('notice_id <= ' . $max_id);
96 if (!is_null($since)) {
97 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
100 $inbox->orderBy('notice_id DESC');
102 if (!is_null($offset)) {
103 $inbox->limit($offset, $limit);
108 if ($inbox->find()) {
109 while ($inbox->fetch()) {
110 $ids[] = $inbox->notice_id;
117 function allowedNickname($nickname)
119 static $blacklist = array('new');
120 return !in_array($nickname, $blacklist);
123 function getMembers($offset=0, $limit=null)
126 'SELECT profile.* ' .
127 'FROM profile JOIN group_member '.
128 'ON profile.id = group_member.profile_id ' .
129 'WHERE group_member.group_id = %d ' .
130 'ORDER BY group_member.created DESC ';
132 if ($limit != null) {
133 if (common_config('db','type') == 'pgsql') {
134 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
136 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
140 $members = new Profile();
142 $members->query(sprintf($qry, $this->id));
146 function getAdmins($offset=0, $limit=null)
149 'SELECT profile.* ' .
150 'FROM profile JOIN group_member '.
151 'ON profile.id = group_member.profile_id ' .
152 'WHERE group_member.group_id = %d ' .
153 'AND group_member.is_admin = 1 ' .
154 'ORDER BY group_member.modified ASC ';
156 if ($limit != null) {
157 if (common_config('db','type') == 'pgsql') {
158 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
160 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
164 $admins = new Profile();
166 $admins->query(sprintf($qry, $this->id));
170 function getBlocked($offset=0, $limit=null)
173 'SELECT profile.* ' .
174 'FROM profile JOIN group_block '.
175 'ON profile.id = group_block.blocked ' .
176 'WHERE group_block.group_id = %d ' .
177 'ORDER BY group_block.modified DESC ';
179 if ($limit != null) {
180 if (common_config('db','type') == 'pgsql') {
181 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
183 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
187 $blocked = new Profile();
189 $blocked->query(sprintf($qry, $this->id));
193 function setOriginal($filename)
195 $imagefile = new ImageFile($this->id, Avatar::path($filename));
197 $orig = clone($this);
198 $this->original_logo = Avatar::url($filename);
199 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
200 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
201 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
202 common_debug(common_log_objstring($this));
203 return $this->update($orig);
206 function getBestName()
208 return ($this->fullname) ? $this->fullname : $this->nickname;
211 function getAliases()
217 $alias = new Group_alias();
219 $alias->group_id = $this->id;
221 if ($alias->find()) {
222 while ($alias->fetch()) {
223 $aliases[] = $alias->alias;
232 function setAliases($newaliases) {
234 $newaliases = array_unique($newaliases);
236 $oldaliases = $this->getAliases();
238 # Delete stuff that's old that not in new
240 $to_delete = array_diff($oldaliases, $newaliases);
242 # Insert stuff that's in new and not in old
244 $to_insert = array_diff($newaliases, $oldaliases);
246 $alias = new Group_alias();
248 $alias->group_id = $this->id;
250 foreach ($to_delete as $delalias) {
251 $alias->alias = $delalias;
252 $result = $alias->delete();
254 common_log_db_error($alias, 'DELETE', __FILE__);
259 foreach ($to_insert as $insalias) {
260 $alias->alias = $insalias;
261 $result = $alias->insert();
263 common_log_db_error($alias, 'INSERT', __FILE__);
271 static function getForNickname($nickname)
273 $nickname = common_canonical_nickname($nickname);
274 $group = User_group::staticGet('nickname', $nickname);
275 if (!empty($group)) {
278 $alias = Group_alias::staticGet('alias', $nickname);
279 if (!empty($alias)) {
280 return User_group::staticGet('id', $alias->group_id);
287 return Design::staticGet('id', $this->design_id);
290 function getUserMembers()
295 if(common_config('db','quote_identifiers'))
296 $user_table = '"user"';
297 else $user_table = 'user';
301 'FROM '. $user_table .' JOIN group_member '.
302 'ON '. $user_table .'.id = group_member.profile_id ' .
303 'WHERE group_member.group_id = %d ';
305 $user->query(sprintf($qry, $this->id));
309 while ($user->fetch()) {
318 static function maxDescription()
320 $desclimit = common_config('group', 'desclimit');
321 // null => use global limit (distinct from 0!)
322 if (is_null($desclimit)) {
323 $desclimit = common_config('site', 'textlimit');
328 static function descriptionTooLong($desc)
330 $desclimit = self::maxDescription();
331 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
334 function asAtomEntry($namespace=false, $source=false)
336 $xs = new XMLStringer(true);
339 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
340 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
345 $xs->elementStart('entry', $attrs);
348 $xs->elementStart('source');
349 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
350 $xs->element('link', array('href' => $this->permalink()));
354 $xs->elementEnd('source');
357 $xs->element('title', null, $this->nickname);
358 $xs->element('summary', null, $this->description);
360 $xs->element('link', array('rel' => 'alternate',
361 'href' => $this->permalink()));
363 $xs->element('id', null, $this->permalink());
365 $xs->element('published', null, common_date_w3dtf($this->created));
366 $xs->element('updated', null, common_date_w3dtf($this->modified));
368 $xs->element('content', array('type' => 'html'), $this->description);
370 $xs->elementEnd('entry');
372 return $xs->getString();
375 function asAtomAuthor()
377 $xs = new XMLStringer(true);
379 $xs->elementStart('author');
380 $xs->element('name', null, $this->nickname);
381 $xs->element('uri', null, $this->permalink());
382 $xs->elementEnd('author');
384 return $xs->getString();
387 function asActivitySubject()
389 $xs = new XMLStringer(true);
391 $xs->elementStart('activity:subject');
392 $xs->element('activity:object', null, 'http://activitystrea.ms/schema/1.0/group');
393 $xs->element('id', null, $this->permalink());
394 $xs->element('title', null, $this->getBestName());
398 'href' => empty($this->homepage_logo)
399 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
400 : $this->homepage_logo
403 $xs->elementEnd('activity:subject');
405 return $xs->getString();
408 static function register($fields) {
410 // MAGICALLY put fields into current scope
414 $group = new User_group();
416 $group->query('BEGIN');
418 $group->nickname = $nickname;
419 $group->fullname = $fullname;
420 $group->homepage = $homepage;
421 $group->description = $description;
422 $group->location = $location;
424 $group->mainpage = $mainpage;
425 $group->created = common_sql_now();
427 $result = $group->insert();
430 common_log_db_error($group, 'INSERT', __FILE__);
431 throw new ServerException(_('Could not create group.'));
434 if (!isset($uri) || empty($uri)) {
435 $orig = clone($group);
436 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
437 $result = $group->update($orig);
439 common_log_db_error($group, 'UPDATE', __FILE__);
440 throw new ServerException(_('Could not set group uri.'));
444 $result = $group->setAliases($aliases);
447 throw new ServerException(_('Could not create aliases.'));
450 $member = new Group_member();
452 $member->group_id = $group->id;
453 $member->profile_id = $userid;
454 $member->is_admin = 1;
455 $member->created = $group->created;
457 $result = $member->insert();
460 common_log_db_error($member, 'INSERT', __FILE__);
461 throw new ServerException(_('Could not set group membership.'));
465 $local_group = new Local_group();
467 $local_group->group_id = $group->id;
468 $local_group->nickname = $nickname;
469 $local_group->created = common_sql_now();
471 $result = $local_group->insert();
474 common_log_db_error($local_group, 'INSERT', __FILE__);
475 throw new ServerException(_('Could not save local group info.'));
479 $group->query('COMMIT');