DatePicker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Registry
  24. */
  25. require_once "Zend/Registry.php";
  26. /**
  27. * @see ZendX_JQuery_View_Helper_UiWidget
  28. */
  29. require_once "ZendX/JQuery/View/Helper/UiWidget.php";
  30. /**
  31. * jQuery Date Picker View Helper
  32. *
  33. * @uses Zend_View_Helper_FormText
  34. * @package ZendX_JQuery
  35. * @subpackage View
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class ZendX_JQuery_View_Helper_DatePicker extends ZendX_JQuery_View_Helper_UiWidget
  40. {
  41. /**
  42. * Create a jQuery UI Widget Date Picker
  43. *
  44. * @link http://docs.jquery.com/UI/Datepicker
  45. * @param string $id
  46. * @param string $value
  47. * @param array $params jQuery Widget Parameters
  48. * @param array $attribs HTML Element Attributes
  49. * @return string
  50. */
  51. public function datePicker($id, $value = null, array $params = array(), array $attribs = array())
  52. {
  53. $attribs = $this->_prepareAttributes($id, $value, $attribs);
  54. if(!isset($params['dateFormat']) && Zend_Registry::isRegistered('Zend_Locale')) {
  55. $params['dateFormat'] = self::resolveZendLocaleToDatePickerFormat();
  56. }
  57. // TODO: Allow translation of DatePicker Text Values to get this action from client to server
  58. $params = ZendX_JQuery::encodeJson($params);
  59. $js = sprintf('%s("#%s").datepicker(%s);',
  60. ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
  61. $attribs['id'],
  62. $params
  63. );
  64. $this->jquery->addOnLoad($js);
  65. return $this->view->formText($id, $value, $attribs);
  66. }
  67. /**
  68. * A Check for Zend_Locale existance has already been done in {@link datePicker()}
  69. * this function only resolves the default format from Zend Locale to
  70. * a jQuery Date Picker readable format. This function can be potentially buggy
  71. * because of its easy nature and is therefore stripped from the core functionality
  72. * to be easily overriden.
  73. *
  74. * @return string
  75. */
  76. public static function resolveZendLocaleToDatePickerFormat($format=null)
  77. {
  78. if($format == null) {
  79. $locale = Zend_Registry::get('Zend_Locale');
  80. if( !($locale instanceof Zend_Locale) ) {
  81. require_once "ZendX/JQuery/Exception.php";
  82. throw new ZendX_JQuery_Exception("Cannot resolve Zend Locale format by default, no application wide locale is set.");
  83. }
  84. /**
  85. * @see Zend_Locale_Format
  86. */
  87. require_once "Zend/Locale/Format.php";
  88. $format = Zend_Locale_Format::getDateFormat($locale);
  89. }
  90. $dateFormat = array(
  91. 'EEEEE' => 'D', 'EEEE' => 'DD', 'EEE' => 'D', 'EE' => 'D', 'E' => 'D',
  92. 'MMMM' => 'MM', 'MMM' => 'M', 'MM' => 'mm', 'M' => 'm',
  93. 'YYYYY' => 'yy', 'YYYY' => 'yy', 'YYY' => 'yy', 'YY' => 'y', 'Y' => 'yy',
  94. 'yyyyy' => 'yy', 'yyyy' => 'yy', 'yyy' => 'yy', 'yy' => 'y', 'y' => 'yy',
  95. 'G' => '', 'e' => '', 'a' => '', 'h' => '', 'H' => '', 'm' => '',
  96. 's' => '', 'S' => '', 'z' => '', 'Z' => '', 'A' => '',
  97. );
  98. $newFormat = "";
  99. $isText = false;
  100. $i = 0;
  101. while($i < strlen($format)) {
  102. $chr = $format[$i];
  103. if($chr == '"' || $chr == "'") {
  104. $isText = !$isText;
  105. }
  106. $replaced = false;
  107. if($isText == false) {
  108. foreach($dateFormat AS $zl => $jql) {
  109. if(substr($format, $i, strlen($zl)) == $zl) {
  110. $chr = $jql;
  111. $i += strlen($zl);
  112. $replaced = true;
  113. }
  114. }
  115. }
  116. if($replaced == false) {
  117. $i++;
  118. }
  119. $newFormat .= $chr;
  120. }
  121. return $newFormat;
  122. }
  123. }