This repository was archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_widget.php
More file actions
286 lines (250 loc) · 8.74 KB
/
_widget.php
File metadata and controls
286 lines (250 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
# ***** BEGIN LICENSE BLOCK *****
#
# This file is part of CountDown, a plugin for Dotclear 2
# Copyright 2007,2010 Moe (http://gniark.net/)
#
# CountDown is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v2.0
# as published by the Free Software Foundation.
#
# CountDown is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
# ***** END LICENSE BLOCK *****
if (!defined('DC_RC_PATH')) {return;}
dcCore::app()->addBehavior('initWidgets',
array('CountDownBehaviors','initWidgets'));
class CountDownBehaviors
{
public static function initWidgets($w)
{
# set timezone
dcCore::app();
$tz = dcCore::app()->blog->settings->system->blog_timezone;
$w->create('CountDown',__('Countdown'),
array('CountDownBehaviors','Show'),
null,
__('A countdown to a future date or stopwatch to a past date'));
$w->CountDown->setting('title',__('Title:'),__('CountDown'),'text');
$w->CountDown->setting('text_before',
__('Text displayed if the date is in the future:'),__('In'),'text');
$w->CountDown->setting('text_after',
__('Text displayed if the date is in the past:'),__('For'),'text');
# create arrays for year, month, day, hour, minute and second
$array_year = $array_month = $array_day = $array_hour = array();
$array_minute = $array_number_of_times = array();
for ($i = 1902;$i <= 2037;$i++)
{
$array_year[$i] = $i;
}
for ($i = 1;$i <= 12;$i++)
{
$i = str_repeat('0',(2-strlen($i))).$i;
$array_month[ucfirst(__(strftime('%B', mktime(0, 0, 0, $i, 1, 1970)))).' ('.$i.')'] = $i;
}
for ($i = 1;$i <= 31;$i++)
{
$i = str_repeat('0',(2-strlen($i))).$i;
$array_day[$i] = $i;
}
for ($i = 0;$i <= 23;$i++)
{
$i = str_repeat('0',(2-strlen($i))).$i;
$array_hour[$i] = $i;
}
for ($i = 0;$i <= 60;$i++)
{
$i = str_repeat('0',(2-strlen($i))).$i;
$array_minute[$i] = $i;
}
for ($i = 1;$i <= 5;$i++)
{
$array_number_of_times[$i] = $i;
}
$array_number_of_times['6 ('.__('all').')'] = 6;
# /create arrays
$w->CountDown->setting('year',ucfirst(__('year')).':',
dt::str('%Y',null,$tz),'combo',$array_year);
$w->CountDown->setting('month',ucfirst(__('month')).':',
dt::str('%m',null,$tz),'combo',$array_month);
$w->CountDown->setting('day',ucfirst(__('day')).':',
dt::str('%d',null,$tz),'combo',$array_day);
$w->CountDown->setting('hour',ucfirst(__('hour')).':',
dt::str('%H',null,$tz),'combo',$array_hour);
$w->CountDown->setting('minute',ucfirst(__('minute')).':',
dt::str('%M',null,$tz),'combo',$array_minute);
$w->CountDown->setting('second',ucfirst(__('second')).':',
dt::str('%S',null,$tz),'combo',$array_minute);
$w->CountDown->setting('number_of_times',
__('Number of values to be displayed:'),'6','combo',
$array_number_of_times);
$w->CountDown->setting('zeros',
__('Show zeros before hours, minutes and seconds'),false,'check');
$w->CountDown->setting('dynamic',
__('Enable dynamic display'),false,'check');
$w->CountDown->setting('dynamic_format',
sprintf(__('Dynamic display format (see <a href="%1$s" %2$s>jQuery Countdown Reference</a>):'),
'http://keith-wood.name/countdownRef.html#format',
'onclick="return window.confirm(\''.
__('Are you sure you want to leave this page?').'\')"'),
__('yowdHMS'),'text');
$w->CountDown->setting('dynamic_layout_before',
sprintf(__('Dynamic display layout if the date is in the future (see <a href="%1$s" %2$s>jQuery Countdown Reference</a>):'),
'http://keith-wood.name/countdownRef.html#layout',
'onclick="return window.confirm(\''.
__('Are you sure you want to leave this page?').'\')"'),
__('In {y<}{yn} {yl}, {y>} {o<}{on} {ol}, {o>} {w<}{wn} {wl}, {w>} {d<}{dn} {dl}, {d>} {hn} {hl}, {mn} {ml} and {sn} {sl}'),
'textarea');
$w->CountDown->setting('dynamic_layout_after',
sprintf(__('Dynamic display layout if the date is in the past (see <a href="%1$s" %2$s>jQuery Countdown Reference</a>):'),
'http://keith-wood.name/countdownRef.html#layout',
'onclick="return window.confirm(\''.
__('Are you sure you want to leave this page?').'\')"'),
__('For {y<}{yn} {yl}, {y>} {o<}{on} {ol}, {o>} {w<}{wn} {wl}, {w>} {d<}{dn} {dl}, {d>} {hn} {hl}, {mn} {ml} and {sn} {sl}'),
'textarea');
$w->CountDown->setting('homeonly',__('Display on:'),0,'combo',
array(
__('All pages') => 0,
__('Home page only') => 1,
__('Except on home page') => 2
)
);
$w->CountDown->setting('content_only',__('Content only'),0,'check');
$w->CountDown->setting('class',__('CSS class:'),'');
$w->CountDown->setting('offline',__('Offline'),0,'check');
}
# escape quotes but not XHTML tags
# inspired by html::escapeJS()
public static function escapeQuotes($str)
{
$str = str_replace("'","\'",$str);
$str = str_replace('"','\"',$str);
return $str;
}
public static function Show($w)
{
# set timezone
dcCore::app();
if ($w->offline)
return;
if (($w->homeonly == 1 && dcCore::app()->url->type != 'default') ||
($w->homeonly == 2 && dcCore::app()->url->type == 'default')) {
return;
}
# get local time
$local_time = dt::addTimeZone(dcCore::app()->blog->settings->system->blog_timezone);
$ts = mktime($w->hour,$w->minute,$w->second,$w->month,$w->day,
$w->year);
# get difference
(int)$diff = ($local_time - $ts);
$after = ($diff > 0) ? true : false;
$diff = abs($diff);
$times = array();
$intervals = array
(
(3600*24*365.24) => array('one'=>__('year'),'more'=>__('years'),
'zeros'=>false),
(3600*24*30.4) => array('one'=>__('month'),'more'=>__('months'),
'zeros'=>false),
(3600*24) => array('one'=>__('day'),'more'=>__('days'),
'zeros'=>false),
(3600) => array('one'=>__('hour'),'more'=>__('hours'),
'zeros'=>true),
(60) => array('one'=>__('minute'),'more'=>__('minutes'),
'zeros'=>true),
(1) => array('one'=>__('second'),'more'=>__('seconds'),
'zeros'=>true),
);
foreach ($intervals as $k => $v)
{
if ($diff >= $k)
{
$time = floor($diff/$k);
$times[] = (($w->zeros AND $v['zeros'])
? sprintf('%02d',$time) : $time).' '.(($time <= 1) ? $v['one']
: $v['more']);
$diff = $diff%$k;
}
}
# output
$text = ($after) ? $w->text_after : $w->text_before;
if (strlen($text) > 0) {$text .= ' ';}
# get times and make a string
$times = array_slice($times,0,$w->number_of_times);
if (count($times) > 1)
{
$last = array_pop($times);
$str = implode(', ',$times).' '.__('and').' '.$last;
}
else {$str = implode('',$times);}
if (!$w->dynamic)
{
$res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').
'<p>'.$text.'<span>'.$str.'</span></p>';
return $w->renderDiv($w->content_only,'countdown '.$w->class,'',$res);
}
else
{
# dynamic display with Countdown for jQuery
if (!is_numeric(dcCore::app()->ctx->countdown))
{
dcCore::app()->ctx->countdown = 0;
}
$id = dcCore::app()->ctx->countdown;
dcCore::app()->ctx->countdown += 1;
$script = '';
if (!defined('COUNTDOWN_SCRIPT'))
{
$script =
'<script type="text/javascript" src="'.
dcCore::app()->blog->getQmarkURL().
'pf=countdown/js/jquery.countdown.min.js"></script>'."\n";
$l10n_file =
'jquery.countdown-'.dcCore::app()->blog->settings->system->lang.'.js';
if (file_exists(dirname(__FILE__).'/js/'.$l10n_file))
{
$script .=
'<script type="text/javascript" src="'.dcCore::app()->blog->getQmarkURL().
'pf=countdown/js/'.$l10n_file.'"></script>'."\n";
}
define('COUNTDOWN_SCRIPT',(bool)true);
}
if ($after)
{
$to = 'since';
$layout = $w->dynamic_layout_after;
}
else
{
$to = 'until';
$layout = $w->dynamic_layout_before;
}
$res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').
'<p id="countdown-'.$id.'">'.$text.$str.'</p>'.
'<script type="text/javascript">'."\n".
'//<![CDATA['."\n".
'$().ready(function() {'.
"$('#countdown-".$id."').countdown({".
# In Javascript, 0 = January, 11 = December
$to.": new Date(".(int)$w->year.",".(int)$w->month."-1,".
(int)$w->day.",".(int)$w->hour.",".(int)$w->minute.",".
(int)$w->second."),
description: '".html::escapeJS($text)."',
format: '".$w->dynamic_format."',
layout: '".$layout."',
expiryText: '".html::escapeJS($w->text_after)."'
});".
'});'."\n".
'//]]>'.
'</script>'."\n";
return $w->renderDiv($w->content_only,'countdown '.$w->class,'',$res);
}
}
}