Dotclear

source: inc/core/class.dc.rs.extensions.php @ 851:0993f64c4809

Revision 851:0993f64c4809, 11.1 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

sexy step 2 : no more cats.

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 full post URL.
120     
121     @param    rs   Invisible parameter
122     @return   <b>string</b>
123     */
124     public static function getURL($rs)
125     {
126          return $rs->core->blog->url.$rs->core->getPostPublicURL(
127                    $rs->post_type,html::sanitizeURL($rs->post_url)
128               );
129     }
130     
131     /**
132     Returns whether post has an excerpt.
133     
134     @param    rs   Invisible parameter
135     @return   <b>boolean</b>
136     */
137     public static function isExtended($rs)
138     {
139          return $rs->post_excerpt_xhtml != '';
140     }
141     
142     /**
143     Returns post timestamp.
144     
145     @param    rs   Invisible parameter
146     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
147     @return   <b>integer</b>
148     */
149     public static function getTS($rs,$type='')
150     {
151          if ($type == 'upddt') {
152               return strtotime($rs->post_upddt);
153          } elseif ($type == 'creadt') {
154               return strtotime($rs->post_creadt);
155          } else {
156               return strtotime($rs->post_dt);
157          }
158     }
159     
160     /**
161     Returns post date formating according to the ISO 8601 standard.
162     
163     @param    rs   Invisible parameter
164     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
165     @return   <b>string</b>
166     */
167     public static function getISO8601Date($rs,$type='')
168     {
169          if ($type == 'upddt' || $type == 'creadt') {
170               return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
171          } else {
172               return dt::iso8601($rs->getTS(),$rs->post_tz);
173          }
174     }
175     
176     /**
177     Returns post date formating according to RFC 822.
178     
179     @param    rs   Invisible parameter
180     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
181     @return   <b>string</b>
182     */
183     public static function getRFC822Date($rs,$type='')
184     {
185          if ($type == 'upddt' || $type == 'creadt') {
186               return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
187          } else {
188               return dt::rfc822($rs->getTS($type),$rs->post_tz);
189          }
190     }
191     
192     /**
193     Returns post date with <var>$format</var> as formatting pattern. If format
194     is empty, uses <var>date_format</var> blog setting.
195     
196     @param    rs   Invisible parameter
197     @param    format    <b>string</b>       Date format pattern
198     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
199     @return   <b>string</b>
200     */
201     public static function getDate($rs,$format,$type='')
202     {
203          if (!$format) {
204               $format = $rs->core->blog->settings->system->date_format;
205          }
206         
207          if ($type == 'upddt') {
208               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
209          } elseif ($type == 'creadt') {
210               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
211          } else {
212               return dt::dt2str($format,$rs->post_dt);
213          }
214     }
215     
216     /**
217     Returns post time with <var>$format</var> as formatting pattern. If format
218     is empty, uses <var>time_format</var> blog setting.
219     
220     @param    rs   Invisible parameter
221     @param    format    <b>string</b>       Time format pattern
222     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
223     @return   <b>string</b>
224     */
225     public static function getTime($rs,$format,$type='')
226     {
227          if (!$format) {
228               $format = $rs->core->blog->settings->system->time_format;
229          }
230         
231          if ($type == 'upddt') {
232               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
233          } elseif ($type == 'creadt') {
234               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
235          } else {
236               return dt::dt2str($format,$rs->post_dt);
237          }
238     }
239     
240     /**
241     Returns author common name using user_id, user_name, user_firstname and
242     user_displayname fields.
243     
244     @param    rs   Invisible parameter
245     @return   <b>string</b>
246     */
247     public static function getAuthorCN($rs)
248     {
249          return dcUtils::getUserCN($rs->user_id, $rs->user_name,
250          $rs->user_firstname, $rs->user_displayname);
251     }
252     
253     /**
254     Returns author common name with a link if he specified one in its
255     preferences.
256     
257     @param    rs   Invisible parameter
258     @return   <b>string</b>
259     */
260     public static function getAuthorLink($rs)
261     {
262          $res = '%1$s';
263          $url = $rs->user_url;
264          if ($url) {
265               $res = '<a href="%2$s">%1$s</a>';
266          }
267         
268          return sprintf($res,html::escapeHTML($rs->getAuthorCN()),html::escapeHTML($url));
269     }
270     
271     /**
272     Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is
273     replaced by "%40" and "." by "%2e".
274     
275     @param    rs   Invisible parameter
276     @param    encoded   <b>boolean</b>      Encode address.
277     @return   <b>string</b>
278     */
279     public static function getAuthorEmail($rs,$encoded=true)
280     {
281          if ($encoded) {
282               return strtr($rs->user_email,array('@'=>'%40','.'=>'%2e'));
283          }
284          return $rs->user_email;
285     }
286     
287     /**
288     Returns post feed unique ID.
289     
290     @param    rs   Invisible parameter
291     @return   <b>string</b>
292     */
293     public static function getFeedID($rs)
294     {
295          return 'urn:md5:'.md5($rs->core->blog->uid.$rs->post_id);
296         
297          $url = parse_url($rs->core->blog->url);
298          $date_part = date('Y-m-d',strtotime($rs->post_creadt));
299         
300          return 'tag:'.$url['host'].','.$date_part.':'.$rs->post_id;
301     }
302     
303     /**
304     Returns post content. If <var>$absolute_urls</var> is true, appends full
305     blog URL to each relative post URLs.
306     
307     @param    rs   Invisible parameter
308     @param    absolute_urls  <b>boolean</b>      With absolute URLs
309     @return   <b>string</b>
310     */
311     public static function getContent($rs,$absolute_urls=false)
312     {
313          if ($absolute_urls) {
314               return html::absoluteURLs($rs->post_content_xhtml,$rs->getURL());
315          } else {
316               return $rs->post_content_xhtml;
317          }
318     }
319     
320     /**
321     Returns post excerpt. If <var>$absolute_urls</var> is true, appends full
322     blog URL to each relative post URLs.
323     
324     @param    rs   Invisible parameter
325     @param    absolute_urls  <b>boolean</b>      With absolute URLs
326     @return   <b>string</b>
327     */
328     public static function getExcerpt($rs,$absolute_urls=false)
329     {
330          if ($absolute_urls) {
331               return html::absoluteURLs($rs->post_excerpt_xhtml,$rs->getURL());
332          } else {
333               return $rs->post_excerpt_xhtml;
334          }
335     }
336     
337     /**
338     Returns post media count using a subquery.
339     
340     @param    rs   Invisible parameter
341     @return   <b>integer</b>
342     */
343     public static function countMedia($rs)
344     {
345          if (isset($rs->_nb_media[$rs->index()]))
346          {
347               return $rs->_nb_media[$rs->index()];
348          }
349          else
350          {
351               $strReq =
352               'SELECT count(media_id) '.
353               'FROM '.$rs->core->prefix.'post_media '.
354               'WHERE post_id = '.(integer) $rs->post_id.' ';
355               
356               $res = (integer) $rs->core->con->select($strReq)->f(0);
357               $rs->_nb_media[$rs->index()] = $res;
358               return $res;
359          }
360     }
361}
362
363/**
364@ingroup DC_CORE
365@brief Dotclear dates record helpers.
366
367This class adds new methods to database dates results.
368You can call them on every record comming from dcBlog::getDates.
369
370@warning You should not give the first argument (usualy $rs) of every described
371function.
372*/
373class rsExtDates
374{
375     /**
376     @param    rs   Invisible parameter
377     @return   <b>integer</b>      Date timestamp
378     */
379     public static function ts($rs)
380     {
381          return strtotime($rs->dt);
382     }
383     
384     /**
385     @param    rs   Invisible parameter
386     @return   <b>string</b>       Date year
387     */
388     public static function year($rs)
389     {
390          return date('Y',strtotime($rs->dt));
391     }
392     
393     /**
394     @param    rs   Invisible parameter
395     @return   <b>string</b>       Date month
396     */
397     public static function month($rs)
398     {
399          return date('m',strtotime($rs->dt));
400     }
401     
402     /**
403     @param    rs   Invisible parameter
404     @return   <b>integer</b>      Date day
405     */
406     public static function day($rs)
407     {
408          return date('d',strtotime($rs->dt));
409     }
410     
411     /**
412     Returns date month archive full URL.
413     
414     @param    rs   Invisible parameter
415     @param    core      <b>dcCore</b>       dcCore instance
416     @return   <b>integer</b>
417     */
418     public static function url($rs,$core)
419     {
420          $url = date('Y/m',strtotime($rs->dt));
421         
422          return $core->blog->url.$core->url->getURLFor('archive',$url);
423     }
424     
425     /**
426     Returns whether date is the first of year.
427     
428     @param    rs   Invisible parameter
429     @return   <b>boolean</b>
430     */
431     public static function yearHeader($rs)
432     {
433          if ($rs->isStart()) {
434               return true;
435          }
436         
437          $y = $rs->year();
438          $rs->movePrev();
439          $py = $rs->year();
440          $rs->moveNext();
441         
442          return $y != $py;
443     }
444     
445     /**
446     Returns whether date is the last of year.
447     
448     @param    rs   Invisible parameter
449     @return   <b>boolean</b>
450     */
451     public static function yearFooter($rs)
452     {
453          if ($rs->isEnd()) {
454               return true;
455          }
456         
457          $y = $rs->year();
458          if ($rs->moveNext()) {
459               $ny = $rs->year();
460               $rs->movePrev();
461               return $y != $ny;
462          }
463          return false;
464         
465     }
466}
467
468/**
469@ingroup DC_CORE
470@brief Dotclear dates record helpers.
471
472This class adds new methods to database dates results.
473You can call them on every record comming from dcAuth::checkUser and
474dcCore::getUsers.
475
476@warning You should not give the first argument (usualy $rs) of every described
477function.
478*/
479class rsExtUser
480{
481     /**
482     Returns a user option.
483     
484     @param    rs   Invisible parameter
485     @param    name      <b>string</b>       Option name
486     @return   <b>string</b>
487     */
488     public static function option($rs,$name)
489     {
490          $options = self::options($rs);
491         
492          if (isset($options[$name])) {
493               return $options[$name];
494          }
495          return null;
496     }
497     
498     /**
499     Returns all user options.
500     
501     @param    rs   Invisible parameter
502     @return   <b>array</b>
503     */
504     public static function options($rs)
505     {
506          $options = @unserialize($rs->user_options);
507          if (is_array($options)) {
508               return $options;
509          }
510          return array();
511     }
512}
513?>
Note: See TracBrowser for help on using the repository browser.

Sites map