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