]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Restructure theme.php to define a class Theme
[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         return common_local_url('showgroup',
43                                 array('nickname' => $this->nickname));
44     }
45
46     function permalink()
47     {
48         return common_local_url('groupbyid',
49                                 array('id' => $this->id));
50     }
51
52     function getNotices($offset, $limit)
53     {
54         $ids = Notice::stream(array($this, '_streamDirect'),
55                               array(),
56                               'user_group:notice_ids:' . $this->id,
57                               $offset, $limit);
58
59         return Notice::getStreamByIds($ids);
60     }
61
62     function _streamDirect($offset, $limit, $since_id, $max_id, $since)
63     {
64         $inbox = new Group_inbox();
65
66         $inbox->group_id = $this->id;
67
68         $inbox->selectAdd();
69         $inbox->selectAdd('notice_id');
70
71         if ($since_id != 0) {
72             $inbox->whereAdd('notice_id > ' . $since_id);
73         }
74
75         if ($max_id != 0) {
76             $inbox->whereAdd('notice_id <= ' . $max_id);
77         }
78
79         if (!is_null($since)) {
80             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
81         }
82
83         $inbox->orderBy('notice_id DESC');
84
85         if (!is_null($offset)) {
86             $inbox->limit($offset, $limit);
87         }
88
89         $ids = array();
90
91         if ($inbox->find()) {
92             while ($inbox->fetch()) {
93                 $ids[] = $inbox->notice_id;
94             }
95         }
96
97         return $ids;
98     }
99
100     function allowedNickname($nickname)
101     {
102         static $blacklist = array('new');
103         return !in_array($nickname, $blacklist);
104     }
105
106     function getMembers($offset=0, $limit=null)
107     {
108         $qry =
109           'SELECT profile.* ' .
110           'FROM profile JOIN group_member '.
111           'ON profile.id = group_member.profile_id ' .
112           'WHERE group_member.group_id = %d ' .
113           'ORDER BY group_member.created DESC ';
114
115         if ($limit != null) {
116             if (common_config('db','type') == 'pgsql') {
117                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
118             } else {
119                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
120             }
121         }
122
123         $members = new Profile();
124
125         $members->query(sprintf($qry, $this->id));
126         return $members;
127     }
128
129     function getAdmins($offset=0, $limit=null)
130     {
131         $qry =
132           'SELECT profile.* ' .
133           'FROM profile JOIN group_member '.
134           'ON profile.id = group_member.profile_id ' .
135           'WHERE group_member.group_id = %d ' .
136           'AND group_member.is_admin = 1 ' .
137           'ORDER BY group_member.modified ASC ';
138
139         if ($limit != null) {
140             if (common_config('db','type') == 'pgsql') {
141                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
142             } else {
143                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
144             }
145         }
146
147         $admins = new Profile();
148
149         $admins->query(sprintf($qry, $this->id));
150         return $admins;
151     }
152
153     function getBlocked($offset=0, $limit=null)
154     {
155         $qry =
156           'SELECT profile.* ' .
157           'FROM profile JOIN group_block '.
158           'ON profile.id = group_block.blocked ' .
159           'WHERE group_block.group_id = %d ' .
160           'ORDER BY group_block.modified DESC ';
161
162         if ($limit != null) {
163             if (common_config('db','type') == 'pgsql') {
164                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
165             } else {
166                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
167             }
168         }
169
170         $blocked = new Profile();
171
172         $blocked->query(sprintf($qry, $this->id));
173         return $blocked;
174     }
175
176     function setOriginal($filename)
177     {
178         $imagefile = new ImageFile($this->id, Avatar::path($filename));
179
180         $orig = clone($this);
181         $this->original_logo = Avatar::url($filename);
182         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
183         $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
184         $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
185         common_debug(common_log_objstring($this));
186         return $this->update($orig);
187     }
188
189     function getBestName()
190     {
191         return ($this->fullname) ? $this->fullname : $this->nickname;
192     }
193
194     function getAliases()
195     {
196         $aliases = array();
197
198         // XXX: cache this
199
200         $alias = new Group_alias();
201
202         $alias->group_id = $this->id;
203
204         if ($alias->find()) {
205             while ($alias->fetch()) {
206                 $aliases[] = $alias->alias;
207             }
208         }
209
210         $alias->free();
211
212         return $aliases;
213     }
214
215     function setAliases($newaliases) {
216
217         $newaliases = array_unique($newaliases);
218
219         $oldaliases = $this->getAliases();
220
221         # Delete stuff that's old that not in new
222
223         $to_delete = array_diff($oldaliases, $newaliases);
224
225         # Insert stuff that's in new and not in old
226
227         $to_insert = array_diff($newaliases, $oldaliases);
228
229         $alias = new Group_alias();
230
231         $alias->group_id = $this->id;
232
233         foreach ($to_delete as $delalias) {
234             $alias->alias = $delalias;
235             $result = $alias->delete();
236             if (!$result) {
237                 common_log_db_error($alias, 'DELETE', __FILE__);
238                 return false;
239             }
240         }
241
242         foreach ($to_insert as $insalias) {
243             $alias->alias = $insalias;
244             $result = $alias->insert();
245             if (!$result) {
246                 common_log_db_error($alias, 'INSERT', __FILE__);
247                 return false;
248             }
249         }
250
251         return true;
252     }
253
254     static function getForNickname($nickname)
255     {
256         $nickname = common_canonical_nickname($nickname);
257         $group = User_group::staticGet('nickname', $nickname);
258         if (!empty($group)) {
259             return $group;
260         }
261         $alias = Group_alias::staticGet('alias', $nickname);
262         if (!empty($alias)) {
263             return User_group::staticGet('id', $alias->group_id);
264         }
265         return null;
266     }
267
268     function getDesign()
269     {
270         return Design::staticGet('id', $this->design_id);
271     }
272
273     function getUserMembers()
274     {
275         // XXX: cache this
276
277         $user = new User();
278         if(common_config('db','quote_identifiers'))
279             $user_table = '"user"';
280         else $user_table = 'user';
281
282         $qry =
283           'SELECT id ' .
284           'FROM '. $user_table .' JOIN group_member '.
285           'ON '. $user_table .'.id = group_member.profile_id ' .
286           'WHERE group_member.group_id = %d ';
287
288         $user->query(sprintf($qry, $this->id));
289
290         $ids = array();
291
292         while ($user->fetch()) {
293             $ids[] = $user->id;
294         }
295
296         $user->free();
297
298         return $ids;
299     }
300
301     static function maxDescription()
302     {
303         $desclimit = common_config('group', 'desclimit');
304         // null => use global limit (distinct from 0!)
305         if (is_null($desclimit)) {
306             $desclimit = common_config('site', 'textlimit');
307         }
308         return $desclimit;
309     }
310
311     static function descriptionTooLong($desc)
312     {
313         $desclimit = self::maxDescription();
314         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
315     }
316
317     function asAtomEntry($namespace=false, $source=false)
318     {
319         $xs = new XMLStringer(true);
320
321         if ($namespace) {
322             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
323                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
324         } else {
325             $attrs = array();
326         }
327
328         $xs->elementStart('entry', $attrs);
329
330         if ($source) {
331             $xs->elementStart('source');
332             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
333             $xs->element('link', array('href' => $this->permalink()));
334         }
335
336         if ($source) {
337             $xs->elementEnd('source');
338         }
339
340         $xs->element('title', null, $this->nickname);
341         $xs->element('summary', null, $this->description);
342
343         $xs->element('link', array('rel' => 'alternate',
344                                    'href' => $this->permalink()));
345
346         $xs->element('id', null, $this->permalink());
347
348         $xs->element('published', null, common_date_w3dtf($this->created));
349         $xs->element('updated', null, common_date_w3dtf($this->modified));
350
351         $xs->element('content', array('type' => 'html'), $this->description);
352
353         $xs->elementEnd('entry');
354
355         return $xs->getString();
356     }
357 }