PageRenderTime 129ms CodeModel.GetById 45ms RepoModel.GetById 4ms app.codeStats 0ms

/modules/contributions/civicrm/packages/HTML/QuickForm/date.php

https://github.com/leoburd/whatsup
PHP | 360 lines | 230 code | 20 blank | 110 comment | 14 complexity | f1b8f40fee4335e691867a3e05a8e2e0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexey Borzov <avb@php.net> |
  17. // | Adam Daniel <adaniel1@eesus.jnj.com> |
  18. // | Bertrand Mansion <bmansion@mamasam.com> |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: date.php,v 1.55 2005/08/04 20:39:05 avb Exp $
  22. require_once 'HTML/QuickForm/group.php';
  23. require_once 'HTML/QuickForm/select.php';
  24. /**
  25. * Class for a group of elements used to input dates (and times).
  26. *
  27. * Inspired by original 'date' element but reimplemented as a subclass
  28. * of HTML_QuickForm_group
  29. *
  30. * @author Alexey Borzov <avb@php.net>
  31. * @access public
  32. */
  33. class HTML_QuickForm_date extends HTML_QuickForm_group
  34. {
  35. // {{{ properties
  36. /**
  37. * Various options to control the element's display.
  38. *
  39. * Currently known options are
  40. * 'format': Format of the date, based on PHP's date() function.
  41. * The following characters are recognised in format string:
  42. * D => Short names of days
  43. * l => Long names of days
  44. * d => Day numbers
  45. * M => Short names of months
  46. * F => Long names of months
  47. * m => Month numbers
  48. * Y => Four digit year
  49. * y => Two digit year
  50. * h => 12 hour format
  51. * H => 23 hour format
  52. * i => Minutes
  53. * s => Seconds
  54. * a => am/pm
  55. * A => AM/PM
  56. * 'minYear': Minimum year in year select
  57. * 'maxYear': Maximum year in year select
  58. * 'addEmptyOption': Should an empty option be added to the top of
  59. * each select box?
  60. * 'emptyOptionValue': The value passed by the empty option.
  61. * 'emptyOptionText': The text displayed for the empty option.
  62. * 'optionIncrement': Step to increase the option values by (works for 'i' and 's')
  63. *
  64. * @access private
  65. * @var array
  66. */
  67. var $_options = array(
  68. 'format' => 'dMY',
  69. 'minYear' => 2001,
  70. 'maxYear' => 2010,
  71. 'addEmptyOption' => false,
  72. 'emptyOptionValue' => '',
  73. 'emptyOptionText' => '&nbsp;',
  74. 'optionIncrement' => array('i' => 1, 's' => 1)
  75. );
  76. /**
  77. * These complement separators, they are appended to the resultant HTML
  78. * @access private
  79. * @var array
  80. */
  81. var $_wrap = array('', '');
  82. /**
  83. * Locale array build from CRM_Utils_Date-provided names
  84. *
  85. * @access private
  86. * @var array
  87. */
  88. var $_locale = array();
  89. // }}}
  90. // {{{ constructor
  91. /**
  92. * Class constructor
  93. *
  94. * @access public
  95. * @param string Element's name
  96. * @param mixed Label(s) for an element
  97. * @param array Options to control the element's display
  98. * @param mixed Either a typical HTML attribute string or an associative array
  99. */
  100. function HTML_QuickForm_date($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
  101. {
  102. $this->_locale = array(
  103. 'weekdays_short'=> CRM_Utils_Date::getAbbrWeekdayNames(),
  104. 'weekdays_long' => CRM_Utils_Date::getFullWeekdayNames(),
  105. 'months_short' => CRM_Utils_Date::getAbbrMonthNames(),
  106. 'months_long' => CRM_Utils_Date::getFullMonthNames()
  107. );
  108. $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  109. $this->_persistantFreeze = true;
  110. $this->_appendName = true;
  111. $this->_type = 'date';
  112. // set the options, do not bother setting bogus ones
  113. if (is_array($options)) {
  114. foreach ($options as $name => $value) {
  115. if (isset($this->_options[$name])) {
  116. if (is_array($value)) {
  117. $this->_options[$name] = @array_merge($this->_options[$name], $value);
  118. } else {
  119. $this->_options[$name] = $value;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. // }}}
  126. // {{{ _createElements()
  127. function _createElements()
  128. {
  129. $this->_separator = $this->_elements = array();
  130. $separator = '';
  131. $locale =& $this->_locale;
  132. $backslash = false;
  133. for ($i = 0, $length = strlen($this->_options['format']); $i < $length; $i++) {
  134. $sign = $this->_options['format']{$i};
  135. if ($backslash) {
  136. $backslash = false;
  137. $separator .= $sign;
  138. } else {
  139. $loadSelect = true;
  140. switch ($sign) {
  141. case 'D':
  142. // Sunday is 0 like with 'w' in date()
  143. $options = $locale['weekdays_short'];
  144. $emptyText = ts('-day of week-');
  145. break;
  146. case 'l':
  147. $options = $locale['weekdays_long'];
  148. $emptyText = ts('-day of week-');
  149. break;
  150. case 'd':
  151. $options = $this->_createOptionList(1, 31);
  152. $emptyText = ts('-day-');
  153. break;
  154. case 'M':
  155. $options = $locale['months_short'];
  156. array_unshift($options , '');
  157. unset($options[0]);
  158. $emptyText = ts('-month-');
  159. break;
  160. case 'm':
  161. $options = $this->_createOptionList(1, 12);
  162. $emptyText = ts('-month-');
  163. break;
  164. case 'F':
  165. $options = $locale['months_long'];
  166. array_unshift($options , '');
  167. unset($options[0]);
  168. $emptyText = ts('-month-');
  169. break;
  170. case 'Y':
  171. $options = $this->_createOptionList(
  172. $this->_options['minYear'],
  173. $this->_options['maxYear'],
  174. $this->_options['minYear'] > $this->_options['maxYear']? -1: 1
  175. );
  176. $emptyText = ts('-year-');
  177. break;
  178. case 'y':
  179. $options = $this->_createOptionList(
  180. $this->_options['minYear'],
  181. $this->_options['maxYear'],
  182. $this->_options['minYear'] > $this->_options['maxYear']? -1: 1
  183. );
  184. array_walk($options, create_function('&$v,$k','$v = substr($v,-2);'));
  185. $emptyText = ts('-year-');
  186. break;
  187. case 'h':
  188. $options = $this->_createOptionList(1, 12);
  189. $emptyText = ts('-hour-');
  190. break;
  191. case 'g':
  192. $options = $this->_createOptionList(1, 12);
  193. array_walk($options, create_function('&$v,$k', '$v = intval($v);'));
  194. break;
  195. case 'H':
  196. $options = $this->_createOptionList(0, 23);
  197. $emptyText = ts('-hour-');
  198. break;
  199. case 'i':
  200. $options = $this->_createOptionList(0, 59, $this->_options['optionIncrement']['i']);
  201. $emptyText = ts('-min-');
  202. break;
  203. case 's':
  204. $options = $this->_createOptionList(0, 59, $this->_options['optionIncrement']['s']);
  205. $emptyText = ts('-sec-');
  206. break;
  207. case 'a':
  208. $options = array('am' => 'am', 'pm' => 'pm');
  209. $emptyText = '-am/pm-';
  210. break;
  211. case 'A':
  212. $options = array('AM' => 'AM', 'PM' => 'PM');
  213. $emptyText = '-AM/PM-';
  214. break;
  215. case 'W':
  216. $options = $this->_createOptionList(1, 53);
  217. break;
  218. case '\\':
  219. $backslash = true;
  220. $loadSelect = false;
  221. break;
  222. default:
  223. $separator .= (' ' == $sign? '&nbsp;': $sign);
  224. $loadSelect = false;
  225. }
  226. if ($loadSelect) {
  227. if (0 < count($this->_elements)) {
  228. $this->_separator[] = $separator;
  229. } else {
  230. $this->_wrap[0] = $separator;
  231. }
  232. $separator = '';
  233. // Should we add an empty option to the top of the select?
  234. if (!is_array($this->_options['addEmptyOption']) && $this->_options['addEmptyOption'] ||
  235. is_array($this->_options['addEmptyOption']) && !empty($this->_options['addEmptyOption'][$sign])) {
  236. // Using '+' array operator to preserve the keys
  237. if (is_array($this->_options['emptyOptionText']) && !empty($this->_options['emptyOptionText'][$sign])) {
  238. $text = $emptyText ? $emptyText : $this->_options['emptyOptionText'][$sign];
  239. $options = array($this->_options['emptyOptionValue'] => $text) + $options;
  240. } else {
  241. $text = $emptyText ? $emptyText : $this->_options['emptyOptionText'];
  242. $options = array($this->_options['emptyOptionValue'] => $text) + $options;
  243. }
  244. }
  245. //modified autogenerated id for date select boxes.
  246. $attribs = $this->getAttributes();
  247. $elementName = $this->getName();
  248. $attribs['id'] = $elementName.'['.$sign.']';
  249. $this->_elements[] =& new HTML_QuickForm_select($sign, null, $options, $attribs);
  250. }
  251. }
  252. }
  253. $this->_wrap[1] = $separator . ($backslash? '\\': '');
  254. }
  255. // }}}
  256. // {{{ _createOptionList()
  257. /**
  258. * Creates an option list containing the numbers from the start number to the end, inclusive
  259. *
  260. * @param int The start number
  261. * @param int The end number
  262. * @param int Increment by this value
  263. * @access private
  264. * @return array An array of numeric options.
  265. */
  266. function _createOptionList($start, $end, $step = 1)
  267. {
  268. for ($i = $start, $options = array(); $start > $end? $i >= $end: $i <= $end; $i += $step) {
  269. $options[$i] = sprintf('%02d', $i);
  270. }
  271. return $options;
  272. }
  273. // }}}
  274. // {{{ setValue()
  275. function setValue($value)
  276. {
  277. if (empty($value)) {
  278. $value = array();
  279. } elseif (is_scalar($value)) {
  280. if (!is_numeric($value)) {
  281. $value = strtotime($value);
  282. }
  283. // might be a unix epoch, then we fill all possible values
  284. $arr = explode('-', date('w-d-n-Y-h-H-i-s-a-A-W', (int)$value));
  285. $value = array(
  286. 'D' => $arr[0],
  287. 'l' => $arr[0],
  288. 'd' => $arr[1],
  289. 'M' => $arr[2],
  290. 'm' => $arr[2],
  291. 'F' => $arr[2],
  292. 'Y' => $arr[3],
  293. 'y' => $arr[3],
  294. 'h' => $arr[4],
  295. 'g' => $arr[4],
  296. 'H' => $arr[5],
  297. 'i' => $arr[6],
  298. 's' => $arr[7],
  299. 'a' => $arr[8],
  300. 'A' => $arr[9],
  301. 'W' => $arr[10]
  302. );
  303. }
  304. parent::setValue($value);
  305. }
  306. // }}}
  307. // {{{ toHtml()
  308. function toHtml()
  309. {
  310. include_once('HTML/QuickForm/Renderer/Default.php');
  311. $renderer =& new HTML_QuickForm_Renderer_Default();
  312. $renderer->setElementTemplate('{element}');
  313. parent::accept($renderer);
  314. return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
  315. }
  316. // }}}
  317. // {{{ accept()
  318. function accept(&$renderer, $required = false, $error = null)
  319. {
  320. $renderer->renderElement($this, $required, $error);
  321. }
  322. // }}}
  323. // {{{ onQuickFormEvent()
  324. function onQuickFormEvent($event, $arg, &$caller)
  325. {
  326. if ('updateValue' == $event) {
  327. // we need to call setValue(), 'cause the default/constant value
  328. // may be in fact a timestamp, not an array
  329. return HTML_QuickForm_element::onQuickFormEvent($event, $arg, $caller);
  330. } else {
  331. return parent::onQuickFormEvent($event, $arg, $caller);
  332. }
  333. }
  334. // }}}
  335. }
  336. ?>