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