/
Standard.php
371 lines (318 loc) · 12.5 KB
/
Standard.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2015-2024
* @package Controller
* @subpackage Customer
*/
namespace Aimeos\Controller\Jobs\Customer\Email\Watch;
/**
* Product notification e-mail job controller.
*
* @package Controller
* @subpackage Customer
*/
class Standard
extends \Aimeos\Controller\Jobs\Base
implements \Aimeos\Controller\Jobs\Iface
{
/** controller/jobs/customer/email/watch/name
* Class name of the used product notification e-mail scheduler controller implementation
*
* Each default job controller can be replace by an alternative imlementation.
* To use this implementation, you have to set the last part of the class
* name as configuration value so the controller factory knows which class it
* has to instantiate.
*
* For example, if the name of the default class is
*
* \Aimeos\Controller\Jobs\Customer\Email\Watch\Standard
*
* and you want to replace it with your own version named
*
* \Aimeos\Controller\Jobs\Customer\Email\Watch\Mywatch
*
* then you have to set the this configuration option:
*
* controller/jobs/customer/email/watch/name = Mywatch
*
* The value is the last part of your own class name and it's case sensitive,
* so take care that the configuration value is exactly named like the last
* part of the class name.
*
* The allowed characters of the class name are A-Z, a-z and 0-9. No other
* characters are possible! You should always start the last part of the class
* name with an upper case character and continue only with lower case characters
* or numbers. Avoid chamel case names like "MyWatch"!
*
* @param string Last part of the class name
* @since 2014.03
*/
/** controller/jobs/customer/email/watch/decorators/excludes
* Excludes decorators added by the "common" option from the customer email watch controllers
*
* Decorators extend the functionality of a class by adding new aspects
* (e.g. log what is currently done), executing the methods of the underlying
* class only in certain conditions (e.g. only for logged in users) or
* modify what is returned to the caller.
*
* This option allows you to remove a decorator added via
* "controller/jobs/common/decorators/default" before they are wrapped
* around the job controller.
*
* controller/jobs/customer/email/watch/decorators/excludes = array( 'decorator1' )
*
* This would remove the decorator named "decorator1" from the list of
* common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
* "controller/jobs/common/decorators/default" to this job controller.
*
* @param array List of decorator names
* @since 2015.09
* @see controller/jobs/common/decorators/default
* @see controller/jobs/customer/email/watch/decorators/global
* @see controller/jobs/customer/email/watch/decorators/local
*/
/** controller/jobs/customer/email/watch/decorators/global
* Adds a list of globally available decorators only to the customer email watch controllers
*
* Decorators extend the functionality of a class by adding new aspects
* (e.g. log what is currently done), executing the methods of the underlying
* class only in certain conditions (e.g. only for logged in users) or
* modify what is returned to the caller.
*
* This option allows you to wrap global decorators
* ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
*
* controller/jobs/customer/email/watch/decorators/global = array( 'decorator1' )
*
* This would add the decorator named "decorator1" defined by
* "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller.
*
* @param array List of decorator names
* @since 2015.09
* @see controller/jobs/common/decorators/default
* @see controller/jobs/customer/email/watch/decorators/excludes
* @see controller/jobs/customer/email/watch/decorators/local
*/
/** controller/jobs/customer/email/watch/decorators/local
* Adds a list of local decorators only to the customer email watch controllers
*
* Decorators extend the functionality of a class by adding new aspects
* (e.g. log what is currently done), executing the methods of the underlying
* class only in certain conditions (e.g. only for logged in users) or
* modify what is returned to the caller.
*
* This option allows you to wrap local decorators
* ("\Aimeos\Controller\Jobs\Customer\Email\Watch\Decorator\*") around this job controller.
*
* controller/jobs/customer/email/watch/decorators/local = array( 'decorator2' )
*
* This would add the decorator named "decorator2" defined by
* "\Aimeos\Controller\Jobs\Customer\Email\Watch\Decorator\Decorator2" only to this job
* controller.
*
* @param array List of decorator names
* @since 2015.09
* @see controller/jobs/common/decorators/default
* @see controller/jobs/customer/email/watch/decorators/excludes
* @see controller/jobs/customer/email/watch/decorators/global
*/
use \Aimeos\Controller\Jobs\Mail;
private array $sites = [];
/**
* Returns the localized name of the job.
*
* @return string Name of the job
*/
public function getName() : string
{
return $this->context()->translate( 'controller/jobs', 'Product notification e-mails' );
}
/**
* Returns the localized description of the job.
*
* @return string Description of the job
*/
public function getDescription() : string
{
return $this->context()->translate( 'controller/jobs', 'Sends e-mails for watched products' );
}
/**
* Executes the job.
*
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs
*/
public function run()
{
$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
$search = $manager->filter( true );
$func = $search->make( 'customer:has', ['product', 'watch'] );
$search->add( $search->is( $func, '!=', null ) )->order( 'customer.id' );
$start = 0;
do
{
$customers = $manager->search( $search->slice( $start ), ['product' => ['watch'], 'price'] );
$customers = $this->notify( $customers );
$customers = $manager->save( $customers );
$count = count( $customers );
$start += $count;
}
while( $count >= $search->getLimit() );
}
/**
* Sends product notifications for the given customers in their language
*
* @param \Aimeos\Map $customers List of customer items implementing \Aimeos\MShop\Customer\Item\Iface
* @return \Aimeos\Map List of customer items implementing \Aimeos\MShop\Customer\Item\Iface
*/
protected function notify( \Aimeos\Map $customers ) : \Aimeos\Map
{
$date = date( 'Y-m-d H:i:s' );
$context = $this->context();
foreach( $customers as $customer )
{
$listItems = $customer->getListItems( 'product', null, null, false );
$products = $this->products( $listItems );
try
{
if( !empty( $products ) )
{
$sites = $this->sites( $customer->getSiteId() );
$context->locale()->setLanguageId( $customer->getPaymentAddress()->getLanguageId() );
$view = $this->view( $customer->getPaymentAddress(), $sites->getTheme()->filter()->last() );
$view->products = $products;
$this->send( $view, $customer->getPaymentAddress(), $sites->getLogo()->filter()->last() );
}
$str = sprintf( 'Sent product notification e-mail to "%1$s"', $customer->getPaymentAddress()->getEmail() );
$context->logger()->debug( $str, 'email/customer/watch' );
}
catch( \Exception $e )
{
$str = 'Error while trying to send product notification e-mail for customer ID "%1$s": %2$s';
$msg = sprintf( $str, $customer->getId(), $e->getMessage() ) . PHP_EOL . $e->getTraceAsString();
$context->logger()->error( $msg, 'email/customer/watch' );
}
$remove = $listItems->diffKeys( $products )->filter( function( $listItem ) use ( $date ) {
return $listItem->getDateEnd() < $date;
} );
$customer->deleteListItems( $remove );
}
return $customers;
}
/**
* Returns a filtered list of products for which a notification should be sent
*
* @param \Aimeos\Map $listItems List of customer list items
* @return array Associative list of list IDs as key and product items values
*/
protected function products( \Aimeos\Map $listItems ) : array
{
$result = [];
$priceManager = \Aimeos\MShop::create( $this->context(), 'price' );
foreach( $listItems as $id => $listItem )
{
try
{
if( $product = $listItem->getRefItem() )
{
$config = $listItem->getConfig();
$prices = $product->getRefItems( 'price', 'default', 'default' );
$price = $priceManager->getLowestPrice( $prices, 1, $config['currency'] ?? null );
if( ( $config['stock'] ?? null ) && $product->inStock()
|| ( $config['price'] ?? null ) && ( $config['pricevalue'] ?? 0 ) > $price->getValue()
) {
$result[$id] = $product->set( 'price', $price );
}
}
}
catch( \Exception $e ) { ; } // no price available
}
return $result;
}
/**
* Sends the notification e-mail for the given customer address and products
*
* @param \Aimeos\Base\View\Iface $view View object
* @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
* @param string|null $logoPath Path to the logo
*/
protected function send( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null )
{
/** controller/jobs/customer/email/watch/template-html
* Relative path to the template for the HTML part of the watch emails.
*
* The template file contains the HTML code and processing instructions
* to generate the result shown in the body of the frontend. The
* configuration string is the path to the template file relative
* to the templates directory (usually in templates/controller/jobs).
* You can overwrite the template file configuration in extensions and
* provide alternative templates.
*
* @param string Relative path to the template
* @since 2022.04
* @see controller/jobs/customer/email/watch/template-text
*/
/** controller/jobs/customer/email/watch/template-text
* Relative path to the template for the text part of the watch emails.
*
* The template file contains the text and processing instructions
* to generate the result shown in the body of the frontend. The
* configuration string is the path to the template file relative
* to the templates directory (usually in templates/controller/jobs).
* You can overwrite the template file configuration in extensions and
* provide alternative templates.
*
* @param string Relative path to the template
* @since 2022.04
* @see controller/jobs/customer/email/watch/template-html
*/
$context = $this->context();
$config = $context->config();
$msg = $this->call( 'mailTo', $address );
$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
$msg->subject( $context->translate( 'client', 'Your watched products' ) )
->html( $view->render( $config->get( 'controller/jobs/customer/email/watch/template-html', 'customer/email/watch/html' ) ) )
->text( $view->render( $config->get( 'controller/jobs/customer/email/watch/template-text', 'customer/email/watch/text' ) ) )
->send();
}
/**
* Returns the list of site items from the given site ID up to the root site
*
* @param string|null $siteId Site ID like "1.2.4."
* @return \Aimeos\Map List of site items
*/
protected function sites( string $siteId = null ) : \Aimeos\Map
{
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
if( !$siteId && !isset( $this->sites[''] ) )
{
$default = $this->context()->config()->get( 'mshop/locale/site', 'default' );
$this->sites[''] = map( $manager->find( $default ) );
}
if( !isset( $this->sites[(string) $siteId] ) )
{
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
$siteIds = explode( '.', trim( (string) $siteId, '.' ) );
$this->sites[$siteId] = $manager->getPath( end( $siteIds ) );
}
return $this->sites[$siteId];
}
/**
* Returns the view populated with common data
*
* @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
* @param string|null $theme Theme name
* @return \Aimeos\Base\View\Iface View object
*/
protected function view( \Aimeos\MShop\Common\Item\Address\Iface $address, string $theme = null ) : \Aimeos\Base\View\Iface
{
$view = $this->call( 'mailView', $address->getLanguageId() );
$view->intro = $this->call( 'mailIntro', $address );
$view->css = $this->call( 'mailCss', $theme );
$view->urlparams = [
'site' => $this->context()->locale()->getSiteItem()->getCode(),
'locale' => $address->getLanguageId(),
];
return $view;
}
}