/app/code/core/Mage/Backend/Block/Widget/Grid/Column/Filter/Date.php

https://bitbucket.org/sunil_nextbits/magento2 · PHP · 179 lines · 116 code · 15 blank · 48 comment · 11 complexity · b83df8be504191bdeada8e888a44e8bc MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Backend
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Date grid column filter
  28. *
  29. * @category Mage
  30. * @package Mage_Backend
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. * @todo date format
  33. */
  34. class Mage_Backend_Block_Widget_Grid_Column_Filter_Date extends Mage_Backend_Block_Widget_Grid_Column_Filter_Abstract
  35. {
  36. protected $_locale;
  37. protected function _prepareLayout()
  38. {
  39. if ($head = $this->getLayout()->getBlock('head')) {
  40. $head->setCanLoadCalendarJs(true);
  41. }
  42. return $this;
  43. }
  44. public function getHtml()
  45. {
  46. $htmlId = Mage::helper('Mage_Core_Helper_Data')->uniqHash($this->_getHtmlId());
  47. $format = $this->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  48. $html = '<div class="range" id="' . $htmlId . '_range"><div class="range-line date">'
  49. . '<span class="label">' . $this->__('From') . ':</span>'
  50. . '<input type="text" name="' . $this->_getHtmlName() . '[from]" id="' . $htmlId . '_from"'
  51. . ' value="' . $this->getEscapedValue('from') . '" class="input-text no-changes" '
  52. . $this->getUiId('filter', $this->_getHtmlName(), 'from') . '/>'
  53. . '</div>';
  54. $html.= '<div class="range-line date">'
  55. . '<span class="label">' . $this->__('To') . ' :</span>'
  56. . '<input type="text" name="' . $this->_getHtmlName() . '[to]" id="' . $htmlId . '_to"'
  57. . ' value="' . $this->getEscapedValue('to') . '" class="input-text no-changes" '
  58. . $this->getUiId('filter', $this->_getHtmlName(), 'to') . '/>'
  59. . '</div></div>';
  60. $html .= '<input type="hidden" name="'.$this->_getHtmlName() . '[locale]"'
  61. . ' value="' . $this->getLocale()->getLocaleCode() . '"/>';
  62. $html .= '<script type="text/javascript">
  63. (function( $ ) {
  64. $("#' . $htmlId . '_range").dateRange({
  65. dateFormat: "' . $format . '",
  66. buttonImage: "' . $this->getViewFileUrl('images/grid-cal.gif') . '",
  67. buttonText: "' . $this->escapeHtml($this->__('Date selector')) . '",
  68. from: {
  69. id: "' . $htmlId . '_from"
  70. },
  71. to: {
  72. id: "' . $htmlId . '_to"
  73. }
  74. })
  75. })(jQuery)
  76. </script>';
  77. return $html;
  78. }
  79. public function getEscapedValue($index=null)
  80. {
  81. $value = $this->getValue($index);
  82. if ($value instanceof Zend_Date) {
  83. return $value->toString($this->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
  84. }
  85. return $value;
  86. }
  87. public function getValue($index=null)
  88. {
  89. if ($index) {
  90. if ($data = $this->getData('value', 'orig_' . $index)) {
  91. return $data;//date('Y-m-d', strtotime($data));
  92. }
  93. return null;
  94. }
  95. $value = $this->getData('value');
  96. if (is_array($value)) {
  97. $value['date'] = true;
  98. }
  99. return $value;
  100. }
  101. public function getCondition()
  102. {
  103. $value = $this->getValue();
  104. return $value;
  105. }
  106. public function setValue($value)
  107. {
  108. if (isset($value['locale'])) {
  109. if (!empty($value['from'])) {
  110. $value['orig_from'] = $value['from'];
  111. $value['from'] = $this->_convertDate($value['from'], $value['locale']);
  112. }
  113. if (!empty($value['to'])) {
  114. $value['orig_to'] = $value['to'];
  115. $value['to'] = $this->_convertDate($value['to'], $value['locale']);
  116. }
  117. }
  118. if (empty($value['from']) && empty($value['to'])) {
  119. $value = null;
  120. }
  121. $this->setData('value', $value);
  122. return $this;
  123. }
  124. /**
  125. * Retrieve locale
  126. *
  127. * @return Mage_Core_Model_Locale
  128. */
  129. public function getLocale()
  130. {
  131. if (!$this->_locale) {
  132. $this->_locale = Mage::app()->getLocale();
  133. }
  134. return $this->_locale;
  135. }
  136. /**
  137. * Convert given date to default (UTC) timezone
  138. *
  139. * @param string $date
  140. * @param string $locale
  141. * @return Zend_Date
  142. */
  143. protected function _convertDate($date, $locale)
  144. {
  145. try {
  146. $dateObj = $this->getLocale()->date(null, null, $locale, false);
  147. //set default timezone for store (admin)
  148. $dateObj->setTimezone(
  149. Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE)
  150. );
  151. //set begining of day
  152. $dateObj->setHour(00);
  153. $dateObj->setMinute(00);
  154. $dateObj->setSecond(00);
  155. //set date with applying timezone of store
  156. $dateObj->set($date, Zend_Date::DATE_SHORT, $locale);
  157. //convert store date to default date in UTC timezone without DST
  158. $dateObj->setTimezone(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
  159. return $dateObj;
  160. } catch (Exception $e) {
  161. return null;
  162. }
  163. }
  164. }