]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge branch '0.9.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)  unique_key
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
24     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
25
26     /* Static get */
27     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_group',$k,$v); }
28
29     /* the code above is auto generated do not remove the tag below */
30     ###END_AUTOCODE
31
32     function defaultLogo($size)
33     {
34         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
35                                   AVATAR_STREAM_SIZE => 'stream',
36                                   AVATAR_MINI_SIZE => 'mini');
37         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
38     }
39
40     function homeUrl()
41     {
42         $url = null;
43         if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
44             $url = common_local_url('showgroup',
45                                     array('nickname' => $this->nickname));
46         }
47         Event::handle('EndUserGroupHomeUrl', array($this, &$url));
48         return $url;
49     }
50
51     function permalink()
52     {
53         $url = null;
54         if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
55             $url = common_local_url('groupbyid',
56                                     array('id' => $this->id));
57         }
58         Event::handle('EndUserGroupPermalink', array($this, &$url));
59         return $url;
60     }
61
62     function getNotices($offset, $limit, $since_id=null, $max_id=null)
63     {
64         $ids = Notice::stream(array($this, '_streamDirect'),
65                               array(),
66                               'user_group:notice_ids:' . $this->id,
67                               $offset, $limit, $since_id, $max_id);
68
69         return Notice::getStreamByIds($ids);
70     }
71
72     function _streamDirect($offset, $limit, $since_id, $max_id, $since)
73     {
74         $inbox = new Group_inbox();
75
76         $inbox->group_id = $this->id;
77
78         $inbox->selectAdd();
79         $inbox->selectAdd('notice_id');
80
81         if ($since_id != 0) {
82             $inbox->whereAdd('notice_id > ' . $since_id);
83         }
84
85         if ($max_id != 0) {
86             $inbox->whereAdd('notice_id <= ' . $max_id);
87         }
88
89         if (!is_null($since)) {
90             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
91         }
92
93         $inbox->orderBy('notice_id DESC');
94
95         if (!is_null($offset)) {
96             $inbox->limit($offset, $limit);
97         }
98
99         $ids = array();
100
101         if ($inbox->find()) {
102             while ($inbox->fetch()) {
103                 $ids[] = $inbox->notice_id;
104             }
105         }
106
107         return $ids;
108     }
109
110     function allowedNickname($nickname)
111     {
112         static $blacklist = array('new');
113         return !in_array($nickname, $blacklist);
114     }
115
116     function getMembers($offset=0, $limit=null)
117     {
118         $qry =
119           'SELECT profile.* ' .
120           'FROM profile JOIN group_member '.
121           'ON profile.id = group_member.profile_id ' .
122           'WHERE group_member.group_id = %d ' .
123           'ORDER BY group_member.created DESC ';
124
125         if ($limit != null) {
126             if (common_config('db','type') == 'pgsql') {
127                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
128             } else {
129                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
130             }
131         }
132
133         $members = new Profile();
134
135         $members->query(sprintf($qry, $this->id));
136         return $members;
137     }
138
139     function getAdmins($offset=0, $limit=null)
140     {
141         $qry =
142           'SELECT profile.* ' .
143           'FROM profile JOIN group_member '.
144           'ON profile.id = group_member.profile_id ' .
145           'WHERE group_member.group_id = %d ' .
146           'AND group_member.is_admin = 1 ' .
147           'ORDER BY group_member.modified ASC ';
148
149         if ($limit != null) {
150             if (common_config('db','type') == 'pgsql') {
151                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
152             } else {
153                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
154             }
155         }
156
157         $admins = new Profile();
158
159         $admins->query(sprintf($qry, $this->id));
160         return $admins;
161     }
162
163     function getBlocked($offset=0, $limit=null)
164     {
165         $qry =
166           'SELECT profile.* ' .
167           'FROM profile JOIN group_block '.
168           'ON profile.id = group_block.blocked ' .
169           'WHERE group_block.group_id = %d ' .
170           'ORDER BY group_block.modified DESC ';
171
172         if ($limit != null) {
173             if (common_config('db','type') == 'pgsql') {
174                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
175             } else {
176                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
177             }
178         }
179
180         $blocked = new Profile();
181
182         $blocked->query(sprintf($qry, $this->id));
183         return $blocked;
184     }
185
186     function setOriginal($filename)
187     {
188         $imagefile = new ImageFile($this->id, Avatar::path($filename));
189
190         $orig = clone($this);
191         $this->original_logo = Avatar::url($filename);
192         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
193         $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
194         $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
195         common_debug(common_log_objstring($this));
196         return $this->update($orig);
197     }
198
199     function getBestName()
200     {
201         return ($this->fullname) ? $this->fullname : $this->nickname;
202     }
203
204     function getAliases()
205     {
206         $aliases = array();
207
208         // XXX: cache this
209
210         $alias = new Group_alias();
211
212         $alias->group_id = $this->id;
213
214         if ($alias->find()) {
215             while ($alias->fetch()) {
216                 $aliases[] = $alias->alias;
217             }
218         }
219
220         $alias->free();
221
222         return $aliases;
223     }
224
225     function setAliases($newaliases) {
226
227         $newaliases = array_unique($newaliases);
228
229         $oldaliases = $this->getAliases();
230
231         # Delete stuff that's old that not in new
232
233         $to_delete = array_diff($oldaliases, $newaliases);
234
235         # Insert stuff that's in new and not in old
236
237         $to_insert = array_diff($newaliases, $oldaliases);
238
239         $alias = new Group_alias();
240
241         $alias->group_id = $this->id;
242
243         foreach ($to_delete as $delalias) {
244             $alias->alias = $delalias;
245             $result = $alias->delete();
246             if (!$result) {
247                 common_log_db_error($alias, 'DELETE', __FILE__);
248                 return false;
249             }
250         }
251
252         foreach ($to_insert as $insalias) {
253             $alias->alias = $insalias;
254             $result = $alias->insert();
255             if (!$result) {
256                 common_log_db_error($alias, 'INSERT', __FILE__);
257                 return false;
258             }
259         }
260
261         return true;
262     }
263
264     static function getForNickname($nickname)
265     {
266         $nickname = common_canonical_nickname($nickname);
267         $group = User_group::staticGet('nickname', $nickname);
268         if (!empty($group)) {
269             return $group;
270         }
271         $alias = Group_alias::staticGet('alias', $nickname);
272         if (!empty($alias)) {
273             return User_group::staticGet('id', $alias->group_id);
274         }
275         return null;
276     }
277
278     function getDesign()
279     {
280         return Design::staticGet('id', $this->design_id);
281     }
282
283     function getUserMembers()
284     {
285         // XXX: cache this
286
287         $user = new User();
288         if(common_config('db','quote_identifiers'))
289             $user_table = '"user"';
290         else $user_table = 'user';
291
292         $qry =
293           'SELECT id ' .
294           'FROM '. $user_table .' JOIN group_member '.
295           'ON '. $user_table .'.id = group_member.profile_id ' .
296           'WHERE group_member.group_id = %d ';
297
298         $user->query(sprintf($qry, $this->id));
299
300         $ids = array();
301
302         while ($user->fetch()) {
303             $ids[] = $user->id;
304         }
305
306         $user->free();
307
308         return $ids;
309     }
310
311     static function maxDescription()
312     {
313         $desclimit = common_config('group', 'desclimit');
314         // null => use global limit (distinct from 0!)
315         if (is_null($desclimit)) {
316             $desclimit = common_config('site', 'textlimit');
317         }
318         return $desclimit;
319     }
320
321     static function descriptionTooLong($desc)
322     {
323         $desclimit = self::maxDescription();
324         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
325     }
326
327     function asAtomEntry($namespace=false, $source=false)
328     {
329         $xs = new XMLStringer(true);
330
331         if ($namespace) {
332             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
333                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
334         } else {
335             $attrs = array();
336         }
337
338         $xs->elementStart('entry', $attrs);
339
340         if ($source) {
341             $xs->elementStart('source');
342             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
343             $xs->element('link', array('href' => $this->permalink()));
344         }
345
346         if ($source) {
347             $xs->elementEnd('source');
348         }
349
350         $xs->element('title', null, $this->nickname);
351         $xs->element('summary', null, $this->description);
352
353         $xs->element('link', array('rel' => 'alternate',
354                                    'href' => $this->permalink()));
355
356         $xs->element('id', null, $this->permalink());
357
358         $xs->element('published', null, common_date_w3dtf($this->created));
359         $xs->element('updated', null, common_date_w3dtf($this->modified));
360
361         $xs->element('content', array('type' => 'html'), $this->description);
362
363         $xs->elementEnd('entry');
364
365         return $xs->getString();
366     }
367
368     function asAtomAuthor()
369     {
370         $xs = new XMLStringer(true);
371
372         $xs->elementStart('author');
373         $xs->element('name', null, $this->nickname);
374         $xs->element('uri', null, $this->permalink());
375         $xs->elementEnd('author');
376
377         return $xs->getString();
378     }
379
380     function asActivitySubject()
381     {
382         $xs = new XMLStringer(true);
383
384         $xs->elementStart('activity:subject');
385         $xs->element('activity:object', null, 'http://activitystrea.ms/schema/1.0/group');
386         $xs->element('id', null, $this->permalink());
387         $xs->element('title', null, $this->getBestName());
388         $xs->element(
389             'link', array(
390                 'rel'  => 'avatar',
391                 'href' =>  empty($this->homepage_logo)
392                     ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
393                     : $this->homepage_logo
394             )
395         );
396         $xs->elementEnd('activity:subject');
397
398         return $xs->getString();
399     }
400
401     static function register($fields) {
402
403         // MAGICALLY put fields into current scope
404
405         extract($fields);
406
407         $group = new User_group();
408
409         $group->query('BEGIN');
410
411         $group->nickname    = $nickname;
412         $group->fullname    = $fullname;
413         $group->homepage    = $homepage;
414         $group->description = $description;
415         $group->location    = $location;
416         $group->created     = common_sql_now();
417
418         $result = $group->insert();
419
420         if (!$result) {
421             common_log_db_error($group, 'INSERT', __FILE__);
422             $this->serverError(
423                 _('Could not create group.'),
424                 500,
425                 $this->format
426             );
427             return;
428         }
429         $result = $group->setAliases($aliases);
430
431         if (!$result) {
432             $this->serverError(
433                 _('Could not create aliases.'),
434                 500,
435                 $this->format
436             );
437             return;
438         }
439
440         $member = new Group_member();
441
442         $member->group_id   = $group->id;
443         $member->profile_id = $userid;
444         $member->is_admin   = 1;
445         $member->created    = $group->created;
446
447         $result = $member->insert();
448
449         if (!$result) {
450             common_log_db_error($member, 'INSERT', __FILE__);
451             $this->serverError(
452                 _('Could not set group membership.'),
453                 500,
454                 $this->format
455             );
456             return;
457         }
458
459         $group->query('COMMIT');
460         return $group;
461     }
462 }