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: class Mage_Adminhtml_Block_Report_Filter_Form extends Mage_Adminhtml_Block_Widget_Form
35: {
36: 37: 38:
39: protected $_reportTypeOptions = array();
40:
41: 42: 43:
44: protected $_fieldVisibility = array();
45:
46: 47: 48:
49: protected $_fieldOptions = array();
50:
51: 52: 53: 54: 55: 56:
57: public function setFieldVisibility($fieldId, $visibility)
58: {
59: $this->_fieldVisibility[$fieldId] = (bool)$visibility;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function getFieldVisibility($fieldId, $defaultVisibility = true)
70: {
71: if (!array_key_exists($fieldId, $this->_fieldVisibility)) {
72: return $defaultVisibility;
73: }
74: return $this->_fieldVisibility[$fieldId];
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: public function setFieldOption($fieldId, $option, $value = null)
85: {
86: if (is_array($option)) {
87: $options = $option;
88: } else {
89: $options = array($option => $value);
90: }
91: if (!array_key_exists($fieldId, $this->_fieldOptions)) {
92: $this->_fieldOptions[$fieldId] = array();
93: }
94: foreach ($options as $k => $v) {
95: $this->_fieldOptions[$fieldId][$k] = $v;
96: }
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: public function addReportTypeOption($key, $value)
107: {
108: $this->_reportTypeOptions[$key] = $this->__($value);
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: protected function _prepareForm()
118: {
119: $actionUrl = $this->getUrl('*/*/sales');
120: $form = new Varien_Data_Form(
121: array('id' => 'filter_form', 'action' => $actionUrl, 'method' => 'get')
122: );
123: $htmlIdPrefix = 'sales_report_';
124: $form->setHtmlIdPrefix($htmlIdPrefix);
125: $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('reports')->__('Filter')));
126:
127: $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
128:
129: $fieldset->addField('store_ids', 'hidden', array(
130: 'name' => 'store_ids'
131: ));
132:
133: $fieldset->addField('report_type', 'select', array(
134: 'name' => 'report_type',
135: 'options' => $this->_reportTypeOptions,
136: 'label' => Mage::helper('reports')->__('Match Period To'),
137: ));
138:
139: $fieldset->addField('period_type', 'select', array(
140: 'name' => 'period_type',
141: 'options' => array(
142: 'day' => Mage::helper('reports')->__('Day'),
143: 'month' => Mage::helper('reports')->__('Month'),
144: 'year' => Mage::helper('reports')->__('Year')
145: ),
146: 'label' => Mage::helper('reports')->__('Period'),
147: 'title' => Mage::helper('reports')->__('Period')
148: ));
149:
150: $fieldset->addField('from', 'date', array(
151: 'name' => 'from',
152: 'format' => $dateFormatIso,
153: 'image' => $this->getSkinUrl('images/grid-cal.gif'),
154: 'label' => Mage::helper('reports')->__('From'),
155: 'title' => Mage::helper('reports')->__('From'),
156: 'required' => true
157: ));
158:
159: $fieldset->addField('to', 'date', array(
160: 'name' => 'to',
161: 'format' => $dateFormatIso,
162: 'image' => $this->getSkinUrl('images/grid-cal.gif'),
163: 'label' => Mage::helper('reports')->__('To'),
164: 'title' => Mage::helper('reports')->__('To'),
165: 'required' => true
166: ));
167:
168: $fieldset->addField('show_empty_rows', 'select', array(
169: 'name' => 'show_empty_rows',
170: 'options' => array(
171: '1' => Mage::helper('reports')->__('Yes'),
172: '0' => Mage::helper('reports')->__('No')
173: ),
174: 'label' => Mage::helper('reports')->__('Empty Rows'),
175: 'title' => Mage::helper('reports')->__('Empty Rows')
176: ));
177:
178: $form->setUseContainer(true);
179: $this->setForm($form);
180:
181: return parent::_prepareForm();
182: }
183:
184: 185: 186: 187: 188: 189:
190: protected function _initFormValues()
191: {
192: $data = $this->getFilterData()->getData();
193: foreach ($data as $key => $value) {
194: if (is_array($value) && isset($value[0])) {
195: $data[$key] = explode(',', $value[0]);
196: }
197: }
198: $this->getForm()->addValues($data);
199: return parent::_initFormValues();
200: }
201:
202: 203: 204: 205: 206:
207: protected function _beforeToHtml()
208: {
209: $result = parent::_beforeToHtml();
210:
211:
212: $fieldset = $this->getForm()->getElement('base_fieldset');
213:
214: if (is_object($fieldset) && $fieldset instanceof Varien_Data_Form_Element_Fieldset) {
215:
216: foreach ($fieldset->getElements() as $field) {
217: if (!$this->getFieldVisibility($field->getId())) {
218: $fieldset->removeField($field->getId());
219: }
220: }
221:
222: foreach ($this->_fieldOptions as $fieldId => $fieldOptions) {
223: $field = $fieldset->getElements()->searchById($fieldId);
224:
225: if ($field) {
226: foreach ($fieldOptions as $k => $v) {
227: $field->setDataUsingMethod($k, $v);
228: }
229: }
230: }
231: }
232:
233: return $result;
234: }
235: }
236: