/[drupal]/contributions/modules/worldclock/worldclock.module
ViewVC logotype

Contents of /contributions/modules/worldclock/worldclock.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Apr 19 01:39:19 2008 UTC (19 months, 1 week ago) by pixture
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-5--1-1, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id:
3
4 /*
5 * World Clock module - displays a world clock block
6 *
7 * This module is for Drupal 5.x only.
8 *
9 * Module by: Hideki Ito (PIXTURE STUDIO)
10 * Mailto: hide@pixture.com
11 * Home Page: http://www.pixture.com/
12 *
13 */
14
15 define("INDEX_NAME", 0);
16 define("INDEX_GMT", 1);
17 define("INDEX_DST", 2);
18 define("INDEX_SMON", 3);
19 define("INDEX_SNTH", 4);
20 define("INDEX_SDOW", 5);
21 define("INDEX_STIME", 6);
22 define("INDEX_EMON", 7);
23 define("INDEX_ENTH", 8);
24 define("INDEX_EDOW", 9);
25 define("INDEX_ETIME", 10);
26
27 /*
28 * Register location array using variable_set().
29 * This function is called from worldclock_init().
30 */
31 function register_location_array()
32 {
33 // Note: wc_location array
34 //
35 // Name Country or area name
36 // GMT Diff GMT time difference -12 to +12 (can be decimal numbers)
37 // DST Y/N 0=No DST, 1=Has DST
38 // DST Start Describes when the DST starts
39 // Mon 1 to 12 (0 is used for no DST countries)
40 // Nth 1=1st, 2=2nd, 3=3rd, 9=Last
41 // DOW Day of the week (0=Sun, 1=Mon, 2=Tue... 6=Sat)
42 // Time 0=0:00AM, 1=1:00AM, 2=2:00AM.....
43 // DST End Describes when the DST ends
44 // Mon 1 to 12 (0 is used for no DST countries)
45 // Nth 1=1st, 2=2nd, 3=3rd, 9=Last
46 // DOW Day of the week (0=Sun, 1=Mon, 2=Tue... 6=Sat)
47 // Time 0=0:00AM, 1=1:00AM, 2=2:00AM.....
48 //
49
50 $wc_location = array(
51 // Name GMT DST ----- Start ------ ------ End -------
52 // Diff Y/N Mon Nth DOW Time Mon Nth DOW Time
53 array( t('-Not Selected-'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
54 array( t('Afghanistan'), 4.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
55 array( t('Argentina'), -3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
56 array( t('Australia EST'), 10, 1, 10, 9, 0, 2, 3, 9, 0, 3 ),
57 array( t('Australia CST'), 9.5, 1, 10, 9, 0, 2, 3, 9, 0, 3 ),
58 array( t('Australia WST'), 8, 1, 10, 9, 0, 2, 3, 9, 0, 3 ),
59 array( t('Austria'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
60 array( t('Bangladesh'), 6, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
61 array( t('Belgium'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
62 array( t('Bhutan'), 6, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
63 array( t('Bolivia'), -4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
64 array( t('Brazil'), -3, 1, 10, 3, 0, 0, 2, 3, 0, 0 ),
65 array( t('Bulgaria'), 2, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
66 array( t('Cambodia'), 7, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
67 array( t('Cameroon'), 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
68 array( t('Canada AB'), -7, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
69 array( t('Canada BC'), -8, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
70 array( t('Canada MB'), -6, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
71 array( t('Canada NB'), -4, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
72 array( t('Canada NF'), -3.5, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
73 array( t('Canada NT'), -7, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
74 array( t('Canada NS'), -4, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
75 array( t('Canada NU(M)'), -7, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
76 array( t('Canada NU(C)'), -6, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
77 array( t('Canada NU(E)'), -5, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
78 array( t('Canada ON(E)'), -5, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
79 array( t('Canada ON(W)'), -6, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
80 array( t('Canada PE'), -4, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
81 array( t('Canada QC'), -5, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
82 array( t('Canada QC(E)'), -4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
83 array( t('Canada SK'), -7, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
84 array( t('Canada SK(L)'), -6, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
85 array( t('Canada YK'), -8, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
86 array( t('Chile'), -4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
87 array( t('China'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
88 array( t('Colombia'), -5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
89 array( t('Costa Rica'), -6, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
90 array( t('Croatia'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
91 array( t('Czech Republic'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
92 array( t('Denmark'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
93 array( t('Ecuador'), -5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
94 array( t('Egypt'), 2, 1, 4, 9, 5, 0, 9, 9, 4, 0 ),
95 array( t('Ethiopia'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
96 array( t('Fiji'), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
97 array( t('Finland'), 2, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
98 array( t('France'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
99 array( t('Germany'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
100 array( t('Ghana'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
101 array( t('Greece'), 2, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
102 array( t('Hungary'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
103 array( t('Iceland'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
104 array( t('India'), 5.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
105 array( t('Iran'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
106 array( t('Israel'), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
107 array( t('Italy'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
108 array( t('Japan'), 9, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
109 array( t('Jordan'), 2, 1, 3, 9, 4, 0, 10, 9, 5, 1 ),
110 array( t('Kenya'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
111 array( t('Korea'), 9, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
112 array( t('Laos'), 7, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
113 array( t('Libya'), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
114 array( t('Madagascar'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
115 array( t('Malaysia'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
116 array( t('Mauritius'), 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
117 array( t('Monaco'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
118 array( t('Mongolia'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
119 array( t('Morocco'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
120 array( t('Myanmar'), 6.3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
121 array( t('Nepal'), 5.45, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
122 array( t('Netherland'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
123 array( t('New Caledonia'), 11, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
124 array( t('New Zealand'), 12, 1, 9, 9, 0, 2, 4, 1, 0, 3 ),
125 array( t('Nigeria'), 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
126 array( t('Norway'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
127 array( t('Oman'), 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
128 array( t('Pakistan'), 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
129 array( t('Panama'), -5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
130 array( t('Peru'), -5, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
131 array( t('Philippines'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
132 array( t('Porland'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
133 array( t('Portugal'), 0, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
134 array( t('Romania'), 2, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
135 array( t('Rwanda'), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
136 array( t('Russia Moscow'), 3, 1, 3, 9, 0, 2, 10, 9, 0, 3 ),
137 array( t('Saudi Arabia'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
138 array( t('Senegal'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
139 array( t('Seychells'), 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
140 array( t('Singapore'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
141 array( t('South Africa'), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
142 array( t('Spain'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
143 array( t('Sudan'), 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
144 array( t('Sweden'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
145 array( t('Switherland'), 1, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
146 array( t('Taiwan'), 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
147 array( t('Thailand'), 7, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
148 array( t('Tonga'), 13, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
149 array( t('Turkey'), 2, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
150 array( t('UK'), 0, 1, 3, 9, 0, 1, 10, 9, 0, 1 ),
151 array( t('USA Alaska'), -9, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
152 array( t('USA Hawaii'), -10, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
153 array( t('USA PST'), -8, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
154 array( t('USA MST'), -7, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
155 array( t('USA CST'), -6, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
156 array( t('USA EST'), -5, 1, 3, 2, 0, 2, 11, 1, 0, 2 ),
157 array( t('Venezuela'), -4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
158 array( t('Vietnam'), 7, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
159 array( t('Zambia'), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),
160 );
161 variable_set('wc_location', $wc_location);
162 }
163
164 /*
165 * Construct an array for location pulldown menu
166 */
167 function get_location_options()
168 {
169 // add this array to $GLOBALS so that other functions can access it
170
171 $loc_array = array();
172 $wc_location = variable_get('wc_location', array());
173
174 $numitems = count($wc_location);
175 for($i = 0 ; $i < $numitems ; $i++) {
176 $loc = $wc_location[$i];
177 $loc_name = $loc[INDEX_NAME];
178 $loc_array[] = $loc_name;
179 }
180 return $loc_array;
181 }
182
183 /*
184 * Construct location information text string
185 */
186 function get_location_info($loc)
187 {
188 // in: $loc - location record array
189 $nth_array = array('', t('1st '), t('2nd '), t('3rd '), t('4th '),
190 '', '', '', '', t('last '));
191 $dow_array = array(t('Sunday'), t('Monday'), t('Tuesday'), t('Wednesday'),
192 t('Thursday'), t('Friday'), t('Saturday'));
193 $mon_array = array('', t('January'), t('February'), t('March'), t('April'),
194 t('May'), t('June'), t('July'), t('August'),
195 t('September'), t('October'), t('November'), t('December'));
196 $out = '';
197 if($loc[INDEX_GMT] >= 0) {
198 $out .= t('GMT +') . $loc[INDEX_GMT] . ', ';
199 }
200 else {
201 $out .= t('GMT ') . $loc[INDEX_GMT] . ', ';
202 }
203 if($loc[INDEX_DST] == 0) {
204 $out .= t('No DST ');
205 }
206 else {
207 $out .= t('DST ');
208 // now add DST start and end information
209 $out .= t(' from !stime:00AM on !snth !sdow in !smonth',
210 array('!stime' => $loc[INDEX_STIME],
211 '!snth' => $nth_array[$loc[INDEX_SNTH]],
212 '!sdow' => $dow_array[$loc[INDEX_SDOW]],
213 '!smonth' => $mon_array[$loc[INDEX_SMON]]));
214 $out .= t(' to !etime:00AM on !enth !edow in !emonth',
215 array('!etime' => $loc[INDEX_ETIME],
216 '!enth' => $nth_array[$loc[INDEX_ENTH]],
217 '!edow' => $dow_array[$loc[INDEX_EDOW]],
218 '!emonth' => $mon_array[$loc[INDEX_EMON]]));
219 }
220 return $out;
221 }
222
223 /*
224 * Help message for the module
225 *
226 * in: $section - which section of the site we are displaying help
227 * return: help text for the section
228 */
229 /*---- for Drupal 5.x ----*/
230 function worldclock_help($section='') {
231 $output = '';
232 switch($section) {
233 case "admin/help#worldclock":
234 $output = '<p>' . t("Displays a world clock") . '</p>';
235 break;
236 }
237 return $output;
238 }
239
240 /*---- for Drupal 6.x ----
241 function worldclock_help($path, $arg) {
242 $output = '';
243 switch($path) {
244 case "admin/help#worldclock":
245 $output = '<p>' . t("Displays a world clock") . '</p>';
246 break;
247 }
248 return $output;
249 }
250 */
251
252 /*
253 * Generate HTML for worldclock block
254 *
255 * in: $op is the operation from the URL
256 * in: $delta - offset
257 * return: block HTML code
258 */
259 function worldclock_block($op='list', $delta=0) {
260
261 if($op == 'list') {
262 $block[0]['info'] = t('World Clock');
263 return $block;
264 }
265 else if($op == 'view') {
266 $block['subject'] = t('World Clock');
267 $block['content'] = '';
268 $out = '';
269 $wc_location = variable_get('wc_location', array());
270
271 // get option
272 $maxnum = variable_get('worldclock_maxnum', 4);
273 $showsec = variable_get('worldclock_showsec', 0);
274 $showdst = variable_get('worldclock_showdst', 0);
275
276 // output table HTML
277 $out .= '<table id="wordclock">';
278 for($j = 1 ; $j <= $maxnum ; $j++) {
279 $loc_no = variable_get('worldclock_location_' . $j, 0);
280 if($loc_no > 0) {
281 // get location name
282 $loc = $wc_location[$loc_no];
283 $loc_name = $loc[INDEX_NAME];
284 $out .= '<tr>';
285 $out .= '<td id="worldclock_location">' . $loc_name . '</td>';
286 $out .= '<td id="worldclock_time">';
287 $out .= '<span id="worldclock' . $j . '"></span></td>';
288 $out .= '</tr>';
289 }
290 }
291 $out .= '</table>';
292
293 // output javascript
294 $out .= '<script language="javascript">';
295 $out .= 'var param = new Array();';
296 $out .= 'var maxnum;';
297 $out .= 'var showsec;';
298 $out .= 'var showdst;';
299 $out .= 'maxnum = ' . $maxnum . ';';
300 $out .= 'showsec = ' . $showsec . ';';
301 $out .= 'showdst = ' . $showdst . ';';
302
303 for($j = 1 ; $j <= $maxnum ; $j++) {
304 $loc_no = variable_get('worldclock_location_' . $j, 0);
305 // get the location record
306 $loc = $wc_location[$loc_no];
307 $loc_name = $loc[INDEX_NAME];
308 $loc_diff = $loc[INDEX_GMT];
309 $loc_dst = $loc[INDEX_DST];
310 $loc_smon = $loc[INDEX_SMON];
311 $loc_snth = $loc[INDEX_SNTH];
312 $loc_sdow = $loc[INDEX_SDOW];
313 $loc_stime = $loc[INDEX_STIME];
314 $loc_emon = $loc[INDEX_EMON];
315 $loc_enth = $loc[INDEX_ENTH];
316 $loc_edow = $loc[INDEX_EDOW];
317 $loc_etime = $loc[INDEX_ETIME];
318 $out .= "param[" . $j . "] = new Array('" .
319 $loc_no . "', '" .
320 $loc_name . "', '" .
321 $loc_diff . "', '" .
322 $loc_dst . "', '" .
323 $loc_smon . "', '" .
324 $loc_snth . "', '" .
325 $loc_sdow . "', '" .
326 $loc_stime . "', '" .
327 $loc_emon . "', '" .
328 $loc_enth . "', '" .
329 $loc_edow . "', '" .
330 $loc_etime . "');";
331 }
332 $out .= 'setTimeout("DisplayClock(maxnum, showsec, showdst, param)", 100);';
333 $out .= '</script>';
334
335 $block['content'] = $out;
336 return $block;
337 }
338 }
339
340 /*
341 * Module settings (admin page)
342 */
343 function worldclock_admin() {
344
345 $wc_location = variable_get('wc_location', array());
346 $maxnum = variable_get('worldclock_maxnum', 4);
347
348 $form['worldclock_maxnum'] = array(
349 '#type' => 'select',
350 '#title' => t('Maximum number of locations'),
351 '#default_value' => $maxnum,
352 '#options' => array(1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8'),
353 );
354
355 $form['worldclock_note'] = array(
356 '#type' => 'markup',
357 '#value' => t('You may leave the location unselected if you do not need to display the time.'),
358 );
359
360 for($j = 1 ; $j <= $maxnum ; $j++) {
361 // show some information
362 $loc_no = variable_get('worldclock_location_' . $j, 0);
363 $loc_info = '';
364 if($loc_no) {
365 $loc = $wc_location[$loc_no];
366 $loc_info = get_location_info($loc);
367 }
368 // create a pull-down menu
369 $form['worldclock_location_' . $j] = array(
370 '#type' => 'select',
371 '#title' => t('Location ') . $j,
372 '#default_value' => variable_get('worldclock_location_' . $j, 0),
373 '#options' => get_location_options(),
374 '#description' => $loc_info,
375 );
376 }
377
378 $form['worldclock_showsec'] = array(
379 '#type' => 'checkbox',
380 '#default_value' => variable_get('worldclock_showsec', 0),
381 '#title' => t('Display seconds'),
382 );
383
384 $form['worldclock_showdst'] = array(
385 '#type' => 'checkbox',
386 '#default_value' => variable_get('worldclock_showdst', 0),
387 '#title' => t('Display DST'),
388 '#description' => t('If checked, * character is displayed after the time when the location is under the DST (Daylight Saving Time).'),
389 );
390 return system_settings_form($form);
391 }
392
393 /*
394 * Menu hook
395 */
396 function worldclock_menu()
397 {
398 $items = array();
399
400 /*---- for Drupal 5.x ----*/
401 $items[] = array(
402 'path' => 'admin/settings/worldclock',
403 'title' => t('World Clock'),
404 'description' => t('Displays a world clock.'),
405 'callback' => 'drupal_get_form',
406 'callback arguments' => 'worldclock_admin',
407 'access' => user_access('access administration pages'),
408 'type' => MENU_NORMAL_ITEM,
409 );
410
411 /*---- for Drupal 6.x ----
412 $items['admin/settings/worldclock'] = array(
413 'title' => 'World Clock',
414 'description' => 'Displays a world clock.',
415 'page callback' => 'drupal_get_form',
416 'page arguments' => array('worldclock_admin'),
417 'access arguments' => array('access administration pages'),
418 'type' => MENU_NORMAL_ITEM,
419 );
420 */
421 return $items;
422 }
423
424 /*
425 * Init hook
426 */
427 function worldclock_init()
428 {
429 // NOTE: adding javascript at the hook_footer() does not work
430 // with Drupal 6.x. So we use hook_init() which works with
431 // both Drupal 5.x and 6.x.
432
433 $path = drupal_get_path('module', 'worldclock') . '/worldclock.js';
434 drupal_add_js($path);
435
436 register_location_array();
437 }
438 ?>

  ViewVC Help
Powered by ViewVC 1.1.2