Source for file clock-defs.php

Documentation is available at clock-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (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: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: clock-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for displaying a javascript clock. */
  24. /* This clock is a digital read-out in a layer or div. */
  25. /* The time and date are taken from the server rather than */
  26. /* relying on possibly innaccurate user browser. */
  27. /* */
  28. /* ******************************************************************** */
  29. /** @package datetime */// are all properties which can be set for the clock.
  30. /** Clock display hours (am/pm) */
  31. ("DISPLAY_24_HOURS", 0);
  32. /** Display to seconds or mins */
  33. ("DISPLAY_SECONDS", 1);
  34. /** Show date or not */
  35. ("DISPLAY_DATE", 2);
  36. /** Show day name or not */
  37. ("DISPLAY_DAYNAME", 3);
  38. /** Abbreviate day name or not */
  39. ("ABBREVIATE_DAYNAME", 4);
  40. /** Show the date first */
  41. ("DISPLAY_DATE_FIRST", 5);
  42. /** Show date and time on one line */
  43. ("DISPLAY_INLINE", 6);
  44. /** Clock overall clock table attributes (eg. style, width etc.) */
  45. ("CLOCK_ATTRS", 7);
  46. /** Clock time string style and/or class */
  47. ("CLOCK_TIME_CSS", 8);
  48. /** Clock date string style and/or class */
  49. ("CLOCK_DATE_CSS", 9);
  50. /** Clock date format M=month no, m=month name, D=day no,
  51. CC=century (20), YY=year (99) */
  52. define("CLOCK_DATE_FORMAT", 10);
  53.  
  54. // ----------------------------------------------------------------------
  55. /**
  56. * The digital_clock class. This is implemented as a 'layer' or 'div'
  57. * and rendered using Javascript. UNless you specify the initial time
  58. * yourself, the time will be taken from the server, rather than from
  59. * the user's browser, which may be innaccurate. Timezone settings are
  60. * then used to adjust it to display the appropriate date & time.
  61. * @package datetime
  62. */
  63. class digital_clock extends HTMLObject {
  64. /** Unique ID for this clock widget */
  65.  
  66. var $clockid = "";
  67. /** Initial time to set the clock to */
  68.  
  69. var $initial_ts = "";
  70. /** The timezone offset in +/- hours from GMT.
  71. If this is left unset, then the displayed time will be
  72. offset to local time according to the client browser
  73. timezone settings. */
  74. var $tz;
  75. /** X-offset from left */
  76.  
  77. var $leftx;
  78. /** Y-offset from top */
  79.  
  80. var $topy;
  81. /** Positioning mode */
  82.  
  83. var $position = "";
  84. /** Visibility setting */
  85.  
  86. var $visibility = "visible";
  87. /** Clock display settings. An array of booleans. */
  88.  
  89. var $display_properties = array();
  90. /** Style or class for time digits */
  91.  
  92. var $css_time;
  93. /** Style or class for date display */
  94.  
  95. var $css_date;
  96. /** Date format eg. 'M d, ccyy' for 'April 4, 2005' */
  97.  
  98. var $date_format = "M d, ccyy";
  99. // Private
  100. /** Interval between clock updates in millisecs */
  101.  
  102. var $update_interval_ms = 1000;
  103. // .....................................................................
  104. /**
  105. * Constructor for the clock
  106. * @param integer $initial_ts Optional initial time - a Unix timestamp
  107. */
  108. function digital_clock($initial_ts="") {
  109. // Set a unique ID for this widget..
  110. $this->clockid = "clock_" . md5(uniqid(rand(), true));
  111.  
  112. // Initial starting time for this clock..
  113. if ($initial_ts != "") {
  114. $this->initial_ts = $initial_ts;
  115. }
  116. else {
  117. $this->initial_ts = time();
  118. }
  119. // Set default styles..
  120. $this->css_time = new StylableObject("font:bold 12pt arial,helvetica,sans-serif");
  121. $this->css_date = new StylableObject("font:bold 8pt arial,helvetica,sans-serif");
  122.  
  123. // Set the default boolean properties..
  124. $this->display_properties[DISPLAY_24_HOURS] = false;
  125. $this->display_properties[DISPLAY_SECONDS] = false;
  126. $this->display_properties[DISPLAY_DATE] = false;
  127. $this->display_properties[DISPLAY_DAYNAME] = false;
  128. $this->display_properties[ABBREVIATE_DAYNAME] = false;
  129. $this->display_properties[DISPLAY_DATE_FIRST] = false;
  130. $this->display_properties[DISPLAY_INLINE] = false;
  131.  
  132. } // digital_clock
  133. // .....................................................................
  134. /** Set the position and the positioning directive for the clock.
  135. * @param integer $leftx Pixels from left margin
  136. * @param integer $topy Pixels from top margin
  137. * @param string $pos Positioning mode "absolute" or "relative"
  138. */
  139. function set_position($leftx="", $topy="", $pos="absolute") {
  140. $this->leftx = $leftx;
  141. $this->topy = $topy;
  142. $this->position = $pos;
  143. } // set_position
  144. // .....................................................................
  145. /** Set the starting visibility for the clock. In case you want to
  146. * reveal/hide it with some javascript.
  147. * @param string $vis Visibility: "visible" or "hidden"
  148. */
  149. function set_visibility($vis="visible") {
  150. $this->visibility = ($vis === "visible") ? "visible" : "hidden";
  151. } // set_visibility
  152. // .....................................................................
  153. /** Set the timezone to use for this clock. The argument should be
  154. * an integer (or decimal) in the range +12 to -12, in hours offset from GMT.
  155. * @param mixed $tz From +12 to -12 hours offset from GMT.
  156. */
  157. function set_timezone($tz) {
  158. if ($tz === "") {
  159. if (isset($this->tz)) {
  160. unset($this->tz);
  161. }
  162. }
  163. else {
  164. $this->tz = $tz;
  165. }
  166. } // set_timezone
  167. // .....................................................................
  168. /** Special set timezone case - setting to GMT (0 hours offset). */
  169.  
  170. function set_to_gmt() {
  171. $this->set_timezone(0);
  172. } // set_to_gmt
  173. // .....................................................................
  174. /** Set the number of seconds to wait before updating the clock each time.
  175. * The default value is every second, but if you aren't showing seconds then
  176. * you might want to offload the browser a little and make it every 30 secs
  177. * or even longer.
  178. * @param integer $secs Number of seconds between updates
  179. */
  180. function set_update_interval_secs($secs) {
  181. $this->update_interval_ms = ceil($secs * 1000);
  182. if ($this->update_interval_ms <= 0) {
  183. $this->update_interval_ms = 1000;
  184. }
  185. } // set_update_interval_secs
  186. // .....................................................................
  187. /** Set a display property for the clock. Display property IDs are defined
  188. * at the top of this script, and can be any type of content. NB: Boolean
  189. * properties are so arranged that the default is always false.
  190. * @param integer $propertyid ID of the property to set
  191. * @param mixed $value Value for the given display property
  192. */
  193. function set_display_property($propertyid, $value) {
  194. if (isset($this->display_properties[$propertyid])) {
  195. $this->display_properties[$propertyid] = $value;
  196. }
  197. } // set_display_property
  198.  
  199. // Some nicer wrappers for the above..
  200. function show_24hrs() { $this->set_display_property(DISPLAY_24_HOURS, true); }
  201. function show_seconds() { $this->set_display_property(DISPLAY_SECONDS, true); }
  202. function show_date() { $this->set_display_property(DISPLAY_DATE, true); }
  203. function show_dayname() { $this->set_display_property(DISPLAY_DAYNAME, true); }
  204. function abbreviate_dayname() { $this->set_display_property(ABBREVIATE_DAYNAME, true); }
  205. function show_date_first() { $this->set_display_property(DISPLAY_DATE_FIRST, true); }
  206. function show_inline() { $this->set_display_property(DISPLAY_INLINE, true); }
  207.  
  208. // .....................................................................
  209. /** Set the style of class for the time display
  210. * @param $css string Style or class name for time display
  211. */
  212. function set_css_time($css) {
  213. $this->css_time->setcss($css);
  214. } // set_css_time
  215. // .....................................................................
  216. /** Set the style of class for the date display
  217. * @param $css string Style or class name for date display
  218. */
  219. function set_css_date($css) {
  220. $this->css_date->setcss($css);
  221. } // set_css_date
  222. // .....................................................................
  223. /** Set the format for the date display. This is just done by replacement
  224. * in a format string where d=month date, M=month name, m=month no, cc=century,
  225. * and yy=year (99). Eg. 'm d, ccyy' would give 'April 4, 2005'. Another
  226. * example would be 'd/m/yy' which would render '4/4/05'.
  227. * @param $css string Style or class name for date display
  228. */
  229. function set_date_format($fmt) {
  230. $this->date_format = $fmt;
  231. } // set_date_format
  232. // .....................................................................
  233. /**
  234. * Render the clock.
  235. */
  236. function html() {
  237. debug_trace($this);
  238. global $RESPONSE;
  239.  
  240. // Set the style and class settings..
  241. $this->display_properties[CLOCK_ATTRS] = $this->attributes();
  242. $this->display_properties[CLOCK_TIME_CSS] = $this->css_time->style_attributes();
  243. $this->display_properties[CLOCK_DATE_CSS] = $this->css_date->style_attributes();
  244. $this->display_properties[CLOCK_DATE_FORMAT] = $this->date_format;
  245.  
  246. // Only insert this stuff into the webpage once..
  247. static $done_once = false;
  248. if (!$done_once) {
  249. $RESPONSE->add_named_script(
  250. "var daynames=new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');\n"
  251. . "var monthnames=new Array('January', 'February', 'March', 'April', 'May', 'June',"
  252. . "'July', 'August', 'September', 'October', 'November', 'December');\n"
  253. . "function writeLayer(clockID,htmlcontent) {\n"
  254. . " if (document.layers){\n"
  255. . " lyr=document.layers[''+clockID+''].document;\n"
  256. . " lyr.open();\n"
  257. . " lyr.write(htmlcontent);\n"
  258. . " lyr.close();\n"
  259. . " }\n"
  260. . " else if (document.all){\n"
  261. . " document.all[''+clockID+''].innerHTML=htmlcontent;\n"
  262. . " }\n"
  263. . " else if (document.getElementById){\n"
  264. . " range=document.createRange();\n"
  265. . " element=document.getElementById(''+clockID+'');\n"
  266. . " range.setStartBefore(element);\n"
  267. . " content=range.createContextualFragment(htmlcontent)\n"
  268. . " while(element.hasChildNodes()) element.removeChild(element.lastChild);\n"
  269. . " element.appendChild(content);\n"
  270. . " }\n"
  271. . "}\n",
  272. "clock"
  273. );
  274. // Assmeble the generic formatTime function..
  275. $js = "function formatTime(clock_dt,clock_props){\n"
  276. . " var timeStr = '';\n"
  277. . " var monthno = clock_dt.getMonth();\n"
  278. . " var monthname = monthnames[monthno];\n"
  279. . " var mthday = String(clock_dt.getDate());\n"
  280. . " var year = String(clock_dt.getFullYear());\n"
  281. . " var shortyear = year.substr(2,2);\n"
  282. . " var century = year.substr(0,2);\n"
  283. . " var dayno = clock_dt.getDay();\n"
  284. . " var dayname = daynames[dayno];\n"
  285. . " var dtfmt = clock_props[" . CLOCK_DATE_FORMAT . "];\n"
  286. . " if(clock_props[" . ABBREVIATE_DAYNAME . "]) dayname = dayname.substring(0,3);\n"
  287. . " var hrs = clock_dt.getHours();\n"
  288. . " var mins = clock_dt.getMinutes();\n"
  289. . " var mm = String(mins);\n"
  290. . " var secs = clock_dt.getSeconds();\n"
  291. . " var ss = String(secs);\n"
  292. . " var ampm = '';\n"
  293. . " if (!clock_props[" . DISPLAY_24_HOURS . "]) {\n"
  294. . " if (hrs > 12) {\n"
  295. . " hrs = hrs - 12;\n"
  296. . " ampm = 'pm';\n"
  297. . " }\n"
  298. . " else if (hrs == 12) {\n"
  299. . " ampm = 'pm';\n"
  300. . " }\n"
  301. . " else if (hrs == 0) {\n"
  302. . " hrs = 12;\n"
  303. . " ampm = 'am';\n"
  304. . " }\n"
  305. . " else {\n"
  306. . " ampm = 'am';\n"
  307. . " }\n"
  308. . " }\n"
  309. . " var hh = String(hrs);\n"
  310. . " var Str = dtStr = tiStr = '';\n"
  311. . " tiStr += '<td' + clock_props[" . CLOCK_TIME_CSS . "] + '>';\n"
  312. . " tiStr += '<span' + clock_props[" . CLOCK_TIME_CSS . "] + '>';\n"
  313. . " tiStr += ((hrs < 10) ? ' ' : '') + hh + ((mins < 10) ? ':0' : ':') + mm;\n"
  314. . " if(clock_props[" . DISPLAY_SECONDS . "]) tiStr += ((secs < 10) ? ':0' : ':') + ss;\n"
  315. . " if(!clock_props[" . DISPLAY_24_HOURS . "]) tiStr += ampm;\n"
  316. . " tiStr += '</span></td>';\n"
  317. . " Str += '<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"' + clock_props[" . CLOCK_ATTRS . "] + '><tr>';\n"
  318. . " if(clock_props[" . DISPLAY_DATE . "]) {\n"
  319. . " dtStr += '<td' + clock_props[" . CLOCK_DATE_CSS . "] + '><span' + clock_props[" . CLOCK_DATE_CSS . "] + '>';"
  320. . " if(clock_props[" . DISPLAY_DAYNAME . "]) {dtStr += dayname + ' '}\n"
  321. . " var dt = dtfmt.replace(/d/, mthday);\n"
  322. . " dt = dt.replace(/M/, monthname);\n"
  323. . " dt = dt.replace(/m/, String(monthno+1));\n"
  324. . " dt = dt.replace(/yy/, shortyear);\n"
  325. . " dt = dt.replace(/cc/, century);\n"
  326. . " dtStr += dt + '</span></td>';\n"
  327. . " if(clock_props[" . DISPLAY_DATE_FIRST . "]) {Str += dtStr;} else {Str += tiStr;}\n"
  328. . " if(!clock_props[" . DISPLAY_INLINE . "]) {Str += '</tr><tr>';}\n"
  329. . " if(clock_props[" . DISPLAY_DATE_FIRST . "]) {Str += tiStr;} else {Str += dtStr;}\n"
  330. . " }\n"
  331. . " else {Str += tiStr;}\n"
  332. . " Str += '</tr></table>';\n"
  333. . " return Str;\n"
  334. . "}\n"
  335. ;
  336. // Finally add the assmebled script..
  337. $RESPONSE->add_named_script($js, "clock");
  338.  
  339. // Only want it inserted once..
  340. $done_once = true;
  341. }
  342.  
  343. // Initial time derived from server, which allows us to
  344. // determine the accuracy of the clock shown to user..
  345. $gmdate = gmdate("Y,m-1,d,H,i,s", $this->initial_ts);
  346.  
  347. // Define names..
  348. $clock_var = "refer_" . $this->clockid;
  349. $local_var = "local_" . $this->clockid;
  350. $props_var = "props_" . $this->clockid;
  351. $fn_showclock = "display_" . $this->clockid;
  352. if (isset($this->tz)) {
  353. $tz_offset_ms = "($this->tz * 60 * 60 * 1000)";
  354. }
  355. else {
  356. $tz_offset_ms = "($clock_var.getTimezoneOffset() * 60 * 1000)";
  357. }
  358. // Add main vars..
  359. $RESPONSE->add_named_script(
  360. "var $clock_var=new Date($gmdate);\n"
  361. . "var $local_var=new Date();\n"
  362. . "$clock_var.setTime($clock_var.getTime() + $tz_offset_ms);\n"
  363. . "var $props_var = new Array();\n"
  364. ,
  365. "clock"
  366. );
  367. $ix = 0;
  368. foreach ($this->display_properties as $propval) {
  369. if (is_bool($propval)) {
  370. $assignment = $props_var . "[" . $ix . "] = " . ($propval === true ? "true" : "false") . ";\n";
  371. }
  372. else {
  373. $assignment = $props_var . "[" . $ix . "] = '$propval';\n";
  374. }
  375. $RESPONSE->add_named_script($assignment, "clock");
  376. $ix += 1;
  377. }
  378.  
  379. $RESPONSE->add_named_script(
  380. "function $fn_showclock(){\n"
  381. . "setTimeout('$fn_showclock()'," . $this->update_interval_ms . ");\n"
  382. . "var now_dt = new Date();\n"
  383. . " $clock_var.setTime($clock_var.getTime() + (now_dt.getTime() - $local_var.getTime()));\n"
  384. . " $local_var.setTime(now_dt.getTime());\n"
  385. . "var s = formatTime($clock_var,$props_var);\n"
  386. . "writeLayer('$this->clockid', s);\n"
  387. . "}\n"
  388. ,
  389. "clock"
  390. );
  391. $div = new HTMLObject();
  392. $div->inherit_attributes($this);
  393. $div->setstyle("visibility:$this->visibility");
  394. $div->setstyle("z-index:1");
  395. if ($this->position != "") {
  396. $div->setstyle("position:$this->position");
  397. if (isset($this->leftx) && $this->leftx != "") {
  398. $div->setstyle("left:$this->leftx");
  399. }
  400. if (isset($this->topy) && $this->topy != "") {
  401. $div->setstyle("top:$this->topy");
  402. }
  403. }
  404. $s = "<div id=\"$this->clockid\"" . $div->attributes() . "></div>";
  405. $RESPONSE->set_onload("$fn_showclock();");
  406. return $s;
  407. debug_trace();
  408. } // html
  409. } // digital_clock class
  410. // ----------------------------------------------------------------------
  411.  
  412. ?>

Documentation generated by phpDocumentor 1.3.0RC3