]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline 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)
283     {
284         $nickname = common_canonical_nickname($nickname);
285         $group = User_group::staticGet('nickname', $nickname);
286         if (!empty($group)) {
287             return $group;
288         }
289         $alias = Group_alias::staticGet('alias', $nickname);
290         if (!empty($alias)) {
291             return User_group::staticGet('id', $alias->group_id);
292         }
293         return null;
294     }
295
296     function getDesign()
297     {
298         return Design::staticGet('id', $this->design_id);
299     }
300
301     function getUserMembers()
302     {
303         // XXX: cache this
304
305         $user = new User();
306         if(common_config('db','quote_identifiers'))
307             $user_table = '"user"';
308         else $user_table = 'user';
309
310         $qry =
311           'SELECT id ' .
312           'FROM '. $user_table .' JOIN group_member '.
313           'ON '. $user_table .'.id = group_member.profile_id ' .
314           'WHERE group_member.group_id = %d ';
315
316         $user->query(sprintf($qry, $this->id));
317
318         $ids = array();
319
320         while ($user->fetch()) {
321             $ids[] = $user->id;
322         }
323
324         $user->free();
325
326         return $ids;
327     }
328
329     static function maxDescription()
330     {
331         $desclimit = common_config('group', 'desclimit');
332         // null => use global limit (distinct from 0!)
333         if (is_null($desclimit)) {
334             $desclimit = common_config('site', 'textlimit');
335         }
336         return $desclimit;
337     }
338
339     static function descriptionTooLong($desc)
340     {
341         $desclimit = self::maxDescription();
342         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
343     }
344
345     function asAtomEntry($namespace=false, $source=false)
346     {
347         $xs = new XMLStringer(true);
348
349         if ($namespace) {
350             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
351                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
352         } else {
353             $attrs = array();
354         }
355
356         $xs->elementStart('entry', $attrs);
357
358         if ($source) {
359             $xs->elementStart('source');
360             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
361             $xs->element('link', array('href' => $this->permalink()));
362         }
363
364         if ($source) {
365             $xs->elementEnd('source');
366         }
367
368         $xs->element('title', null, $this->nickname);
369         $xs->element('summary', null, $this->description);
370
371         $xs->element('link', array('rel' => 'alternate',
372                                    'href' => $this->permalink()));
373
374         $xs->element('id', null, $this->permalink());
375
376         $xs->element('published', null, common_date_w3dtf($this->created));
377         $xs->element('updated', null, common_date_w3dtf($this->modified));
378
379         $xs->element('content', array('type' => 'html'), $this->description);
380
381         $xs->elementEnd('entry');
382
383         return $xs->getString();
384     }
385
386     function asAtomAuthor()
387     {
388         $xs = new XMLStringer(true);
389
390         $xs->elementStart('author');
391         $xs->element('name', null, $this->nickname);
392         $xs->element('uri', null, $this->permalink());
393         $xs->elementEnd('author');
394
395         return $xs->getString();
396     }
397
398     /**
399      * Returns an XML string fragment with group information as an
400      * Activity Streams <activity:subject> element.
401      *
402      * Assumes that 'activity' namespace has been previously defined.
403      *
404      * @return string
405      */
406     function asActivitySubject()
407     {
408         return $this->asActivityNoun('subject');
409     }
410
411     /**
412      * Returns an XML string fragment with group information as an
413      * Activity Streams noun object with the given element type.
414      *
415      * Assumes that 'activity', 'georss', and 'poco' namespace has been
416      * previously defined.
417      *
418      * @param string $element one of 'actor', 'subject', 'object', 'target'
419      *
420      * @return string
421      */
422     function asActivityNoun($element)
423     {
424         $noun = ActivityObject::fromGroup($this);
425         return $noun->asString('activity:' . $element);
426     }
427
428     function getAvatar()
429     {
430         return empty($this->homepage_logo)
431             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
432             : $this->homepage_logo;
433     }
434
435     static function register($fields) {
436
437         // MAGICALLY put fields into current scope
438
439         extract($fields);
440
441         $group = new User_group();
442
443         $group->query('BEGIN');
444
445         $group->nickname    = $nickname;
446         $group->fullname    = $fullname;
447         $group->homepage    = $homepage;
448         $group->description = $description;
449         $group->location    = $location;
450         $group->uri         = $uri;
451         $group->mainpage    = $mainpage;
452         $group->created     = common_sql_now();
453
454         $result = $group->insert();
455
456         if (!$result) {
457             common_log_db_error($group, 'INSERT', __FILE__);
458             throw new ServerException(_('Could not create group.'));
459         }
460
461         if (!isset($uri) || empty($uri)) {
462             $orig = clone($group);
463             $group->uri = common_local_url('groupbyid', array('id' => $group->id));
464             $result = $group->update($orig);
465             if (!$result) {
466                 common_log_db_error($group, 'UPDATE', __FILE__);
467                 throw new ServerException(_('Could not set group URI.'));
468             }
469         }
470
471         $result = $group->setAliases($aliases);
472
473         if (!$result) {
474             throw new ServerException(_('Could not create aliases.'));
475         }
476
477         $member = new Group_member();
478
479         $member->group_id   = $group->id;
480         $member->profile_id = $userid;
481         $member->is_admin   = 1;
482         $member->created    = $group->created;
483
484         $result = $member->insert();
485
486         if (!$result) {
487             common_log_db_error($member, 'INSERT', __FILE__);
488             throw new ServerException(_('Could not set group membership.'));
489         }
490
491         if ($local) {
492             $local_group = new Local_group();
493
494             $local_group->group_id = $group->id;
495             $local_group->nickname = $nickname;
496             $local_group->created  = common_sql_now();
497
498             $result = $local_group->insert();
499
500             if (!$result) {
501                 common_log_db_error($local_group, 'INSERT', __FILE__);
502                 throw new ServerException(_('Could not save local group info.'));
503             }
504         }
505
506         $group->query('COMMIT');
507         return $group;
508     }
509 }