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_Category extends Mage_XmlConnect_Block_Catalog
35: {
36: 37: 38: 39: 40:
41: protected function _toHtml()
42: {
43:
44: $categoryXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<category></category>');
45: $categoryId = $this->getRequest()->getParam('id', null);
46: if ($categoryId === null) {
47: $categoryId = Mage::app()->getStore()->getRootCategoryId();
48: }
49:
50: $productsXmlObj = $productListBlock = false;
51:
52: $categoryModel = Mage::getModel('catalog/category')->load($categoryId);
53: if ($categoryModel->getId()) {
54: $hasMoreProductItems = 0;
55: $productListBlock = $this->getChild('product_list');
56: if ($productListBlock && $categoryModel->getLevel() > 1) {
57: $layer = Mage::getSingleton('catalog/layer');
58: $productsXmlObj = $productListBlock->setCategory($categoryModel)->setLayer($layer)
59: ->getProductsXmlObject();
60: $hasMoreProductItems = (int)$productListBlock->getHasProductItems();
61: }
62:
63: $infoBlock = $this->getChild('category_info');
64: if ($infoBlock) {
65: $categoryInfoXmlObj = $infoBlock->setCategory($categoryModel)->getCategoryInfoXmlObject();
66: $categoryInfoXmlObj->addChild('has_more_items', $hasMoreProductItems);
67: $categoryXmlObj->appendChild($categoryInfoXmlObj);
68: }
69: }
70:
71: $categoryCollection = $this->getCurrentChildCategories();
72:
73:
74: if (sizeof($categoryCollection)) {
75: $itemsXmlObj = $categoryXmlObj->addChild('items');
76: foreach ($categoryCollection as $item) {
77:
78: $item = Mage::getModel('catalog/category')->load($item->getId());
79:
80: $itemXmlObj = $itemsXmlObj->addChild('item');
81: $itemXmlObj->addChild('label', $categoryXmlObj->escapeXml($item->getName()));
82: $itemXmlObj->addChild('entity_id', $item->getId());
83: $itemXmlObj->addChild('content_type', $item->hasChildren() ? 'categories' : 'products');
84: if (!is_null($categoryId)) {
85: $itemXmlObj->addChild('parent_id', $item->getParentId());
86: }
87: $icon = Mage::helper('xmlconnect/catalog_category_image')->initialize($item, 'thumbnail')
88: ->resize(Mage::helper('xmlconnect/image')->getImageSizeForContent('category'));
89:
90: $iconXml = $itemXmlObj->addChild('icon', $icon);
91:
92: $file = Mage::helper('xmlconnect')->urlToPath($icon);
93: $iconXml->addAttribute('modification_time', filemtime($file));
94: }
95: }
96:
97: if ($productListBlock && $productsXmlObj) {
98: $categoryXmlObj->appendChild($productsXmlObj);
99: }
100: return $categoryXmlObj->asNiceXml();
101: }
102:
103: 104: 105: 106: 107:
108: public function getCurrentChildCategories()
109: {
110: $layer = Mage::getSingleton('catalog/layer');
111: $category = $layer->getCurrentCategory();
112:
113: $categories = $category->getChildrenCategories();
114: $productCollection = Mage::getResourceModel('catalog/product_collection');
115: $layer->prepareProductCollection($productCollection);
116: $productCollection->addCountToCategories($categories);
117: return $categories;
118: }
119: }
120: