]> 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('title', null, $profile->nickname . " - " . common_config('site', 'name'));
375             $xs->element('link', array('href' => $this->permalink()));
376         }
377
378         if ($source) {
379             $xs->elementEnd('source');
380         }
381
382         $xs->element('title', null, $this->nickname);
383         $xs->element('summary', null, $this->description);
384
385         $xs->element('link', array('rel' => 'alternate',
386                                    'href' => $this->permalink()));
387
388         $xs->element('id', null, $this->permalink());
389
390         $xs->element('published', null, common_date_w3dtf($this->created));
391         $xs->element('updated', null, common_date_w3dtf($this->modified));
392
393         $xs->element('content', array('type' => 'html'), $this->description);
394
395         $xs->elementEnd('entry');
396
397         return $xs->getString();
398     }
399
400     function asAtomAuthor()
401     {
402         $xs = new XMLStringer(true);
403
404         $xs->elementStart('author');
405         $xs->element('name', null, $this->nickname);
406         $xs->element('uri', null, $this->permalink());
407         $xs->elementEnd('author');
408
409         return $xs->getString();
410     }
411
412     /**
413      * Returns an XML string fragment with group information as an
414      * Activity Streams <activity:subject> element.
415      *
416      * Assumes that 'activity' namespace has been previously defined.
417      *
418      * @return string
419      */
420     function asActivitySubject()
421     {
422         return $this->asActivityNoun('subject');
423     }
424
425     /**
426      * Returns an XML string fragment with group information as an
427      * Activity Streams noun object with the given element type.
428      *
429      * Assumes that 'activity', 'georss', and 'poco' namespace has been
430      * previously defined.
431      *
432      * @param string $element one of 'actor', 'subject', 'object', 'target'
433      *
434      * @return string
435      */
436     function asActivityNoun($element)
437     {
438         $noun = ActivityObject::fromGroup($this);
439         return $noun->asString('activity:' . $element);
440     }
441
442     function getAvatar()
443     {
444         return empty($this->homepage_logo)
445             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
446             : $this->homepage_logo;
447     }
448
449     static function register($fields) {
450
451         // MAGICALLY put fields into current scope
452
453         extract($fields);
454
455         $group = new User_group();
456
457         $group->query('BEGIN');
458         
459         if (empty($uri)) {
460             // fill in later...
461             $uri = null;
462         }
463
464         $group->nickname    = $nickname;
465         $group->fullname    = $fullname;
466         $group->homepage    = $homepage;
467         $group->description = $description;
468         $group->location    = $location;
469         $group->uri         = $uri;
470         $group->mainpage    = $mainpage;
471         $group->created     = common_sql_now();
472
473         $result = $group->insert();
474
475         if (!$result) {
476             common_log_db_error($group, 'INSERT', __FILE__);
477             throw new ServerException(_('Could not create group.'));
478         }
479
480         if (!isset($uri) || empty($uri)) {
481             $orig = clone($group);
482             $group->uri = common_local_url('groupbyid', array('id' => $group->id));
483             $result = $group->update($orig);
484             if (!$result) {
485                 common_log_db_error($group, 'UPDATE', __FILE__);
486                 throw new ServerException(_('Could not set group URI.'));
487             }
488         }
489
490         $result = $group->setAliases($aliases);
491
492         if (!$result) {
493             throw new ServerException(_('Could not create aliases.'));
494         }
495
496         $member = new Group_member();
497
498         $member->group_id   = $group->id;
499         $member->profile_id = $userid;
500         $member->is_admin   = 1;
501         $member->created    = $group->created;
502
503         $result = $member->insert();
504
505         if (!$result) {
506             common_log_db_error($member, 'INSERT', __FILE__);
507             throw new ServerException(_('Could not set group membership.'));
508         }
509
510         if ($local) {
511             $local_group = new Local_group();
512
513             $local_group->group_id = $group->id;
514             $local_group->nickname = $nickname;
515             $local_group->created  = common_sql_now();
516
517             $result = $local_group->insert();
518
519             if (!$result) {
520                 common_log_db_error($local_group, 'INSERT', __FILE__);
521                 throw new ServerException(_('Could not save local group info.'));
522             }
523         }
524
525         $group->query('COMMIT');
526         return $group;
527     }
528 }