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

/app/code/core/Mage/GiftMessage/Helper/Message.php

https://bitbucket.org/acidel/buykoala
PHP | 314 lines | 152 code | 26 blank | 136 comment | 26 complexity | eb3249ad93f7dd08b5244db16caa330c 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_GiftMessage
  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. * Gift Message helper
  28. *
  29. * @category Mage
  30. * @package Mage_GiftMessage
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_GiftMessage_Helper_Message extends Mage_Core_Helper_Data
  34. {
  35. /**
  36. * Giftmessages allow section in configuration
  37. *
  38. */
  39. const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items';
  40. const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order';
  41. /**
  42. * Next id for edit gift message block
  43. *
  44. * @var integer
  45. */
  46. protected $_nextId = 0;
  47. /**
  48. * Inner cache
  49. *
  50. * @var array
  51. */
  52. protected $_innerCache = array();
  53. /**
  54. * Retrive old stule edit button html for editing of giftmessage in popup
  55. *
  56. * @param string $type
  57. * @param Varien_Object $entity
  58. * @return string
  59. */
  60. public function getButton($type, Varien_Object $entity)
  61. {
  62. if (!$this->isMessagesAvailable($type, $entity)) {
  63. return '&nbsp;';
  64. }
  65. return Mage::getSingleton('core/layout')->createBlock('giftmessage/message_helper')
  66. ->setId('giftmessage_button_' . $this->_nextId++)
  67. ->setCanDisplayContainer(true)
  68. ->setEntity($entity)
  69. ->setType($type)->toHtml();
  70. }
  71. /**
  72. * Retrive inline giftmessage edit form for specified entity
  73. *
  74. * @param string $type
  75. * @param Varien_Object $entity
  76. * @param boolean $dontDisplayContainer
  77. * @return string
  78. */
  79. public function getInline($type, Varien_Object $entity, $dontDisplayContainer=false)
  80. {
  81. if (!in_array($type, array('onepage_checkout','multishipping_adress')) && !$this->isMessagesAvailable($type, $entity)) {
  82. return '';
  83. }
  84. return Mage::getSingleton('core/layout')->createBlock('giftmessage/message_inline')
  85. ->setId('giftmessage_form_' . $this->_nextId++)
  86. ->setDontDisplayContainer($dontDisplayContainer)
  87. ->setEntity($entity)
  88. ->setType($type)->toHtml();
  89. }
  90. /**
  91. * Check availability of giftmessages for specified entity.
  92. *
  93. * @param string $type
  94. * @param Varien_Object $entity
  95. * @param Mage_Core_Model_Store|integer $store
  96. * @return boolean
  97. */
  98. public function isMessagesAvailable($type, Varien_Object $entity, $store = null)
  99. {
  100. if ($type == 'items') {
  101. $items = $entity->getAllItems();
  102. if(!is_array($items) || empty($items)) {
  103. return Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $store);
  104. }
  105. if($entity instanceof Mage_Sales_Model_Quote) {
  106. $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
  107. }
  108. else {
  109. $_type = 'order_item';
  110. }
  111. foreach ($items as $item) {
  112. if ($item->getParentItem()) {
  113. continue;
  114. }
  115. if ($this->isMessagesAvailable($_type, $item, $store)) {
  116. return true;
  117. }
  118. }
  119. } elseif ($type == 'item') {
  120. return $this->_getDependenceFromStoreConfig(
  121. $entity->getProduct()->getGiftMessageAvailable(),
  122. $store
  123. );
  124. } elseif ($type == 'order_item') {
  125. return $this->_getDependenceFromStoreConfig(
  126. $entity->getGiftMessageAvailable(),
  127. $store
  128. );
  129. } elseif ($type == 'address_item') {
  130. $storeId = is_numeric($store) ? $store : Mage::app()->getStore($store)->getId();
  131. if (!$this->isCached('address_item_' . $entity->getProductId())) {
  132. $this->setCached(
  133. 'address_item_' . $entity->getProductId(),
  134. Mage::getModel('catalog/product')
  135. ->setStoreId($storeId)
  136. ->load($entity->getProductId())
  137. ->getGiftMessageAvailable()
  138. );
  139. }
  140. return $this->_getDependenceFromStoreConfig(
  141. $this->getCached('address_item_' . $entity->getProductId()),
  142. $store
  143. );
  144. } else {
  145. return Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, $store);
  146. }
  147. return false;
  148. }
  149. /**
  150. * Check availablity of gift messages from store config if flag eq 2.
  151. *
  152. * @param int $productGiftMessageAllow
  153. * @param Mage_Core_Model_Store|integer $store
  154. * @return boolean
  155. */
  156. protected function _getDependenceFromStoreConfig($productGiftMessageAllow, $store=null)
  157. {
  158. $result = Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $store);
  159. if ($productGiftMessageAllow === '' || is_null($productGiftMessageAllow)) {
  160. return $result;
  161. } else {
  162. return $productGiftMessageAllow;
  163. }
  164. }
  165. /**
  166. * Alias for isMessagesAvailable(...)
  167. *
  168. * @param string $type
  169. * @param Varien_Object $entity
  170. * @param Mage_Core_Model_Store|integer $store
  171. * @return boolen
  172. */
  173. public function getIsMessagesAvailable($type, Varien_Object $entity, $store=null)
  174. {
  175. return $this->isMessagesAvailable($type, $entity, $store);
  176. }
  177. /**
  178. * Retrive escaped and preformated gift message text for specified entity
  179. *
  180. * @param Varien_Object $entity
  181. * @return unknown
  182. */
  183. public function getEscapedGiftMessage(Varien_Object $entity)
  184. {
  185. $message = $this->getGiftMessageForEntity($entity);
  186. if ($message) {
  187. return nl2br($this->htmlEscape($message->getMessage()));
  188. }
  189. return null;
  190. }
  191. /**
  192. * Retrive gift message for entity. If message not exists return null
  193. *
  194. * @param Varien_Object $entity
  195. * @return Mage_GiftMessage_Model_Message
  196. */
  197. public function getGiftMessageForEntity(Varien_Object $entity)
  198. {
  199. if($entity->getGiftMessageId() && !$entity->getGiftMessage()) {
  200. $message = $this->getGiftMessage($entity->getGiftMessageId());
  201. $entity->setGiftMessage($message);
  202. }
  203. return $entity->getGiftMessage();
  204. }
  205. /**
  206. * Retrive internal cached data with specified key.
  207. *
  208. * If cached data not found return null.
  209. *
  210. * @param string $key
  211. * @return mixed|null
  212. */
  213. public function getCached($key)
  214. {
  215. if($this->isCached($key)) {
  216. return $this->_innerCache[$key];
  217. }
  218. return null;
  219. }
  220. /**
  221. * Check availability for internal cached data with specified key
  222. *
  223. * @param string $key
  224. * @return boolean
  225. */
  226. public function isCached($key)
  227. {
  228. return isset($this->_innerCache[$key]);
  229. }
  230. /**
  231. * Set internal cache data with specified key
  232. *
  233. * @param string $key
  234. * @param mixed $value
  235. * @return Mage_GiftMessage_Helper_Message
  236. */
  237. public function setCached($key, $value)
  238. {
  239. $this->_innerCache[$key] = $value;
  240. return $this;
  241. }
  242. /**
  243. * Check availability for onepage checkout items
  244. *
  245. * @param array $items
  246. * @param Mage_Core_Model_Store|integer $store
  247. * @return boolen
  248. */
  249. public function getAvailableForQuoteItems($quote, $store=null)
  250. {
  251. foreach($quote->getAllItems() as $item) {
  252. if($this->isMessagesAvailable('item', $item, $store)) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. /**
  259. * Check availability for multishiping checkout items
  260. *
  261. * @param array $items
  262. * @param Mage_Core_Model_Store|integer $store
  263. * @return boolen
  264. */
  265. public function getAvailableForAddressItems($items, $store=null)
  266. {
  267. foreach($items as $item) {
  268. if($this->isMessagesAvailable('address_item', $item, $store)) {
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * Retrive gift message with specified id
  276. *
  277. * @param integer $messageId
  278. * @return Mage_GiftMessage_Model_Message
  279. */
  280. public function getGiftMessage($messageId=null)
  281. {
  282. $message = Mage::getModel('giftmessage/message');
  283. if(!is_null($messageId)) {
  284. $message->load($messageId);
  285. }
  286. return $message;
  287. }
  288. }