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_Adminhtml
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Date range promo widget chooser
29: * Currently works without localized format
30: */
31: class Mage_Adminhtml_Block_Promo_Widget_Chooser_Daterange extends Mage_Adminhtml_Block_Abstract
32: {
33: /**
34: * HTML ID of the element that will obtain the joined chosen values
35: *
36: * @var string
37: */
38: protected $_targetElementId = '';
39:
40: /**
41: * From/To values to be rendered
42: *
43: * @var array
44: */
45: protected $_rangeValues = array('from' => '', 'to' => '');
46:
47: /**
48: * Range string delimiter for from/to dates
49: *
50: * @var string
51: */
52: protected $_rangeDelimiter = '...';
53:
54: /**
55: * Render the chooser HTML
56: * Target element should be set.
57: *
58: * @return string
59: */
60: protected function _toHtml()
61: {
62: if (empty($this->_targetElementId)) {
63: return '';
64: }
65:
66: $idSuffix = Mage::helper('core')->uniqHash();
67: $form = new Varien_Data_Form();
68: foreach (array(
69: 'from' => Mage::helper('adminhtml')->__('From'),
70: 'to' => Mage::helper('adminhtml')->__('To')) as $key => $label) {
71: $id = "{$key}_{$idSuffix}";
72: $element = new Varien_Data_Form_Element_Date(array(
73: 'format' => Varien_Date::DATE_INTERNAL_FORMAT, // hardcode because hardcoded values delimiter
74: 'label' => $label,
75: 'image' => $this->getSkinUrl('images/grid-cal.gif'),
76: 'onchange' => "dateTimeChoose_{$idSuffix}()", // won't work through Event.observe()
77: 'value' => $this->_rangeValues[$key],
78: ));
79: $element->setId($id);
80: $form->addElement($element);
81: }
82: return $form->toHtml() . "<script type=\"text/javascript\">
83: dateTimeChoose_{$idSuffix} = function() {
84: $('{$this->_targetElementId}').value = $('from_{$idSuffix}').value + '{$this->_rangeDelimiter}' + $('to_{$idSuffix}').value;
85: };
86: </script>";
87: }
88:
89: /**
90: * Target element ID setter
91: *
92: * @param string $value
93: * @return Mage_Adminhtml_Block_Promo_Widget_Chooser_Daterange
94: */
95: public function setTargetElementId($value)
96: {
97: $this->_targetElementId = trim($value);
98: return $this;
99: }
100:
101: /**
102: * Range values setter
103: *
104: * @param string $from
105: * @param string $to
106: * @return Mage_Adminhtml_Block_Promo_Widget_Chooser_Daterange
107: */
108: public function setRangeValues($from, $to)
109: {
110: $this->_rangeValues = array('from' => $from, 'to' => $to);
111: return $this;
112: }
113:
114: /**
115: * Range values setter, string implementation.
116: * Automatically attempts to split the string by delimiter
117: *
118: * @param string $delimitedString
119: * @return Mage_Adminhtml_Block_Promo_Widget_Chooser_Daterange
120: */
121: public function setRangeValue($delimitedString)
122: {
123: $split = explode($this->_rangeDelimiter, $delimitedString, 2);
124: $from = $split[0]; $to = '';
125: if (isset($split[1])) {
126: $to = $split[1];
127: }
128: return $this->setRangeValues($from, $to);
129: }
130:
131: /**
132: * Range delimiter setter
133: *
134: * @param string $value
135: * @return Mage_Adminhtml_Block_Promo_Widget_Chooser_Daterange
136: */
137: public function setRangeDelimiter($value)
138: {
139: $this->_rangeDelimiter = (string)$value;
140: return $this;
141: }
142: }
143: