/
Default.php
533 lines (466 loc) · 19.1 KB
/
Default.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2012
* @license LGPLv3, http://www.arcavias.com/en/license
* @package Client
* @subpackage Html
*/
/**
* Default implementation of catalog detail section HTML clients.
*
* @package Client
* @subpackage Html
*/
class Client_Html_Catalog_Detail_Default
extends Client_Html_Abstract
{
/** client/html/catalog/detail/default/subparts
* List of HTML sub-clients rendered within the catalog detail section
*
* The output of the frontend is composed of the code generated by the HTML
* clients. Each HTML client can consist of serveral (or none) sub-clients
* that are responsible for rendering certain sub-parts of the output. The
* sub-clients can contain HTML clients themselves and therefore a
* hierarchical tree of HTML clients is composed. Each HTML client creates
* the output that is placed inside the container of its parent.
*
* At first, always the HTML code generated by the parent is printed, then
* the HTML code of its sub-clients. The order of the HTML sub-clients
* determines the order of the output of these sub-clients inside the parent
* container. If the configured list of clients is
*
* array( "subclient1", "subclient2" )
*
* you can easily change the order of the output by reordering the subparts:
*
* client/html/<clients>/subparts = array( "subclient1", "subclient2" )
*
* You can also remove one or more parts if they shouldn't be rendered:
*
* client/html/<clients>/subparts = array( "subclient1" )
*
* As the clients only generates structural HTML, the layout defined via CSS
* should support adding, removing or reordering content by a fluid like
* design.
*
* @param array List of sub-client names
* @since 2014.03
* @category Developer
*/
private $_subPartPath = 'client/html/catalog/detail/default/subparts';
/** client/html/catalog/detail/social/name
* Name of the social part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Social_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.09
* @category Developer
*/
/** client/html/catalog/detail/image/name
* Name of the image part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Image_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
/** client/html/catalog/detail/basic/name
* Name of the basic part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Basic_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
/** client/html/catalog/detail/actions/name
* Name of the actions part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Actions_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.09
* @category Developer
*/
/** client/html/catalog/detail/basket/name
* Name of the basket part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Basket_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
/** client/html/catalog/detail/bundle/name
* Name of the bundle part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Bundle_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.11
* @category Developer
*/
/** client/html/catalog/detail/additional/name
* Name of the additional part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Additional_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
/** client/html/catalog/detail/suggest/name
* Name of the suggest part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Suggest_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
/** client/html/catalog/detail/seen/name
* Name of the seen part used by the catalog detail client implementation
*
* Use "Myname" if your class is named "Client_Html_Catalog_Detail_Seen_Myname".
* The name is case-sensitive and you should avoid camel case names like "MyName".
*
* @param string Last part of the client class name
* @since 2014.03
* @category Developer
*/
private $_subPartNames = array( 'social', 'image', 'basic', 'actions', 'basket', 'bundle', 'additional', 'suggest', 'seen' );
private $_tags = array();
private $_expire;
private $_cache;
private $_view;
/**
* Returns the HTML code for insertion into the body.
*
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page
* @param array &$tags Result array for the list of tags that are associated to the output
* @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
* @return string HTML code
*/
public function getBody( $uid = '', array &$tags = array(), &$expire = null )
{
if( ( $html = $this->_getCached( 'body', $uid ) ) === null )
{
$context = $this->_getContext();
$view = $this->getView();
try
{
$view = $this->_setViewParams( $view, $tags, $expire );
$output = '';
foreach( $this->_getSubClients() as $subclient ) {
$output .= $subclient->setView( $view )->getBody( $uid, $tags, $expire );
}
$view->detailBody = $output;
}
catch( Client_Html_Exception $e )
{
$error = array( $context->getI18n()->dt( 'client/html', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( Controller_Frontend_Exception $e )
{
$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( MShop_Exception $e )
{
$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( Exception $e )
{
$context->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
$error = array( $context->getI18n()->dt( 'client/html', 'A non-recoverable error occured' ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
/** client/html/catalog/detail/default/template-body
* Relative path to the HTML body template of the catalog detail client.
*
* 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 layouts directory (usually in client/html/layouts).
*
* You can overwrite the template file configuration in extensions and
* provide alternative templates. These alternative templates should be
* named like the default one but with the string "default" replaced by
* an unique name. You may use the name of your project for this. If
* you've implemented an alternative client class as well, "default"
* should be replaced by the name of the new class.
*
* @param string Relative path to the template creating code for the HTML page body
* @since 2014.03
* @category Developer
* @see client/html/catalog/detail/default/template-header
*/
$tplconf = 'client/html/catalog/detail/default/template-body';
$default = 'catalog/detail/body-default.html';
$html = $view->render( $this->_getTemplate( $tplconf, $default ) );
$this->_setCached( 'body', $uid, $html, $tags, $expire );
}
else
{
$html = $this->modifyBody( $html, $uid );
}
return $html;
}
/**
* Returns the HTML string for insertion into the header.
*
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page
* @param array &$tags Result array for the list of tags that are associated to the output
* @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
* @return string String including HTML tags for the header
*/
public function getHeader( $uid = '', array &$tags = array(), &$expire = null )
{
if( ( $html = $this->_getCached( 'header', $uid ) ) === null )
{
$context = $this->_getContext();
$view = $this->getView();
try
{
$view = $this->_setViewParams( $view, $tags, $expire );
$output = '';
foreach( $this->_getSubClients() as $subclient ) {
$output .= $subclient->setView( $view )->getHeader( $uid, $tags, $expire );
}
$view->detailHeader = $output;
/** client/html/catalog/detail/default/template-header
* Relative path to the HTML header template of the catalog detail client.
*
* The template file contains the HTML code and processing instructions
* to generate the HTML code that is inserted into the HTML page header
* of the rendered page in the frontend. The configuration string is the
* path to the template file relative to the layouts directory (usually
* in client/html/layouts).
*
* You can overwrite the template file configuration in extensions and
* provide alternative templates. These alternative templates should be
* named like the default one but with the string "default" replaced by
* an unique name. You may use the name of your project for this. If
* you've implemented an alternative client class as well, "default"
* should be replaced by the name of the new class.
*
* @param string Relative path to the template creating code for the HTML page head
* @since 2014.03
* @category Developer
* @see client/html/catalog/detail/default/template-body
*/
$tplconf = 'client/html/catalog/detail/default/template-header';
$default = 'catalog/detail/header-default.html';
$html = $view->render( $this->_getTemplate( $tplconf, $default ) );
$this->_setCached( 'header', $uid, $html, $tags, $expire );
}
catch( Exception $e )
{
$context->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
}
}
else
{
$html = $this->modifyHeader( $html, $uid );
}
return $html;
}
/**
* Returns the sub-client given by its name.
*
* @param string $type Name of the client type
* @param string|null $name Name of the sub-client (Default if null)
* @return Client_Html_Interface Sub-client object
*/
public function getSubClient( $type, $name = null )
{
return $this->_createSubClient( 'catalog/detail/' . $type, $name );
}
/**
* Processes the input, e.g. store given values.
* A view must be available and this method doesn't generate any output
* besides setting view variables.
*/
public function process()
{
$context = $this->_getContext();
$view = $this->getView();
try
{
$params = $this->_getClientParams( $view->param() );
$context->getSession()->set( 'arcavias/catalog/detail/params/last', $params );
parent::process();
}
catch( Client_Html_Exception $e )
{
$error = array( $this->_getContext()->getI18n()->dt( 'client/html', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( Controller_Frontend_Exception $e )
{
$error = array( $this->_getContext()->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( MShop_Exception $e )
{
$error = array( $this->_getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
catch( Exception $e )
{
$context->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
$error = array( $context->getI18n()->dt( 'client/html', 'A non-recoverable error occured' ) );
$view->detailErrorList = $view->get( 'detailErrorList', array() ) + $error;
}
}
/**
* Returns the cache entry for the given unique ID and type.
*
* @param string $type Type of the cache entry, i.e. "body" or "header"
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page
* @return string Cached entry or empty string if not available
*/
protected function _getCached( $type, $uid )
{
if( !isset( $this->_cache ) )
{
$context = $this->_getContext();
$config = $context->getConfig()->get( 'client/html/catalog/detail', array() );
$keys = array(
'body' => $this->_getParamHash( array( 'd' ), $uid . ':catalog:detail-body', $config ),
'header' => $this->_getParamHash( array( 'd' ), $uid . ':catalog:detail-header', $config ),
);
$entries = $context->getCache()->getList( $keys );
$this->_cache = array();
foreach( $keys as $key => $hash ) {
$this->_cache[$key] = ( array_key_exists( $hash, $entries ) ? $entries[$hash] : null );
}
}
return ( array_key_exists( $type, $this->_cache ) ? $this->_cache[$type] : null );
}
/**
* Returns the cache entry for the given type and unique ID.
*
* @param string $type Type of the cache entry, i.e. "body" or "header"
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page
* @param string $value Value string that should be stored for the given key
* @param array $tags List of tag strings that should be assoicated to the
* given value in the cache
* @param string|null $expire Date/time string in "YYYY-MM-DD HH:mm:ss"
* format when the cache entry expires
*/
protected function _setCached( $type, $uid, $value, array $tags, $expire )
{
$context = $this->_getContext();
try
{
$config = $context->getConfig()->get( 'client/html/catalog/detail', array() );
$key = $this->_getParamHash( array( 'd' ), $uid . ':catalog:detail-' . $type, $config );
$context->getCache()->set( $key, $value, array_unique( $tags ), $expire );
}
catch( Exception $e )
{
$msg = sprintf( 'Unable to set cache entry: %1$s', $e->getMessage() );
$context->getLogger()->log( $msg, MW_Logger_Abstract::NOTICE );
}
}
/**
* Returns the list of sub-client names configured for the client.
*
* @return array List of HTML client names
*/
protected function _getSubClientNames()
{
return $this->_getContext()->getConfig()->get( $this->_subPartPath, $this->_subPartNames );
}
/**
* Sets the necessary parameter values in the view.
*
* @param MW_View_Interface $view The view object which generates the HTML output
* @param array &$tags Result array for the list of tags that are associated to the output
* @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
* @return MW_View_Interface Modified view object
*/
protected function _setViewParams( MW_View_Interface $view, array &$tags = array(), &$expire = null )
{
if( !isset( $this->_view ) )
{
$context = $this->_getContext();
$config = $context->getConfig();
$prodid = $view->param( 'd-product-id' );
$default = array( 'media', 'price', 'text', 'attribute', 'product' );
/** client/html/catalog/domains
* A list of domain names whose items should be available in the catalog view templates
*
* @see client/html/catalog/detail/domains
*/
$domains = $config->get( 'client/html/catalog/domains', $default );
/** client/html/catalog/detail/domains
* A list of domain names whose items should be available in the product detail view template
*
* The templates rendering product details usually add the images,
* prices, texts, attributes, products, etc. associated to the product
* item. If you want to display additional or less content, you can
* configure your own list of domains (attribute, media, price, product,
* text, etc. are domains) whose items are fetched from the storage.
* Please keep in mind that the more domains you add to the configuration,
* the more time is required for fetching the content!
*
* Since version 2014.05 this configuration option overwrites the
* "client/html/catalog/domains" option that allows to configure the
* domain names of the items fetched for all catalog related data.
*
* @param array List of domain names
* @since 2014.03
* @category Developer
* @see client/html/catalog/domains
* @see client/html/catalog/list/domains
*/
$domains = $config->get( 'client/html/catalog/detail/domains', $domains );
$manager = MShop_Factory::createManager( $context, 'product' );
$productItem = $manager->getItem( $prodid, $domains );
$this->_addMetaItem( $productItem, 'product', $this->_expire, $this->_tags );
$this->_addMetaList( $prodid, 'product', $this->_expire );
$attrManager = MShop_Factory::createManager( $context, 'attribute' );
$attrSearch = $attrManager->createSearch( true );
$expr = array(
$attrSearch->compare( '==', 'attribute.id', array_keys( $productItem->getRefItems( 'attribute' ) ) ),
$attrSearch->getConditions(),
);
$attrSearch->setConditions( $attrSearch->combine( '&&', $expr ) );
$attributes = $attrManager->searchItems( $attrSearch, $default );
$this->_addMetaItem( $attributes, 'attribute', $this->_expire, $this->_tags );
$this->_addMetaList( array_keys( $attributes ), 'attribute', $this->_expire );
$mediaManager = MShop_Factory::createManager( $context, 'media' );
$mediaSearch = $mediaManager->createSearch( true );
$expr = array(
$mediaSearch->compare( '==', 'media.id', array_keys( $productItem->getRefItems( 'media' ) ) ),
$mediaSearch->getConditions(),
);
$mediaSearch->setConditions( $mediaSearch->combine( '&&', $expr ) );
$media = $mediaManager->searchItems( $mediaSearch, $default );
$this->_addMetaItem( $media, 'media', $this->_expire, $this->_tags );
$this->_addMetaList( array_keys( $media ), 'media', $this->_expire );
$view->detailProductItem = $productItem;
$view->detailProductAttributeItems = $attributes;
$view->detailProductMediaItems = $media;
$view->detailParams = $this->_getClientParams( $view->param() );
$this->_view = $view;
}
$expire = $this->_expires( $this->_expire, $expire );
$tags = array_merge( $tags, $this->_tags );
return $this->_view;
}
}