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_XmlConnect_Block_Catalog_Search extends Mage_XmlConnect_Block_Catalog
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _toHtml()
44: {
45: $searchXmlObject = Mage::getModel('xmlconnect/simplexml_element', '<search></search>');
46: $filtersXmlObject = Mage::getModel('xmlconnect/simplexml_element', '<filters></filters>');
47:
48: $helper = Mage::helper('catalogsearch');
49: if (method_exists($helper, 'getEngine')) {
50: $engine = Mage::helper('catalogsearch')->getEngine();
51: if ($engine instanceof Varien_Object) {
52: $isLayeredNavigationAllowed = $engine->isLeyeredNavigationAllowed();
53: } else {
54: $isLayeredNavigationAllowed = true;
55: }
56: } else {
57: $isLayeredNavigationAllowed = true;
58: }
59:
60: $hasMoreProductItems = 0;
61:
62: 63: 64:
65: $productListBlock = $this->getChild('product_list');
66: if ($productListBlock) {
67: $layer = Mage::getSingleton('catalogsearch/layer');
68: $productsXmlObj = $productListBlock->setLayer($layer)
69: ->setNeedBlockApplyingFilters(!$isLayeredNavigationAllowed)->getProductsXmlObject();
70: $searchXmlObject->appendChild($productsXmlObj);
71: $hasMoreProductItems = (int)$productListBlock->getHasProductItems();
72: }
73:
74: $searchXmlObject->addAttribute('has_more_items', $hasMoreProductItems);
75:
76: 77: 78:
79: $showFiltersAndOrders = (bool) count($productsXmlObj);
80: $requestParams = $this->getRequest()->getParams();
81: foreach ($requestParams as $key => $value) {
82: if (0 === strpos($key, parent::REQUEST_SORT_ORDER_PARAM_REFIX)
83: || 0 === strpos($key, parent::REQUEST_FILTER_PARAM_REFIX)
84: ) {
85: $showFiltersAndOrders = false;
86: break;
87: }
88: }
89: if ($isLayeredNavigationAllowed && $productListBlock && $showFiltersAndOrders) {
90: $filters = $productListBlock->getCollectedFilters();
91: 92: 93:
94: foreach ($filters as $filter) {
95: if (!$this->_isFilterItemsHasValues($filter)) {
96: continue;
97: }
98: $item = $filtersXmlObject->addChild('item');
99: $item->addChild('name', $searchXmlObject->escapeXml($filter->getName()));
100: $item->addChild('code', $filter->getRequestVar());
101: $values = $item->addChild('values');
102:
103: foreach ($filter->getItems() as $valueItem) {
104: $count = (int)$valueItem->getCount();
105: if (!$count) {
106: continue;
107: }
108: $value = $values->addChild('value');
109: $value->addChild('id', $valueItem->getValueString());
110: $value->addChild('label', $searchXmlObject->escapeXml($valueItem->getLabel()));
111: $value->addChild('count', $count);
112: }
113: }
114: $searchXmlObject->appendChild($filtersXmlObject);
115: }
116:
117: 118: 119:
120: if ($showFiltersAndOrders) {
121: $searchXmlObject->appendChild($this->getProductSortFeildsXmlObject());
122: }
123:
124: return $searchXmlObject->asNiceXml();
125: }
126:
127: 128: 129: 130: 131: 132:
133: protected function _isFilterItemsHasValues($filter)
134: {
135: if (!$filter->getItemsCount()) {
136: return false;
137: }
138: foreach ($filter->getItems() as $valueItem) {
139: if ((int)$valueItem->getCount()) {
140: return true;
141: }
142: }
143: return false;
144: }
145: }
146: