PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/saf/lib/MailForm/Widget/Datetime.php

https://github.com/olegiv/sitellite
PHP | 285 lines | 122 code | 20 blank | 143 comment | 22 complexity | 0ab3a9b52a0e2c641debf4e0d2a95545 MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | Sitellite Content Management System |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2010 Sitellite.org Community |
  7. // +----------------------------------------------------------------------+
  8. // | This software is released under the GNU GPL License. |
  9. // | Please see the accompanying file docs/LICENSE for licensing details. |
  10. // | |
  11. // | You should have received a copy of the GNU GPL License |
  12. // | along with this program; if not, visit www.sitellite.org. |
  13. // | The license text is also available at the following web site |
  14. // | address: <http://www.sitellite.org/index/license |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: John Luxford <john.luxford@gmail.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // Datetime widget. Displays 3 select boxes representing the year, month,
  20. // and day, as well as three text boxes representing the hour, minute, and
  21. // second.
  22. //
  23. /**
  24. * Datetime widget. Displays 3 select boxes representing the year, month,
  25. * and day, as well as three text boxes representing the hour, minute, and
  26. * second.
  27. *
  28. * Please note: MySQL handles timestamp and datetime column types differently, and while
  29. * both are supported by this widget, it is important to be aware of these differences
  30. * when designing your database tables.
  31. *
  32. * New in 1.2:
  33. * - setValue method now supports dates in the format ############## as well as
  34. * ####-##-## ##:##:##.
  35. *
  36. * New in 1.4:
  37. * - Added a setDefault(value) method.
  38. *
  39. * <code>
  40. * <?php
  41. *
  42. * $widget = new MF_Widget_datetime ('name');
  43. * $widget->validation ('is "foo"');
  44. * $widget->setValue ('foo');
  45. * $widget->error_message = 'Oops! This widget is being unruly!';
  46. *
  47. * ?>
  48. * </code>
  49. *
  50. * @package MailForm
  51. * @author John Luxford <john.luxford@gmail.com>
  52. * @license http://www.sitellite.org/index/license GNU GPL License
  53. * @version 1.4, 2002-03-25, $Id: Datetime.php,v 1.3 2007/10/06 00:06:30 lux Exp $
  54. * @access public
  55. *
  56. */
  57. class MF_Widget_datetime extends MF_Widget {
  58. /**
  59. * Unused and deprecated.
  60. *
  61. * @access private
  62. *
  63. */
  64. var $date;
  65. /**
  66. * Unused and deprecated.
  67. *
  68. * @access private
  69. *
  70. */
  71. var $time;
  72. /**
  73. * A way to pass extra parameters to the HTML form tag, for
  74. * example 'enctype="multipart/formdata"'.
  75. *
  76. * @access public
  77. *
  78. */
  79. var $extra = '';
  80. /**
  81. * This is the short name for this widget. The short name is
  82. * the class name minus the 'MF_Widget_' prefix.
  83. *
  84. * @access public
  85. *
  86. */
  87. var $type = 'datetime';
  88. /**
  89. * Constructor Method.
  90. *
  91. * @access public
  92. * @param string $name
  93. *
  94. */
  95. function MF_Widget_datetime ($name) {
  96. // initialize core Widget settings
  97. parent::MF_Widget ($name);
  98. // load Hidden and Select widgets, on which this widget depends
  99. $GLOBALS['loader']->import ('saf.MailForm.Widget.Hidden');
  100. $GLOBALS['loader']->import ('saf.MailForm.Widget.Date');
  101. $GLOBALS['loader']->import ('saf.MailForm.Widget.Time');
  102. /*
  103. // initialize custom widget settings
  104. $this->data_value_DATE = date ('Y-m-d');
  105. $this->data_value_TIME = date ('H:i:s');
  106. list (
  107. $this->data_value_DATE_YEAR,
  108. $this->data_value_DATE_MONTH,
  109. $this->data_value_DATE_DAY
  110. ) = explode ('-', $this->data_value_DATE);
  111. list (
  112. $this->data_value_TIME_HOUR,
  113. $this->data_value_TIME_MINUTE,
  114. $this->data_value_TIME_SECOND
  115. ) = explode (':', $this->data_value_TIME);*/
  116. }
  117. /**
  118. * Sets the actual value for this widget. An optional second
  119. * parameter can be passed, which is unused here, but can be used in
  120. * complex widget types to assign parts of a value and piece it together
  121. * from multiple physical form fields.
  122. *
  123. * @access public
  124. * @param string $value
  125. * @param string $inner_component
  126. *
  127. */
  128. function setValue ($value = '', $inner_component = '') {
  129. if (! empty ($inner_component)) {
  130. $this->{'data_value_' . $inner_component} = $value;
  131. } elseif (! strstr ($value, ' ')) {
  132. if (preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $value, $regs)) {
  133. $this->data_value_DATE_YEAR = $regs[1];
  134. $this->data_value_DATE_MONTH = $regs[2];
  135. $this->data_value_DATE_DAY = $regs[3];
  136. $this->data_value_TIME_HOUR = $regs[4];
  137. $this->data_value_TIME_MINUTE = $regs[5];
  138. $this->data_value_TIME_SECOND = $regs[6];
  139. }
  140. } else {
  141. list (
  142. $this->data_value_DATE,
  143. $this->data_value_TIME
  144. ) = explode (' ', $value);
  145. list (
  146. $this->data_value_DATE_YEAR,
  147. $this->data_value_DATE_MONTH,
  148. $this->data_value_DATE_DAY
  149. ) = explode ('-', $this->data_value_DATE);
  150. list (
  151. $this->data_value_TIME_HOUR,
  152. $this->data_value_TIME_MINUTE,
  153. $this->data_value_TIME_SECOND
  154. ) = preg_split ('/:/', $this->data_value_TIME);
  155. }
  156. }
  157. /**
  158. * Gives this widget a default value. Accepts a date string
  159. * of the format 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'.
  160. *
  161. * @access public
  162. * @param string $value
  163. *
  164. */
  165. function setDefault ($value) {
  166. if (strstr ($value, ' ')) {
  167. list ($this->data_value_DATE, $this->data_value_TIME) = explode (' ', $value);
  168. list (
  169. $this->data_value_DATE_YEAR,
  170. $this->data_value_DATE_MONTH,
  171. $this->data_value_DATE_DAY
  172. ) = explode ('-', $this->data_value_DATE);
  173. list (
  174. $this->data_value_TIME_HOUR,
  175. $this->data_value_TIME_MINUTE,
  176. $this->data_value_TIME_SECOND
  177. ) = explode (':', $this->data_value_TIME);
  178. } else {
  179. // handle YYYYMMDDHHMMSS format
  180. if (preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $value, $regs)) {
  181. $this->data_value_DATE_YEAR = $regs[1];
  182. $this->data_value_DATE_MONTH = $regs[2];
  183. $this->data_value_DATE_DAY = $regs[3];
  184. $this->data_value_TIME_HOUR = $regs[4];
  185. $this->data_value_TIME_MINUTE = $regs[5];
  186. $this->data_value_TIME_SECOND = $regs[6];
  187. }
  188. }
  189. }
  190. /**
  191. * Fetches the actual value for this widget.
  192. *
  193. * @access public
  194. * @param object $cgi
  195. * @return string
  196. *
  197. */
  198. function getValue ($cgi = '') {
  199. if (! is_object ($cgi)) {
  200. if (empty ($this->data_value_DATE_YEAR) && empty ($this->data_value_DATE_MONTH) && empty ($this->data_value_DATE_DAY) && empty ($this->data_value_TIME_HOUR) && empty ($this->data_value_TIME_MINUTE) && empty ($this->data_value_TIME_SECOND)) {
  201. return '';
  202. }
  203. return
  204. $this->data_value_DATE_YEAR . '-' .
  205. $this->data_value_DATE_MONTH . '-' .
  206. $this->data_value_DATE_DAY . ' ' .
  207. $this->data_value_TIME_HOUR . ':' .
  208. $this->data_value_TIME_MINUTE . ':' .
  209. $this->data_value_TIME_SECOND;
  210. } else {
  211. if (empty ($cgi->{'MF_MF_' . $this->name . '_DATE_YEAR'}) && empty ($cgi->{'MF_MF_' . $this->name . '_DATE_MONTH'}) && empty ($cgi->{'MF_MF_' . $this->name . '_DATE_DAY'}) && empty ($cgi->{'MF_MF_' . $this->name . '_TIME_HOUR'}) && empty ($cgi->{'MF_MF_' . $this->name . '_TIME_MINUTE'}) && empty ($cgi->{'MF_MF_' . $this->name . '_TIME_SECOND'})) {
  212. return '';
  213. }
  214. return
  215. $cgi->{'MF_MF_' . $this->name . '_DATE_YEAR'} . '-' .
  216. $cgi->{'MF_MF_' . $this->name . '_DATE_MONTH'} . '-' .
  217. $cgi->{'MF_MF_' . $this->name . '_DATE_DAY'} . ' ' .
  218. $cgi->{'MF_MF_' . $this->name . '_TIME_HOUR'} . ':' .
  219. $cgi->{'MF_MF_' . $this->name . '_TIME_MINUTE'} . ':' .
  220. $cgi->{'MF_MF_' . $this->name . '_TIME_SECOND'};
  221. }
  222. }
  223. /**
  224. * Returns the display HTML for this widget. The optional
  225. * parameter determines whether or not to automatically display the widget
  226. * nicely, or whether to simply return the widget (for use in a template).
  227. *
  228. * @access public
  229. * @param boolean $generate_html
  230. * @return string
  231. *
  232. */
  233. function display ($generate_html = 0) {
  234. global $simple;
  235. $_date = new MF_Widget_date ('MF_' . $this->name . '_DATE');
  236. $_date->nullable = $this->nullable;
  237. $_date->setValue (
  238. $this->data_value_DATE_YEAR . '-' .
  239. $this->data_value_DATE_MONTH . '-' .
  240. $this->data_value_DATE_DAY
  241. );
  242. $_time = new MF_Widget_time ('MF_' . $this->name . '_TIME');
  243. $_time->nullable = $this->nullable;
  244. $_time->setValue (
  245. $this->data_value_TIME_HOUR . ':' .
  246. $this->data_value_TIME_MINUTE . ':' .
  247. $this->data_value_TIME_SECOND
  248. );
  249. $_date->extra = $this->extra;
  250. $_time->extra = $this->extra;
  251. // echo '<pre>'; print_r ($this); print_r ($_date); echo "\n"; print_r ($_time); echo '</pre>';
  252. $_datetime = new MF_Widget_hidden ($this->name);
  253. if ($generate_html) {
  254. $data = $_datetime->display (0) . "\n";
  255. $data .= "\t<tr>\n\t\t<td valign=\"top\" class=\"label\">" . '<label for="' . $this->name . '"' . $this->invalid () . '>' . $simple->fill ($this->label_template, $this, '', true) . "</label></td>\n\t\t<td class=\"field\">" .
  256. $_date->display (0) . '<br />' . $_time->display (0) .
  257. "</td>\n\t</tr>\n";
  258. } else {
  259. $data = $_datetime->display (0);
  260. $data .= $_date->display (0) . '<br />' . $_time->display (0);
  261. }
  262. return $data;
  263. }
  264. }
  265. ?>