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, $since)
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 if (!is_null($since)) {
112 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
115 $inbox->orderBy('notice_id DESC');
117 if (!is_null($offset)) {
118 $inbox->limit($offset, $limit);
123 if ($inbox->find()) {
124 while ($inbox->fetch()) {
125 $ids[] = $inbox->notice_id;
132 function allowedNickname($nickname)
134 static $blacklist = array('new');
135 return !in_array($nickname, $blacklist);
138 function getMembers($offset=0, $limit=null)
141 'SELECT profile.* ' .
142 'FROM profile JOIN group_member '.
143 'ON profile.id = group_member.profile_id ' .
144 'WHERE group_member.group_id = %d ' .
145 'ORDER BY group_member.created DESC ';
147 if ($limit != null) {
148 if (common_config('db','type') == 'pgsql') {
149 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
151 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
155 $members = new Profile();
157 $members->query(sprintf($qry, $this->id));
161 function getAdmins($offset=0, $limit=null)
164 'SELECT profile.* ' .
165 'FROM profile JOIN group_member '.
166 'ON profile.id = group_member.profile_id ' .
167 'WHERE group_member.group_id = %d ' .
168 'AND group_member.is_admin = 1 ' .
169 'ORDER BY group_member.modified ASC ';
171 if ($limit != null) {
172 if (common_config('db','type') == 'pgsql') {
173 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
175 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
179 $admins = new Profile();
181 $admins->query(sprintf($qry, $this->id));
185 function getBlocked($offset=0, $limit=null)
188 'SELECT profile.* ' .
189 'FROM profile JOIN group_block '.
190 'ON profile.id = group_block.blocked ' .
191 'WHERE group_block.group_id = %d ' .
192 'ORDER BY group_block.modified DESC ';
194 if ($limit != null) {
195 if (common_config('db','type') == 'pgsql') {
196 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
198 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
202 $blocked = new Profile();
204 $blocked->query(sprintf($qry, $this->id));
208 function setOriginal($filename)
210 $imagefile = new ImageFile($this->id, Avatar::path($filename));
212 $orig = clone($this);
213 $this->original_logo = Avatar::url($filename);
214 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
215 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
216 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
217 common_debug(common_log_objstring($this));
218 return $this->update($orig);
221 function getBestName()
223 return ($this->fullname) ? $this->fullname : $this->nickname;
226 function getAliases()
232 $alias = new Group_alias();
234 $alias->group_id = $this->id;
236 if ($alias->find()) {
237 while ($alias->fetch()) {
238 $aliases[] = $alias->alias;
247 function setAliases($newaliases) {
249 $newaliases = array_unique($newaliases);
251 $oldaliases = $this->getAliases();
253 # Delete stuff that's old that not in new
255 $to_delete = array_diff($oldaliases, $newaliases);
257 # Insert stuff that's in new and not in old
259 $to_insert = array_diff($newaliases, $oldaliases);
261 $alias = new Group_alias();
263 $alias->group_id = $this->id;
265 foreach ($to_delete as $delalias) {
266 $alias->alias = $delalias;
267 $result = $alias->delete();
269 common_log_db_error($alias, 'DELETE', __FILE__);
274 foreach ($to_insert as $insalias) {
275 $alias->alias = $insalias;
276 $result = $alias->insert();
278 common_log_db_error($alias, 'INSERT', __FILE__);
286 static function getForNickname($nickname)
288 $nickname = common_canonical_nickname($nickname);
289 $group = User_group::staticGet('nickname', $nickname);
290 if (!empty($group)) {
293 $alias = Group_alias::staticGet('alias', $nickname);
294 if (!empty($alias)) {
295 return User_group::staticGet('id', $alias->group_id);
302 return Design::staticGet('id', $this->design_id);
305 function getUserMembers()
310 if(common_config('db','quote_identifiers'))
311 $user_table = '"user"';
312 else $user_table = 'user';
316 'FROM '. $user_table .' JOIN group_member '.
317 'ON '. $user_table .'.id = group_member.profile_id ' .
318 'WHERE group_member.group_id = %d ';
320 $user->query(sprintf($qry, $this->id));
324 while ($user->fetch()) {
333 static function maxDescription()
335 $desclimit = common_config('group', 'desclimit');
336 // null => use global limit (distinct from 0!)
337 if (is_null($desclimit)) {
338 $desclimit = common_config('site', 'textlimit');
343 static function descriptionTooLong($desc)
345 $desclimit = self::maxDescription();
346 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
349 function asAtomEntry($namespace=false, $source=false)
351 $xs = new XMLStringer(true);
354 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
355 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
360 $xs->elementStart('entry', $attrs);
363 $xs->elementStart('source');
364 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
365 $xs->element('link', array('href' => $this->permalink()));
369 $xs->elementEnd('source');
372 $xs->element('title', null, $this->nickname);
373 $xs->element('summary', null, $this->description);
375 $xs->element('link', array('rel' => 'alternate',
376 'href' => $this->permalink()));
378 $xs->element('id', null, $this->permalink());
380 $xs->element('published', null, common_date_w3dtf($this->created));
381 $xs->element('updated', null, common_date_w3dtf($this->modified));
383 $xs->element('content', array('type' => 'html'), $this->description);
385 $xs->elementEnd('entry');
387 return $xs->getString();
390 function asAtomAuthor()
392 $xs = new XMLStringer(true);
394 $xs->elementStart('author');
395 $xs->element('name', null, $this->nickname);
396 $xs->element('uri', null, $this->permalink());
397 $xs->elementEnd('author');
399 return $xs->getString();
403 * Returns an XML string fragment with group information as an
404 * Activity Streams <activity:subject> element.
406 * Assumes that 'activity' namespace has been previously defined.
410 function asActivitySubject()
412 return $this->asActivityNoun('subject');
416 * Returns an XML string fragment with group information as an
417 * Activity Streams noun object with the given element type.
419 * Assumes that 'activity', 'georss', and 'poco' namespace has been
420 * previously defined.
422 * @param string $element one of 'actor', 'subject', 'object', 'target'
426 function asActivityNoun($element)
428 $noun = ActivityObject::fromGroup($this);
429 return $noun->asString('activity:' . $element);
434 return empty($this->homepage_logo)
435 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
436 : $this->homepage_logo;
439 static function register($fields) {
441 // MAGICALLY put fields into current scope
445 $group = new User_group();
447 $group->query('BEGIN');
449 $group->nickname = $nickname;
450 $group->fullname = $fullname;
451 $group->homepage = $homepage;
452 $group->description = $description;
453 $group->location = $location;
455 $group->mainpage = $mainpage;
456 $group->created = common_sql_now();
458 $result = $group->insert();
461 common_log_db_error($group, 'INSERT', __FILE__);
462 throw new ServerException(_('Could not create group.'));
465 if (!isset($uri) || empty($uri)) {
466 $orig = clone($group);
467 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
468 $result = $group->update($orig);
470 common_log_db_error($group, 'UPDATE', __FILE__);
471 throw new ServerException(_('Could not set group uri.'));
475 $result = $group->setAliases($aliases);
478 throw new ServerException(_('Could not create aliases.'));
481 $member = new Group_member();
483 $member->group_id = $group->id;
484 $member->profile_id = $userid;
485 $member->is_admin = 1;
486 $member->created = $group->created;
488 $result = $member->insert();
491 common_log_db_error($member, 'INSERT', __FILE__);
492 throw new ServerException(_('Could not set group membership.'));
496 $local_group = new Local_group();
498 $local_group->group_id = $group->id;
499 $local_group->nickname = $nickname;
500 $local_group->created = common_sql_now();
502 $result = $local_group->insert();
505 common_log_db_error($local_group, 'INSERT', __FILE__);
506 throw new ServerException(_('Could not save local group info.'));
510 $group->query('COMMIT');