Dotclear

source: plugins/dayMode/class.dc.calendar.php @ 952

Revision 952, 5.4 KB checked in by pep, 15 years ago (diff)

dayMode : Filtre sur le post_type

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of dayMode, a plugin for Dotclear 2.
5#
6# Copyright (c) 2006-2009 Pep and contributors
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK ------------------------------------
12
13class dcCalendar
14{
15     const     SUNDAY_TS = 1042329600;
16
17     protected $base      = null;
18     protected $dts       = null;
19     protected $post_type = 'post';
20
21     public $weekstart = 0;
22
23     public function __construct($core, $_ctx, $post_type = 'post')
24     {
25          $this->core      = $core;
26          $this->blog      = $core->blog;
27          $this->con       = $core->blog->con;
28          $this->post_type = $post_type;
29
30          $year = $month = '';
31          $this->cday = 0;
32          if ($_ctx->exists('day')) {
33               $month = $_ctx->day->month();
34               $year  = $_ctx->day->year();
35               $this->cday = (integer)$_ctx->day->day();
36          }
37          elseif ($_ctx->exists('archives')) {
38               $month = $_ctx->archives->month();
39               $year  = $_ctx->archives->year();
40          }
41          else {
42               $recent = dcDayTools::getEarlierDate(array('post_type' => $this->post_type));
43               $month = $recent->month();
44               $year  = $recent->year();
45          }
46
47          $month_dates = $this->blog->getDates(
48               array(
49                    'month'     => $month,
50                    'year'      => $year,
51                    'post_type' => $this->post_type
52               )
53          );
54
55          $this->dts = array();
56          while ($month_dates->fetch()) {
57               $this->dts[] = $month_dates->dt;
58          }
59
60          $this->base = array(
61               'dt'    => date('Y-m-01 00:00:00',strtotime($month_dates->dt)),
62               'url'   => $month_dates->url($this->core),
63               'month' => $month,
64               'year'  => $year
65          );
66
67          $this->base['ts'] = strtotime($this->base['dt']);
68     }
69
70
71     public function draw()
72     {
73          $link_next = $link_prev = '';
74
75          $l_next = $this->blog->getDates(
76               array(
77                    'next'      => $this->base['dt'],
78                    'type'      => 'month',
79                    'post_type' => $this->post_type
80               )
81          );
82          if (!$l_next->isEmpty()) {
83               $link_next =
84               ' <a href="'.$l_next->url($this->core).'" title="'.
85               dt::str('%B %Y', $l_next->ts()).'">&#187;</a>';
86          }
87
88          $l_prev = $this->blog->getDates(
89               array(
90                    'previous' => $this->base['dt'],
91                    'type' => 'month',
92                    'post_type' => $this->post_type
93               )
94          );
95          if (!$l_prev->isEmpty()) {
96               $link_prev = '<a href="'.$l_prev->url($this->core).'" title="'.
97               dt::str('%B %Y', $l_prev->ts()).'">&#171;</a> ';
98          }
99
100          $res =
101               '<table summary="'.__('Calendar').'">'.
102               '<caption>'.
103               $link_prev.
104               dt::str('%B %Y',$this->base['ts']).
105               $link_next.
106               '</caption>';
107
108          $first_ts = self::SUNDAY_TS + ((integer)$this->weekstart * 86400);
109          $last_ts = $first_ts + (6 * 86400);
110          $first = date('w',$this->base['ts']);
111          $first = ($first == 0)?7:$first;
112          $first = $first - $this->weekstart;
113          $limit = date('t',$this->base['ts']);
114
115          $res .= '<thead><tr>';
116          for ($j = $first_ts; $j <= $last_ts; $j = $j+86400) {
117               $res .=
118                    '<th scope="col"><abbr title="'.dt::str('%A',$j).'">'.
119                    dt::str('%a',$j).'</abbr></th>';
120          }
121
122          $res .= '</tr></thead><tbody>';
123          $d = 1; $i = 0; $dstart = false;
124          $y = $this->base['year'];
125          $m = $this->base['month'];
126
127          while ($i < 42) {
128               if ($i%7 == 0) {
129                    $res .= '<tr>';
130               }
131               if ($i == $first) {
132                    $dstart = true;
133               }
134               if ($dstart && !checkdate($m,$d,$y)) {
135                    $dstart = false;
136               }
137               if (in_array(sprintf('%4d-%02d-%02d 00:00:00',$y,$m,$d),$this->dts)) {
138                    $url = $this->base['url'].'/'.sprintf('%02d',$d);
139                    $link = '<a href="'.$url.'">%s</a>';
140               } else {
141                    $link = '%s';
142               }
143
144               $class = ($this->cday == $d && $dstart)?' class="active"':'';
145
146               $res .= '<td'.$class.'>';
147               $res .= ($dstart)? sprintf($link,$d):' ';
148               $res .= '</td>';
149
150               if (($i+1)%7 == 0) {
151                    $res .= '</tr>';
152                    if ($d>=$limit) { $i = 42; }
153               }
154               $i++;
155               if ($dstart) { $d++; }
156          }
157
158          $res .= '</tbody></table>';
159
160          return $res;
161     }
162}
163
164class dcDayTools
165{
166     public static function getEarlierDate($params = array())
167     {
168          global $core;
169
170          $cat_field = $catReq = $limit = '';
171          if (isset($params['ts_type']) && $params['ts_type'] == 'day') {
172               $dt_f = '%Y-%m-%d 00:00:00';
173          } else {
174               $dt_f = '%Y-%m-%d %H:%M:%S';
175          }
176
177          if (!empty($params['cat_id'])) {
178               $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' ';
179               $cat_field = ', C.cat_url ';
180          }
181          elseif (!empty($params['cat_url'])) {
182               $catReq = "AND C.cat_url = '".$core->blog->con->escape($params['cat_url'])."' ";
183               $cat_field = ', C.cat_url ';
184          }
185
186          $strReq = 'SELECT DISTINCT('.$core->blog->con->dateFormat('MAX(post_dt)',$dt_f).') AS dt '.
187                    'FROM '.$core->blog->prefix.'post P LEFT JOIN '.
188                    $core->blog->prefix.'category C '.
189                    'ON P.cat_id = C.cat_id '.
190                    "WHERE P.blog_id = '".$core->blog->con->escape($core->blog->id)."' ".
191                    $catReq;
192
193          if (!$core->auth->check('contentadmin',$core->blog->id)) {
194               $strReq .= 'AND ((post_status = 1 ';
195
196               if ($core->blog->without_password) {
197                    $strReq .= 'AND post_password IS NULL ';
198               }
199               $strReq .= ') ';
200
201               if ($core->auth->userID()) {
202                    $strReq .= "OR P.user_id = '".$core->blog->con->escape($core->auth->userID())."')";
203               }
204               else {
205                    $strReq .= ') ';
206               }
207          }
208
209          if (!empty($params['post_type'])) {
210               $strReq .= "AND post_type = '".$core->blog->con->escape($params['post_type'])."' ";
211          }
212                   
213          if (!empty($params['cat_id'])) {
214               $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' ';
215          }
216
217          if (!empty($params['cat_url'])) {
218               $strReq .= "AND C.cat_url = '".$core->blog->con->escape($params['cat_url'])."' ";
219          }
220
221          $rs = $core->blog->con->select($strReq);
222          $rs->extend('rsExtDates');
223          return $rs;
224     }
225}
226?>
Note: See TracBrowser for help on using the repository browser.

Sites map