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
28 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
30 /* the code above is auto generated do not remove the tag below */
33 function defaultLogo($size)
35 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
36 AVATAR_STREAM_SIZE => 'stream',
37 AVATAR_MINI_SIZE => 'mini');
38 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
44 if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
45 $url = common_local_url('showgroup',
46 array('nickname' => $this->nickname));
48 Event::handle('EndUserGroupHomeUrl', array($this, &$url));
55 if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
56 $url = common_local_url('groupbyid',
57 array('id' => $this->id));
59 Event::handle('EndUserGroupPermalink', array($this, &$url));
63 function getNotices($offset, $limit, $since_id=null, $max_id=null)
65 $ids = Notice::stream(array($this, '_streamDirect'),
67 'user_group:notice_ids:' . $this->id,
68 $offset, $limit, $since_id, $max_id);
70 return Notice::getStreamByIds($ids);
73 function _streamDirect($offset, $limit, $since_id, $max_id, $since)
75 $inbox = new Group_inbox();
77 $inbox->group_id = $this->id;
80 $inbox->selectAdd('notice_id');
83 $inbox->whereAdd('notice_id > ' . $since_id);
87 $inbox->whereAdd('notice_id <= ' . $max_id);
90 if (!is_null($since)) {
91 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
94 $inbox->orderBy('notice_id DESC');
96 if (!is_null($offset)) {
97 $inbox->limit($offset, $limit);
102 if ($inbox->find()) {
103 while ($inbox->fetch()) {
104 $ids[] = $inbox->notice_id;
111 function allowedNickname($nickname)
113 static $blacklist = array('new');
114 return !in_array($nickname, $blacklist);
117 function getMembers($offset=0, $limit=null)
120 'SELECT profile.* ' .
121 'FROM profile JOIN group_member '.
122 'ON profile.id = group_member.profile_id ' .
123 'WHERE group_member.group_id = %d ' .
124 'ORDER BY group_member.created DESC ';
126 if ($limit != null) {
127 if (common_config('db','type') == 'pgsql') {
128 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
130 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
134 $members = new Profile();
136 $members->query(sprintf($qry, $this->id));
140 function getAdmins($offset=0, $limit=null)
143 'SELECT profile.* ' .
144 'FROM profile JOIN group_member '.
145 'ON profile.id = group_member.profile_id ' .
146 'WHERE group_member.group_id = %d ' .
147 'AND group_member.is_admin = 1 ' .
148 'ORDER BY group_member.modified ASC ';
150 if ($limit != null) {
151 if (common_config('db','type') == 'pgsql') {
152 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
154 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
158 $admins = new Profile();
160 $admins->query(sprintf($qry, $this->id));
164 function getBlocked($offset=0, $limit=null)
167 'SELECT profile.* ' .
168 'FROM profile JOIN group_block '.
169 'ON profile.id = group_block.blocked ' .
170 'WHERE group_block.group_id = %d ' .
171 'ORDER BY group_block.modified DESC ';
173 if ($limit != null) {
174 if (common_config('db','type') == 'pgsql') {
175 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
177 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
181 $blocked = new Profile();
183 $blocked->query(sprintf($qry, $this->id));
187 function setOriginal($filename)
189 $imagefile = new ImageFile($this->id, Avatar::path($filename));
191 $orig = clone($this);
192 $this->original_logo = Avatar::url($filename);
193 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
194 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
195 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
196 common_debug(common_log_objstring($this));
197 return $this->update($orig);
200 function getBestName()
202 return ($this->fullname) ? $this->fullname : $this->nickname;
205 function getAliases()
211 $alias = new Group_alias();
213 $alias->group_id = $this->id;
215 if ($alias->find()) {
216 while ($alias->fetch()) {
217 $aliases[] = $alias->alias;
226 function setAliases($newaliases) {
228 $newaliases = array_unique($newaliases);
230 $oldaliases = $this->getAliases();
232 # Delete stuff that's old that not in new
234 $to_delete = array_diff($oldaliases, $newaliases);
236 # Insert stuff that's in new and not in old
238 $to_insert = array_diff($newaliases, $oldaliases);
240 $alias = new Group_alias();
242 $alias->group_id = $this->id;
244 foreach ($to_delete as $delalias) {
245 $alias->alias = $delalias;
246 $result = $alias->delete();
248 common_log_db_error($alias, 'DELETE', __FILE__);
253 foreach ($to_insert as $insalias) {
254 $alias->alias = $insalias;
255 $result = $alias->insert();
257 common_log_db_error($alias, 'INSERT', __FILE__);
265 static function getForNickname($nickname)
267 $nickname = common_canonical_nickname($nickname);
268 $group = User_group::staticGet('nickname', $nickname);
269 if (!empty($group)) {
272 $alias = Group_alias::staticGet('alias', $nickname);
273 if (!empty($alias)) {
274 return User_group::staticGet('id', $alias->group_id);
281 return Design::staticGet('id', $this->design_id);
284 function getUserMembers()
289 if(common_config('db','quote_identifiers'))
290 $user_table = '"user"';
291 else $user_table = 'user';
295 'FROM '. $user_table .' JOIN group_member '.
296 'ON '. $user_table .'.id = group_member.profile_id ' .
297 'WHERE group_member.group_id = %d ';
299 $user->query(sprintf($qry, $this->id));
303 while ($user->fetch()) {
312 static function maxDescription()
314 $desclimit = common_config('group', 'desclimit');
315 // null => use global limit (distinct from 0!)
316 if (is_null($desclimit)) {
317 $desclimit = common_config('site', 'textlimit');
322 static function descriptionTooLong($desc)
324 $desclimit = self::maxDescription();
325 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
328 function asAtomEntry($namespace=false, $source=false)
330 $xs = new XMLStringer(true);
333 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
334 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
339 $xs->elementStart('entry', $attrs);
342 $xs->elementStart('source');
343 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
344 $xs->element('link', array('href' => $this->permalink()));
348 $xs->elementEnd('source');
351 $xs->element('title', null, $this->nickname);
352 $xs->element('summary', null, $this->description);
354 $xs->element('link', array('rel' => 'alternate',
355 'href' => $this->permalink()));
357 $xs->element('id', null, $this->permalink());
359 $xs->element('published', null, common_date_w3dtf($this->created));
360 $xs->element('updated', null, common_date_w3dtf($this->modified));
362 $xs->element('content', array('type' => 'html'), $this->description);
364 $xs->elementEnd('entry');
366 return $xs->getString();
369 function asAtomAuthor()
371 $xs = new XMLStringer(true);
373 $xs->elementStart('author');
374 $xs->element('name', null, $this->nickname);
375 $xs->element('uri', null, $this->permalink());
376 $xs->elementEnd('author');
378 return $xs->getString();
381 function asActivitySubject()
383 $xs = new XMLStringer(true);
385 $xs->elementStart('activity:subject');
386 $xs->element('activity:object', null, 'http://activitystrea.ms/schema/1.0/group');
387 $xs->element('id', null, $this->permalink());
388 $xs->element('title', null, $this->getBestName());
392 'href' => empty($this->homepage_logo)
393 ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
394 : $this->homepage_logo
397 $xs->elementEnd('activity:subject');
399 return $xs->getString();
402 static function register($fields) {
404 // MAGICALLY put fields into current scope
408 $group = new User_group();
410 $group->query('BEGIN');
412 $group->nickname = $nickname;
413 $group->fullname = $fullname;
414 $group->homepage = $homepage;
415 $group->description = $description;
416 $group->location = $location;
417 $group->created = common_sql_now();
419 $result = $group->insert();
422 common_log_db_error($group, 'INSERT', __FILE__);
424 _('Could not create group.'),
430 $result = $group->setAliases($aliases);
434 _('Could not create aliases.'),
441 $member = new Group_member();
443 $member->group_id = $group->id;
444 $member->profile_id = $userid;
445 $member->is_admin = 1;
446 $member->created = $group->created;
448 $result = $member->insert();
451 common_log_db_error($member, 'INSERT', __FILE__);
453 _('Could not set group membership.'),
460 $group->query('COMMIT');