This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
MailAlert.php
292 lines (250 loc) · 12.4 KB
/
MailAlert.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* 2007-2016 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class MailAlert extends ObjectModel
{
public $id_customer;
public $customer_email;
public $id_product;
public $id_product_attribute;
public $id_shop;
public $id_lang;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'mailalert_customer_oos',
'primary' => 'id_customer',
'fields' => array(
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'customer_email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true),
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'id_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
),
);
public static function customerHasNotification($id_customer, $id_product, $id_product_attribute, $id_shop = null, $id_lang = null, $guest_email = '')
{
if ($id_shop == null) {
$id_shop = Context::getContext()->shop->id;
}
if ($id_lang == null) {
$id_lang = Context::getContext()->language->id;
}
$customer = new Customer($id_customer);
$customer_email = $customer->email;
$guest_email = pSQL($guest_email);
$id_customer = (int) $id_customer;
$customer_email = pSQL($customer_email);
$where = $id_customer == 0 ? "customer_email = '$guest_email'" : "(id_customer=$id_customer OR customer_email='$customer_email')";
$sql = '
SELECT *
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . $where . '
AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
AND `id_shop` = ' . (int) $id_shop;
return count(Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql));
}
public static function deleteAlert($id_customer, $customer_email, $id_product, $id_product_attribute, $id_shop = null)
{
$sql = '
DELETE FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . (($id_customer > 0) ? '(`customer_email` = \'' . pSQL($customer_email) . '\' OR `id_customer` = ' . (int) $id_customer . ')' :
'`customer_email` = \'' . pSQL($customer_email) . '\'') .
' AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
AND `id_shop` = ' . ($id_shop != null ? (int) $id_shop : (int) Context::getContext()->shop->id);
return Db::getInstance()->execute($sql);
}
/*
* Get objects that will be viewed on "My alerts" page
*/
public static function getMailAlerts($id_customer, $id_lang, Shop $shop = null)
{
if (!Validate::isUnsignedId($id_customer) || !Validate::isUnsignedId($id_lang)) {
die(Tools::displayError());
}
if (!$shop) {
$shop = Context::getContext()->shop;
}
$customer = new Customer($id_customer);
$products = MailAlert::getProducts($customer, $id_lang);
$products_number = count($products);
if (empty($products) === true || !$products_number) {
return array();
}
for ($i = 0; $i < $products_number; ++$i) {
$obj = new Product((int) $products[$i]['id_product'], false, (int) $id_lang);
if (!Validate::isLoadedObject($obj)) {
continue;
}
if (isset($products[$i]['id_product_attribute']) &&
Validate::isUnsignedInt($products[$i]['id_product_attribute'])
) {
$attributes = self::getProductAttributeCombination($products[$i]['id_product_attribute'], $id_lang);
$products[$i]['attributes_small'] = '';
if ($attributes) {
foreach ($attributes as $row) {
$products[$i]['attributes_small'] .= $row['attribute_name'] . ', ';
}
}
$products[$i]['attributes_small'] = rtrim($products[$i]['attributes_small'], ', ');
$products[$i]['id_shop'] = $shop->id;
/* Get cover */
$attrgrps = $obj->getAttributesGroups((int) $id_lang);
foreach ($attrgrps as $attrgrp) {
if ($attrgrp['id_product_attribute'] == (int) $products[$i]['id_product_attribute']
&& $images = Product::_getAttributeImageAssociations((int) $attrgrp['id_product_attribute'])
) {
$products[$i]['cover'] = $obj->id . '-' . array_pop($images);
break;
}
}
}
if (!isset($products[$i]['cover']) || !$products[$i]['cover']) {
$images = $obj->getImages((int) $id_lang);
foreach ($images as $image) {
if ($image['cover']) {
$products[$i]['cover'] = $obj->id . '-' . $image['id_image'];
break;
}
}
}
if (!isset($products[$i]['cover'])) {
$products[$i]['cover'] = Language::getIsoById($id_lang) . '-default';
}
$products[$i]['link'] = $obj->getLink();
$products[$i]['link_rewrite'] = $obj->link_rewrite;
}
return $products;
}
public static function sendCustomerAlert($id_product, $id_product_attribute)
{
$link = new Link();
$context = Context::getContext()->cloneContext();
$customers = self::getCustomers($id_product, $id_product_attribute);
foreach ($customers as $customer) {
$id_shop = (int) $customer['id_shop'];
$id_lang = (int) $customer['id_lang'];
$context->shop->id = $id_shop;
$context->language->id = $id_lang;
$product = new Product((int) $id_product, false, $id_lang, $id_shop);
$product_link = $link->getProductLink($product, $product->link_rewrite, null, null, $id_lang, $id_shop);
$template_vars = array(
'{product}' => (is_array($product->name) ? $product->name[$id_lang] : $product->name),
'{product_link}' => $product_link,
);
if ($customer['id_customer']) {
$customer = new Customer((int) $customer['id_customer']);
$customer_email = $customer->email;
$customer_id = (int) $customer->id;
} else {
$customer_id = 0;
$customer_email = $customer['customer_email'];
}
$iso = Language::getIsoById($id_lang);
if (file_exists(dirname(__FILE__) . '/mails/' . $iso . '/customer_qty.txt') &&
file_exists(dirname(__FILE__) . '/mails/' . $iso . '/customer_qty.html')) {
Mail::Send(
$id_lang,
'customer_qty',
Mail::l('Product available', $id_lang),
$template_vars,
(string) $customer_email,
null,
(string) Configuration::get('PS_SHOP_EMAIL', null, null, $id_shop),
(string) Configuration::get('PS_SHOP_NAME', null, null, $id_shop),
null,
null,
dirname(__FILE__) . '/mails/',
false,
$id_shop
);
}
Hook::exec(
'actionModuleMailAlertSendCustomer',
array('product' => (is_array($product->name) ? $product->name[$id_lang] : $product->name),
'link' => $product_link,
'customer' => $customer,
'product_obj' => $product, )
);
self::deleteAlert((int) $customer_id, (string) $customer_email, (int) $id_product, (int) $id_product_attribute, $id_shop);
}
}
/*
* Generate correctly the address for an email
*/
public static function getFormatedAddress(Address $address, $line_sep, $fields_style = array())
{
return AddressFormat::generateAddress($address, array('avoid' => array()), $line_sep, ' ', $fields_style);
}
/*
* Get products according to alerts
*/
public static function getProducts($customer, $id_lang)
{
$list_shop_ids = Shop::getContextListShopID(false);
$sql = '
SELECT ma.`id_product`, p.`quantity` AS product_quantity, pl.`name`, ma.`id_product_attribute`
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '` ma
JOIN `' . _DB_PREFIX_ . 'product` p ON (p.`id_product` = ma.`id_product`)
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.id_shop IN (' . implode(', ', $list_shop_ids) . '))
WHERE product_shop.`active` = 1
AND (ma.`id_customer` = ' . (int) $customer->id . ' OR ma.`customer_email` = \'' . pSQL($customer->email) . '\')
AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestriction(false, 'ma');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
/*
* Get product combinations
*/
public static function getProductAttributeCombination($id_product_attribute, $id_lang)
{
$sql = '
SELECT al.`name` AS attribute_name
FROM `' . _DB_PREFIX_ . 'product_attribute_combination` pac
LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON (a.`id_attribute` = pac.`id_attribute`)
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON (ag.`id_attribute_group` = a.`id_attribute_group`)
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int) $id_lang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . (int) $id_lang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
' . Shop::addSqlAssociation('product_attribute', 'pa') . '
WHERE pac.`id_product_attribute` = ' . (int) $id_product_attribute;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
/*
* Get customers waiting for alert on the specified product/product attribute
*/
public static function getCustomers($id_product, $id_product_attribute)
{
$sql = '
SELECT id_customer, customer_email, id_shop, id_lang
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE `id_product` = ' . (int) $id_product . ' AND `id_product_attribute` = ' . (int) $id_product_attribute;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
}