]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / classes / User_group.php
1 <?php
2 /**
3  * Table Definition for user_group
4  */
5
6 class User_group extends Memcached_DataObject
7 {
8     ###START_AUTOCODE
9     /* the code below is auto generated do not remove the above tag */
10
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)
27
28     /* Static get */
29     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); }
30
31     /* the code above is auto generated do not remove the tag below */
32     ###END_AUTOCODE
33
34     function defaultLogo($size)
35     {
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');
40     }
41
42     function homeUrl()
43     {
44         $url = null;
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;
49             } else {
50                 $url = common_local_url('showgroup',
51                                         array('nickname' => $this->nickname));
52             }
53         }
54         Event::handle('EndUserGroupHomeUrl', array($this, &$url));
55         return $url;
56     }
57
58     function permalink()
59     {
60         $url = null;
61         if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
62             $url = common_local_url('groupbyid',
63                                     array('id' => $this->id));
64         }
65         Event::handle('EndUserGroupPermalink', array($this, &$url));
66         return $url;
67     }
68
69     function getNotices($offset, $limit, $since_id=null, $max_id=null)
70     {
71         $ids = Notice::stream(array($this, '_streamDirect'),
72                               array(),
73                               'user_group:notice_ids:' . $this->id,
74                               $offset, $limit, $since_id, $max_id);
75
76         return Notice::getStreamByIds($ids);
77     }
78
79     function _streamDirect($offset, $limit, $since_id, $max_id, $since)
80     {
81         $inbox = new Group_inbox();
82
83         $inbox->group_id = $this->id;
84
85         $inbox->selectAdd();
86         $inbox->selectAdd('notice_id');
87
88         if ($since_id != 0) {
89             $inbox->whereAdd('notice_id > ' . $since_id);
90         }
91
92         if ($max_id != 0) {
93             $inbox->whereAdd('notice_id <= ' . $max_id);
94         }
95
96         if (!is_null($since)) {
97             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
98         }
99
100         $inbox->orderBy('notice_id DESC');
101
102         if (!is_null($offset)) {
103             $inbox->limit($offset, $limit);
104         }
105
106         $ids = array();
107
108         if ($inbox->find()) {
109             while ($inbox->fetch()) {
110                 $ids[] = $inbox->notice_id;
111             }
112         }
113
114         return $ids;
115     }
116
117     function allowedNickname($nickname)
118     {
119         static $blacklist = array('new');
120         return !in_array($nickname, $blacklist);
121     }
122
123     function getMembers($offset=0, $limit=null)
124     {
125         $qry =
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 ';
131
132         if ($limit != null) {
133             if (common_config('db','type') == 'pgsql') {
134                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
135             } else {
136                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
137             }
138         }
139
140         $members = new Profile();
141
142         $members->query(sprintf($qry, $this->id));
143         return $members;
144     }
145
146     function getAdmins($offset=0, $limit=null)
147     {
148         $qry =
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 ';
155
156         if ($limit != null) {
157             if (common_config('db','type') == 'pgsql') {
158                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
159             } else {
160                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
161             }
162         }
163
164         $admins = new Profile();
165
166         $admins->query(sprintf($qry, $this->id));
167         return $admins;
168     }
169
170     function getBlocked($offset=0, $limit=null)
171     {
172         $qry =
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 ';
178
179         if ($limit != null) {
180             if (common_config('db','type') == 'pgsql') {
181                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
182             } else {
183                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
184             }
185         }
186
187         $blocked = new Profile();
188
189         $blocked->query(sprintf($qry, $this->id));
190         return $blocked;
191     }
192
193     function setOriginal($filename)
194     {
195         $imagefile = new ImageFile($this->id, Avatar::path($filename));
196
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);
204     }
205
206     function getBestName()
207     {
208         return ($this->fullname) ? $this->fullname : $this->nickname;
209     }
210
211     function getAliases()
212     {
213         $aliases = array();
214
215         // XXX: cache this
216
217         $alias = new Group_alias();
218
219         $alias->group_id = $this->id;
220
221         if ($alias->find()) {
222             while ($alias->fetch()) {
223                 $aliases[] = $alias->alias;
224             }
225         }
226
227         $alias->free();
228
229         return $aliases;
230     }
231
232     function setAliases($newaliases) {
233
234         $newaliases = array_unique($newaliases);
235
236         $oldaliases = $this->getAliases();
237
238         # Delete stuff that's old that not in new
239
240         $to_delete = array_diff($oldaliases, $newaliases);
241
242         # Insert stuff that's in new and not in old
243
244         $to_insert = array_diff($newaliases, $oldaliases);
245
246         $alias = new Group_alias();
247
248         $alias->group_id = $this->id;
249
250         foreach ($to_delete as $delalias) {
251             $alias->alias = $delalias;
252             $result = $alias->delete();
253             if (!$result) {
254                 common_log_db_error($alias, 'DELETE', __FILE__);
255                 return false;
256             }
257         }
258
259         foreach ($to_insert as $insalias) {
260             $alias->alias = $insalias;
261             $result = $alias->insert();
262             if (!$result) {
263                 common_log_db_error($alias, 'INSERT', __FILE__);
264                 return false;
265             }
266         }
267
268         return true;
269     }
270
271     static function getForNickname($nickname)
272     {
273         $nickname = common_canonical_nickname($nickname);
274         $group = User_group::staticGet('nickname', $nickname);
275         if (!empty($group)) {
276             return $group;
277         }
278         $alias = Group_alias::staticGet('alias', $nickname);
279         if (!empty($alias)) {
280             return User_group::staticGet('id', $alias->group_id);
281         }
282         return null;
283     }
284
285     function getDesign()
286     {
287         return Design::staticGet('id', $this->design_id);
288     }
289
290     function getUserMembers()
291     {
292         // XXX: cache this
293
294         $user = new User();
295         if(common_config('db','quote_identifiers'))
296             $user_table = '"user"';
297         else $user_table = 'user';
298
299         $qry =
300           'SELECT id ' .
301           'FROM '. $user_table .' JOIN group_member '.
302           'ON '. $user_table .'.id = group_member.profile_id ' .
303           'WHERE group_member.group_id = %d ';
304
305         $user->query(sprintf($qry, $this->id));
306
307         $ids = array();
308
309         while ($user->fetch()) {
310             $ids[] = $user->id;
311         }
312
313         $user->free();
314
315         return $ids;
316     }
317
318     static function maxDescription()
319     {
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');
324         }
325         return $desclimit;
326     }
327
328     static function descriptionTooLong($desc)
329     {
330         $desclimit = self::maxDescription();
331         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
332     }
333
334     function asAtomEntry($namespace=false, $source=false)
335     {
336         $xs = new XMLStringer(true);
337
338         if ($namespace) {
339             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
340                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
341         } else {
342             $attrs = array();
343         }
344
345         $xs->elementStart('entry', $attrs);
346
347         if ($source) {
348             $xs->elementStart('source');
349             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
350             $xs->element('link', array('href' => $this->permalink()));
351         }
352
353         if ($source) {
354             $xs->elementEnd('source');
355         }
356
357         $xs->element('title', null, $this->nickname);
358         $xs->element('summary', null, $this->description);
359
360         $xs->element('link', array('rel' => 'alternate',
361                                    'href' => $this->permalink()));
362
363         $xs->element('id', null, $this->permalink());
364
365         $xs->element('published', null, common_date_w3dtf($this->created));
366         $xs->element('updated', null, common_date_w3dtf($this->modified));
367
368         $xs->element('content', array('type' => 'html'), $this->description);
369
370         $xs->elementEnd('entry');
371
372         return $xs->getString();
373     }
374
375     function asAtomAuthor()
376     {
377         $xs = new XMLStringer(true);
378
379         $xs->elementStart('author');
380         $xs->element('name', null, $this->nickname);
381         $xs->element('uri', null, $this->permalink());
382         $xs->elementEnd('author');
383
384         return $xs->getString();
385     }
386
387     function asActivitySubject()
388     {
389         $xs = new XMLStringer(true);
390
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());
395         $xs->element(
396             'link', array(
397                 'rel'  => 'avatar',
398                 'href' =>  empty($this->homepage_logo)
399                     ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
400                     : $this->homepage_logo
401             )
402         );
403         $xs->elementEnd('activity:subject');
404
405         return $xs->getString();
406     }
407
408     static function register($fields) {
409
410         // MAGICALLY put fields into current scope
411
412         extract($fields);
413
414         $group = new User_group();
415
416         $group->query('BEGIN');
417
418         $group->nickname    = $nickname;
419         $group->fullname    = $fullname;
420         $group->homepage    = $homepage;
421         $group->description = $description;
422         $group->location    = $location;
423         $group->uri         = $uri;
424         $group->mainpage    = $mainpage;
425         $group->created     = common_sql_now();
426
427         $result = $group->insert();
428
429         if (!$result) {
430             common_log_db_error($group, 'INSERT', __FILE__);
431             throw new ServerException(_('Could not create group.'));
432         }
433
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);
438             if (!$result) {
439                 common_log_db_error($group, 'UPDATE', __FILE__);
440                 throw new ServerException(_('Could not set group uri.'));
441             }
442         }
443
444         $result = $group->setAliases($aliases);
445
446         if (!$result) {
447             throw new ServerException(_('Could not create aliases.'));
448         }
449
450         $member = new Group_member();
451
452         $member->group_id   = $group->id;
453         $member->profile_id = $userid;
454         $member->is_admin   = 1;
455         $member->created    = $group->created;
456
457         $result = $member->insert();
458
459         if (!$result) {
460             common_log_db_error($member, 'INSERT', __FILE__);
461             throw new ServerException(_('Could not set group membership.'));
462         }
463
464         if ($local) {
465             $local_group = new Local_group();
466
467             $local_group->group_id = $group->id;
468             $local_group->nickname = $nickname;
469             $local_group->created  = common_sql_now();
470
471             $result = $local_group->insert();
472
473             if (!$result) {
474                 common_log_db_error($local_group, 'INSERT', __FILE__);
475                 throw new ServerException(_('Could not save local group info.'));
476             }
477         }
478
479         $group->query('COMMIT');
480         return $group;
481     }
482 }