Development

/plugins/sfFormExtraPlugin/lib/widget/sfWidgetFormJQueryDate.class.php

You must first sign up to be able to contribute.

root/plugins/sfFormExtraPlugin/lib/widget/sfWidgetFormJQueryDate.class.php

Revision 15844, 4.2 kB (checked in by fabien, 4 years ago)

[sfFormExtraPlugin] forced date format returned by the date picker (based on a patch from xdade - refs #5619)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormJQueryDate represents a date widget rendered by JQuery UI.
13  *
14  * This widget needs JQuery and JQuery UI to work.
15  *
16  * @package    symfony
17  * @subpackage widget
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfWidgetFormJQueryDate extends sfWidgetFormDate
22 {
23   /**
24    * Configures the current widget.
25    *
26    * Available options:
27    *
28    *  * image:   The image path to represent the widget (false by default)
29    *  * config:  A JavaScript array that configures the JQuery date widget
30    *  * culture: The user culture
31    *
32    * @param array $options     An array of options
33    * @param array $attributes  An array of default HTML attributes
34    *
35    * @see sfWidgetForm
36    */
37   protected function configure($options = array(), $attributes = array())
38   {
39     $this->addOption('image', false);
40     $this->addOption('config', '{}');
41     $this->addOption('culture', '');
42
43     parent::configure($options, $attributes);
44
45     if ('en' == $this->getOption('culture'))
46     {
47       $this->setOption('culture', 'en');
48     }
49   }
50
51   /**
52    * @param  string $name        The element name
53    * @param  string $value       The date displayed in this widget
54    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
55    * @param  array  $errors      An array of errors for the field
56    *
57    * @return string An HTML tag string
58    *
59    * @see sfWidgetForm
60    */
61   public function render($name, $value = null, $attributes = array(), $errors = array())
62   {
63     $prefix = $this->generateId($name);
64
65     $image = '';
66     if (false !== $this->getOption('image'))
67     {
68       $image = sprintf(', buttonImage: "%s", buttonImageOnly: true', $this->getOption('image'));
69     }
70
71     return parent::render($name, $value, $attributes, $errors).
72            $this->renderTag('input', array('type' => 'hidden', 'size' => 10, 'id' => $id = $this->generateId($name).'_jquery_control', 'disabled' => 'disabled')).
73            sprintf(<<<EOF
74 <script type="text/javascript">
75   function wfd_%s_read_linked()
76   {
77     jQuery("#%s").val(jQuery("#%s").val() + "/" + jQuery("#%s").val() + "/" + jQuery("#%s").val());
78
79     return {};
80   }
81
82   function wfd_%s_update_linked(date)
83   {
84     jQuery("#%s").val(date.substring(3, 5));
85     jQuery("#%s").val(date.substring(0, 2));
86     jQuery("#%s").val(date.substring(6, 10));
87   }
88
89   function wfd_%s_check_linked_days()
90   {
91     var daysInMonth = 32 - new Date(jQuery("#%s").val(), jQuery("#%s").val() - 1, 32).getDate();
92     jQuery("#%s option").attr("disabled", "");
93     jQuery("#%s option:gt(" + (%s) +")").attr("disabled", "disabled");
94
95     if (jQuery("#%s").val() > daysInMonth)
96     {
97       jQuery("#%s").val(daysInMonth);
98     }
99   }
100
101   jQuery(document).ready(function() {
102     jQuery("#%s").datepicker(jQuery.extend({}, {
103       minDate:    new Date(%s, 1 - 1, 1),
104       maxDate:    new Date(%s, 12 - 1, 31),
105       beforeShow: wfd_%s_read_linked,
106       onSelect:   wfd_%s_update_linked,
107       showOn:     "both"
108       %s
109     }, jQuery.datepicker.regional["%s"], %s, {dateFormat: "yy-mm-dd"}));
110   });
111
112   jQuery("#%s, #%s, #%s").change(wfd_%s_check_linked_days);
113 </script>
114 EOF
115       ,
116       $prefix, $id,
117       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'), $this->generateId($name.'[day]'),
118       $prefix,
119       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'), $this->generateId($name.'[day]'),
120       $prefix,
121       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'),
122       $this->generateId($name.'[day]'), $this->generateId($name.'[day]'),
123       ($this->getOption('can_be_empty') ? 'daysInMonth' : 'daysInMonth - 1'),
124       $this->generateId($name.'[day]'), $this->generateId($name.'[day]'),
125       $id,
126       min($this->getOption('years')), max($this->getOption('years')),
127       $prefix, $prefix, $image, $this->getOption('culture'), $this->getOption('config'),
128       $this->generateId($name.'[day]'), $this->generateId($name.'[month]'), $this->generateId($name.'[year]'),
129       $prefix
130     );
131   }
132 }
133
Note: See TracBrowser for help on using the browser.