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) unique_key
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
24 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
27 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_group',$k,$v); }
29 /* the code above is auto generated do not remove the tag below */
32 function defaultLogo($size)
34 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
35 AVATAR_STREAM_SIZE => 'stream',
36 AVATAR_MINI_SIZE => 'mini');
37 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
43 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
44 $url = common_local_url('showgroup',
45 array('nickname' => $this->nickname));
47 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
54 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
55 $url = common_local_url('groupbyid',
56 array('id' => $this->id));
58 Event::handle('EndUserGroupPermalink', array($this, &$url));
62 function getNotices($offset, $limit, $since_id=null, $max_id=null)
64 $ids = Notice::stream(array($this, '_streamDirect'),
66 'user_group:notice_ids:' . $this->id,
67 $offset, $limit, $since_id, $max_id);
69 return Notice::getStreamByIds($ids);
72 function _streamDirect($offset, $limit, $since_id, $max_id, $since)
74 $inbox = new Group_inbox();
76 $inbox->group_id = $this->id;
79 $inbox->selectAdd('notice_id');
82 $inbox->whereAdd('notice_id > ' . $since_id);
86 $inbox->whereAdd('notice_id <= ' . $max_id);
89 if (!is_null($since)) {
90 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
93 $inbox->orderBy('notice_id DESC');
95 if (!is_null($offset)) {
96 $inbox->limit($offset, $limit);
101 if ($inbox->find()) {
102 while ($inbox->fetch()) {
103 $ids[] = $inbox->notice_id;
110 function allowedNickname($nickname)
112 static $blacklist = array('new');
113 return !in_array($nickname, $blacklist);
116 function getMembers($offset=0, $limit=null)
119 'SELECT profile.* ' .
120 'FROM profile JOIN group_member '.
121 'ON profile.id = group_member.profile_id ' .
122 'WHERE group_member.group_id = %d ' .
123 'ORDER BY group_member.created DESC ';
125 if ($limit != null) {
126 if (common_config('db','type') == 'pgsql') {
127 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
129 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
133 $members = new Profile();
135 $members->query(sprintf($qry, $this->id));
139 function getAdmins($offset=0, $limit=null)
142 'SELECT profile.* ' .
143 'FROM profile JOIN group_member '.
144 'ON profile.id = group_member.profile_id ' .
145 'WHERE group_member.group_id = %d ' .
146 'AND group_member.is_admin = 1 ' .
147 'ORDER BY group_member.modified ASC ';
149 if ($limit != null) {
150 if (common_config('db','type') == 'pgsql') {
151 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
153 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
157 $admins = new Profile();
159 $admins->query(sprintf($qry, $this->id));
163 function getBlocked($offset=0, $limit=null)
166 'SELECT profile.* ' .
167 'FROM profile JOIN group_block '.
168 'ON profile.id = group_block.blocked ' .
169 'WHERE group_block.group_id = %d ' .
170 'ORDER BY group_block.modified DESC ';
172 if ($limit != null) {
173 if (common_config('db','type') == 'pgsql') {
174 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
176 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
180 $blocked = new Profile();
182 $blocked->query(sprintf($qry, $this->id));
186 function setOriginal($filename)
188 $imagefile = new ImageFile($this->id, Avatar::path($filename));
190 $orig = clone($this);
191 $this->original_logo = Avatar::url($filename);
192 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
193 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
194 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
195 common_debug(common_log_objstring($this));
196 return $this->update($orig);
199 function getBestName()
201 return ($this->fullname) ? $this->fullname : $this->nickname;
204 function getAliases()
210 $alias = new Group_alias();
212 $alias->group_id = $this->id;
214 if ($alias->find()) {
215 while ($alias->fetch()) {
216 $aliases[] = $alias->alias;
225 function setAliases($newaliases) {
227 $newaliases = array_unique($newaliases);
229 $oldaliases = $this->getAliases();
231 # Delete stuff that's old that not in new
233 $to_delete = array_diff($oldaliases, $newaliases);
235 # Insert stuff that's in new and not in old
237 $to_insert = array_diff($newaliases, $oldaliases);
239 $alias = new Group_alias();
241 $alias->group_id = $this->id;
243 foreach ($to_delete as $delalias) {
244 $alias->alias = $delalias;
245 $result = $alias->delete();
247 common_log_db_error($alias, 'DELETE', __FILE__);
252 foreach ($to_insert as $insalias) {
253 $alias->alias = $insalias;
254 $result = $alias->insert();
256 common_log_db_error($alias, 'INSERT', __FILE__);
264 static function getForNickname($nickname)
266 $nickname = common_canonical_nickname($nickname);
267 $group = User_group::staticGet('nickname', $nickname);
268 if (!empty($group)) {
271 $alias = Group_alias::staticGet('alias', $nickname);
272 if (!empty($alias)) {
273 return User_group::staticGet('id', $alias->group_id);
280 return Design::staticGet('id', $this->design_id);
283 function getUserMembers()
288 if(common_config('db','quote_identifiers'))
289 $user_table = '"user"';
290 else $user_table = 'user';
294 'FROM '. $user_table .' JOIN group_member '.
295 'ON '. $user_table .'.id = group_member.profile_id ' .
296 'WHERE group_member.group_id = %d ';
298 $user->query(sprintf($qry, $this->id));
302 while ($user->fetch()) {
311 static function maxDescription()
313 $desclimit = common_config('group', 'desclimit');
314 // null => use global limit (distinct from 0!)
315 if (is_null($desclimit)) {
316 $desclimit = common_config('site', 'textlimit');
321 static function descriptionTooLong($desc)
323 $desclimit = self::maxDescription();
324 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
327 function asAtomEntry($namespace=false, $source=false)
329 $xs = new XMLStringer(true);
332 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
333 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
338 $xs->elementStart('entry', $attrs);
341 $xs->elementStart('source');
342 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
343 $xs->element('link', array('href' => $this->permalink()));
347 $xs->elementEnd('source');
350 $xs->element('title', null, $this->nickname);
351 $xs->element('summary', null, $this->description);
353 $xs->element('link', array('rel' => 'alternate',
354 'href' => $this->permalink()));
356 $xs->element('id', null, $this->permalink());
358 $xs->element('published', null, common_date_w3dtf($this->created));
359 $xs->element('updated', null, common_date_w3dtf($this->modified));
361 $xs->element('content', array('type' => 'html'), $this->description);
363 $xs->elementEnd('entry');
365 return $xs->getString();
368 function asAtomAuthor()
370 $xs = new XMLStringer(true);
372 $xs->elementStart('author');
373 $xs->element('name', null, $this->nickname);
374 $xs->element('uri', null, $this->permalink());
375 $xs->elementEnd('author');
377 return $xs->getString();
380 function asActivitySubject()
382 $xs = new XMLStringer(true);
384 $xs->elementStart('activity:subject');
385 $xs->element('activity:object', null, 'http://activitystrea.ms/schema/1.0/group');
386 $xs->element('id', null, $this->permalink());
387 $xs->element('title', null, $this->getBestName());
391 'href' => empty($this->homepage_logo)
392 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
393 : $this->homepage_logo
396 $xs->elementEnd('activity:subject');
398 return $xs->getString();
401 static function register($fields) {
403 // MAGICALLY put fields into current scope
407 $group = new User_group();
409 $group->query('BEGIN');
411 $group->nickname = $nickname;
412 $group->fullname = $fullname;
413 $group->homepage = $homepage;
414 $group->description = $description;
415 $group->location = $location;
416 $group->created = common_sql_now();
418 $result = $group->insert();
421 common_log_db_error($group, 'INSERT', __FILE__);
423 _('Could not create group.'),
429 $result = $group->setAliases($aliases);
433 _('Could not create aliases.'),
440 $member = new Group_member();
442 $member->group_id = $group->id;
443 $member->profile_id = $userid;
444 $member->is_admin = 1;
445 $member->created = $group->created;
447 $result = $member->insert();
450 common_log_db_error($member, 'INSERT', __FILE__);
452 _('Could not set group membership.'),
459 $group->query('COMMIT');