136
|
1 |
<?php |
|
2 |
/* |
|
3 |
Copyright (c) 2005, Alex Tingle. $Revision: 287 $ |
|
4 |
|
|
5 |
This program is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU General Public License |
|
7 |
as published by the Free Software Foundation; either version 2 |
|
8 |
of the License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU General Public License |
|
16 |
along with this program; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 |
*/ |
|
19 |
|
|
20 |
/** Calendar class. Encapsulates all functionality concerning the calendar - |
|
21 |
* how many days in each month, leap years, days of the week, locale |
|
22 |
* names etc. */ |
|
23 |
class ec3_Date |
|
24 |
{ |
|
25 |
var $year_num =0; |
|
26 |
var $month_num=0; |
|
27 |
var $day_num =1; |
|
28 |
var $_unixdate =0; |
|
29 |
var $_days_in_month =0; |
|
30 |
|
|
31 |
function ec3_Date($year_num=0,$month_num=0,$day_num=0) |
|
32 |
{ |
|
33 |
global $ec3; |
|
34 |
if($year_num>0) |
|
35 |
{ |
|
36 |
$this->year_num =$year_num; |
|
37 |
$this->month_num=$month_num; |
|
38 |
$this->day_num =$day_num; |
|
39 |
} |
|
40 |
if(0==$this->year_num && is_single()) |
|
41 |
$this->from_single(); |
|
42 |
if(0==$this->year_num && $ec3->is_date_range) |
|
43 |
$this->from_date_range(); |
|
44 |
if(0==$this->year_num) |
|
45 |
$this->from_date(); // Falls back to today. |
|
46 |
} |
|
47 |
|
|
48 |
function from_single() |
|
49 |
{ |
|
50 |
global $wp_query; |
|
51 |
if($wp_query->posts && $wp_query->posts[0]->ec3_schedule) |
|
52 |
{ |
|
53 |
$this->year_num= |
|
54 |
intval(mysql2date('Y',$wp_query->posts[0]->ec3_schedule[0]->start)); |
|
55 |
$this->month_num= |
|
56 |
intval(mysql2date('m',$wp_query->posts[0]->ec3_schedule[0]->start)); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
function from_date_range() |
|
61 |
{ |
|
62 |
global $wp_query; |
|
63 |
if('' != $wp_query->query_vars['ec3_from']) |
|
64 |
{ |
|
65 |
$c=explode('_',$wp_query->query_vars['ec3_from']); |
|
66 |
$this->year_num=intval($c[0]); |
|
67 |
$this->month_num=intval($c[1]); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
/** Utility function. Calculates the value of month/year for the current |
|
72 |
* page. Code block from wp-includes/template-functions-general.php |
|
73 |
* (get_calendar function). */ |
|
74 |
function from_date() |
|
75 |
{ |
|
76 |
global |
|
77 |
$m, |
|
78 |
$monthnum, |
|
79 |
$wpdb, |
|
80 |
$year; |
|
81 |
|
|
82 |
if (isset($_GET['w'])) { |
|
83 |
$w = ''.intval($_GET['w']); |
|
84 |
} |
|
85 |
|
|
86 |
// Let's figure out when we are |
|
87 |
if (!empty($monthnum) && !empty($year)) { |
|
88 |
$thismonth = ''.zeroise(intval($monthnum), 2); |
|
89 |
$thisyear = ''.intval($year); |
|
90 |
} elseif (!empty($w)) { |
|
91 |
// We need to get the month from MySQL |
|
92 |
$thisyear = ''.intval(substr($m, 0, 4)); |
|
93 |
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's |
|
94 |
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')"); |
|
95 |
} elseif (!empty($m)) { |
|
96 |
// $calendar = substr($m, 0, 6); |
|
97 |
$thisyear = ''.intval(substr($m, 0, 4)); |
|
98 |
if (strlen($m) < 6) { |
|
99 |
$thismonth = '01'; |
|
100 |
} else { |
|
101 |
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); |
|
102 |
} |
|
103 |
} else { |
|
104 |
$thisyear=ec3_strftime("%Y"); |
|
105 |
$thismonth=ec3_strftime("%m"); |
|
106 |
} |
|
107 |
|
|
108 |
$this->year_num =intval($thisyear); |
|
109 |
$this->month_num=intval($thismonth); |
|
110 |
$this->day_num =1; |
|
111 |
} |
|
112 |
|
|
113 |
/** Month arithmetic. Returns a new date object. */ |
|
114 |
function plus_months($month_count) |
|
115 |
{ |
|
116 |
$result=new ec3_Date($this->year_num,$this->month_num,$this->day_num); |
|
117 |
$result->month_num += $month_count; |
|
118 |
if($month_count>0) |
|
119 |
{ |
|
120 |
while($result->month_num>12) |
|
121 |
{ |
|
122 |
$result->month_num -= 12; |
|
123 |
$result->year_num++; |
|
124 |
} |
|
125 |
} |
|
126 |
else |
|
127 |
{ |
|
128 |
while($result->month_num<1) |
|
129 |
{ |
|
130 |
$result->month_num += 12; |
|
131 |
$result->year_num--; |
|
132 |
} |
|
133 |
} |
|
134 |
return $result; |
|
135 |
} |
|
136 |
/** Convenience function for accessing plus_months(). */ |
|
137 |
function prev_month() { return $this->plus_months(-1); } |
|
138 |
function next_month() { return $this->plus_months( 1); } |
|
139 |
|
|
140 |
/** Modifies the current object to be one day in the future. */ |
|
141 |
function increment_day() |
|
142 |
{ |
|
143 |
$this->day_num++; |
|
144 |
if($this->day_num > $this->days_in_month()) |
|
145 |
{ |
|
146 |
$this->day_num=1; |
|
147 |
$this->month_num++; |
|
148 |
if($this->month_num>12) |
|
149 |
{ |
|
150 |
$this->month_num=1; |
|
151 |
$this->year_num++; |
|
152 |
} |
|
153 |
$this->_days_in_month=0; |
|
154 |
} |
|
155 |
$this->_unixdate=0; |
|
156 |
} |
|
157 |
|
|
158 |
function month_id() // e.g. ec3_2005_06 |
|
159 |
{ |
|
160 |
return 'ec3_' . $this->year_num . '_' . $this->month_num; |
|
161 |
} |
|
162 |
function day_id() // e.g. ec3_2005_06_25 |
|
163 |
{ |
|
164 |
$result='ec3_'.$this->year_num.'_'.$this->month_num.'_'.$this->day_num; |
|
165 |
global $ec3_today_id; |
|
166 |
if($result==$ec3_today_id) |
|
167 |
return 'today'; |
|
168 |
else |
|
169 |
return $result; |
|
170 |
} |
|
171 |
function day_link() |
|
172 |
{ |
|
173 |
global $ec3; |
|
174 |
if($ec3->show_only_events) |
|
175 |
{ |
|
176 |
return get_option('home') . '/?m=' |
|
177 |
. $this->year_num |
|
178 |
. zeroise($this->month_num, 2) |
|
179 |
. zeroise($this->day_num, 2) |
|
180 |
. "&cat=" . $ec3->event_category; |
|
181 |
} |
|
182 |
else |
|
183 |
return get_day_link($this->year_num,$this->month_num,$this->day_num); |
|
184 |
} |
|
185 |
function month_name() // e.g. June |
|
186 |
{ |
|
187 |
global $month; |
|
188 |
return $month[zeroise($this->month_num,2)]; |
|
189 |
} |
|
190 |
function month_abbrev() // e.g. Jun |
|
191 |
{ |
|
192 |
global $month_abbrev; |
|
193 |
return $month_abbrev[ $this->month_name() ]; |
|
194 |
} |
|
195 |
function month_link() |
|
196 |
{ |
|
197 |
global $ec3; |
|
198 |
if($ec3->show_only_events) |
|
199 |
{ |
|
200 |
return get_option('home') . '/?m=' |
|
201 |
. $this->year_num |
|
202 |
. zeroise($this->month_num, 2) |
|
203 |
. "&cat=" . $ec3->event_category; |
|
204 |
} |
|
205 |
else |
|
206 |
return get_month_link($this->year_num,$this->month_num); |
|
207 |
} |
|
208 |
function days_in_month() |
|
209 |
{ |
|
210 |
if(0==$this->_days_in_month) |
|
211 |
$this->_days_in_month=intval(date('t', $this->to_unixdate())); |
|
212 |
return $this->_days_in_month; |
|
213 |
} |
|
214 |
function week_day() |
|
215 |
{ |
|
216 |
return intval(date('w', $this->to_unixdate())); |
|
217 |
} |
|
218 |
function to_unixdate() |
|
219 |
{ |
|
220 |
if(0==intval($this->_unixdate)) |
|
221 |
{ |
|
222 |
$this->_unixdate = |
|
223 |
mktime(0,0,0, $this->month_num,$this->day_num,$this->year_num); |
|
224 |
} |
|
225 |
return $this->_unixdate; |
|
226 |
} |
|
227 |
} // end class ec3_Date |
|
228 |
|
|
229 |
|
|
230 |
/** Converts a MySQL date object into an EC3 date object. */ |
|
231 |
function ec3_mysql2date(&$mysqldate) |
|
232 |
{ |
|
233 |
$as_str=mysql2date('Y,n,j',$mysqldate); |
|
234 |
$as_arr=explode(',',$as_str); |
|
235 |
return new ec3_Date($as_arr[0],$as_arr[1],$as_arr[2]); |
|
236 |
} |
|
237 |
|
|
238 |
/** Converts a day or month Id to a PHP date. */ |
|
239 |
function ec3_dayid2php(&$id) |
|
240 |
{ |
|
241 |
$parts=explode('_',$id); |
|
242 |
$year =intval($parts[1]); |
|
243 |
$month=intval($parts[2]); |
|
244 |
$day =(count($parts)>=4? intval($parts[3]): 1); |
|
245 |
return mktime( 0,0,0, $month,$day,$year); |
|
246 |
} |
|
247 |
|
|
248 |
?> |