]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Return empty array when no subscriptions to remote
[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 getUri()
59     {
60         $uri = null;
61         if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
62             if (!empty($this->uri)) {
63                 $uri = $this->uri;
64             } else {
65                 $uri = common_local_url('groupbyid',
66                                         array('id' => $this->id));
67             }
68         }
69         Event::handle('EndUserGroupGetUri', array($this, &$uri));
70         return $uri;
71     }
72
73     function permalink()
74     {
75         $url = null;
76         if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
77             $url = common_local_url('groupbyid',
78                                     array('id' => $this->id));
79         }
80         Event::handle('EndUserGroupPermalink', array($this, &$url));
81         return $url;
82     }
83
84     function getNotices($offset, $limit, $since_id=null, $max_id=null)
85     {
86         $ids = Notice::stream(array($this, '_streamDirect'),
87                               array(),
88                               'user_group:notice_ids:' . $this->id,
89                               $offset, $limit, $since_id, $max_id);
90
91         return Notice::getStreamByIds($ids);
92     }
93
94     function _streamDirect($offset, $limit, $since_id, $max_id, $since)
95     {
96         $inbox = new Group_inbox();
97
98         $inbox->group_id = $this->id;
99
100         $inbox->selectAdd();
101         $inbox->selectAdd('notice_id');
102
103         if ($since_id != 0) {
104             $inbox->whereAdd('notice_id > ' . $since_id);
105         }
106
107         if ($max_id != 0) {
108             $inbox->whereAdd('notice_id <= ' . $max_id);
109         }
110
111         if (!is_null($since)) {
112             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
113         }
114
115         $inbox->orderBy('notice_id DESC');
116
117         if (!is_null($offset)) {
118             $inbox->limit($offset, $limit);
119         }
120
121         $ids = array();
122
123         if ($inbox->find()) {
124             while ($inbox->fetch()) {
125                 $ids[] = $inbox->notice_id;
126             }
127         }
128
129         return $ids;
130     }
131
132     function allowedNickname($nickname)
133     {
134         static $blacklist = array('new');
135         return !in_array($nickname, $blacklist);
136     }
137
138     function getMembers($offset=0, $limit=null)
139     {
140         $qry =
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 ';
146
147         if ($limit != null) {
148             if (common_config('db','type') == 'pgsql') {
149                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
150             } else {
151                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
152             }
153         }
154
155         $members = new Profile();
156
157         $members->query(sprintf($qry, $this->id));
158         return $members;
159     }
160
161     function getAdmins($offset=0, $limit=null)
162     {
163         $qry =
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 ';
170
171         if ($limit != null) {
172             if (common_config('db','type') == 'pgsql') {
173                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
174             } else {
175                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
176             }
177         }
178
179         $admins = new Profile();
180
181         $admins->query(sprintf($qry, $this->id));
182         return $admins;
183     }
184
185     function getBlocked($offset=0, $limit=null)
186     {
187         $qry =
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 ';
193
194         if ($limit != null) {
195             if (common_config('db','type') == 'pgsql') {
196                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
197             } else {
198                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
199             }
200         }
201
202         $blocked = new Profile();
203
204         $blocked->query(sprintf($qry, $this->id));
205         return $blocked;
206     }
207
208     function setOriginal($filename)
209     {
210         $imagefile = new ImageFile($this->id, Avatar::path($filename));
211
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);
219     }
220
221     function getBestName()
222     {
223         return ($this->fullname) ? $this->fullname : $this->nickname;
224     }
225
226     function getAliases()
227     {
228         $aliases = array();
229
230         // XXX: cache this
231
232         $alias = new Group_alias();
233
234         $alias->group_id = $this->id;
235
236         if ($alias->find()) {
237             while ($alias->fetch()) {
238                 $aliases[] = $alias->alias;
239             }
240         }
241
242         $alias->free();
243
244         return $aliases;
245     }
246
247     function setAliases($newaliases) {
248
249         $newaliases = array_unique($newaliases);
250
251         $oldaliases = $this->getAliases();
252
253         # Delete stuff that's old that not in new
254
255         $to_delete = array_diff($oldaliases, $newaliases);
256
257         # Insert stuff that's in new and not in old
258
259         $to_insert = array_diff($newaliases, $oldaliases);
260
261         $alias = new Group_alias();
262
263         $alias->group_id = $this->id;
264
265         foreach ($to_delete as $delalias) {
266             $alias->alias = $delalias;
267             $result = $alias->delete();
268             if (!$result) {
269                 common_log_db_error($alias, 'DELETE', __FILE__);
270                 return false;
271             }
272         }
273
274         foreach ($to_insert as $insalias) {
275             $alias->alias = $insalias;
276             $result = $alias->insert();
277             if (!$result) {
278                 common_log_db_error($alias, 'INSERT', __FILE__);
279                 return false;
280             }
281         }
282
283         return true;
284     }
285
286     static function getForNickname($nickname)
287     {
288         $nickname = common_canonical_nickname($nickname);
289         $group = User_group::staticGet('nickname', $nickname);
290         if (!empty($group)) {
291             return $group;
292         }
293         $alias = Group_alias::staticGet('alias', $nickname);
294         if (!empty($alias)) {
295             return User_group::staticGet('id', $alias->group_id);
296         }
297         return null;
298     }
299
300     function getDesign()
301     {
302         return Design::staticGet('id', $this->design_id);
303     }
304
305     function getUserMembers()
306     {
307         // XXX: cache this
308
309         $user = new User();
310         if(common_config('db','quote_identifiers'))
311             $user_table = '"user"';
312         else $user_table = 'user';
313
314         $qry =
315           'SELECT id ' .
316           'FROM '. $user_table .' JOIN group_member '.
317           'ON '. $user_table .'.id = group_member.profile_id ' .
318           'WHERE group_member.group_id = %d ';
319
320         $user->query(sprintf($qry, $this->id));
321
322         $ids = array();
323
324         while ($user->fetch()) {
325             $ids[] = $user->id;
326         }
327
328         $user->free();
329
330         return $ids;
331     }
332
333     static function maxDescription()
334     {
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');
339         }
340         return $desclimit;
341     }
342
343     static function descriptionTooLong($desc)
344     {
345         $desclimit = self::maxDescription();
346         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
347     }
348
349     function asAtomEntry($namespace=false, $source=false)
350     {
351         $xs = new XMLStringer(true);
352
353         if ($namespace) {
354             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
355                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
356         } else {
357             $attrs = array();
358         }
359
360         $xs->elementStart('entry', $attrs);
361
362         if ($source) {
363             $xs->elementStart('source');
364             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
365             $xs->element('link', array('href' => $this->permalink()));
366         }
367
368         if ($source) {
369             $xs->elementEnd('source');
370         }
371
372         $xs->element('title', null, $this->nickname);
373         $xs->element('summary', null, $this->description);
374
375         $xs->element('link', array('rel' => 'alternate',
376                                    'href' => $this->permalink()));
377
378         $xs->element('id', null, $this->permalink());
379
380         $xs->element('published', null, common_date_w3dtf($this->created));
381         $xs->element('updated', null, common_date_w3dtf($this->modified));
382
383         $xs->element('content', array('type' => 'html'), $this->description);
384
385         $xs->elementEnd('entry');
386
387         return $xs->getString();
388     }
389
390     function asAtomAuthor()
391     {
392         $xs = new XMLStringer(true);
393
394         $xs->elementStart('author');
395         $xs->element('name', null, $this->nickname);
396         $xs->element('uri', null, $this->permalink());
397         $xs->elementEnd('author');
398
399         return $xs->getString();
400     }
401
402     /**
403      * Returns an XML string fragment with group information as an
404      * Activity Streams <activity:subject> element.
405      *
406      * Assumes that 'activity' namespace has been previously defined.
407      *
408      * @return string
409      */
410     function asActivitySubject()
411     {
412         return $this->asActivityNoun('subject');
413     }
414
415     /**
416      * Returns an XML string fragment with group information as an
417      * Activity Streams noun object with the given element type.
418      *
419      * Assumes that 'activity', 'georss', and 'poco' namespace has been
420      * previously defined.
421      *
422      * @param string $element one of 'actor', 'subject', 'object', 'target'
423      *
424      * @return string
425      */
426     function asActivityNoun($element)
427     {
428         $noun = ActivityObject::fromGroup($this);
429         return $noun->asString('activity:' . $element);
430     }
431
432     function getAvatar()
433     {
434         return empty($this->homepage_logo)
435             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
436             : $this->homepage_logo;
437     }
438
439     static function register($fields) {
440
441         // MAGICALLY put fields into current scope
442
443         extract($fields);
444
445         $group = new User_group();
446
447         $group->query('BEGIN');
448
449         $group->nickname    = $nickname;
450         $group->fullname    = $fullname;
451         $group->homepage    = $homepage;
452         $group->description = $description;
453         $group->location    = $location;
454         $group->uri         = $uri;
455         $group->mainpage    = $mainpage;
456         $group->created     = common_sql_now();
457
458         $result = $group->insert();
459
460         if (!$result) {
461             common_log_db_error($group, 'INSERT', __FILE__);
462             throw new ServerException(_('Could not create group.'));
463         }
464
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);
469             if (!$result) {
470                 common_log_db_error($group, 'UPDATE', __FILE__);
471                 throw new ServerException(_('Could not set group uri.'));
472             }
473         }
474
475         $result = $group->setAliases($aliases);
476
477         if (!$result) {
478             throw new ServerException(_('Could not create aliases.'));
479         }
480
481         $member = new Group_member();
482
483         $member->group_id   = $group->id;
484         $member->profile_id = $userid;
485         $member->is_admin   = 1;
486         $member->created    = $group->created;
487
488         $result = $member->insert();
489
490         if (!$result) {
491             common_log_db_error($member, 'INSERT', __FILE__);
492             throw new ServerException(_('Could not set group membership.'));
493         }
494
495         if ($local) {
496             $local_group = new Local_group();
497
498             $local_group->group_id = $group->id;
499             $local_group->nickname = $nickname;
500             $local_group->created  = common_sql_now();
501
502             $result = $local_group->insert();
503
504             if (!$result) {
505                 common_log_db_error($local_group, 'INSERT', __FILE__);
506                 throw new ServerException(_('Could not save local group info.'));
507             }
508         }
509
510         $group->query('COMMIT');
511         return $group;
512     }
513 }