/
ProductController.php
887 lines (732 loc) · 25.4 KB
/
ProductController.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
<?php
ClassLoader::import('application.model.product.Product');
ClassLoader::import('application.controller.FrontendController');
ClassLoader::import('application.controller.CategoryController');
ClassLoader::import('application.model.presentation.CategoryPresentation', true);
ClassLoader::importNow('application.helper.CreateHandleString');
/**
*
* @author Integry Systems
* @package application.controller
*/
class ProductController extends FrontendController
{
public $filters = array();
public function init()
{
parent::init();
$this->addBlock('PRODUCT-ATTRIBUTE-SUMMARY', 'attributeSummary', 'product/block/attributeSummary');
$this->addBlock('PRODUCT-PURCHASE', 'purchase', 'product/block/purchase');
$this->addBlock('PRODUCT-PRICE', 'price', 'product/block/price');
$this->addBlock('PRODUCT-UP-SELL', 'upSell', 'product/block/upsell');
$this->addBlock('PRODUCT-OPTIONS', 'options', 'product/block/options');
$this->addBlock('PRODUCT-VARIATIONS', 'variations', 'product/block/variations');
$this->addBlock('PRODUCT-TO-CART', 'addToCart', 'product/block/toCart');
$this->addBlock('PRODUCT-ACTIONS', 'actions', 'product/block/actions');
$this->addBlock('PRODUCT-IMAGES', 'images', 'product/block/images');
$this->addBlock('PRODUCT-SUMMARY', 'summary', 'product/block/summary');
$this->addBlock('PRODUCT-MAININFO', 'mainInfo', 'product/block/mainInfo');
$this->addBlock('PRODUCT-OVERVIEW', 'overview', 'product/block/overview');
$this->addBlock('PRODUCT-RATING-SUMMARY', 'ratingSummary', 'product/ratingSummary');
$this->addBlock('PRODUCT-PURCHASE-VARIATIONS', 'purchaseVariations', 'product/block/purchaseVariations');
}
public function index()
{
$this->loadLanguageFile('Category');
$product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA, array('ProductImage', 'Manufacturer', 'Category'));
$this->product = $product;
if (!$product->isEnabled->get() || $product->parent->get())
{
throw new ARNotFoundException('Product', $product->getID());
}
$product->loadPricing();
$this->category = $product->getCategory();
$this->categoryID = $product->getCategory()->getID();
// get category path for breadcrumb
$path = $product->category->get()->getPathNodeArray();
include_once(ClassLoader::getRealPath('application.helper.smarty') . '/function.categoryUrl.php');
include_once(ClassLoader::getRealPath('application.helper.smarty') . '/function.productUrl.php');
foreach ($path as $nodeArray)
{
$url = createCategoryUrl(array('data' => $nodeArray), $this->application);
if ($nodeArray['isEnabled'])
{
$this->addBreadCrumb($nodeArray['name_lang'], $url);
}
}
// add filters to breadcrumb
CategoryController::getAppliedFilters();
// for root category products
if (!isset($nodeArray))
{
$nodeArray = array();
}
$params = array('data' => $nodeArray, 'filters' => array());
foreach ($this->filters as $filter)
{
$f = $filter->toArray();
$params['filters'][] = $f;
$url = createCategoryUrl($params, $this->application);
$this->addBreadCrumb($f['name_lang'], $url);
}
$productArray = $product->toArray();
$handle = empty($productArray['URL']) ? $productArray['name_lang'] : $productArray['URL'];
$this->redirect301($this->request->get('producthandle'), createHandleString($handle));
//ProductSpecification::loadSpecificationForProductArray($productArray);
// filter empty attributes
foreach ($productArray['attributes'] as $key => $attr)
{
if ((empty($attr['value']) && empty($attr['values']) && empty($attr['value_lang'])))
{
unset($productArray['attributes'][$key]);
}
}
// attribute summary
$productArray['listAttributes'] = array();
foreach ($productArray['attributes'] as $key => $attr)
{
if ($attr['SpecField']['isDisplayedInList'])
{
$productArray['listAttributes'][] = $attr;
}
if (!$attr['SpecField']['isDisplayed'])
{
unset($productArray['attributes'][$key]);
}
}
// add product title to breacrumb
$this->addBreadCrumb($productArray['name_lang'], createProductUrl(array('product' => $productArray), $this->application));
// manufacturer filter
if ($product->manufacturer->get())
{
$manFilter = new ManufacturerFilter($product->manufacturer->get()->getID(), $product->manufacturer->get()->name->get());
}
// get category page route
end($this->breadCrumb);
$last = prev($this->breadCrumb);
$catRoute = $this->router->getRouteFromUrl($last['url']);
$response = new ActionResponse();
$response->set('product', $productArray);
$response->set('category', $productArray['Category']);
$response->set('quantity', $this->getQuantities($product));
$response->set('currency', $this->request->get('currency', $this->application->getDefaultCurrencyCode()));
$response->set('catRoute', $catRoute);
// ratings
if ($this->config->get('ENABLE_RATINGS'))
{
if ($product->ratingCount->get() > 0)
{
// rating summaries
ClassLoader::import('application.model.product.ProductRatingSummary');
$response->set('rating', ProductRatingSummary::getProductRatingsArray($product));
}
ClassLoader::import('application.model.category.ProductRatingType');
$ratingTypes = ProductRatingType::getProductRatingTypeArray($product);
$response->set('ratingTypes', $ratingTypes);
$response->set('ratingForm', $this->buildRatingForm($ratingTypes, $product));
$response->set('isRated', $this->isRated($product));
$response->set('isLoginRequiredToRate', $this->isLoginRequiredToRate());
$response->set('isPurchaseRequiredToRate', $this->isPurchaseRequiredToRate($product));
}
// add to cart form
$response->set('cartForm', $this->buildAddToCartForm($this->getOptions(), $this->getVariations()));
// related products
$related = $this->getRelatedProducts($product);
// items purchased together
$together = $product->getProductsPurchasedTogether($this->config->get('NUM_PURCHASED_TOGETHER'), true);
$spec = array();
foreach ($related as $key => $group)
{
foreach ($related[$key] as $i => &$prod)
{
$spec[] =& $related[$key][$i];
}
}
foreach ($together as &$prod)
{
$spec[] =& $prod;
}
ProductSpecification::loadSpecificationForRecordSetArray($spec);
$response->set('related', $related);
$response->set('together', $together);
if (isset($manFilter))
{
$response->set('manufacturerFilter', $manFilter);
}
$response->set('variations', $this->getVariations());
// reviews
if ($this->config->get('ENABLE_REVIEWS') && $product->reviewCount->get() && ($numReviews = $this->config->get('NUM_REVIEWS_IN_PRODUCT_PAGE')))
{
$f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('ProductReview', 'isEnabled'), true));
$f->setLimit($numReviews);
$reviews = $product->getRelatedRecordSetArray('ProductReview', $f);
$this->pullRatingDetailsForReviewArray($reviews);
$response->set('reviews', $reviews);
}
// bundled products
if ($product->isBundle())
{
$bundleData = ProductBundle::getBundledProductArray($product);
$bundledProducts = array();
foreach ($bundleData as &$bundled)
{
$bundledProducts[] =& $bundled['RelatedProduct'];
}
ProductPrice::loadPricesForRecordSetArray($bundledProducts);
$response->set('bundleData', $bundleData);
$currency = Currency::getInstanceByID($this->getRequestCurrency());
$total = ProductBundle::getTotalBundlePrice($product, $currency);
$response->set('bundleTotal', $currency->getFormattedPrice($total));
$saving = $total - $product->getPrice($currency);
$response->set('bundleSavingTotal', $currency->getFormattedPrice($saving));
$response->set('bundleSavingPercent', $total ? round(($saving / $total) * 100) : 0);
}
// contact form
if ($this->config->get('PRODUCT_INQUIRY_FORM'))
{
$response->set('contactForm', $this->buildContactForm());
}
// display theme
if ($theme = CategoryPresentation::getThemeByProduct($product))
{
if ($theme->getTheme())
{
$this->application->setTheme($theme->getTheme());
}
$response->set('presentation', $theme->toFlatArray());
}
// product images
$images = $product->getImageArray();
if ($theme && $theme->isVariationImages->get())
{
if ($variations = $this->getVariations())
{
foreach ($variations['products'] as $prod)
{
if (!empty($prod['DefaultImage']['ID']))
{
$images[] = $prod['DefaultImage'];
}
}
}
}
$response->set('images', $images);
// discounted pricing
$response->set('quantityPricing', $product->getPricingHandler()->getDiscountPrices($this->user, $this->getRequestCurrency()));
$response->set('files', $this->getPublicFiles());
// additional categories
$f = new ARSelectFilter();
$f->setOrder(new ARFieldHandle('Category', 'lft'));
$f->mergeCondition(new EqualsCond(new ARFieldHandle('Category', 'isEnabled') , true));
$pathC = new OrChainCondition();
$pathF = new ARSelectFilter($pathC);
$categories = array();
foreach ($product->getRelatedRecordSetArray('ProductCategory', $f, array('Category')) as $cat)
{
$categories[] = array($cat['Category']);
$cond = new OperatorCond(new ARFieldHandle('Category', 'lft'), $cat['Category']['lft'], "<");
$cond->addAND(new OperatorCond(new ARFieldHandle('Category', 'rgt'), $cat['Category']['rgt'], ">"));
$pathC->addAnd($cond);
}
if ($categories)
{
$pathF->setOrder(new ARFieldHandle('Category', 'lft') , 'DESC');
$pathF->mergeCondition(new EqualsCond(new ARFieldHandle('Category', 'isEnabled'), true));
foreach (ActiveRecordModel::getRecordSetArray('Category', $pathF, array('Category')) as $parent)
{
if (!$parent['isEnabled'])
{
continue;
}
foreach ($categories as &$cat)
{
if (($cat[0]['lft'] > $parent['lft']) && ($cat[0]['rgt'] < $parent['rgt']) && ($parent['ID'] > Category::ROOT_ID))
{
$cat[] = $parent;
}
}
}
foreach ($categories as &$cat)
{
$cat = array_reverse($cat);
}
$response->set('additionalCategories', $categories);
}
$response->set('enlargeProductThumbnailOnMouseOver',
$this->config->get('_ENLARGE_PRODUCT_THUMBNAILS_ON') == 'P_THUMB_ENLARGE_MOUSEOVER');
return $response;
}
public function variations()
{
$currency = $this->request->get('currency', $this->application->getDefaultCurrencyCode());
$product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA, array('ProductImage', 'Manufacturer', 'Category'));
$this->product = $product;
$variations = $this->getVariations();
remove_recursion($variations);
echo "new Product.Variations($('variations'), " . json_encode($variations) . ", {currency: '" . $currency . "'});";
}
public function priceBlock()
{
return new BlockResponse();
}
public function addToCartBlock()
{
return new BlockResponse();
}
public function optionsBlock()
{
$response = new BlockResponse();
$response->set('allOptions', $this->getOptions(true));
$response->set('options', $this->getOptions());
return $response;
}
public function variationsBlock()
{
return new BlockResponse('variations', $this->getVariations());
}
public function upSellBlock()
{
// upsell products
$upsell = $this->getRelatedProducts($this->product, /*type:*/ 1);
foreach ($upsell as $key => $group)
{
foreach ($upsell[$key] as $i => &$prod)
{
$spec[] =& $upsell[$key][$i];
}
}
if (count($upsell))
{
ProductSpecification::loadSpecificationForRecordSetArray($spec);
}
$response = new BlockResponse();
$response->set('upsell', $upsell);
return $response;
}
public function overviewBlock()
{
return new BlockResponse();
}
public function actionsBlock()
{
return new BlockResponse();
}
public function purchaseBlock()
{
return new BlockResponse();
}
public function imagesBlock()
{
return new BlockResponse();
}
public function summaryBlock()
{
return new BlockResponse();
}
public function mainInfoBlock()
{
return new BlockResponse();
}
public function ratingSummaryBlock()
{
return new BlockResponse();
}
public function attributeSummaryBlock()
{
return new BlockResponse();
}
public function purchaseVariationsBlock()
{
$variations = $this->getVariations();
if (!$variations)
{
return null;
}
$prefixes = $ids = array();
foreach ($variations['products'] as $product)
{
$prefixes[] = 'product_' . $product['ID'] . '_';
$ids[] = $product['ID'];
}
// load product instances
Product::getRecordSet(select(in('Product.ID', $ids)));
foreach ($variations['products'] as $product)
{
$quant = $this->getQuantities(Product::getInstanceByID($product['ID']));
$quant = array('' => 0) + $quant;
$quantities[$product['ID']] = $quant;
}
// check if there price is the same for all variations
$curr = $this->getRequestCurrency();
$samePrice = true;
foreach ($variations['products'] as $product)
{
if (!empty($product['price_' . $curr]) && (0 != $product['price_' . $curr]))
{
$samePrice = false;
break;
}
}
$response = new BlockResponse('variations', $variations);
$response->set('cartForm', $this->buildAddToCartForm($this->getOptions(), array(), $prefixes));
$response->set('quantities', $quantities);
$response->set('samePrice', $samePrice);
return $response;
}
private function getOptions($all = false)
{
if (!isset($this->allOptions))
{
$this->allOptions = $this->product->getOptionsArray();
$this->options = $this->allOptions;
foreach ($this->options as $key => $option)
{
if (!$option['isDisplayed'])
{
unset($this->options[$key]);
}
}
}
return $all ? $this->allOptions : $this->options;
}
public function getVariations()
{
// variations
if (!isset($this->variations))
{
$this->variations = $this->product->getVariationData($this->application);
}
return $this->variations;
}
public function publicFilesBlock()
{
}
public function rate()
{
$product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA);
$ratingTypes = ProductRatingType::getProductRatingTypes($product);
$validator = $this->buildRatingValidator($ratingTypes->toArray(), $product, true);
$redirect = new ActionRedirectResponse('product', 'index', array('id' => $product->getID()));
if ($validator->isValid())
{
$msg = $this->translate('_msg_rating_added');
if ($this->isAddingReview())
{
$review = ProductReview::getNewInstance($product, $this->user);
$review->loadRequestData($this->request);
$review->ip->set($this->request->getIpLong());
// approval status
$approval = $this->config->get('APPROVE_REVIEWS');
$review->isEnabled->set(('APPROVE_REVIEWS_AUTO' == $approval) || (('APPROVE_REVIEWS_USER' == $approval) && !$this->user->isAnonymous()));
$review->save();
$msg = $this->translate('_msg_review_added');
}
foreach ($ratingTypes as $type)
{
$rating = ProductRating::getNewInstance($product, $type, $this->user);
$rating->rating->set($this->request->get('rating_' . $type->getID()));
if (isset($review))
{
$rating->review->set($review);
}
$rating->ip->set($this->request->getIpLong());
$rating->save();
}
if ($this->isAjax())
{
$response = new JSONResponse(array('message' => $msg), 'success');
}
else
{
$this->setMessage($msg);
$response = $redirect;
}
$response->setCookie('rating_' . $product->getID(), true, strtotime('+' . $this->config->get('RATING_SAME_IP_TIME') . ' hours'), $this->router->getBaseDirFromUrl());
return $response;
}
else
{
if ($this->isAjax())
{
return new JSONResponse(array('errors' => $validator->getErrorList()));
}
else
{
return $redirect;
}
}
}
public function reviews()
{
if (!$this->config->get('ENABLE_REVIEWS'))
{
throw new HTTPStatusException(404);
}
$response = $this->index();
$page = $this->request->get('page', 1);
$perPage = $this->config->get('REVIEWS_PER_PAGE');
$offsetStart = ($this->request->get('page', 1) - 1) * $perPage;
$f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('ProductReview', 'isEnabled'), true));
$f->setLimit($perPage, $offsetStart);
$f->setOrder(new ARFieldHandle('ProductReview', 'dateCreated'), 'DESC');
$reviews = $this->product->getRelatedRecordSetArray('ProductReview', $f);
$this->pullRatingDetailsForReviewArray($reviews);
$response->set('reviews', $reviews);
$response->set('offsetStart', $offsetStart + 1);
$response->set('offsetEnd', min($offsetStart + $perPage, $this->product->reviewCount->get()));
$response->set('page', $page);
$response->set('perPage', $perPage);
$response->set('url', $this->router->createUrl(array('controller' => 'product', 'action' => 'reviews', 'id' => $this->product->getID(), 'page' => '_000_')));
$this->addBreadCrumb($this->translate('_reviews'), '');
return $response;
}
public function sendContactForm()
{
$product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA);
$redirect = new ActionRedirectResponse('product', 'index', array('id' => $product->getID()));
$validator = $this->buildContactValidator();
if ($validator->isValid())
{
$email = new Email($this->application);
$email->setTemplate('contactForm/productInquiry');
$email->setFrom($this->request->get('email'), $this->request->get('name'));
$email->setTo($this->config->get('NOTIFICATION_EMAIL'), $this->config->get('STORE_NAME'));
$email->set('message', $this->request->get('msg'));
$email->set('product', $product->toArray());
$email->send();
$msg = $this->translate('_inquiry_form_sent');
if ($this->isAjax())
{
$response = new JSONResponse(array('message' => $msg), 'success');
}
else
{
$this->setMessage($msg);
$response = $redirect;
}
return $response;
}
else
{
if ($this->isAjax())
{
return new JSONResponse(array('errors' => $validator->getErrorList()));
}
else
{
return $redirect;
}
}
}
public function buildAddToCartValidator($options, $variations, $prefix = '')
{
$validator = $this->getValidator("addToCart", $this->getRequest());
$prefixes = (array)$prefix;
// option validation
foreach ($prefixes as $prefix)
{
foreach ($options as $option)
{
if ($option['isRequired'])
{
$optField = $prefix . 'option_' . $option['ID'];
OrderController::addOptionValidation($validator, $option, $optField);
}
}
if (isset($variations['variations']))
{
foreach ($variations['variations'] as $variation)
{
$validator->addCheck($prefix . 'variation_' . $variation['ID'], new IsNotEmptyCheck($this->translate('_err_option_0')));
}
}
$validator->addCheck($prefix . 'count', new IsNumericCheck(''));
$validator->addFilter($prefix . 'count', new NumericFilter());
}
return $validator;
}
/**
* Allowed shopping cart quantities
*/
private function getQuantities(Product $product)
{
$maxOrderable = $product->getMaxOrderableCount();
$maxQuant = $product->getMinimumQuantity() + (19 * $product->getQuantityStep());
$maxOrderable = is_null($maxOrderable) ? $maxQuant : min($maxQuant, $maxOrderable);
$fractionalStep = $this->product->getParentValue('fractionalStep');
$quantities = range($product->getMinimumQuantity(), $maxOrderable, max($fractionalStep, 1));
return array_combine($quantities, $quantities);
}
private function pullRatingDetailsForReviewArray(&$reviews)
{
$ids = $indexes = array();
foreach ($reviews as $index => $review)
{
$ids[] = $review['ID'];
$indexes[$review['ID']] = $index;
}
$f = new ARSelectFilter(new INCond(new ARFieldHandle('ProductRating', 'reviewID'), $ids));
$f->setOrder(new ARFieldHandle('ProductRatingType', 'position'));
$ratings = ActiveRecordModel::getRecordSetArray('ProductRating', $f, array('ProductRatingType'));
foreach ($ratings as $rating)
{
$review =& $reviews[$indexes[$rating['reviewID']]];
if (!isset($review['ratings']))
{
$review['ratings'] = array();
}
$review['ratings'][] = $rating;
}
}
/**
* @return Form
*/
private function buildAddToCartForm($options, $variations, $prefix = '')
{
return new Form($this->buildAddToCartValidator($options, $variations, $prefix));
}
private function buildRatingForm($ratingTypes, Product $product)
{
return new Form($this->buildRatingValidator($ratingTypes, $product));
}
private function buildRatingValidator($ratingTypes, Product $product, $isRating = false)
{
$validator = $this->getValidator("productRating", $this->getRequest());
// option validation
foreach ($ratingTypes as $type)
{
$validator->addCheck('rating_' . $type['ID'], new IsNotEmptyCheck($this->translate('_err_no_rating_selected')));
}
if ($this->isRated($product, $isRating))
{
$validator->addCheck('rating', new VariableCheck(true, new IsEmptyCheck($this->translate('_err_already_rated'))));
}
$validator->addCheck('rating', new VariableCheck($this->isLoginRequiredToRate(), new IsEmptyCheck($this->maketext('_msg_rating_login_required', $this->router->createUrl(array('controller' => 'user', 'action' => 'login'))))));
$validator->addCheck('rating', new VariableCheck($this->isPurchaseRequiredToRate($product), new IsEmptyCheck($this->translate('_msg_rating_purchase_required'))));
if ($this->isAddingReview())
{
$validator->addCheck('nickname', new IsNotEmptyCheck($this->translate('_err_no_review_nickname')));
$validator->addCheck('title', new IsNotEmptyCheck($this->translate('_err_no_review_summary')));
$validator->addCheck('text', new IsNotEmptyCheck($this->translate('_err_no_review_text')));
}
return $validator;
}
private function isAddingReview()
{
return $this->config->get('ENABLE_REVIEWS') && ($this->config->get('REVIEWS_WITH_RATINGS') || ($this->request->get('nickname') || $this->request->get('title') || $this->request->get('text')));
}
private function isRated(Product $product, $isRating = false)
{
if (!empty($_COOKIE['rating_' . $product->getID()]))
{
return true;
}
if ($isRating)
{
ClassLoader::importNow("application.helper.getDateFromString");
$f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('ProductRating', 'productID'), $product->getID()));
if (!$this->user->isAnonymous())
{
$cond = new EqualsCond(new ARFieldHandle('ProductRating', 'userID'), $this->user->getID());
}
else
{
if ($hours = $this->config->get('RATING_SAME_IP_TIME'))
{
$cond = new EqualsCond(new ARFieldHandle('ProductRating', 'ip'), $this->request->getIPLong());
$cond->addAnd(new MoreThanCond(new ARFieldHandle('ProductRating', 'dateCreated'), getDateFromString('-' . $hours . ' hours')));
}
}
if (isset($cond))
{
$f->mergeCondition($cond);
return ActiveRecordModel::getRecordCount('ProductRating', $f) > 0;
}
}
}
private function isLoginRequiredToRate()
{
return $this->user->isAnonymous() && !$this->config->get('ENABLE_ANONYMOUS_RATINGS');
}
private function isPurchaseRequiredToRate(Product $product)
{
if ($this->config->get('REQUIRE_PURCHASE_TO_RATE'))
{
if ($this->user->isAnonymous())
{
return true;
}
if (is_null($this->isPurchaseRequiredToRate))
{
ClassLoader::import('application.model.order.CustomerOrder');
$f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('CustomerOrder', 'userID'), $this->user->getID()));
$f->mergeCondition(new EqualsCond(new ARFieldHandle('OrderedItem', 'productID'), $product->getID()));
$f->mergeCondition(new EqualsCond(new ARFieldHandle('CustomerOrder', 'isFinalized'), 1));
$f->setLimit(1);
$this->isPurchaseRequiredToRate = ActiveRecordModel::getRecordCount('OrderedItem', $f, array('CustomerOrder')) < 1;
}
return $this->isPurchaseRequiredToRate;
}
}
private function getRelatedProducts(Product $product, $type=0)
{
// get related products
$related = $product->getRelatedProductsWithGroupsArray($type);
$rel = array();
foreach ($related as $r)
{
if (!isset($r['RelatedProduct']))
{
continue;
}
$p = $r['RelatedProduct'];
// @todo: make ActiveRecord automatically recognize the correct parent object
$p['DefaultImage'] = $r['DefaultImage'];
if (isset($r['ProductRelationshipGroup']))
{
$p['ProductRelationshipGroup'] = $r['ProductRelationshipGroup'];
}
$rel[] = $p;
}
ProductPrice::loadPricesForRecordSetArray($rel);
// sort related products into groups
$byGroup = array();
foreach ($rel as $r)
{
$groupID = isset($r['ProductRelationshipGroup']) ? $r['ProductRelationshipGroup']['ID'] : 0;
$byGroup[$groupID][] = $r;
}
return $byGroup;
}
private function getPublicFiles()
{
$f = select(eq('ProductFile.isPublic', true));
$f->setOrder(f('ProductFileGroup.position'));
$f->setOrder(f('ProductFile.position'));
return $this->product->getRelatedRecordSetArray('ProductFile', $f, array('ProductFileGroup'));
}
private function buildContactForm()
{
return new Form($this->buildContactValidator());
}
public function buildContactValidator(Request $request = null)
{
$this->loadLanguageFile('ContactForm');
$request = $request ? $request : $this->request;
$validator = $this->getValidator("productContactForm", $request);
$validator->addCheck('name', new IsNotEmptyCheck($this->translate('_err_name')));
$validator->addCheck('email', new IsNotEmptyCheck($this->translate('_err_email')));
$validator->addCheck('msg', new IsNotEmptyCheck($this->translate('_err_message')));
$validator->addCheck('surname', new MaxLengthCheck('Please do not enter anything here', 0));
return $validator;
}
public function getCategory()
{
return $this->category;
}
public function getProduct()
{
return $this->product;
}
}
?>