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_Catalog_Product_Widget_Chooser extends Mage_Adminhtml_Block_Widget_Grid
35: {
36: protected $_selectedProducts = array();
37:
38: 39: 40: 41: 42:
43: public function __construct($arguments=array())
44: {
45: parent::__construct($arguments);
46: $this->setDefaultSort('name');
47: $this->setUseAjax(true);
48: }
49:
50: 51: 52: 53: 54: 55:
56: public function prepareElementHtml(Varien_Data_Form_Element_Abstract $element)
57: {
58: $uniqId = Mage::helper('core')->uniqHash($element->getId());
59: $sourceUrl = $this->getUrl('*/catalog_product_widget/chooser', array(
60: 'uniq_id' => $uniqId,
61: 'use_massaction' => false,
62: ));
63:
64: $chooser = $this->getLayout()->createBlock('widget/adminhtml_widget_chooser')
65: ->setElement($element)
66: ->setTranslationHelper($this->getTranslationHelper())
67: ->setConfig($this->getConfig())
68: ->setFieldsetId($this->getFieldsetId())
69: ->setSourceUrl($sourceUrl)
70: ->setUniqId($uniqId);
71:
72: if ($element->getValue()) {
73: $value = explode('/', $element->getValue());
74: $productId = false;
75: if (isset($value[0]) && isset($value[1]) && $value[0] == 'product') {
76: $productId = $value[1];
77: }
78: $categoryId = isset($value[2]) ? $value[2] : false;
79: $label = '';
80: if ($categoryId) {
81: $label = Mage::getResourceSingleton('catalog/category')
82: ->getAttributeRawValue($categoryId, 'name', Mage::app()->getStore()) . '/';
83: }
84: if ($productId) {
85: $label .= Mage::getResourceSingleton('catalog/product')
86: ->getAttributeRawValue($productId, 'name', Mage::app()->getStore());
87: }
88: $chooser->setLabel($label);
89: }
90:
91: $element->setData('after_element_html', $chooser->toHtml());
92: return $element;
93: }
94:
95: 96: 97: 98: 99:
100: public function getCheckboxCheckCallback()
101: {
102: if ($this->getUseMassaction()) {
103: return "function (grid, element) {
104: $(grid.containerId).fire('product:changed', {element: element});
105: }";
106: }
107: }
108:
109: 110: 111: 112: 113:
114: public function getRowClickCallback()
115: {
116: if (!$this->getUseMassaction()) {
117: $chooserJsObject = $this->getId();
118: return '
119: function (grid, event) {
120: var trElement = Event.findElement(event, "tr");
121: var productId = trElement.down("td").innerHTML;
122: var productName = trElement.down("td").next().next().innerHTML;
123: var optionLabel = productName;
124: var optionValue = "product/" + productId.replace(/^\s+|\s+$/g,"");
125: if (grid.categoryId) {
126: optionValue += "/" + grid.categoryId;
127: }
128: if (grid.categoryName) {
129: optionLabel = grid.categoryName + " / " + optionLabel;
130: }
131: '.$chooserJsObject.'.setElementValue(optionValue);
132: '.$chooserJsObject.'.setElementLabel(optionLabel);
133: '.$chooserJsObject.'.close();
134: }
135: ';
136: }
137: }
138:
139: 140: 141: 142: 143:
144: public function getCategoryClickListenerJs()
145: {
146: $js = '
147: function (node, e) {
148: {jsObject}.addVarToUrl("category_id", node.attributes.id);
149: {jsObject}.reload({jsObject}.url);
150: {jsObject}.categoryId = node.attributes.id != "none" ? node.attributes.id : false;
151: {jsObject}.categoryName = node.attributes.id != "none" ? node.text : false;
152: }
153: ';
154: $js = str_replace('{jsObject}', $this->getJsObjectName(), $js);
155: return $js;
156: }
157:
158: 159: 160: 161: 162: 163:
164: protected function _addColumnFilterToCollection($column)
165: {
166: if ($column->getId() == 'in_products') {
167: $selected = $this->getSelectedProducts();
168: if ($column->getFilter()->getValue()) {
169: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$selected));
170: } else {
171: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$selected));
172: }
173: } else {
174: parent::_addColumnFilterToCollection($column);
175: }
176: return $this;
177: }
178:
179: 180: 181: 182: 183:
184: protected function _prepareCollection()
185: {
186:
187: $collection = Mage::getResourceModel('catalog/product_collection')
188: ->setStoreId(0)
189: ->addAttributeToSelect('name');
190:
191: if ($categoryId = $this->getCategoryId()) {
192: $category = Mage::getModel('catalog/category')->load($categoryId);
193: if ($category->getId()) {
194:
195: $productIds = $category->getProductsPosition();
196: $productIds = array_keys($productIds);
197: if (empty($productIds)) {
198: $productIds = 0;
199: }
200: $collection->addFieldToFilter('entity_id', array('in' => $productIds));
201: }
202: }
203:
204: if ($productTypeId = $this->getProductTypeId()) {
205: $collection->addAttributeToFilter('type_id', $productTypeId);
206: }
207:
208: $this->setCollection($collection);
209: return parent::_prepareCollection();
210: }
211:
212: 213: 214: 215: 216:
217: protected function _prepareColumns()
218: {
219: if ($this->getUseMassaction()) {
220: $this->addColumn('in_products', array(
221: 'header_css_class' => 'a-center',
222: 'type' => 'checkbox',
223: 'name' => 'in_products',
224: 'inline_css' => 'checkbox entities',
225: 'field_name' => 'in_products',
226: 'values' => $this->getSelectedProducts(),
227: 'align' => 'center',
228: 'index' => 'entity_id',
229: 'use_index' => true,
230: ));
231: }
232:
233: $this->addColumn('entity_id', array(
234: 'header' => Mage::helper('catalog')->__('ID'),
235: 'sortable' => true,
236: 'width' => '60px',
237: 'index' => 'entity_id'
238: ));
239: $this->addColumn('chooser_sku', array(
240: 'header' => Mage::helper('catalog')->__('SKU'),
241: 'name' => 'chooser_sku',
242: 'width' => '80px',
243: 'index' => 'sku'
244: ));
245: $this->addColumn('chooser_name', array(
246: 'header' => Mage::helper('catalog')->__('Product Name'),
247: 'name' => 'chooser_name',
248: 'index' => 'name'
249: ));
250:
251: return parent::_prepareColumns();
252: }
253:
254: 255: 256: 257: 258:
259: public function getGridUrl()
260: {
261: return $this->getUrl('*/catalog_product_widget/chooser', array(
262: 'products_grid' => true,
263: '_current' => true,
264: 'uniq_id' => $this->getId(),
265: 'use_massaction' => $this->getUseMassaction(),
266: 'product_type_id' => $this->getProductTypeId()
267: ));
268: }
269:
270: 271: 272: 273: 274: 275:
276: public function setSelectedProducts($selectedProducts)
277: {
278: $this->_selectedProducts = $selectedProducts;
279: return $this;
280: }
281:
282: 283: 284: 285: 286:
287: public function getSelectedProducts()
288: {
289: if ($selectedProducts = $this->getRequest()->getParam('selected_products', null)) {
290: $this->setSelectedProducts($selectedProducts);
291: }
292: return $this->_selectedProducts;
293: }
294: }
295: