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: class Mage_Adminhtml_Block_Promo_Widget_Chooser extends Mage_Adminhtml_Block_Widget_Grid
31: {
32: 33: 34: 35: 36:
37: public function __construct($arguments=array())
38: {
39: parent::__construct($arguments);
40: $this->setDefaultSort('rule_id');
41: $this->setDefaultDir('ASC');
42: $this->setUseAjax(true);
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function prepareElementHtml(Varien_Data_Form_Element_Abstract $element)
52: {
53: $uniqId = Mage::helper('core')->uniqHash($element->getId());
54: $sourceUrl = $this->getUrl('*/promo_quote/chooser', array('uniq_id' => $uniqId));
55:
56: $chooser = $this->getLayout()->createBlock('widget/adminhtml_widget_chooser')
57: ->setElement($element)
58: ->setTranslationHelper($this->getTranslationHelper())
59: ->setConfig($this->getConfig())
60: ->setFieldsetId($this->getFieldsetId())
61: ->setSourceUrl($sourceUrl)
62: ->setUniqId($uniqId);
63:
64: if ($element->getValue()) {
65: $rule = Mage::getModel('salesrule/rule')->load((int)$element->getValue());
66: if ($rule->getId()) {
67: $chooser->setLabel($rule->getName());
68: }
69: }
70:
71: $element->setData('after_element_html', $chooser->toHtml());
72: return $element;
73: }
74:
75: 76: 77: 78: 79:
80: public function getRowClickCallback()
81: {
82: $chooserJsObject = $this->getId();
83: $js = '
84: function (grid, event) {
85: var trElement = Event.findElement(event, "tr");
86: var ruleName = trElement.down("td").next().innerHTML;
87: var ruleId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
88: '.$chooserJsObject.'.setElementValue(ruleId);
89: '.$chooserJsObject.'.setElementLabel(ruleName);
90: '.$chooserJsObject.'.close();
91: }
92: ';
93: return $js;
94: }
95:
96: 97: 98: 99: 100:
101: protected function _prepareCollection()
102: {
103: $collection = Mage::getModel('salesrule/rule')->getResourceCollection();
104: $this->setCollection($collection);
105:
106: Mage::dispatchEvent('adminhtml_block_promo_widget_chooser_prepare_collection', array(
107: 'collection' => $collection
108: ));
109:
110: return parent::_prepareCollection();
111: }
112:
113: 114: 115: 116: 117:
118: protected function _prepareColumns()
119: {
120: $this->addColumn('rule_id', array(
121: 'header' => Mage::helper('salesrule')->__('ID'),
122: 'align' => 'right',
123: 'width' => '50px',
124: 'index' => 'rule_id',
125: ));
126:
127: $this->addColumn('name', array(
128: 'header' => Mage::helper('salesrule')->__('Rule Name'),
129: 'align' => 'left',
130: 'index' => 'name',
131: ));
132:
133: $this->addColumn('coupon_code', array(
134: 'header' => Mage::helper('salesrule')->__('Coupon Code'),
135: 'align' => 'left',
136: 'width' => '150px',
137: 'index' => 'code',
138: ));
139:
140: $this->addColumn('from_date', array(
141: 'header' => Mage::helper('salesrule')->__('Date Start'),
142: 'align' => 'left',
143: 'width' => '120px',
144: 'type' => 'date',
145: 'index' => 'from_date',
146: ));
147:
148: $this->addColumn('to_date', array(
149: 'header' => Mage::helper('salesrule')->__('Date Expire'),
150: 'align' => 'left',
151: 'width' => '120px',
152: 'type' => 'date',
153: 'default' => '--',
154: 'index' => 'to_date',
155: ));
156:
157: $this->addColumn('is_active', array(
158: 'header' => Mage::helper('salesrule')->__('Status'),
159: 'align' => 'left',
160: 'width' => '80px',
161: 'index' => 'is_active',
162: 'type' => 'options',
163: 'options' => array(
164: 1 => 'Active',
165: 0 => 'Inactive',
166: ),
167: ));
168:
169: return parent::_prepareColumns();
170: }
171:
172: 173: 174: 175: 176:
177: public function getGridUrl()
178: {
179: return $this->getUrl('*/promo_quote/chooser', array('_current' => true));
180: }
181: }
182: