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:
35: class Mage_ProductAlert_Model_Email extends Mage_Core_Model_Abstract
36: {
37: const XML_PATH_EMAIL_PRICE_TEMPLATE = 'catalog/productalert/email_price_template';
38: const XML_PATH_EMAIL_STOCK_TEMPLATE = 'catalog/productalert/email_stock_template';
39: const XML_PATH_EMAIL_IDENTITY = 'catalog/productalert/email_identity';
40:
41: 42: 43: 44: 45:
46: protected $_type = 'price';
47:
48: 49: 50: 51: 52:
53: protected $_website;
54:
55: 56: 57: 58: 59:
60: protected $_customer;
61:
62: 63: 64: 65: 66:
67: protected $_priceProducts = array();
68:
69: 70: 71: 72: 73:
74: protected $_stockProducts = array();
75:
76: 77: 78: 79: 80:
81: protected $_priceBlock;
82:
83: 84: 85: 86: 87:
88: protected $_stockBlock;
89:
90: 91: 92: 93: 94:
95: public function setType($type)
96: {
97: $this->_type = $type;
98: }
99:
100: 101: 102: 103: 104:
105: public function getType()
106: {
107: return $this->_type;
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function setWebsite(Mage_Core_Model_Website $website)
117: {
118: $this->_website = $website;
119: return $this;
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function setWebsiteId($websiteId)
129: {
130: $this->_website = Mage::app()->getWebsite($websiteId);
131: return $this;
132: }
133:
134: 135: 136: 137: 138: 139:
140: public function setCustomerId($customerId)
141: {
142: $this->_customer = Mage::getModel('customer/customer')->load($customerId);
143: return $this;
144: }
145:
146: 147: 148: 149: 150: 151:
152: public function setCustomer(Mage_Customer_Model_Customer $customer)
153: {
154: $this->_customer = $customer;
155: return $this;
156: }
157:
158: 159: 160: 161: 162:
163: public function clean()
164: {
165: $this->_customer = null;
166: $this->_priceProducts = array();
167: $this->_stockProducts = array();
168:
169: return $this;
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function addPriceProduct(Mage_Catalog_Model_Product $product)
179: {
180: $this->_priceProducts[$product->getId()] = $product;
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function addStockProduct(Mage_Catalog_Model_Product $product)
191: {
192: $this->_stockProducts[$product->getId()] = $product;
193: return $this;
194: }
195:
196: 197: 198: 199: 200:
201: protected function _getPriceBlock()
202: {
203: if (is_null($this->_priceBlock)) {
204: $this->_priceBlock = Mage::helper('productalert')
205: ->createBlock('productalert/email_price');
206: }
207: return $this->_priceBlock;
208: }
209:
210: 211: 212: 213: 214:
215: protected function _getStockBlock()
216: {
217: if (is_null($this->_stockBlock)) {
218: $this->_stockBlock = Mage::helper('productalert')
219: ->createBlock('productalert/email_stock');
220: }
221: return $this->_stockBlock;
222: }
223:
224: 225: 226: 227: 228:
229: public function send()
230: {
231: if (is_null($this->_website) || is_null($this->_customer)) {
232: return false;
233: }
234: if (($this->_type == 'price' && count($this->_priceProducts) == 0) || ($this->_type == 'stock' && count($this->_stockProducts) == 0)) {
235: return false;
236: }
237: if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
238: return false;
239: }
240:
241: $store = $this->_website->getDefaultStore();
242: $storeId = $store->getId();
243:
244: if ($this->_type == 'price' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId)) {
245: return false;
246: } elseif ($this->_type == 'stock' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId)) {
247: return false;
248: }
249:
250: if ($this->_type != 'price' && $this->_type != 'stock') {
251: return false;
252: }
253:
254: $appEmulation = Mage::getSingleton('core/app_emulation');
255: $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
256:
257: if ($this->_type == 'price') {
258: $this->_getPriceBlock()
259: ->setStore($store)
260: ->reset();
261: foreach ($this->_priceProducts as $product) {
262: $product->setCustomerGroupId($this->_customer->getGroupId());
263: $this->_getPriceBlock()->addProduct($product);
264: }
265: $block = $this->_getPriceBlock()->toHtml();
266: $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId);
267: } else {
268: $this->_getStockBlock()
269: ->setStore($store)
270: ->reset();
271: foreach ($this->_stockProducts as $product) {
272: $product->setCustomerGroupId($this->_customer->getGroupId());
273: $this->_getStockBlock()->addProduct($product);
274: }
275: $block = $this->_getStockBlock()->toHtml();
276: $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId);
277: }
278:
279: $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
280:
281: Mage::getModel('core/email_template')
282: ->setDesignConfig(array(
283: 'area' => 'frontend',
284: 'store' => $storeId
285: ))->sendTransactional(
286: $templateId,
287: Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId),
288: $this->_customer->getEmail(),
289: $this->_customer->getName(),
290: array(
291: 'customerName' => $this->_customer->getName(),
292: 'alertGrid' => $block
293: )
294: );
295:
296: return true;
297: }
298: }
299: