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