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');
42 return common_local_url('showgroup',
43 array('nickname' => $this->nickname));
48 return common_local_url('groupbyid',
49 array('id' => $this->id));
52 function getNotices($offset, $limit)
54 $ids = Notice::stream(array($this, '_streamDirect'),
56 'user_group:notice_ids:' . $this->id,
59 return Notice::getStreamByIds($ids);
62 function _streamDirect($offset, $limit, $since_id, $max_id, $since)
64 $inbox = new Group_inbox();
66 $inbox->group_id = $this->id;
69 $inbox->selectAdd('notice_id');
72 $inbox->whereAdd('notice_id > ' . $since_id);
76 $inbox->whereAdd('notice_id <= ' . $max_id);
79 if (!is_null($since)) {
80 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
83 $inbox->orderBy('notice_id DESC');
85 if (!is_null($offset)) {
86 $inbox->limit($offset, $limit);
92 while ($inbox->fetch()) {
93 $ids[] = $inbox->notice_id;
100 function allowedNickname($nickname)
102 static $blacklist = array('new');
103 return !in_array($nickname, $blacklist);
106 function getMembers($offset=0, $limit=null)
109 'SELECT profile.* ' .
110 'FROM profile JOIN group_member '.
111 'ON profile.id = group_member.profile_id ' .
112 'WHERE group_member.group_id = %d ' .
113 'ORDER BY group_member.created DESC ';
115 if ($limit != null) {
116 if (common_config('db','type') == 'pgsql') {
117 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
119 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
123 $members = new Profile();
125 $members->query(sprintf($qry, $this->id));
129 function getAdmins($offset=0, $limit=null)
132 'SELECT profile.* ' .
133 'FROM profile JOIN group_member '.
134 'ON profile.id = group_member.profile_id ' .
135 'WHERE group_member.group_id = %d ' .
136 'AND group_member.is_admin = 1 ' .
137 'ORDER BY group_member.modified ASC ';
139 if ($limit != null) {
140 if (common_config('db','type') == 'pgsql') {
141 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
143 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
147 $admins = new Profile();
149 $admins->query(sprintf($qry, $this->id));
153 function getBlocked($offset=0, $limit=null)
156 'SELECT profile.* ' .
157 'FROM profile JOIN group_block '.
158 'ON profile.id = group_block.blocked ' .
159 'WHERE group_block.group_id = %d ' .
160 'ORDER BY group_block.modified DESC ';
162 if ($limit != null) {
163 if (common_config('db','type') == 'pgsql') {
164 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
166 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
170 $blocked = new Profile();
172 $blocked->query(sprintf($qry, $this->id));
176 function setOriginal($filename)
178 $imagefile = new ImageFile($this->id, Avatar::path($filename));
180 $orig = clone($this);
181 $this->original_logo = Avatar::url($filename);
182 $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
183 $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
184 $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
185 common_debug(common_log_objstring($this));
186 return $this->update($orig);
189 function getBestName()
191 return ($this->fullname) ? $this->fullname : $this->nickname;
194 function getAliases()
200 $alias = new Group_alias();
202 $alias->group_id = $this->id;
204 if ($alias->find()) {
205 while ($alias->fetch()) {
206 $aliases[] = $alias->alias;
215 function setAliases($newaliases) {
217 $newaliases = array_unique($newaliases);
219 $oldaliases = $this->getAliases();
221 # Delete stuff that's old that not in new
223 $to_delete = array_diff($oldaliases, $newaliases);
225 # Insert stuff that's in new and not in old
227 $to_insert = array_diff($newaliases, $oldaliases);
229 $alias = new Group_alias();
231 $alias->group_id = $this->id;
233 foreach ($to_delete as $delalias) {
234 $alias->alias = $delalias;
235 $result = $alias->delete();
237 common_log_db_error($alias, 'DELETE', __FILE__);
242 foreach ($to_insert as $insalias) {
243 $alias->alias = $insalias;
244 $result = $alias->insert();
246 common_log_db_error($alias, 'INSERT', __FILE__);
254 static function getForNickname($nickname)
256 $nickname = common_canonical_nickname($nickname);
257 $group = User_group::staticGet('nickname', $nickname);
258 if (!empty($group)) {
261 $alias = Group_alias::staticGet('alias', $nickname);
262 if (!empty($alias)) {
263 return User_group::staticGet('id', $alias->group_id);
270 return Design::staticGet('id', $this->design_id);
273 function getUserMembers()
278 if(common_config('db','quote_identifiers'))
279 $user_table = '"user"';
280 else $user_table = 'user';
284 'FROM '. $user_table .' JOIN group_member '.
285 'ON '. $user_table .'.id = group_member.profile_id ' .
286 'WHERE group_member.group_id = %d ';
288 $user->query(sprintf($qry, $this->id));
292 while ($user->fetch()) {
301 static function maxDescription()
303 $desclimit = common_config('group', 'desclimit');
304 // null => use global limit (distinct from 0!)
305 if (is_null($desclimit)) {
306 $desclimit = common_config('site', 'textlimit');
311 static function descriptionTooLong($desc)
313 $desclimit = self::maxDescription();
314 return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
317 function asAtomEntry($namespace=false, $source=false)
319 $xs = new XMLStringer(true);
322 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
323 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
328 $xs->elementStart('entry', $attrs);
331 $xs->elementStart('source');
332 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
333 $xs->element('link', array('href' => $this->permalink()));
337 $xs->elementEnd('source');
340 $xs->element('title', null, $this->nickname);
341 $xs->element('summary', null, $this->description);
343 $xs->element('link', array('rel' => 'alternate',
344 'href' => $this->permalink()));
346 $xs->element('id', null, $this->permalink());
348 $xs->element('published', null, common_date_w3dtf($this->created));
349 $xs->element('updated', null, common_date_w3dtf($this->modified));
351 $xs->element('content', array('type' => 'html'), $this->description);
353 $xs->elementEnd('entry');
355 return $xs->getString();
358 static function register($fields) {
360 // MAGICALLY put fields into current scope
364 $group = new User_group();
366 $group->query('BEGIN');
368 $group->nickname = $nickname;
369 $group->fullname = $fullname;
370 $group->homepage = $homepage;
371 $group->description = $description;
372 $group->location = $location;
373 $group->created = common_sql_now();
375 $result = $group->insert();
378 common_log_db_error($group, 'INSERT', __FILE__);
380 _('Could not create group.'),
386 $result = $group->setAliases($aliases);
390 _('Could not create aliases.'),
397 $member = new Group_member();
399 $member->group_id = $group->id;
400 $member->profile_id = $userid;
401 $member->is_admin = 1;
402 $member->created = $group->created;
404 $result = $member->insert();
407 common_log_db_error($member, 'INSERT', __FILE__);
409 _('Could not set group membership.'),
416 $group->query('COMMIT');