00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_ProductAlert_Model_Email extends Mage_Core_Model_Abstract
00036 {
00037 const XML_PATH_EMAIL_PRICE_TEMPLATE = 'catalog/productalert/email_price_template';
00038 const XML_PATH_EMAIL_STOCK_TEMPLATE = 'catalog/productalert/email_stock_template';
00039 const XML_PATH_EMAIL_IDENTITY = 'catalog/productalert/email_identity';
00040
00041
00042
00043
00044
00045
00046 protected $_type = 'price';
00047
00048
00049
00050
00051
00052
00053 protected $_website;
00054
00055
00056
00057
00058
00059
00060 protected $_customer;
00061
00062
00063
00064
00065
00066
00067 protected $_priceProducts = array();
00068
00069
00070
00071
00072
00073
00074 protected $_stockProducts = array();
00075
00076
00077
00078
00079
00080
00081 protected $_priceBlock;
00082
00083
00084
00085
00086
00087
00088 protected $_stockBlock;
00089
00090
00091
00092
00093
00094
00095 public function setType($type)
00096 {
00097 $this->_type = $type;
00098 }
00099
00100
00101
00102
00103
00104
00105 public function getType()
00106 {
00107 return $this->_type;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 public function setWebsite(Mage_Core_Model_Website $website)
00117 {
00118 $this->_website = $website;
00119 return $this;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 public function setWebsiteId($websiteId)
00129 {
00130 $this->_website = Mage::app()->getWebsite($websiteId);
00131 return $this;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 public function setCustomerId($customerId)
00141 {
00142 $this->_customer = Mage::getModel('customer/customer')->load($customerId);
00143 return $this;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152 public function setCustomer(Mage_Customer_Model_Customer $customer)
00153 {
00154 $this->_customer = $customer;
00155 return $this;
00156 }
00157
00158
00159
00160
00161
00162
00163 public function clean()
00164 {
00165 $this->_customer = null;
00166 $this->_priceProducts = array();
00167 $this->_stockProducts = array();
00168
00169 return $this;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public function addPriceProduct(Mage_Catalog_Model_Product $product)
00179 {
00180 $this->_priceProducts[$product->getId()] = $product;
00181 return $this;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 public function addStockProduct(Mage_Catalog_Model_Product $product)
00191 {
00192 $this->_stockProducts[$product->getId()] = $product;
00193 return $this;
00194 }
00195
00196
00197
00198
00199
00200
00201 protected function _getPriceBlock()
00202 {
00203 if (is_null($this->_priceBlock)) {
00204 $this->_priceBlock = Mage::helper('productalert')
00205 ->createBlock('productalert/email_price');
00206 }
00207 return $this->_priceBlock;
00208 }
00209
00210
00211
00212
00213
00214
00215 protected function _getStockBlock()
00216 {
00217 if (is_null($this->_stockBlock)) {
00218 $this->_stockBlock = Mage::helper('productalert')
00219 ->createBlock('productalert/email_stock');
00220 }
00221 return $this->_stockBlock;
00222 }
00223
00224
00225
00226
00227
00228
00229 public function send()
00230 {
00231 if (is_null($this->_website) || is_null($this->_customer)) {
00232 return false;
00233 }
00234 if (($this->_type == 'price' && count($this->_priceProducts) == 0) || ($this->_type == 'stock' && count($this->_stockProducts) == 0)) {
00235 return false;
00236 }
00237 if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
00238 return false;
00239 }
00240
00241 $store = $this->_website->getDefaultStore();
00242 $storeId = $store->getId();
00243
00244 if ($this->_type == 'price' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId)) {
00245 return false;
00246 } elseif ($this->_type == 'stock' && !Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId)) {
00247 return false;
00248 }
00249
00250 Mage::getDesign()->setStore($storeId);
00251 Mage::getDesign()->setArea('frontend');
00252
00253 $translate = Mage::getSingleton('core/translate');
00254
00255 $translate->setTranslateInline(false);
00256
00257 if ($this->_type == 'price') {
00258 $this->_getPriceBlock()->setStore($store);
00259 foreach ($this->_priceProducts as $product) {
00260 $product->setCustomerGroupId($this->_customer->getGroupId());
00261 $this->_getPriceBlock()->addProduct($product);
00262 }
00263 $block = $this->_getPriceBlock()->toHtml();
00264 $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_PRICE_TEMPLATE, $storeId);
00265 }
00266 elseif ($this->_type == 'stock') {
00267 $this->_getStockBlock()->setStore($store);
00268 foreach ($this->_stockProducts as $product) {
00269 $product->setCustomerGroupId($this->_customer->getGroupId());
00270 $this->_getStockBlock()->addProduct($product);
00271 }
00272 $block = $this->_getStockBlock()->toHtml();
00273 $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_STOCK_TEMPLATE, $storeId);
00274 }
00275 else {
00276 return false;
00277 }
00278
00279 Mage::getModel('core/email_template')
00280 ->setDesignConfig(array(
00281 'area' => 'frontend',
00282 'store' => $storeId
00283 ))->sendTransactional(
00284 $templateId,
00285 Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId),
00286 $this->_customer->getEmail(),
00287 $this->_customer->getName(),
00288 array(
00289 'customerName' => $this->_customer->getName(),
00290 'alertGrid' => $block
00291 )
00292 );
00293
00294 $translate->setTranslateInline(true);
00295
00296 return true;
00297 }
00298 }