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

/app/code/core/Mage/ProductAlert/Model/Email.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 301 lines | 139 code | 30 blank | 132 comment | 26 complexity | 4790a3c20b28cba3edd90cae78b278e7 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_ProductAlert
  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. * ProductAlert Email processor
  28. *
  29. * @category Mage
  30. * @package Mage_ProductAlert
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_ProductAlert_Model_Email extends Mage_Core_Model_Abstract
  34. {
  35. const XML_PATH_EMAIL_PRICE_TEMPLATE = 'catalog/productalert/email_price_template';
  36. const XML_PATH_EMAIL_STOCK_TEMPLATE = 'catalog/productalert/email_stock_template';
  37. const XML_PATH_EMAIL_IDENTITY = 'catalog/productalert/email_identity';
  38. /**
  39. * Type
  40. *
  41. * @var string
  42. */
  43. protected $_type = 'price';
  44. /**
  45. * Website Model
  46. *
  47. * @var Mage_Core_Model_Website
  48. */
  49. protected $_website;
  50. /**
  51. * Customer model
  52. *
  53. * @var Mage_Customer_Model_Customer
  54. */
  55. protected $_customer;
  56. /**
  57. * Products collection where changed price
  58. *
  59. * @var array
  60. */
  61. protected $_priceProducts = array();
  62. /**
  63. * Product collection which of back in stock
  64. *
  65. * @var array
  66. */
  67. protected $_stockProducts = array();
  68. /**
  69. * Price block
  70. *
  71. * @var Mage_ProductAlert_Block_Email_Price
  72. */
  73. protected $_priceBlock;
  74. /**
  75. * Stock block
  76. *
  77. * @var Mage_ProductAlert_Block_Email_Stock
  78. */
  79. protected $_stockBlock;
  80. /**
  81. * Set model type
  82. *
  83. * @param string $type
  84. */
  85. public function setType($type)
  86. {
  87. $this->_type = $type;
  88. }
  89. /**
  90. * Retrieve model type
  91. *
  92. * @return string
  93. */
  94. public function getType()
  95. {
  96. return $this->_type;
  97. }
  98. /**
  99. * Set website model
  100. *
  101. * @param Mage_Core_Model_Website $website
  102. * @return Mage_ProductAlert_Model_Email
  103. */
  104. public function setWebsite(Mage_Core_Model_Website $website)
  105. {
  106. $this->_website = $website;
  107. return $this;
  108. }
  109. /**
  110. * Set website id
  111. *
  112. * @param int $websiteId
  113. * @return Mage_ProductAlert_Model_Email
  114. */
  115. public function setWebsiteId($websiteId)
  116. {
  117. $this->_website = Mage::app()->getWebsite($websiteId);
  118. return $this;
  119. }
  120. /**
  121. * Set customer by id
  122. *
  123. * @param int $customerId
  124. * @return Mage_ProductAlert_Model_Email
  125. */
  126. public function setCustomerId($customerId)
  127. {
  128. $this->_customer = Mage::getModel('customer/customer')->load($customerId);
  129. return $this;
  130. }
  131. /**
  132. * Set customer model
  133. *
  134. * @param Mage_Customer_Model_Customer $customer
  135. * @return Mage_ProductAlert_Model_Email
  136. */
  137. public function setCustomer(Mage_Customer_Model_Customer $customer)
  138. {
  139. $this->_customer = $customer;
  140. return $this;
  141. }
  142. /**
  143. * Clean data
  144. *
  145. * @return Mage_ProductAlert_Model_Email
  146. */
  147. public function clean()
  148. {
  149. $this->_customer = null;
  150. $this->_priceProducts = array();
  151. $this->_stockProducts = array();
  152. return $this;
  153. }
  154. /**
  155. * Add product (price change) to collection
  156. *
  157. * @param Mage_Catalog_Model_Product $product
  158. * @return Mage_ProductAlert_Model_Email
  159. */
  160. public function addPriceProduct(Mage_Catalog_Model_Product $product)
  161. {
  162. $this->_priceProducts[$product->getId()] = $product;
  163. return $this;
  164. }
  165. /**
  166. * Add product (back in stock) to collection
  167. *
  168. * @param Mage_Catalog_Model_Product $product
  169. * @return Mage_ProductAlert_Model_Email
  170. */
  171. public function addStockProduct(Mage_Catalog_Model_Product $product)
  172. {
  173. $this->_stockProducts[$product->getId()] = $product;
  174. return $this;
  175. }
  176. /**
  177. * Retrieve price block
  178. *
  179. * @return Mage_ProductAlert_Block_Email_Price
  180. */
  181. protected function _getPriceBlock()
  182. {
  183. if (is_null($this->_priceBlock)) {
  184. $this->_priceBlock = Mage::helper('productalert')
  185. ->createBlock('productalert/email_price');
  186. }
  187. return $this->_priceBlock;
  188. }
  189. /**
  190. * Retrieve stock block
  191. *
  192. * @return Mage_ProductAlert_Block_Email_Stock
  193. */
  194. protected function _getStockBlock()
  195. {
  196. if (is_null($this->_stockBlock)) {
  197. $this->_stockBlock = Mage::helper('productalert')
  198. ->createBlock('productalert/email_stock');
  199. }
  200. return $this->_stockBlock;
  201. }
  202. /**
  203. * Send customer email
  204. *
  205. * @return bool
  206. */
  207. public function send()
  208. {
  209. if (is_null($this->_website) || is_null($this->_customer)) {
  210. return false;
  211. }
  212. if (($this->_type == 'price' && count($this->_priceProducts) == 0)
  213. || ($this->_type == 'stock' && count($this->_stockProducts) == 0)
  214. ) {
  215. return false;
  216. }
  217. if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
  218. return false;
  219. }
  220. $store = Mage::getModel('core/store')->load($this->_customer->getStoreId());
  221. $storeId = $store->getId();
  222. if ($this->_type == 'price' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId)) {
  223. return false;
  224. } elseif ($this->_type == 'stock' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId)) {
  225. return false;
  226. }
  227. if ($this->_type != 'price' && $this->_type != 'stock') {
  228. return false;
  229. }
  230. $appEmulation = Mage::getSingleton('core/app_emulation');
  231. $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
  232. Mage::app()->getTranslator()->init('frontend', true);
  233. if ($this->_type == 'price') {
  234. $this->_getPriceBlock()
  235. ->setStore($store)
  236. ->reset();
  237. foreach ($this->_priceProducts as $product) {
  238. $product->setCustomerGroupId($this->_customer->getGroupId());
  239. $this->_getPriceBlock()->addProduct($product);
  240. }
  241. $block = $this->_getPriceBlock()->toHtml();
  242. $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId);
  243. } else {
  244. $this->_getStockBlock()
  245. ->setStore($store)
  246. ->reset();
  247. foreach ($this->_stockProducts as $product) {
  248. $product->setCustomerGroupId($this->_customer->getGroupId());
  249. $this->_getStockBlock()->addProduct($product);
  250. }
  251. $block = $this->_getStockBlock()->toHtml();
  252. $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId);
  253. }
  254. $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
  255. Mage::getModel('core/email_template')
  256. ->setDesignConfig(array(
  257. 'area' => 'frontend',
  258. 'store' => $storeId
  259. ))->sendTransactional(
  260. $templateId,
  261. Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId),
  262. $this->_customer->getEmail(),
  263. $this->_customer->getName(),
  264. array(
  265. 'customerName' => $this->_customer->getName(),
  266. 'alertGrid' => $block
  267. )
  268. );
  269. return true;
  270. }
  271. }