Dotclear

source: inc/core/class.dc.rs.extensions.php @ 1107:3943962d69b8

Revision 1107:3943962d69b8, 10.8 KB checked in by JcDenis, 13 years ago (diff)

merge with default branch in 2.5-RC

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3# This file is part of Dotclear 2.
4#
5# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
6# Licensed under the GPL version 2.0 license.
7# See LICENSE file or
8# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9#
10# -- END LICENSE BLOCK -----------------------------------------
11if (!defined('DC_RC_PATH')) { return; }
12
13/**
14@ingroup DC_CORE
15@brief Dotclear post record helpers.
16
17This class adds new methods to database post results.
18You can call them on every record comming from dcBlog::getPosts and similar
19methods.
20
21@warning You should not give the first argument (usualy $rs) of every described
22function.
23*/
24class rsExtPost
25{
26     /**
27     Returns whether post is editable.
28     
29     @param    rs   Invisible parameter
30     @return   <b>boolean</b>
31     */
32     public static function isEditable($rs)
33     {
34          # If user is admin or contentadmin, true
35          if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) {
36               return true;
37          }
38         
39          # No user id in result ? false
40          if (!$rs->exists('user_id')) {
41               return false;
42          }
43         
44          # If user is usage and owner of the entrie
45          if ($rs->core->auth->check('usage',$rs->core->blog->id)
46          && $rs->user_id == $rs->core->auth->userID()) {
47               return true;
48          }
49         
50          return false;
51     }
52     
53     /**
54     Returns whether post is deletable
55     
56     @param    rs   Invisible parameter
57     @return   <b>boolean</b>
58     */
59     public static function isDeletable($rs)
60     {
61          # If user is admin, or contentadmin, true
62          if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) {
63               return true;
64          }
65         
66          # No user id in result ? false
67          if (!$rs->exists('user_id')) {
68               return false;
69          }
70         
71          # If user has delete rights and is owner of the entrie
72          if ($rs->core->auth->check('delete',$rs->core->blog->id)
73          && $rs->user_id == $rs->core->auth->userID()) {
74               return true;
75          }
76         
77          return false;
78     }
79     
80     /**
81     Returns whether post is the first one of its day.
82     
83     @param    rs   Invisible parameter
84     @return   <b>boolean</b>
85     */
86     public static function firstPostOfDay($rs)
87     {
88          if ($rs->isStart()) {
89               return true;
90          }
91         
92          $cdate = date('Ymd',strtotime($rs->post_dt));
93          $rs->movePrev();
94          $ndate = date('Ymd',strtotime($rs->post_dt));
95          $rs->moveNext();
96          return $ndate != $cdate;
97     }
98     
99     /**
100     Returns whether post is the last one of its day.
101     
102     @param    rs   Invisible parameter
103     @return   <b>boolean</b>
104     */
105     public static function lastPostOfDay($rs)
106     {
107          if ($rs->isEnd()) {
108               return true;
109          }
110         
111          $cdate = date('Ymd',strtotime($rs->post_dt));
112          $rs->moveNext();
113          $ndate = date('Ymd',strtotime($rs->post_dt));
114          $rs->movePrev();
115          return $ndate != $cdate;
116     }
117     
118     /**
119     Returns whether post has been updated since publication.
120
121     @return <b>boolean</b>
122     */
123     public static function isRepublished($rs)
124     {
125          return ($rs->getTS('upddt') + dt::getTimeOffset($rs->post_tz)) > $rs->getTS();
126     }
127     
128     /**
129     Returns full post URL.
130     
131     @param    rs   Invisible parameter
132     @return   <b>string</b>
133     */
134     public static function getURL($rs)
135     {
136          return $rs->core->blog->url.$rs->core->getPostPublicURL(
137                    $rs->post_type,html::sanitizeURL($rs->post_url)
138               );
139     }
140     
141     /**
142     Returns whether post has an excerpt.
143     
144     @param    rs   Invisible parameter
145     @return   <b>boolean</b>
146     */
147     public static function isExtended($rs)
148     {
149          return $rs->post_excerpt_xhtml != '';
150     }
151     
152     /**
153     Returns post timestamp.
154     
155     @param    rs   Invisible parameter
156     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
157     @return   <b>integer</b>
158     */
159     public static function getTS($rs,$type='')
160     {
161          if ($type == 'upddt') {
162               return strtotime($rs->post_upddt);
163          } elseif ($type == 'creadt') {
164               return strtotime($rs->post_creadt);
165          } else {
166               return strtotime($rs->post_dt);
167          }
168     }
169     
170     /**
171     Returns post date formating according to the ISO 8601 standard.
172     
173     @param    rs   Invisible parameter
174     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
175     @return   <b>string</b>
176     */
177     public static function getISO8601Date($rs,$type='')
178     {
179          if ($type == 'upddt' || $type == 'creadt') {
180               return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
181          } else {
182               return dt::iso8601($rs->getTS(),$rs->post_tz);
183          }
184     }
185     
186     /**
187     Returns post date formating according to RFC 822.
188     
189     @param    rs   Invisible parameter
190     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
191     @return   <b>string</b>
192     */
193     public static function getRFC822Date($rs,$type='')
194     {
195          if ($type == 'upddt' || $type == 'creadt') {
196               return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
197          } else {
198               return dt::rfc822($rs->getTS($type),$rs->post_tz);
199          }
200     }
201     
202     /**
203     Returns post date with <var>$format</var> as formatting pattern. If format
204     is empty, uses <var>date_format</var> blog setting.
205     
206     @param    rs   Invisible parameter
207     @param    format    <b>string</b>       Date format pattern
208     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
209     @return   <b>string</b>
210     */
211     public static function getDate($rs,$format,$type='')
212     {
213          if (!$format) {
214               $format = $rs->core->blog->settings->system->date_format;
215          }
216         
217          if ($type == 'upddt') {
218               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
219          } elseif ($type == 'creadt') {
220               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
221          } else {
222               return dt::dt2str($format,$rs->post_dt);
223          }
224     }
225     
226     /**
227     Returns post time with <var>$format</var> as formatting pattern. If format
228     is empty, uses <var>time_format</var> blog setting.
229     
230     @param    rs   Invisible parameter
231     @param    format    <b>string</b>       Time format pattern
232     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
233     @return   <b>string</b>
234     */
235     public static function getTime($rs,$format,$type='')
236     {
237          if (!$format) {
238               $format = $rs->core->blog->settings->system->time_format;
239          }
240         
241          if ($type == 'upddt') {
242               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
243          } elseif ($type == 'creadt') {
244               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
245          } else {
246               return dt::dt2str($format,$rs->post_dt);
247          }
248     }
249     
250     /**
251     Returns author common name using user_id, user_name, user_firstname and
252     user_displayname fields.
253     
254     @param    rs   Invisible parameter
255     @return   <b>string</b>
256     */
257     public static function getAuthorCN($rs)
258     {
259          return dcUtils::getUserCN($rs->user_id, $rs->user_name,
260          $rs->user_firstname, $rs->user_displayname);
261     }
262     
263     /**
264     Returns author common name with a link if he specified one in its
265     preferences.
266     
267     @param    rs   Invisible parameter
268     @return   <b>string</b>
269     */
270     public static function getAuthorLink($rs)
271     {
272          $res = '%1$s';
273          $url = $rs->user_url;
274          if ($url) {
275               $res = '<a href="%2$s">%1$s</a>';
276          }
277         
278          return sprintf($res,html::escapeHTML($rs->getAuthorCN()),html::escapeHTML($url));
279     }
280     
281     /**
282     Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is
283     replaced by "%40" and "." by "%2e".
284     
285     @param    rs   Invisible parameter
286     @param    encoded   <b>boolean</b>      Encode address.
287     @return   <b>string</b>
288     */
289     public static function getAuthorEmail($rs,$encoded=true)
290     {
291          if ($encoded) {
292               return strtr($rs->user_email,array('@'=>'%40','.'=>'%2e'));
293          }
294          return $rs->user_email;
295     }
296     
297     /**
298     Returns post feed unique ID.
299     
300     @param    rs   Invisible parameter
301     @return   <b>string</b>
302     */
303     public static function getFeedID($rs)
304     {
305          return 'urn:md5:'.md5($rs->core->blog->uid.$rs->post_id);
306         
307          $url = parse_url($rs->core->blog->url);
308          $date_part = date('Y-m-d',strtotime($rs->post_creadt));
309         
310          return 'tag:'.$url['host'].','.$date_part.':'.$rs->post_id;
311     }
312     
313     /**
314     Returns post content. If <var>$absolute_urls</var> is true, appends full
315     blog URL to each relative post URLs.
316     
317     @param    rs   Invisible parameter
318     @param    absolute_urls  <b>boolean</b>      With absolute URLs
319     @return   <b>string</b>
320     */
321     public static function getContent($rs,$absolute_urls=false)
322     {
323          if ($absolute_urls) {
324               return html::absoluteURLs($rs->post_content_xhtml,$rs->getURL());
325          } else {
326               return $rs->post_content_xhtml;
327          }
328     }
329     
330     /**
331     Returns post excerpt. If <var>$absolute_urls</var> is true, appends full
332     blog URL to each relative post URLs.
333     
334     @param    rs   Invisible parameter
335     @param    absolute_urls  <b>boolean</b>      With absolute URLs
336     @return   <b>string</b>
337     */
338     public static function getExcerpt($rs,$absolute_urls=false)
339     {
340          if ($absolute_urls) {
341               return html::absoluteURLs($rs->post_excerpt_xhtml,$rs->getURL());
342          } else {
343               return $rs->post_excerpt_xhtml;
344          }
345     }
346     
347}
348
349/**
350@ingroup DC_CORE
351@brief Dotclear dates record helpers.
352
353This class adds new methods to database dates results.
354You can call them on every record comming from dcBlog::getDates.
355
356@warning You should not give the first argument (usualy $rs) of every described
357function.
358*/
359class rsExtDates
360{
361     /**
362     @param    rs   Invisible parameter
363     @return   <b>integer</b>      Date timestamp
364     */
365     public static function ts($rs)
366     {
367          return strtotime($rs->dt);
368     }
369     
370     /**
371     @param    rs   Invisible parameter
372     @return   <b>string</b>       Date year
373     */
374     public static function year($rs)
375     {
376          return date('Y',strtotime($rs->dt));
377     }
378     
379     /**
380     @param    rs   Invisible parameter
381     @return   <b>string</b>       Date month
382     */
383     public static function month($rs)
384     {
385          return date('m',strtotime($rs->dt));
386     }
387     
388     /**
389     @param    rs   Invisible parameter
390     @return   <b>integer</b>      Date day
391     */
392     public static function day($rs)
393     {
394          return date('d',strtotime($rs->dt));
395     }
396     
397     /**
398     Returns date month archive full URL.
399     
400     @param    rs   Invisible parameter
401     @param    core      <b>dcCore</b>       dcCore instance
402     @return   <b>integer</b>
403     */
404     public static function url($rs,$core)
405     {
406          $url = date('Y/m',strtotime($rs->dt));
407         
408          return $core->blog->url.$core->url->getURLFor('archive',$url);
409     }
410     
411     /**
412     Returns whether date is the first of year.
413     
414     @param    rs   Invisible parameter
415     @return   <b>boolean</b>
416     */
417     public static function yearHeader($rs)
418     {
419          if ($rs->isStart()) {
420               return true;
421          }
422         
423          $y = $rs->year();
424          $rs->movePrev();
425          $py = $rs->year();
426          $rs->moveNext();
427         
428          return $y != $py;
429     }
430     
431     /**
432     Returns whether date is the last of year.
433     
434     @param    rs   Invisible parameter
435     @return   <b>boolean</b>
436     */
437     public static function yearFooter($rs)
438     {
439          if ($rs->isEnd()) {
440               return true;
441          }
442         
443          $y = $rs->year();
444          if ($rs->moveNext()) {
445               $ny = $rs->year();
446               $rs->movePrev();
447               return $y != $ny;
448          }
449          return false;
450         
451     }
452}
453
454/**
455@ingroup DC_CORE
456@brief Dotclear dates record helpers.
457
458This class adds new methods to database dates results.
459You can call them on every record comming from dcAuth::checkUser and
460dcCore::getUsers.
461
462@warning You should not give the first argument (usualy $rs) of every described
463function.
464*/
465class rsExtUser
466{
467     /**
468     Returns a user option.
469     
470     @param    rs   Invisible parameter
471     @param    name      <b>string</b>       Option name
472     @return   <b>string</b>
473     */
474     public static function option($rs,$name)
475     {
476          $options = self::options($rs);
477         
478          if (isset($options[$name])) {
479               return $options[$name];
480          }
481          return null;
482     }
483     
484     /**
485     Returns all user options.
486     
487     @param    rs   Invisible parameter
488     @return   <b>array</b>
489     */
490     public static function options($rs)
491     {
492          $options = @unserialize($rs->user_options);
493          if (is_array($options)) {
494               return $options;
495          }
496          return array();
497     }
498}
499?>
Note: See TracBrowser for help on using the repository browser.

Sites map