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_Category_Widget_Chooser extends Mage_Adminhtml_Block_Catalog_Category_Tree
35: {
36: protected $_selectedCategories = array();
37:
38: 39: 40: 41:
42: public function __construct()
43: {
44: parent::__construct();
45: $this->setTemplate('catalog/category/widget/tree.phtml');
46: $this->_withProductCount = false;
47: }
48:
49: 50: 51: 52: 53: 54:
55: public function setSelectedCategories($selectedCategories)
56: {
57: $this->_selectedCategories = $selectedCategories;
58: return $this;
59: }
60:
61: 62: 63: 64: 65:
66: public function getSelectedCategories()
67: {
68: return $this->_selectedCategories;
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function prepareElementHtml(Varien_Data_Form_Element_Abstract $element)
78: {
79: $uniqId = Mage::helper('core')->uniqHash($element->getId());
80: $sourceUrl = $this->getUrl('*/catalog_category_widget/chooser', array('uniq_id' => $uniqId, 'use_massaction' => false));
81:
82: $chooser = $this->getLayout()->createBlock('widget/adminhtml_widget_chooser')
83: ->setElement($element)
84: ->setTranslationHelper($this->getTranslationHelper())
85: ->setConfig($this->getConfig())
86: ->setFieldsetId($this->getFieldsetId())
87: ->setSourceUrl($sourceUrl)
88: ->setUniqId($uniqId);
89:
90: if ($element->getValue()) {
91: $value = explode('/', $element->getValue());
92: $categoryId = false;
93: if (isset($value[0]) && isset($value[1]) && $value[0] == 'category') {
94: $categoryId = $value[1];
95: }
96: if ($categoryId) {
97: $label = Mage::getSingleton('catalog/category')->load($categoryId)->getName();
98: $chooser->setLabel($label);
99: }
100: }
101:
102: $element->setData('after_element_html', $chooser->toHtml());
103: return $element;
104: }
105:
106: 107: 108: 109: 110:
111: public function getNodeClickListener()
112: {
113: if ($this->getData('node_click_listener')) {
114: return $this->getData('node_click_listener');
115: }
116: if ($this->getUseMassaction()) {
117: $js = '
118: function (node, e) {
119: if (node.ui.toggleCheck) {
120: node.ui.toggleCheck(true);
121: }
122: }
123: ';
124: } else {
125: $chooserJsObject = $this->getId();
126: $js = '
127: function (node, e) {
128: '.$chooserJsObject.'.setElementValue("category/" + node.attributes.id);
129: '.$chooserJsObject.'.setElementLabel(node.text);
130: '.$chooserJsObject.'.close();
131: }
132: ';
133: }
134: return $js;
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: protected function _getNodeJson($node, $level = 0)
145: {
146: $item = parent::_getNodeJson($node, $level);
147: if (in_array($node->getId(), $this->getSelectedCategories())) {
148: $item['checked'] = true;
149: }
150: $item['is_anchor'] = (int)$node->getIsAnchor();
151: $item['url_key'] = $node->getData('url_key');
152: return $item;
153: }
154:
155: 156: 157: 158: 159:
160: public function getCategoryCollection()
161: {
162: return parent::getCategoryCollection()->addAttributeToSelect('url_key')->addAttributeToSelect('is_anchor');
163: }
164:
165: 166: 167: 168: 169:
170: public function getLoadTreeUrl($expanded=null)
171: {
172: return $this->getUrl('*/catalog_category_widget/categoriesJson', array(
173: '_current'=>true,
174: 'uniq_id' => $this->getId(),
175: 'use_massaction' => $this->getUseMassaction()
176: ));
177: }
178: }
179: