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_Product_List extends Mage_XmlConnect_Block_Catalog_Product
35: {
36: 37: 38: 39: 40:
41: protected $_productCollection = null;
42:
43: 44: 45: 46: 47:
48: protected $_collectedFilters = array();
49:
50: 51: 52: 53: 54:
55: public function getProductsXmlObject()
56: {
57: $productsXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<products></products>');
58: $collection = $this->_getProductCollection();
59:
60: if (!$collection) {
61: return false;
62: }
63: foreach ($collection->getItems() as $product) {
64: $productXmlObj = $this->productToXmlObject($product);
65: if ($productXmlObj) {
66: $productsXmlObj->appendChild($productXmlObj);
67: }
68: }
69:
70: return $productsXmlObj;
71: }
72:
73: 74: 75: 76: 77:
78: public function getCollectedFilters()
79: {
80: return $this->_collectedFilters;
81: }
82:
83: 84: 85: 86: 87:
88: protected function _getProductCollection()
89: {
90: if (is_null($this->_productCollection)) {
91: $filters = array();
92: $request = $this->getRequest();
93: $requestParams = $request->getParams();
94: $layer = $this->getLayer();
95: if (!$layer) {
96: return null;
97: }
98: $category = $this->getCategory();
99: if ($category && is_object($category) && $category->getId()) {
100: $layer->setCurrentCategory($category);
101: }
102: if (!$this->getNeedBlockApplyingFilters()) {
103: $attributes = $layer->getFilterableAttributes();
104: 105: 106:
107: foreach ($attributes as $attributeItem) {
108: $attributeCode = $attributeItem->getAttributeCode();
109: list($filterModel, $filterBlock) = $this->helper('xmlconnect')->getFilterByKey($attributeCode);
110:
111: $filterModel->setLayer($layer)->setAttributeModel($attributeItem);
112:
113: $filterParam = parent::REQUEST_FILTER_PARAM_REFIX . $attributeCode;
114: 115: 116:
117: if (isset($requestParams[$filterParam])) {
118: $filterModel->setRequestVar($filterParam);
119: }
120: $filterModel->apply($request, $filterBlock);
121: $filters[] = $filterModel;
122: }
123:
124: 125: 126:
127: list($categoryFilter, $categoryFilterBlock) = $this->helper('xmlconnect')->getFilterByKey('category');
128: $filterParam = parent::REQUEST_FILTER_PARAM_REFIX . $categoryFilter->getRequestVar();
129: $categoryFilter->setLayer($layer)->setRequestVar($filterParam)
130: ->apply($this->getRequest(), $categoryFilterBlock);
131: $filters[] = $categoryFilter;
132:
133: $this->_collectedFilters = $filters;
134: }
135:
136: 137: 138:
139: $layer = $this->getLayer();
140: $collection = $layer->getProductCollection();
141:
142: 143: 144:
145: $this->_prepareCollection($collection);
146:
147: 148: 149:
150: $offset = (int)$request->getParam('offset', 0);
151: $count = (int)$request->getParam('count', 0);
152: $count = $count <= 0 ? 1 : $count;
153: if ($offset + $count < $collection->getSize()) {
154: $this->setHasProductItems(1);
155: }
156: $collection->getSelect()->limit($count, $offset);
157: $collection->setFlag('require_stock_items', true);
158:
159: $this->_productCollection = $collection;
160: }
161: return $this->_productCollection;
162: }
163:
164: 165: 166: 167: 168: 169:
170: protected function _prepareCollection($collection)
171: {
172: 173: 174:
175: $reguest = $this->getRequest();
176: foreach ($reguest->getParams() as $key => $value) {
177: if (0 === strpos($key, parent::REQUEST_SORT_ORDER_PARAM_REFIX)) {
178: $key = str_replace(parent::REQUEST_SORT_ORDER_PARAM_REFIX, '', $key);
179: if ($value != 'desc') {
180: $value = 'asc';
181: }
182: $collection->addAttributeToSort($key, $value);
183: }
184: }
185: $collection->addAttributeToSelect(array('image', 'name', 'description'));
186:
187: return $this;
188: }
189:
190: 191: 192: 193: 194:
195: protected function _toHtml()
196: {
197: return $this->getProductsXmlObject()->asNiceXml();
198: }
199: }
200: