/app/code/core/Mage/XmlConnect/Block/Catalog/Search.php

https://bitbucket.org/acidel/buykoala · PHP · 147 lines · 81 code · 10 blank · 56 comment · 15 complexity · dedad7216eb9cf511320579e15b96cf7 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Product search results renderer
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Catalog_Search extends Mage_XmlConnect_Block_Catalog
  34. {
  35. /**
  36. * Search results xml renderer
  37. * XML also contains filters that can be apply (accorfingly already applyed filters
  38. * and search query) and sort fields
  39. *
  40. * @return string
  41. */
  42. protected function _toHtml()
  43. {
  44. $searchXmlObject = Mage::getModel('xmlconnect/simplexml_element', '<search></search>');
  45. $filtersXmlObject = Mage::getModel('xmlconnect/simplexml_element', '<filters></filters>');
  46. $helper = Mage::helper('catalogsearch');
  47. if (method_exists($helper, 'getEngine')) {
  48. $engine = Mage::helper('catalogsearch')->getEngine();
  49. if ($engine instanceof Varien_Object) {
  50. $isLayeredNavigationAllowed = $engine->isLeyeredNavigationAllowed();
  51. } else {
  52. $isLayeredNavigationAllowed = true;
  53. }
  54. } else {
  55. $isLayeredNavigationAllowed = true;
  56. }
  57. $hasMoreProductItems = 0;
  58. /**
  59. * Products
  60. */
  61. $productListBlock = $this->getChild('product_list');
  62. if ($productListBlock) {
  63. $layer = Mage::getSingleton('catalogsearch/layer');
  64. $productsXmlObj = $productListBlock->setLayer($layer)
  65. ->setNeedBlockApplyingFilters(!$isLayeredNavigationAllowed)->getProductsXmlObject();
  66. $searchXmlObject->appendChild($productsXmlObj);
  67. $hasMoreProductItems = (int)$productListBlock->getHasProductItems();
  68. }
  69. $searchXmlObject->addAttribute('has_more_items', $hasMoreProductItems);
  70. /**
  71. * Filters
  72. */
  73. $showFiltersAndOrders = (bool) count($productsXmlObj);
  74. $requestParams = $this->getRequest()->getParams();
  75. foreach ($requestParams as $key => $value) {
  76. if (0 === strpos($key, parent::REQUEST_SORT_ORDER_PARAM_REFIX)
  77. || 0 === strpos($key, parent::REQUEST_FILTER_PARAM_REFIX)
  78. ) {
  79. $showFiltersAndOrders = false;
  80. break;
  81. }
  82. }
  83. if ($isLayeredNavigationAllowed && $productListBlock && $showFiltersAndOrders) {
  84. $filters = $productListBlock->getCollectedFilters();
  85. /**
  86. * Render filters xml
  87. */
  88. foreach ($filters as $filter) {
  89. if (!$this->_isFilterItemsHasValues($filter)) {
  90. continue;
  91. }
  92. $item = $filtersXmlObject->addChild('item');
  93. $item->addChild('name', $searchXmlObject->xmlentities($filter->getName()));
  94. $item->addChild('code', $filter->getRequestVar());
  95. $values = $item->addChild('values');
  96. foreach ($filter->getItems() as $valueItem) {
  97. $count = (int)$valueItem->getCount();
  98. if (!$count) {
  99. continue;
  100. }
  101. $value = $values->addChild('value');
  102. $value->addChild('id', $valueItem->getValueString());
  103. $value->addChild(
  104. 'label', $searchXmlObject->xmlentities(strip_tags($valueItem->getLabel()))
  105. );
  106. $value->addChild('count', $count);
  107. }
  108. }
  109. $searchXmlObject->appendChild($filtersXmlObject);
  110. }
  111. /**
  112. * Sort fields
  113. */
  114. if ($showFiltersAndOrders) {
  115. $searchXmlObject->appendChild($this->getProductSortFeildsXmlObject());
  116. }
  117. return $searchXmlObject->asNiceXml();
  118. }
  119. /**
  120. * Check if items of specified filter have values
  121. *
  122. * @param object $filter filter model
  123. * @return bool
  124. */
  125. protected function _isFilterItemsHasValues($filter)
  126. {
  127. if (!$filter->getItemsCount()) {
  128. return false;
  129. }
  130. foreach ($filter->getItems() as $valueItem) {
  131. if ((int)$valueItem->getCount()) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. }