PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/app/code/core/Mage/XmlConnect/Block/Homebanners.php

https://bitbucket.org/mkrasuski/magento-ce
PHP | 156 lines | 93 code | 12 blank | 51 comment | 5 complexity | 49e7f36437bd22355102f02f57066ed7 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Home banners list renderer
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Homebanners extends Mage_Core_Block_Abstract
  34. {
  35. /**
  36. * List of images separated by device those have to be shown on home banners page
  37. *
  38. * @return array
  39. */
  40. private function getBannerTypeArray()
  41. {
  42. return array(
  43. Mage_XmlConnect_Helper_Data::DEVICE_TYPE_ANDROID => array(
  44. Mage_XmlConnect_Model_Device_Android::IMAGE_TYPE_PORTRAIT_BANNER
  45. ),
  46. Mage_XmlConnect_Helper_Data::DEVICE_TYPE_IPHONE => array(
  47. Mage_XmlConnect_Model_Device_Iphone::IMAGE_TYPE_PORTRAIT_BANNER,
  48. ),
  49. Mage_XmlConnect_Helper_Data::DEVICE_TYPE_IPAD => array(
  50. Mage_XmlConnect_Model_Device_Ipad::IMAGE_TYPE_LANDSCAPE_BANNER,
  51. Mage_XmlConnect_Model_Device_Ipad::IMAGE_TYPE_PORTRAIT_BANNER,
  52. ));
  53. }
  54. /**
  55. * Render home banners list xml
  56. *
  57. * @return string
  58. */
  59. protected function _toHtml()
  60. {
  61. /** @var $homeBannersXmlObj Mage_XmlConnect_Model_Simplexml_Element */
  62. $homeBannersXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<home_banners></home_banners>');
  63. /** @var $deviceHelper Mage_XmlConnect_Helper_Data */
  64. $deviceHelper = Mage::helper('xmlconnect');
  65. /** @var $imagesModel Mage_XmlConnect_Model_Images */
  66. $imagesModel = Mage::getModel('xmlconnect/images');
  67. $bannerTypeCollection = $this->getBannerTypeArray();
  68. $deviceType = $deviceHelper->getDeviceType();
  69. foreach ($bannerTypeCollection[$deviceType] as $bannerType) {
  70. $bannerImageCollection = $imagesModel->getDeviceImagesByType($bannerType);
  71. foreach ($bannerImageCollection as $bannerImage) {
  72. $itemXmlObj = $homeBannersXmlObj->addCustomChild('item', null, array(
  73. 'entity_id' => $bannerImage['image_id'],
  74. 'type' => $bannerType
  75. ));
  76. $originalFile = Mage_XmlConnect_Model_Images::getBasePath($bannerImage['image_file']);
  77. $bannerUrl = $imagesModel->getScreenSizeImageUrlByType($bannerImage['image_file'], $bannerType);
  78. $itemXmlObj->addCustomChild('image', $bannerUrl, array(
  79. 'modification_time' => filemtime($originalFile)
  80. ));
  81. $this->_addImageAction($itemXmlObj, $bannerImage['image_id']);
  82. }
  83. }
  84. return $homeBannersXmlObj->asNiceXml();
  85. }
  86. /**
  87. * Add action info to xml
  88. *
  89. * @param Mage_XmlConnect_Model_Simplexml_Element $imageXml
  90. * @param int $imageId
  91. * @return Mage_XmlConnect_Block_Homebanners
  92. */
  93. protected function _addImageAction(Mage_XmlConnect_Model_Simplexml_Element $imageXml, $imageId)
  94. {
  95. $imageActionData = Mage::helper('xmlconnect')->getApplication()->getImageActionModel()
  96. ->getImageActionData($imageId);
  97. if (empty($imageActionData)) {
  98. return $this;
  99. }
  100. switch ($imageActionData['action_type']) {
  101. case Mage_XmlConnect_Model_ImageAction::ACTION_TYPE_CMS:
  102. $page = Mage::getModel('cms/page')->setStoreId(Mage::app()->getStore()->getId())
  103. ->load($imageActionData['entity_action'], 'identifier');
  104. if ($page->getId()) {
  105. $actionXml = $imageXml->addCustomChild('action', null, array(
  106. 'type' => $imageActionData['action_type']
  107. ));
  108. $actionXml->addCustomChild('attribute', $imageActionData['entity_action'], array(
  109. 'name' => 'id',
  110. ));
  111. $actionXml->addCustomChild('attribute', $page->getTitle(), array(
  112. 'name' => 'title'
  113. ));
  114. }
  115. break;
  116. case Mage_XmlConnect_Model_ImageAction::ACTION_TYPE_PRODUCT:
  117. $product = Mage::getModel('catalog/product')->load($imageActionData['entity_action']);
  118. if ($product->getId()) {
  119. $actionXml = $imageXml->addCustomChild('action', null, array(
  120. 'type' => $imageActionData['action_type']
  121. ));
  122. $actionXml->addCustomChild('attribute', $imageActionData['entity_action'], array(
  123. 'name' => 'id',
  124. ));
  125. }
  126. break;
  127. case Mage_XmlConnect_Model_ImageAction::ACTION_TYPE_CATEGORY:
  128. $category = Mage::getModel('catalog/category')->load($imageActionData['entity_action']);
  129. if ($category->getEntityId()) {
  130. $actionXml = $imageXml->addCustomChild('action', null, array(
  131. 'type' => $imageActionData['action_type']
  132. ));
  133. $actionXml->addCustomChild('attribute', $imageActionData['entity_action'], array(
  134. 'name' => 'id',
  135. ));
  136. }
  137. break;
  138. default:
  139. Mage::throwException($this->__('Action type doesn\'t recognized.'));
  140. break;
  141. }
  142. return $this;
  143. }
  144. }