calendar.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * File: calendar.php | (c) dynarch.com 2004
  4. * Distributed as part of "The Coolest DHTML Calendar"
  5. * under the same terms.
  6. * -----------------------------------------------------------------
  7. * This file implements a simple PHP wrapper for the calendar. It
  8. * allows you to easily include all the calendar files and setup the
  9. * calendar by instantiating and calling a PHP object.
  10. */
  11. define('NEWLINE', "\n");
  12. class DHTML_Calendar {
  13. var $calendar_lib_path;
  14. var $calendar_file;
  15. var $calendar_lang_file;
  16. var $calendar_setup_file;
  17. var $calendar_theme_file;
  18. var $calendar_options;
  19. function DHTML_Calendar($calendar_lib_path = '/calendar/',
  20. $lang = 'en',
  21. $theme = 'calendar-win2k-1',
  22. $stripped = true) {
  23. if ($stripped) {
  24. $this->calendar_file = 'calendar_stripped.js';
  25. $this->calendar_setup_file = 'calendar-setup_stripped.js';
  26. } else {
  27. $this->calendar_file = 'calendar.js';
  28. $this->calendar_setup_file = 'calendar-setup.js';
  29. }
  30. $this->calendar_lang_file = 'lang/calendar-' . $lang . '.js';
  31. $this->calendar_theme_file = $theme.'.css';
  32. $this->calendar_lib_path = preg_replace('/\/+$/', '/', $calendar_lib_path);
  33. $this->calendar_options = array('ifFormat' => '%Y/%m/%d',
  34. 'daFormat' => '%Y/%m/%d');
  35. }
  36. function set_option($name, $value) {
  37. $this->calendar_options[$name] = $value;
  38. }
  39. function load_files() {
  40. echo $this->get_load_files_code();
  41. }
  42. function get_load_files_code() {
  43. $code = ( '<link rel="stylesheet" type="text/css" media="all" href="' .
  44. $this->calendar_lib_path . $this->calendar_theme_file .
  45. '" />' . NEWLINE );
  46. $code .= ( '<script type="text/javascript" src="' .
  47. $this->calendar_lib_path . $this->calendar_file .
  48. '"></script>' . NEWLINE );
  49. $code .= ( '<script type="text/javascript" src="' .
  50. $this->calendar_lib_path . $this->calendar_lang_file .
  51. '"></script>' . NEWLINE );
  52. $code .= ( '<script type="text/javascript" src="' .
  53. $this->calendar_lib_path . $this->calendar_setup_file .
  54. '"></script>' );
  55. return $code;
  56. }
  57. function _make_calendar($other_options = array()) {
  58. $js_options = $this->_make_js_hash(array_merge($this->calendar_options, $other_options));
  59. $code = ( '<script type="text/javascript">Calendar.setup({' .
  60. $js_options .
  61. '});</script>' );
  62. return $code;
  63. }
  64. function make_input_field($cal_options = array(), $field_attributes = array()) {
  65. $id = $this->_gen_id();
  66. $attrstr = $this->_make_html_attr(array_merge($field_attributes,
  67. array('id' => $this->_field_id($id),
  68. 'type' => 'text')));
  69. echo '<input ' . $attrstr .'/>';
  70. echo '<a href="#" id="'. $this->_trigger_id($id) . '">' .
  71. '<img align="middle" border="0" src="' . $this->calendar_lib_path . 'img.gif" alt="" /></a>';
  72. $options = array_merge($cal_options,
  73. array('inputField' => $this->_field_id($id),
  74. 'button' => $this->_trigger_id($id)));
  75. echo $this->_make_calendar($options);
  76. }
  77. /// PRIVATE SECTION
  78. function _field_id($id) { return 'f-calendar-field-' . $id; }
  79. function _trigger_id($id) { return 'f-calendar-trigger-' . $id; }
  80. function _gen_id() { static $id = 0; return ++$id; }
  81. function _make_js_hash($array) {
  82. $jstr = '';
  83. reset($array);
  84. while (list($key, $val) = each($array)) {
  85. if (is_bool($val))
  86. $val = $val ? 'true' : 'false';
  87. else if (!is_numeric($val))
  88. $val = '"'.$val.'"';
  89. if ($jstr) $jstr .= ',';
  90. $jstr .= '"' . $key . '":' . $val;
  91. }
  92. return $jstr;
  93. }
  94. function _make_html_attr($array) {
  95. $attrstr = '';
  96. reset($array);
  97. while (list($key, $val) = each($array)) {
  98. $attrstr .= $key . '="' . $val . '" ';
  99. }
  100. return $attrstr;
  101. }
  102. };
  103. ?>