]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge commit 'origin/testing' into 0.9.x
[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)
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         $inbox->orderBy('notice_id DESC');
112
113         if (!is_null($offset)) {
114             $inbox->limit($offset, $limit);
115         }
116
117         $ids = array();
118
119         if ($inbox->find()) {
120             while ($inbox->fetch()) {
121                 $ids[] = $inbox->notice_id;
122             }
123         }
124
125         return $ids;
126     }
127
128     function allowedNickname($nickname)
129     {
130         static $blacklist = array('new');
131         return !in_array($nickname, $blacklist);
132     }
133
134     function getMembers($offset=0, $limit=null)
135     {
136         $qry =
137           'SELECT profile.* ' .
138           'FROM profile JOIN group_member '.
139           'ON profile.id = group_member.profile_id ' .
140           'WHERE group_member.group_id = %d ' .
141           'ORDER BY group_member.created DESC ';
142
143         if ($limit != null) {
144             if (common_config('db','type') == 'pgsql') {
145                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
146             } else {
147                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
148             }
149         }
150
151         $members = new Profile();
152
153         $members->query(sprintf($qry, $this->id));
154         return $members;
155     }
156
157     function getAdmins($offset=0, $limit=null)
158     {
159         $qry =
160           'SELECT profile.* ' .
161           'FROM profile JOIN group_member '.
162           'ON profile.id = group_member.profile_id ' .
163           'WHERE group_member.group_id = %d ' .
164           'AND group_member.is_admin = 1 ' .
165           'ORDER BY group_member.modified ASC ';
166
167         if ($limit != null) {
168             if (common_config('db','type') == 'pgsql') {
169                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
170             } else {
171                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
172             }
173         }
174
175         $admins = new Profile();
176
177         $admins->query(sprintf($qry, $this->id));
178         return $admins;
179     }
180
181     function getBlocked($offset=0, $limit=null)
182     {
183         $qry =
184           'SELECT profile.* ' .
185           'FROM profile JOIN group_block '.
186           'ON profile.id = group_block.blocked ' .
187           'WHERE group_block.group_id = %d ' .
188           'ORDER BY group_block.modified DESC ';
189
190         if ($limit != null) {
191             if (common_config('db','type') == 'pgsql') {
192                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
193             } else {
194                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
195             }
196         }
197
198         $blocked = new Profile();
199
200         $blocked->query(sprintf($qry, $this->id));
201         return $blocked;
202     }
203
204     function setOriginal($filename)
205     {
206         $imagefile = new ImageFile($this->id, Avatar::path($filename));
207
208         $orig = clone($this);
209         $this->original_logo = Avatar::url($filename);
210         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
211         $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
212         $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
213         common_debug(common_log_objstring($this));
214         return $this->update($orig);
215     }
216
217     function getBestName()
218     {
219         return ($this->fullname) ? $this->fullname : $this->nickname;
220     }
221
222     function getAliases()
223     {
224         $aliases = array();
225
226         // XXX: cache this
227
228         $alias = new Group_alias();
229
230         $alias->group_id = $this->id;
231
232         if ($alias->find()) {
233             while ($alias->fetch()) {
234                 $aliases[] = $alias->alias;
235             }
236         }
237
238         $alias->free();
239
240         return $aliases;
241     }
242
243     function setAliases($newaliases) {
244
245         $newaliases = array_unique($newaliases);
246
247         $oldaliases = $this->getAliases();
248
249         # Delete stuff that's old that not in new
250
251         $to_delete = array_diff($oldaliases, $newaliases);
252
253         # Insert stuff that's in new and not in old
254
255         $to_insert = array_diff($newaliases, $oldaliases);
256
257         $alias = new Group_alias();
258
259         $alias->group_id = $this->id;
260
261         foreach ($to_delete as $delalias) {
262             $alias->alias = $delalias;
263             $result = $alias->delete();
264             if (!$result) {
265                 common_log_db_error($alias, 'DELETE', __FILE__);
266                 return false;
267             }
268         }
269
270         foreach ($to_insert as $insalias) {
271             $alias->alias = $insalias;
272             $result = $alias->insert();
273             if (!$result) {
274                 common_log_db_error($alias, 'INSERT', __FILE__);
275                 return false;
276             }
277         }
278
279         return true;
280     }
281
282     static function getForNickname($nickname, $profile=null)
283     {
284         $nickname = common_canonical_nickname($nickname);
285
286         // Are there any matching remote groups this profile's in?
287         if ($profile) {
288             $group = $profile->getGroups();
289             while ($group->fetch()) {
290                 if ($group->nickname == $nickname) {
291                     // @fixme is this the best way?
292                     return clone($group);
293                 }
294             }
295         }
296
297         // If not, check local groups.
298
299         $group = Local_group::staticGet('nickname', $nickname);
300         if (!empty($group)) {
301             return User_group::staticGet('id', $group->group_id);
302         }
303         $alias = Group_alias::staticGet('alias', $nickname);
304         if (!empty($alias)) {
305             return User_group::staticGet('id', $alias->group_id);
306         }
307         return null;
308     }
309
310     function getDesign()
311     {
312         return Design::staticGet('id', $this->design_id);
313     }
314
315     function getUserMembers()
316     {
317         // XXX: cache this
318
319         $user = new User();
320         if(common_config('db','quote_identifiers'))
321             $user_table = '"user"';
322         else $user_table = 'user';
323
324         $qry =
325           'SELECT id ' .
326           'FROM '. $user_table .' JOIN group_member '.
327           'ON '. $user_table .'.id = group_member.profile_id ' .
328           'WHERE group_member.group_id = %d ';
329
330         $user->query(sprintf($qry, $this->id));
331
332         $ids = array();
333
334         while ($user->fetch()) {
335             $ids[] = $user->id;
336         }
337
338         $user->free();
339
340         return $ids;
341     }
342
343     static function maxDescription()
344     {
345         $desclimit = common_config('group', 'desclimit');
346         // null => use global limit (distinct from 0!)
347         if (is_null($desclimit)) {
348             $desclimit = common_config('site', 'textlimit');
349         }
350         return $desclimit;
351     }
352
353     static function descriptionTooLong($desc)
354     {
355         $desclimit = self::maxDescription();
356         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
357     }
358
359     function asAtomEntry($namespace=false, $source=false)
360     {
361         $xs = new XMLStringer(true);
362
363         if ($namespace) {
364             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
365                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
366         } else {
367             $attrs = array();
368         }
369
370         $xs->elementStart('entry', $attrs);
371
372         if ($source) {
373             $xs->elementStart('source');
374             $xs->element('id', null, $this->permalink());
375             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
376             $xs->element('link', array('href' => $this->permalink()));
377             $xs->element('updated', null, $this->modified);
378             $xs->elementEnd('source');
379         }
380
381         $xs->element('title', null, $this->nickname);
382         $xs->element('summary', null, common_xml_safe_str($this->description));
383
384         $xs->element('link', array('rel' => 'alternate',
385                                    'href' => $this->permalink()));
386
387         $xs->element('id', null, $this->permalink());
388
389         $xs->element('published', null, common_date_w3dtf($this->created));
390         $xs->element('updated', null, common_date_w3dtf($this->modified));
391
392         $xs->element(
393             'content',
394             array('type' => 'html'),
395             common_xml_safe_str($this->description)
396         );
397
398         $xs->elementEnd('entry');
399
400         return $xs->getString();
401     }
402
403     function asAtomAuthor()
404     {
405         $xs = new XMLStringer(true);
406
407         $xs->elementStart('author');
408         $xs->element('name', null, $this->nickname);
409         $xs->element('uri', null, $this->permalink());
410         $xs->elementEnd('author');
411
412         return $xs->getString();
413     }
414
415     /**
416      * Returns an XML string fragment with group information as an
417      * Activity Streams <activity:subject> element.
418      *
419      * Assumes that 'activity' namespace has been previously defined.
420      *
421      * @return string
422      */
423     function asActivitySubject()
424     {
425         return $this->asActivityNoun('subject');
426     }
427
428     /**
429      * Returns an XML string fragment with group information as an
430      * Activity Streams noun object with the given element type.
431      *
432      * Assumes that 'activity', 'georss', and 'poco' namespace has been
433      * previously defined.
434      *
435      * @param string $element one of 'actor', 'subject', 'object', 'target'
436      *
437      * @return string
438      */
439     function asActivityNoun($element)
440     {
441         $noun = ActivityObject::fromGroup($this);
442         return $noun->asString('activity:' . $element);
443     }
444
445     function getAvatar()
446     {
447         return empty($this->homepage_logo)
448             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
449             : $this->homepage_logo;
450     }
451
452     static function register($fields) {
453
454         // MAGICALLY put fields into current scope
455
456         extract($fields);
457
458         $group = new User_group();
459
460         $group->query('BEGIN');
461
462         if (empty($uri)) {
463             // fill in later...
464             $uri = null;
465         }
466
467         $group->nickname    = $nickname;
468         $group->fullname    = $fullname;
469         $group->homepage    = $homepage;
470         $group->description = $description;
471         $group->location    = $location;
472         $group->uri         = $uri;
473         $group->mainpage    = $mainpage;
474         $group->created     = common_sql_now();
475
476         $result = $group->insert();
477
478         if (!$result) {
479             common_log_db_error($group, 'INSERT', __FILE__);
480             throw new ServerException(_('Could not create group.'));
481         }
482
483         if (!isset($uri) || empty($uri)) {
484             $orig = clone($group);
485             $group->uri = common_local_url('groupbyid', array('id' => $group->id));
486             $result = $group->update($orig);
487             if (!$result) {
488                 common_log_db_error($group, 'UPDATE', __FILE__);
489                 throw new ServerException(_('Could not set group URI.'));
490             }
491         }
492
493         $result = $group->setAliases($aliases);
494
495         if (!$result) {
496             throw new ServerException(_('Could not create aliases.'));
497         }
498
499         $member = new Group_member();
500
501         $member->group_id   = $group->id;
502         $member->profile_id = $userid;
503         $member->is_admin   = 1;
504         $member->created    = $group->created;
505
506         $result = $member->insert();
507
508         if (!$result) {
509             common_log_db_error($member, 'INSERT', __FILE__);
510             throw new ServerException(_('Could not set group membership.'));
511         }
512
513         if ($local) {
514             $local_group = new Local_group();
515
516             $local_group->group_id = $group->id;
517             $local_group->nickname = $nickname;
518             $local_group->created  = common_sql_now();
519
520             $result = $local_group->insert();
521
522             if (!$result) {
523                 common_log_db_error($local_group, 'INSERT', __FILE__);
524                 throw new ServerException(_('Could not save local group info.'));
525             }
526         }
527
528         $group->query('COMMIT');
529         return $group;
530     }
531 }