Development

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

You must first sign up to be able to contribute.

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

Revision 30755, 4.8 kB (checked in by fabien, 2 years ago)

[sfFormExtraPlugin] fixed sfWidgetFormJQueryDate doesn't refresh disabled after the date was chosen from calendar (closes #8817, patch from foboskilla)

  • 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 sfWidgetForm
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    *  * date_widget: The date widget instance to use as a "base" class
32    *
33    * @param array $options     An array of options
34    * @param array $attributes  An array of default HTML attributes
35    *
36    * @see sfWidgetForm
37    */
38   protected function configure($options = array(), $attributes = array())
39   {
40     $this->addOption('image', false);
41     $this->addOption('config', '{}');
42     $this->addOption('culture', '');
43     $this->addOption('date_widget', new sfWidgetFormDate());
44
45     parent::configure($options, $attributes);
46
47     if ('en' == $this->getOption('culture'))
48     {
49       $this->setOption('culture', 'en');
50     }
51   }
52
53   /**
54    * @param  string $name        The element name
55    * @param  string $value       The date displayed in this widget
56    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
57    * @param  array  $errors      An array of errors for the field
58    *
59    * @return string An HTML tag string
60    *
61    * @see sfWidgetForm
62    */
63   public function render($name, $value = null, $attributes = array(), $errors = array())
64   {
65     $prefix = str_replace('-', '_', $this->generateId($name));
66
67     $image = '';
68     if (false !== $this->getOption('image'))
69     {
70       $image = sprintf(', buttonImage: "%s", buttonImageOnly: true', $this->getOption('image'));
71     }
72
73     if ($this->getOption('date_widget') instanceof sfWidgetFormDateTime)
74     {
75       $years = $this->getOption('date_widget')->getDateWidget()->getOption('years');
76     }
77     else
78     {
79       $years = $this->getOption('date_widget')->getOption('years');
80     }
81
82     return $this->getOption('date_widget')->render($name, $value, $attributes, $errors).
83            $this->renderTag('input', array('type' => 'hidden', 'size' => 10, 'id' => $id = $this->generateId($name).'_jquery_control', 'disabled' => 'disabled')).
84            sprintf(<<<EOF
85 <script type="text/javascript">
86   function wfd_%s_read_linked()
87   {
88     jQuery("#%s").val(jQuery("#%s").val() + "-" + jQuery("#%s").val() + "-" + jQuery("#%s").val());
89
90     return {};
91   }
92
93   function wfd_%s_update_linked(date)
94   {
95     jQuery("#%s").val(parseInt(date.substring(0, 4), 10));
96     jQuery("#%s").val(parseInt(date.substring(5, 7), 10));
97     jQuery("#%s").val(parseInt(date.substring(8), 10));
98
99     wfd_%s_check_linked_days();
100   }
101
102   function wfd_%s_check_linked_days()
103   {
104     var daysInMonth = 32 - new Date(jQuery("#%s").val(), jQuery("#%s").val() - 1, 32).getDate();
105
106     jQuery("#%s option").attr("disabled", "");
107     jQuery("#%s option:gt(" + (%s) +")").attr("disabled", "disabled");
108
109     if (jQuery("#%s").val() > daysInMonth)
110     {
111       jQuery("#%s").val(daysInMonth);
112     }
113   }
114
115   jQuery(document).ready(function() {
116     jQuery("#%s").datepicker(jQuery.extend({}, {
117       minDate:    new Date(%s, 1 - 1, 1),
118       maxDate:    new Date(%s, 12 - 1, 31),
119       beforeShow: wfd_%s_read_linked,
120       onSelect:   wfd_%s_update_linked,
121       showOn:     "button"
122       %s
123     }, jQuery.datepicker.regional["%s"], %s, {dateFormat: "yy-mm-dd"}));
124     wfd_%s_check_linked_days();
125   });
126
127   jQuery("#%s, #%s, #%s").change(wfd_%s_check_linked_days);
128 </script>
129 EOF
130       ,
131       $prefix, $id,
132       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'), $this->generateId($name.'[day]'),
133       $prefix,
134       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'), $this->generateId($name.'[day]'),
135       $prefix, $prefix,
136       $this->generateId($name.'[year]'), $this->generateId($name.'[month]'),
137       $this->generateId($name.'[day]'), $this->generateId($name.'[day]'),
138       ($this->getOption('date_widget')->getOption('can_be_empty') ? 'daysInMonth' : 'daysInMonth - 1'),
139       $this->generateId($name.'[day]'), $this->generateId($name.'[day]'),
140       $id,
141       min($years), max($years),
142       $prefix, $prefix, $image, $this->getOption('culture'), $this->getOption('config'),
143       $prefix,
144       $this->generateId($name.'[day]'), $this->generateId($name.'[month]'), $this->generateId($name.'[year]'),
145       $prefix
146     );
147   }
148 }
149
Note: See TracBrowser for help on using the browser.