Dotclear

source: inc/core/class.dc.rs.extensions.php @ 3167:9b0e5988c0eb

Revision 3167:9b0e5988c0eb, 21.2 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Cope with sort of "un-attached" media → allows new features as "featured media" plugin. Video and Audio HTML5 element are now used (if possible) rather than Flash object for attachments.

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3# This file is part of Dotclear 2.
4#
5# Copyright (c) 2003-2013 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 comments are enabled on post.
120
121     @param    rs   Invisible parameter
122     @return   <b>boolean</b>
123     */
124     public static function commentsActive($rs)
125     {
126          return
127          $rs->core->blog->settings->system->allow_comments
128          && $rs->post_open_comment
129          && ($rs->core->blog->settings->system->comments_ttl == 0 ||
130          time()-($rs->core->blog->settings->system->comments_ttl*86400) < $rs->getTS());
131     }
132
133     /**
134     Returns whether trackbacks are enabled on post.
135
136     @param    rs   Invisible parameter
137     @return   <b>boolean</b>
138     */
139     public static function trackbacksActive($rs)
140     {
141          return
142          $rs->core->blog->settings->system->allow_trackbacks
143          && $rs->post_open_tb
144          && ($rs->core->blog->settings->system->trackbacks_ttl == 0 ||
145          time()-($rs->core->blog->settings->system->trackbacks_ttl*86400) < $rs->getTS());
146     }
147
148     /**
149     Returns whether post has at least one comment.
150
151     @param    rs   Invisible parameter
152     @return   <b>boolean</b>
153     */
154     public static function hasComments($rs)
155     {
156          return $rs->nb_comment > 0;
157     }
158
159     /**
160     Returns whether post has at least one trackbacks.
161
162     @return   <b>boolean</b>
163     */
164     public static function hasTrackbacks($rs)
165     {
166          return $rs->nb_trackback > 0;
167     }
168
169     /**
170     Returns whether post has been updated since publication.
171
172     @return <b>boolean</b>
173     */
174     public static function isRepublished($rs)
175     {
176          return ($rs->getTS('upddt') + dt::getTimeOffset($rs->post_tz)) > $rs->getTS();
177     }
178
179     /**
180     Returns full post URL.
181
182     @param    rs   Invisible parameter
183     @return   <b>string</b>
184     */
185     public static function getURL($rs)
186     {
187          return $rs->core->blog->url.$rs->core->getPostPublicURL(
188                    $rs->post_type,html::sanitizeURL($rs->post_url)
189               );
190     }
191
192     /**
193     Returns full post category URL.
194
195     @param    rs   Invisible parameter
196     @return   <b>string</b>
197     */
198     public static function getCategoryURL($rs)
199     {
200          return $rs->core->blog->url.$rs->core->url->getURLFor('category',html::sanitizeURL($rs->cat_url));
201     }
202
203     /**
204     Returns whether post has an excerpt.
205
206     @param    rs   Invisible parameter
207     @return   <b>boolean</b>
208     */
209     public static function isExtended($rs)
210     {
211          return $rs->post_excerpt_xhtml != '';
212     }
213
214     /**
215     Returns post timestamp.
216
217     @param    rs   Invisible parameter
218     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
219     @return   <b>integer</b>
220     */
221     public static function getTS($rs,$type='')
222     {
223          if ($type == 'upddt') {
224               return strtotime($rs->post_upddt);
225          } elseif ($type == 'creadt') {
226               return strtotime($rs->post_creadt);
227          } else {
228               return strtotime($rs->post_dt);
229          }
230     }
231
232     /**
233     Returns post date formating according to the ISO 8601 standard.
234
235     @param    rs   Invisible parameter
236     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
237     @return   <b>string</b>
238     */
239     public static function getISO8601Date($rs,$type='')
240     {
241          if ($type == 'upddt' || $type == 'creadt') {
242               return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
243          } else {
244               return dt::iso8601($rs->getTS(),$rs->post_tz);
245          }
246     }
247
248     /**
249     Returns post date formating according to RFC 822.
250
251     @param    rs   Invisible parameter
252     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
253     @return   <b>string</b>
254     */
255     public static function getRFC822Date($rs,$type='')
256     {
257          if ($type == 'upddt' || $type == 'creadt') {
258               return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz);
259          } else {
260               return dt::rfc822($rs->getTS($type),$rs->post_tz);
261          }
262     }
263
264     /**
265     Returns post date with <var>$format</var> as formatting pattern. If format
266     is empty, uses <var>date_format</var> blog setting.
267
268     @param    rs   Invisible parameter
269     @param    format    <b>string</b>       Date format pattern
270     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
271     @return   <b>string</b>
272     */
273     public static function getDate($rs,$format,$type='')
274     {
275          if (!$format) {
276               $format = $rs->core->blog->settings->system->date_format;
277          }
278
279          if ($type == 'upddt') {
280               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
281          } elseif ($type == 'creadt') {
282               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
283          } else {
284               return dt::dt2str($format,$rs->post_dt);
285          }
286     }
287
288     /**
289     Returns post time with <var>$format</var> as formatting pattern. If format
290     is empty, uses <var>time_format</var> blog setting.
291
292     @param    rs   Invisible parameter
293     @param    format    <b>string</b>       Time format pattern
294     @param    type <b>string</b>       (dt|upddt|creadt) defaults to post_dt
295     @return   <b>string</b>
296     */
297     public static function getTime($rs,$format,$type='')
298     {
299          if (!$format) {
300               $format = $rs->core->blog->settings->system->time_format;
301          }
302
303          if ($type == 'upddt') {
304               return dt::dt2str($format,$rs->post_upddt,$rs->post_tz);
305          } elseif ($type == 'creadt') {
306               return dt::dt2str($format,$rs->post_creadt,$rs->post_tz);
307          } else {
308               return dt::dt2str($format,$rs->post_dt);
309          }
310     }
311
312     /**
313     Returns author common name using user_id, user_name, user_firstname and
314     user_displayname fields.
315
316     @param    rs   Invisible parameter
317     @return   <b>string</b>
318     */
319     public static function getAuthorCN($rs)
320     {
321          return dcUtils::getUserCN($rs->user_id, $rs->user_name,
322          $rs->user_firstname, $rs->user_displayname);
323     }
324
325     /**
326     Returns author common name with a link if he specified one in its
327     preferences.
328
329     @param    rs   Invisible parameter
330     @return   <b>string</b>
331     */
332     public static function getAuthorLink($rs)
333     {
334          $res = '%1$s';
335          $url = $rs->user_url;
336          if ($url) {
337               $res = '<a href="%2$s">%1$s</a>';
338          }
339
340          return sprintf($res,html::escapeHTML($rs->getAuthorCN()),html::escapeHTML($url));
341     }
342
343     /**
344     Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is
345     replaced by "%40" and "." by "%2e".
346
347     @param    rs   Invisible parameter
348     @param    encoded   <b>boolean</b>      Encode address.
349     @return   <b>string</b>
350     */
351     public static function getAuthorEmail($rs,$encoded=true)
352     {
353          if ($encoded) {
354               return strtr($rs->user_email,array('@'=>'%40','.'=>'%2e'));
355          }
356          return $rs->user_email;
357     }
358
359     /**
360     Returns post feed unique ID.
361
362     @param    rs   Invisible parameter
363     @return   <b>string</b>
364     */
365     public static function getFeedID($rs)
366     {
367          return 'urn:md5:'.md5($rs->core->blog->uid.$rs->post_id);
368
369          $url = parse_url($rs->core->blog->url);
370          $date_part = date('Y-m-d',strtotime($rs->post_creadt));
371
372          return 'tag:'.$url['host'].','.$date_part.':'.$rs->post_id;
373     }
374
375     /**
376     Returns trackback RDF information block in HTML comment.
377
378     @param    rs   Invisible parameter
379     @return   <b>string</b>
380     */
381     public static function getTrackbackData($rs)
382     {
383          return
384          "<![CDATA[>\n".
385          "<!--[\n".
386          '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n".
387          '  xmlns:dc="http://purl.org/dc/elements/1.1/"'."\n".
388          '  xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">'."\n".
389          "<rdf:Description\n".
390          '  rdf:about="'.$rs->getURL().'"'."\n".
391          '  dc:identifier="'.$rs->getURL().'"'."\n".
392          '  dc:title="'.htmlspecialchars($rs->post_title,ENT_COMPAT,'UTF-8').'"'."\n".
393          '  trackback:ping="'.$rs->getTrackbackLink().'" />'."\n".
394          "</rdf:RDF>\n".
395          "<!]]><!---->\n";
396     }
397
398     /**
399     Returns post trackback full URL.
400
401     @param    rs   Invisible parameter
402     @return   <b>string</b>
403     */
404     public static function getTrackbackLink($rs)
405     {
406          return $rs->core->blog->url.$rs->core->url->getURLFor('trackback',$rs->post_id);
407     }
408
409     /**
410     Returns post content. If <var>$absolute_urls</var> is true, appends full
411     blog URL to each relative post URLs.
412
413     @param    rs   Invisible parameter
414     @param    absolute_urls  <b>boolean</b>      With absolute URLs
415     @return   <b>string</b>
416     */
417     public static function getContent($rs,$absolute_urls=false)
418     {
419          if ($absolute_urls) {
420               return html::absoluteURLs($rs->post_content_xhtml,$rs->getURL());
421          } else {
422               return $rs->post_content_xhtml;
423          }
424     }
425
426     /**
427     Returns post excerpt. If <var>$absolute_urls</var> is true, appends full
428     blog URL to each relative post URLs.
429
430     @param    rs   Invisible parameter
431     @param    absolute_urls  <b>boolean</b>      With absolute URLs
432     @return   <b>string</b>
433     */
434     public static function getExcerpt($rs,$absolute_urls=false)
435     {
436          if ($absolute_urls) {
437               return html::absoluteURLs($rs->post_excerpt_xhtml,$rs->getURL());
438          } else {
439               return $rs->post_excerpt_xhtml;
440          }
441     }
442
443     /**
444     Returns post media count using a subquery.
445
446     @param    rs   Invisible parameter
447     @return   <b>integer</b>
448     */
449     public static function countMedia($rs,$link_type=null)
450     {
451          if (isset($rs->_nb_media[$rs->index()]))
452          {
453               return $rs->_nb_media[$rs->index()];
454          }
455          else
456          {
457               $strReq =
458               'SELECT count(media_id) '.
459               'FROM '.$rs->core->prefix.'post_media '.
460               'WHERE post_id = '.(integer) $rs->post_id.' ';
461               if ($link_type != null) {
462                    $strReq .= "AND link_type = '".$rs->core->con->escape($link_type)."'";
463               }
464
465               $res = (integer) $rs->core->con->select($strReq)->f(0);
466               $rs->_nb_media[$rs->index()] = $res;
467               return $res;
468          }
469     }
470}
471
472/**
473@ingroup DC_CORE
474@brief Dotclear comment record helpers.
475
476This class adds new methods to database comment results.
477You can call them on every record comming from dcBlog::getComments and similar
478methods.
479
480@warning You should not give the first argument (usualy $rs) of every described
481function.
482*/
483class rsExtComment
484{
485     /**
486     Returns comment date with <var>$format</var> as formatting pattern. If
487     format is empty, uses <var>date_format</var> blog setting.
488
489     @param    rs   Invisible parameter
490     @param    format    <b>string</b>       Date format pattern
491     @param    type <b>string</b>       (dt|upddt) defaults to comment_dt
492     @return   <b>string</b>
493     */
494     public static function getDate($rs,$format,$type='')
495     {
496          if (!$format) {
497               $format = $rs->core->blog->settings->system->date_format;
498          }
499
500          if ($type == 'upddt') {
501               return dt::dt2str($format,$rs->comment_upddt,$rs->comment_tz);
502          } else {
503               return dt::dt2str($format,$rs->comment_dt);
504          }
505     }
506
507     /**
508     Returns comment time with <var>$format</var> as formatting pattern. If
509     format is empty, uses <var>time_format</var> blog setting.
510
511     @param    rs   Invisible parameter
512     @param    format    <b>string</b>       Date format pattern
513     @param    type <b>string</b>       (dt|upddt) defaults to comment_dt
514     @return   <b>string</b>
515     */
516     public static function getTime($rs,$format,$type='')
517     {
518          if (!$format) {
519               $format = $rs->core->blog->settings->system->time_format;
520          }
521
522          if ($type == 'upddt') {
523               return dt::dt2str($format,$rs->comment_updt,$rs->comment_tz);
524          } else {
525               return dt::dt2str($format,$rs->comment_dt);
526          }
527     }
528
529     /**
530     Returns comment timestamp.
531
532     @param    rs   Invisible parameter
533     @param    type <b>string</b>       (dt|upddt) defaults to comment_dt
534     @return   <b>integer</b>
535     */
536     public static function getTS($rs,$type='')
537     {
538          if ($type == 'upddt') {
539               return strtotime($rs->comment_upddt);
540          } else {
541               return strtotime($rs->comment_dt);
542          }
543     }
544
545     /**
546     Returns comment date formating according to the ISO 8601 standard.
547
548     @param    rs   Invisible parameter
549     @param    type <b>string</b>       (dt|upddt) defaults to comment_dt
550     @return   <b>string</b>
551     */
552     public static function getISO8601Date($rs,$type='')
553     {
554          if ($type == 'upddt') {
555               return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->comment_tz),$rs->comment_tz);
556          } else {
557               return dt::iso8601($rs->getTS(),$rs->comment_tz);
558          }
559     }
560
561     /**
562     Returns comment date formating according to RFC 822.
563
564     @param    rs   Invisible parameter
565     @param    type <b>string</b>       (dt|upddt) defaults to comment_dt
566     @return   <b>string</b>
567     */
568     public static function getRFC822Date($rs,$type='')
569     {
570          if ($type == 'upddt') {
571               return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->comment_tz),$rs->comment_tz);
572          } else {
573               return dt::rfc822($rs->getTS(),$rs->comment_tz);
574          }
575     }
576
577     /**
578     Returns comment content. If <var>$absolute_urls</var> is true, appends full
579     blog URL to each relative post URLs.
580
581     @param    rs   Invisible parameter
582     @param    absolute_urls  <b>boolean</b>      With absolute URLs
583     @return   <b>string</b>
584     */
585     public static function getContent($rs,$absolute_urls=false)
586     {
587          $res = $rs->comment_content;
588
589          if ($rs->core->blog->settings->system->comments_nofollow) {
590               $res = preg_replace_callback('#<a(.*?href=".*?".*?)>#ms',array('self','noFollowURL'),$res);
591          }
592
593          if ($absolute_urls) {
594               $res = html::absoluteURLs($res,$rs->getPostURL());
595          }
596
597          return $res;
598     }
599
600     private static function noFollowURL($m)
601     {
602          if (preg_match('/rel="nofollow"/',$m[1])) {
603               return $m[0];
604          }
605
606          return '<a'.$m[1].' rel="nofollow">';
607     }
608
609     /**
610     Returns comment author link to his website if he specified one.
611
612     @param    rs   Invisible parameter
613     @return   <b>string</b>
614     */
615     public static function getAuthorURL($rs)
616     {
617          if (trim($rs->comment_site)) {
618               return trim($rs->comment_site);
619          }
620     }
621
622     /**
623     Returns comment post full URL.
624
625     @param    rs   Invisible parameter
626     @return   <b>string</b>
627     */
628     public static function getPostURL($rs)
629     {
630          return $rs->core->blog->url.$rs->core->getPostPublicURL(
631                    $rs->post_type,html::sanitizeURL($rs->post_url)
632               );
633     }
634
635     /**
636     Returns comment author name in a link to his website if he specified one.
637
638     @param    rs   Invisible parameter
639     @return   <b>string</b>
640     */
641     public static function getAuthorLink($rs)
642     {
643          $res = '%1$s';
644          $url = $rs->getAuthorURL();
645          if ($url) {
646               $res = '<a href="%2$s"%3$s>%1$s</a>';
647          }
648
649          $nofollow = '';
650          if ($rs->core->blog->settings->system->comments_nofollow) {
651               $nofollow = ' rel="nofollow"';
652          }
653
654          return sprintf($res,html::escapeHTML($rs->comment_author),html::escapeHTML($url),$nofollow);
655     }
656
657     /**
658     Returns comment author e-mail address. If <var>$encoded</var> is true,
659     "@" sign is replaced by "%40" and "." by "%2e".
660
661     @param    rs   Invisible parameter
662     @param    encoded   <b>boolean</b>      Encode address.
663     @return   <b>string</b>
664     */
665     public static function getEmail($rs,$encoded=true)
666     {
667          if ($encoded) {
668               return strtr($rs->comment_email,array('@'=>'%40','.'=>'%2e'));
669          }
670          return $rs->comment_email;
671     }
672
673     /**
674     Returns trackback site title if comment is a trackback.
675
676     @param    rs   Invisible parameter
677     @return   <b>string</b>
678     */
679     public static function getTrackbackTitle($rs)
680     {
681          if ($rs->comment_trackback == 1 &&
682          preg_match('|<p><strong>(.*?)</strong></p>|msU',$rs->comment_content,
683          $match)) {
684               return html::decodeEntities($match[1]);
685          }
686     }
687
688     /**
689     Returns trackback content if comment is a trackback.
690
691     @param    rs   Invisible parameter
692     @return   <b>string</b>
693     */
694     public static function getTrackbackContent($rs)
695     {
696          if ($rs->comment_trackback == 1) {
697               return preg_replace('|<p><strong>.*?</strong></p>|msU','',
698               $rs->comment_content);
699          }
700     }
701
702     /**
703     Returns comment feed unique ID.
704
705     @param    rs   Invisible parameter
706     @return   <b>string</b>
707     */
708     public static function getFeedID($rs)
709     {
710          return 'urn:md5:'.md5($rs->core->blog->uid.$rs->comment_id);
711
712          $url = parse_url($rs->core->blog->url);
713          $date_part = date('Y-m-d',strtotime($rs->comment_dt));
714
715          return 'tag:'.$url['host'].','.$date_part.':'.$rs->comment_id;
716     }
717
718     /**
719     Returns whether comment is from the post author.
720
721     @param    rs   Invisible parameter
722     @return   <b>boolean</b>
723     */
724     public static function isMe($rs)
725     {
726          return
727          $rs->comment_email && $rs->comment_site &&
728          $rs->comment_email == $rs->user_email &&
729          $rs->comment_site == $rs->user_url;
730     }
731}
732
733/**
734@ingroup DC_CORE
735@brief Dotclear dates record helpers.
736
737This class adds new methods to database dates results.
738You can call them on every record comming from dcBlog::getDates.
739
740@warning You should not give the first argument (usualy $rs) of every described
741function.
742*/
743class rsExtDates
744{
745     /**
746     @param    rs   Invisible parameter
747     @return   <b>integer</b>      Date timestamp
748     */
749     public static function ts($rs)
750     {
751          return strtotime($rs->dt);
752     }
753
754     /**
755     @param    rs   Invisible parameter
756     @return   <b>string</b>       Date year
757     */
758     public static function year($rs)
759     {
760          return date('Y',strtotime($rs->dt));
761     }
762
763     /**
764     @param    rs   Invisible parameter
765     @return   <b>string</b>       Date month
766     */
767     public static function month($rs)
768     {
769          return date('m',strtotime($rs->dt));
770     }
771
772     /**
773     @param    rs   Invisible parameter
774     @return   <b>integer</b>      Date day
775     */
776     public static function day($rs)
777     {
778          return date('d',strtotime($rs->dt));
779     }
780
781     /**
782     Returns date month archive full URL.
783
784     @param    rs   Invisible parameter
785     @param    core      <b>dcCore</b>       dcCore instance
786     @return   <b>integer</b>
787     */
788     public static function url($rs,$core)
789     {
790          $url = date('Y/m',strtotime($rs->dt));
791
792          return $core->blog->url.$core->url->getURLFor('archive',$url);
793     }
794
795     /**
796     Returns whether date is the first of year.
797
798     @param    rs   Invisible parameter
799     @return   <b>boolean</b>
800     */
801     public static function yearHeader($rs)
802     {
803          if ($rs->isStart()) {
804               return true;
805          }
806
807          $y = $rs->year();
808          $rs->movePrev();
809          $py = $rs->year();
810          $rs->moveNext();
811
812          return $y != $py;
813     }
814
815     /**
816     Returns whether date is the last of year.
817
818     @param    rs   Invisible parameter
819     @return   <b>boolean</b>
820     */
821     public static function yearFooter($rs)
822     {
823          if ($rs->isEnd()) {
824               return true;
825          }
826
827          $y = $rs->year();
828          if ($rs->moveNext()) {
829               $ny = $rs->year();
830               $rs->movePrev();
831               return $y != $ny;
832          }
833          return false;
834
835     }
836}
837
838/**
839@ingroup DC_CORE
840@brief Dotclear dates record helpers.
841
842This class adds new methods to database dates results.
843You can call them on every record comming from dcAuth::checkUser and
844dcCore::getUsers.
845
846@warning You should not give the first argument (usualy $rs) of every described
847function.
848*/
849class rsExtUser
850{
851     private static $sortfield;
852     private static $sortsign;
853     /**
854     Returns a user option.
855
856     @param    rs   Invisible parameter
857     @param    name      <b>string</b>       Option name
858     @return   <b>string</b>
859     */
860     public static function option($rs,$name)
861     {
862          $options = self::options($rs);
863
864          if (isset($options[$name])) {
865               return $options[$name];
866          }
867          return null;
868     }
869
870     /**
871     Returns all user options.
872
873     @param    rs   Invisible parameter
874     @return   <b>array</b>
875     */
876     public static function options($rs)
877     {
878          $options = @unserialize($rs->user_options);
879          if (is_array($options)) {
880               return $options;
881          }
882          return array();
883     }
884
885     /**
886     Converts this record to a {@link extStaticRecord} instance.
887
888     @param    rs   Invisible parameter
889      */
890     public static function toExtStatic($rs)
891     {
892          if ($rs instanceof extStaticRecord) {
893               return $rs;
894          }
895          return new extStaticRecord($rs);
896     }
897}
898
899class rsExtBlog
900{
901     private static $sortfield;
902     private static $sortsign;
903     /**
904     Converts this record to a {@link extStaticRecord} instance.
905
906     @param    rs   Invisible parameter
907      */
908     public static function toExtStatic($rs)
909     {
910          if ($rs instanceof extStaticRecord) {
911               return $rs;
912          }
913          return new extStaticRecord($rs);
914     }
915}
916
917class extStaticRecord extends staticRecord
918{
919     private $sortfield;
920     private $sortsign;
921
922     public function __construct($rs)
923     {
924          parent::__construct($rs->__data,$rs->__info);
925     }
926
927     /**
928     Lexically sort.
929
930     @param    field     <b>string<b>   sort field
931     @param  order  <b>string<b>   sort order
932      */
933     public function lexicalSort($field,$order='asc')
934     {
935          $this->sortfield = $field;
936          $this->sortsign = strtolower($order) == 'asc' ? 1 : -1;
937
938          usort($this->__data,array($this,'lexicalSortCallback'));
939
940          $this->sortfield = null;
941          $this->sortsign = null;
942     }
943     private function lexicalSortCallback($a,$b)
944     {
945          $a = $a[$this->sortfield];
946          $b = $b[$this->sortfield];
947
948          # Integer values
949          if ($a == (string) (integer) $a && $b == (string) (integer) $b) {
950               $a = (integer) $a;
951               $b = (integer) $b;
952               return ($a - $b) * $this->sortsign;
953          }
954
955          return strcoll(strtolower(dcUtils::removeDiacritics($a)),strtolower(dcUtils::removeDiacritics($b))) * $this->sortsign;
956     }
957}
Note: See TracBrowser for help on using the repository browser.

Sites map