00001 <?php
00026 require_once "Zend/Registry.php";
00027
00031 require_once "ZendX/JQuery/View/Helper/UiWidget.php";
00032
00042 class ZendX_JQuery_View_Helper_DatePicker extends ZendX_JQuery_View_Helper_UiWidget
00043 {
00054 public function datePicker($id, $value = null, array $params = array(), array $attribs = array())
00055 {
00056 $attribs = $this->_prepareAttributes($id, $value, $attribs);
00057
00058 if(!isset($params['dateFormat']) && Zend_Registry::isRegistered('Zend_Locale')) {
00059 $params['dateFormat'] = self::resolveZendLocaleToDatePickerFormat();
00060 }
00061
00062
00063 $params = ZendX_JQuery::encodeJson($params);
00064
00065 $js = sprintf('%s("#%s").datepicker(%s);',
00066 ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
00067 $attribs['id'],
00068 $params
00069 );
00070
00071 $this->jquery->addOnLoad($js);
00072
00073 return $this->view->formText($id, $value, $attribs);
00074 }
00075
00085 public static function resolveZendLocaleToDatePickerFormat($format=null)
00086 {
00087 if($format == null) {
00088 $locale = Zend_Registry::get('Zend_Locale');
00089 if( !($locale instanceof Zend_Locale) ) {
00090 require_once "ZendX/JQuery/Exception.php";
00091 throw new ZendX_JQuery_Exception("Cannot resolve Zend Locale format by default, no application wide locale is set.");
00092 }
00096 require_once "Zend/Locale/Format.php";
00097 $format = Zend_Locale_Format::getDateFormat($locale);
00098 }
00099
00100 $dateFormat = array(
00101 'EEEEE' => 'D', 'EEEE' => 'DD', 'EEE' => 'D', 'EE' => 'D', 'E' => 'D',
00102 'MMMM' => 'MM', 'MMM' => 'M', 'MM' => 'mm', 'M' => 'm',
00103 'YYYYY' => 'yy', 'YYYY' => 'yy', 'YYY' => 'yy', 'YY' => 'y', 'Y' => 'yy',
00104 'yyyyy' => 'yy', 'yyyy' => 'yy', 'yyy' => 'yy', 'yy' => 'y', 'y' => 'yy',
00105 'G' => '', 'e' => '', 'a' => '', 'h' => '', 'H' => '', 'm' => '',
00106 's' => '', 'S' => '', 'z' => '', 'Z' => '', 'A' => '',
00107 );
00108
00109 $newFormat = "";
00110 $isText = false;
00111 $i = 0;
00112 while($i < strlen($format)) {
00113 $chr = $format[$i];
00114 if($chr == '"' || $chr == "'") {
00115 $isText = !$isText;
00116 }
00117 $replaced = false;
00118 if($isText == false) {
00119 foreach($dateFormat AS $zl => $jql) {
00120 if(substr($format, $i, strlen($zl)) == $zl) {
00121 $chr = $jql;
00122 $i += strlen($zl);
00123 $replaced = true;
00124 }
00125 }
00126 }
00127 if($replaced == false) {
00128 $i++;
00129 }
00130 $newFormat .= $chr;
00131 }
00132
00133 return $newFormat;
00134 }
00135 }