1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34:
35: class Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Date extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Abstract
36: {
37: protected $_locale;
38:
39: protected function _prepareLayout()
40: {
41: if ($head = $this->getLayout()->getBlock('head')) {
42: $head->setCanLoadCalendarJs(true);
43: }
44: return $this;
45: }
46:
47: public function getHtml()
48: {
49: $htmlId = $this->_getHtmlId() . microtime(true);
50: $format = $this->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
51: $html = '<div class="range"><div class="range-line date">
52: <span class="label">' . Mage::helper('adminhtml')->__('From').':</span>
53: <input type="text" name="'.$this->_getHtmlName().'[from]" id="'.$htmlId.'_from" value="'.$this->getEscapedValue('from').'" class="input-text no-changes"/>
54: <img src="' . Mage::getDesign()->getSkinUrl('images/grid-cal.gif') . '" alt="" class="v-middle" id="'.$htmlId.'_from_trig" title="'.$this->htmlEscape(Mage::helper('adminhtml')->__('Date selector')).'"/>
55: </div>';
56: $html.= '<div class="range-line date">
57: <span class="label">' . Mage::helper('adminhtml')->__('To').' :</span>
58: <input type="text" name="'.$this->_getHtmlName().'[to]" id="'.$htmlId.'_to" value="'.$this->getEscapedValue('to').'" class="input-text no-changes"/>
59: <img src="' . Mage::getDesign()->getSkinUrl('images/grid-cal.gif') . '" alt="" class="v-middle" id="'.$htmlId.'_to_trig" title="'.$this->htmlEscape(Mage::helper('adminhtml')->__('Date selector')).'"/>
60: </div></div>';
61: $html.= '<input type="hidden" name="'.$this->_getHtmlName().'[locale]" value="'.$this->getLocale()->getLocaleCode().'"/>';
62: $html.= '<script type="text/javascript">
63: Calendar.setup({
64: inputField : "'.$htmlId.'_from",
65: ifFormat : "'.$format.'",
66: button : "'.$htmlId.'_from_trig",
67: align : "Bl",
68: singleClick : true
69: });
70: Calendar.setup({
71: inputField : "'.$htmlId.'_to",
72: ifFormat : "'.$format.'",
73: button : "'.$htmlId.'_to_trig",
74: align : "Bl",
75: singleClick : true
76: });
77:
78: $("'.$htmlId.'_to_trig").observe("click", showCalendar);
79: $("'.$htmlId.'_from_trig").observe("click", showCalendar);
80:
81: function showCalendar(event){
82: var element = event.element(event);
83: var offset = $(element).viewportOffset();
84: var scrollOffset = $(element).cumulativeScrollOffset();
85: var dimensionsButton = $(element).getDimensions();
86: var index = $("widget-chooser").getStyle("zIndex");
87:
88: $$("div.calendar").each(function(item){
89: if ($(item).visible()) {
90: var dimensionsCalendar = $(item).getDimensions();
91:
92: $(item).setStyle({
93: "zIndex" : index + 1,
94: "left" : offset[0] + scrollOffset[0] - dimensionsCalendar.width + dimensionsButton.width + "px",
95: "top" : offset[1] + scrollOffset[1] + dimensionsButton.height + "px"
96: });
97: };
98: });
99: };
100: </script>';
101: return $html;
102: }
103:
104: public function getEscapedValue($index=null)
105: {
106: $value = $this->getValue($index);
107: if ($value instanceof Zend_Date) {
108: return $value->toString($this->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
109: }
110: return $value;
111: }
112:
113: public function getValue($index=null)
114: {
115: if ($index) {
116: if ($data = $this->getData('value', 'orig_' . $index)) {
117: return $data;
118: }
119: return null;
120: }
121: $value = $this->getData('value');
122: if (is_array($value)) {
123: $value['date'] = true;
124: }
125: return $value;
126: }
127:
128: public function getCondition()
129: {
130: $value = $this->getValue();
131:
132: return $value;
133: }
134:
135: public function setValue($value)
136: {
137: if (isset($value['locale'])) {
138: if (!empty($value['from'])) {
139: $value['orig_from'] = $value['from'];
140: $value['from'] = $this->_convertDate($value['from'], $value['locale']);
141: }
142: if (!empty($value['to'])) {
143: $value['orig_to'] = $value['to'];
144: $value['to'] = $this->_convertDate($value['to'], $value['locale']);
145: }
146: }
147: if (empty($value['from']) && empty($value['to'])) {
148: $value = null;
149: }
150: $this->setData('value', $value);
151: return $this;
152: }
153:
154: 155: 156: 157: 158:
159: public function getLocale()
160: {
161: if (!$this->_locale) {
162: $this->_locale = Mage::app()->getLocale();
163: }
164: return $this->_locale;
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: protected function _convertDate($date, $locale)
175: {
176: try {
177: $dateObj = $this->getLocale()->date(null, null, $locale, false);
178:
179:
180: $dateObj->setTimezone(Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE));
181:
182:
183: $dateObj->setHour(00);
184: $dateObj->setMinute(00);
185: $dateObj->setSecond(00);
186:
187:
188: $dateObj->set($date, Zend_Date::DATE_SHORT, $locale);
189:
190:
191: $dateObj->setTimezone(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
192:
193: return $dateObj;
194: }
195: catch (Exception $e) {
196: return null;
197: }
198: }
199: }
200: