Projekt

Allgemein

Profil

GX-Bug #61173 » CheckStatusController.inc.php

Wilken Haase, 19.02.2019 17:12

 
1
<?php
2
/* --------------------------------------------------------------
3
   CheckStatusController.inc.php 2018-09-12
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2018 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10
*/
11

    
12
/**
13
 * Class CheckStatusController
14
 *
15
 * @extends    HttpViewController
16
 * @category   System
17
 * @package    HttpViewControllers
18
 */
19
class CheckStatusController extends HttpViewController
20
{
21
	/**
22
	 * @todo get rid of old AjaxHandler
23
	 * @todo use GET and POST REST-API like
24
	 *
25
	 * @return HttpControllerResponse
26
	 */
27
	public function actionDefault()
28
	{
29
		$ajaxHandler = MainFactory::create('PropertiesCombisAjaxHandler');
30
		
31
		$ajaxHandler->set_data('GET', array('action' => 'get_selection_template'));
32
		$ajaxHandler->set_data('POST', array(
33
			'properties_values_ids' => $this->_getQueryParameter('properties_values_ids'),
34
			'quantity'              => $this->_getQueryParameter('products_qty'),
35
			'products_id'           => $this->_getQueryParameter('products_id')
36
		));
37
		$ajaxHandler->proceed();
38
		$selectionTemplate = json_decode($ajaxHandler->get_response());
39
		
40
		$propertiesView = MainFactory::create_object('PropertiesView');
41
		$combiStatus    = $propertiesView->get_combis_status_json($this->_getQueryParameter('products_id'),
42
		                                                          $this->_getQueryParameter('properties_values_ids'),
43
		                                                          $this->_getQueryParameter('products_qty'));
44
		$combiStatus    = json_decode($combiStatus);
45
		
46
		/** @var ProductReadService $productReadService */
47
		$productReadService = StaticGXCoreLoader::getService('ProductRead');
48
		$quantityChecker    = '';
49
		$product            = $productReadService->getProductById(new IdType((int)$this->_getQueryParameter('products_id')));
50
		
51
		if($product->getSettings()->getPriceStatus() === 0
52
           && $_SESSION['customers_status']['customers_status_show_price'] == '1'
53
           && !$product->isFsk18())
54
		{
55
			$ajaxHandler = MainFactory::create('OrderAjaxHandler');
56
			$ajaxHandler->set_data('GET', array(
57
				'action' => 'quantity_checker',
58
				'qty'    => $this->_getQueryParameter('products_qty'),
59
				'id'     => $this->_getQueryParameter('products_id')
60
			));
61
			$ajaxHandler->proceed();
62
			$quantityChecker = $ajaxHandler->get_response();
63
		}
64
		
65
		$result = $this->_getPropertiesResponseArray($selectionTemplate, $combiStatus, $quantityChecker, $product);
66
		
67
		if(!is_null($this->_getQueryParameter('id')))
68
		{
69
			$ajaxHandler = MainFactory::create('AttributesAjaxHandler');
70
			
71
			$_POST['properties_values_ids'] = $this->_getQueryParameter('properties_values_ids');
72
			$_POST['products_id']           = (int)$this->_getQueryParameter('products_id');
73
			
74
			$getArray = array(
75
				'action'       => 'calculate_price',
76
				'products_qty' => $this->_getQueryParameter('products_qty'),
77
				'products_id'  => $this->_getQueryParameter('products_id'),
78
				'id'           => $this->_getQueryParameter('id')
79
			);
80
			
81
			$ajaxHandler->set_data('GET', $getArray);
82
			$ajaxHandler->set_data('POST', $this->_getQueryParametersCollection()->getArray());
83
			$ajaxHandler->proceed();
84
			$result['content']['price']['value'] = $ajaxHandler->get_response();
85
			
86
			$result['attrImages'] = $this->_getAttributesImagesData();
87
			$result['content']['images'] = array(
88
				'selector' => 'attributeImages',
89
				'type'     => 'html',
90
				'value'    => $this->_getAttributesImagesHtml($ajaxHandler)
91
			);
92
		}
93
		
94
		return MainFactory::create('JsonHttpControllerResponse', $result);
95
	}
96
	
97
	
98
	/**
99
	 * @todo get rid of old AjaxHandler
100
	 * @todo use GET and POST REST-API like
101
	 *
102
	 * @return HttpControllerResponse
103
	 */
104
	public function actionAttributes()
105
	{
106
		$ajaxHandler = MainFactory::create('AttributesAjaxHandler');
107
		
108
		$weight          = $this->_getAttributesWeight($ajaxHandler);
109
		$price           = $this->_getAttributesPrice($ajaxHandler);
110
		$images          = array(
111
			'html'            => $this->_getAttributesImagesHtml($ajaxHandler),
112
			'attributes_data' => $this->_getAttributesImagesData()
113
		);
114
		$quantityChecker = $this->_getQuantityChecker();
115
		$stockChecker = $this->_getStockChecker();
116
		
117
		$result = $this->_getAttributesResponseArray($weight, $price, $images, $quantityChecker, $stockChecker);
118
		
119
		return MainFactory::create('JsonHttpControllerResponse', $result);
120
	}
121
	
122
	
123
	/**
124
	 * @param mixed                   $selectionTemplate
125
	 * @param mixed                   $combiStatus
126
	 * @param string                  $p_quantityChecker
127
	 * @param \StoredProductInterface $product
128
	 *
129
	 * @return array
130
	 */
131
	protected function _getPropertiesResponseArray($selectionTemplate, $combiStatus, $p_quantityChecker, StoredProductInterface $product)
132
	{
133
		$languageTextManager = MainFactory::create('LanguageTextManager', 'product_info');
134
		
135
		$discount = $this->_getDiscount();
136
		
137
		$result = array(
138
			'success'     => ($combiStatus->STATUS_CODE === 1 || $combiStatus->STATUS_CODE === 2)
139
			                 && ($selectionTemplate->status === 'stock_allowed'
140
			                     || $selectionTemplate->status === 'valid_quantity')
141
			                 && empty($p_quantityChecker),
142
			'status_code' => $combiStatus->STATUS_CODE,
143
			'content'     => array(
144
				'price'                  => array(
145
					'selector' => 'price',
146
					'type'     => 'html',
147
					'value'    => $this->_formatPrice($selectionTemplate->price, $product)
148
				),
149
				'qty'                    => array(
150
					'selector' => 'quantity',
151
					'type'     => 'text',
152
					'value'    => $selectionTemplate->quantity
153
				),
154
				'shipping'               => array(
155
					'selector' => 'shippingTime',
156
					'type'     => 'text',
157
					'value'    => $selectionTemplate->shipping_status_name
158
				),
159
				'shippingIcon'           => array(
160
					'selector' => 'shippingTimeImage',
161
					'type'     => 'attribute',
162
					'key'      => 'src',
163
					'value'    => 'images/icons/status/' . $selectionTemplate->shipping_status_image
164
				),
165
				'shippingIconAlt'        => array(
166
					'selector' => 'shippingTimeImage',
167
					'type'     => 'attribute',
168
					'key'      => 'alt',
169
					'value'    => ($selectionTemplate->shipping_status_name) ? $selectionTemplate->shipping_status_name
170
						                                                     : $languageTextManager->get_text('unknown_shippingtime')
171
				),
172
				'weight'                 => array(
173
					'selector' => 'weight',
174
					'type'     => 'text',
175
					'value'    => $selectionTemplate->weight
176
				),
177
				'model'                  => array(
178
					'selector' => 'modelNumber',
179
					'type'     => 'html',
180
					'value'    => $selectionTemplate->model
181
				),
182
				'message'                => array(
183
					'selector' => 'messageCart',
184
					'type'     => 'html',
185
					'value'    => (!empty($selectionTemplate->message))
186
						? $selectionTemplate->message
187
						: $p_quantityChecker
188
				),
189
				'messageNoCombiSelected' => array(
190
					'selector' => 'messageCart',
191
					'type'     => 'html',
192
					'value'    => $combiStatus->STATUS_CODE === -1
193
						? $combiStatus->STATUS_TEXT
194
						: ''
195
				),
196
				'filter'                 => array(
197
					'selector' => 'propertiesForm',
198
					'type'     => 'replace',
199
					'value'    => $selectionTemplate->html
200
				),
201
				'ribbon'                 => array(
202
					'selector' => 'ribbonSpecial',
203
					'type'     => 'html',
204
					'value'    => $discount
205
				)
206
			)
207
		);
208
		
209
		if(!empty($selectionTemplate->message))
210
		{
211
			$result['content']['help'] = array(
212
				'selector' => 'messageHelp',
213
				'type'     => 'replace',
214
				'value'    => ''
215
			);
216
		}
217
		
218
		return $result;
219
	}
220
	
221
	
222
	/**
223
	 * @param string $p_weight
224
	 * @param string $p_price
225
	 * @param string $p_images
226
	 * @param string $p_quantityChecker
227
	 * @param string $stockChecker
228
	 *
229
	 * @return array
230
	 */
231
	protected function _getAttributesResponseArray($p_weight, $p_price, $p_images, $p_quantityChecker, $stockChecker)
232
	{
233
		$discount = $this->_getDiscount();
234
		
235
		$messageCart = $p_quantityChecker;
236
		$messageCart .= !empty($messageCart)? '<br />' : '';
237
		$messageCart .= $stockChecker['message'];
238
		
239
		$result = array(
240
			'success'     => empty($p_quantityChecker) && $stockChecker['canCheckout'],
241
			'status_code' => 1,
242
			'attrImages'  => $p_images['attributes_data'],
243
			'content'     => array(
244
				'weight'  => array(
245
					'selector' => 'weight',
246
					'type'     => 'text',
247
					'value'    => $p_weight
248
				),
249
				'price'   => array(
250
					'selector' => 'price',
251
					'type'     => 'html',
252
					'value'    => $p_price
253
				),
254
				'images'  => array(
255
					'selector' => 'attributeImages',
256
					'type'     => 'html',
257
					'value'    => $p_images['html']
258
				),
259
				'message' => array(
260
					'selector' => 'messageCart',
261
					'type'     => 'html',
262
					'value'    => $messageCart
263
				),
264
				'ribbon'  => array(
265
					'selector' => 'ribbonSpecial',
266
					'type'     => 'html',
267
					'value'    => $discount
268
				)
269
			)
270
		);
271
		
272
		if(!empty($p_quantityChecker))
273
		{
274
			$result['content']['help'] = array(
275
				'selector' => 'messageHelp',
276
				'type'     => 'replace',
277
				'value'    => ''
278
			);
279
		}
280
		
281
		return $result;
282
	}
283
	
284
	
285
	/**
286
	 * @return string
287
	 */
288
	protected function _getDiscount()
289
	{
290
		require_once DIR_FS_INC . 'xtc_get_tax_class_id.inc.php';
291
		
292
		$combiPrice = 0;
293
		$discount   = '';
294
		$xtcPrice   = new xtcPrice($_SESSION['currency'], $_SESSION['customers_status']['customers_status_id']);
295
		
296
		if(!is_null($this->_getQueryParameter('properties_values_ids')))
297
		{
298
			$propertiesControl = MainFactory::create_object('PropertiesControl');
299
			$combiId           = $propertiesControl->get_combis_id_by_value_ids_array(xtc_get_prid($this->_getQueryParameter('products_id')),
300
			                                                                          $this->_getQueryParameter('properties_values_ids'));
301
			$combiPrice        = $xtcPrice->get_properties_combi_price($combiId);
302
		}
303
		
304
		$specialPrice = $xtcPrice->xtcCheckSpecial($this->_getQueryParameter('products_id')) + $combiPrice;
305
		$normalPrice  = $xtcPrice->getPprice($this->_getQueryParameter('products_id')) + $combiPrice;
306
		
307
		if(is_array($this->_getQueryParameter('id')))
308
		{
309
			foreach($this->_getQueryParameter('id') as $optionId => $valueId)
310
			{
311
				$optionPrice = $xtcPrice->xtcGetOptionPrice($this->_getQueryParameter('products_id'), $optionId,
312
				                                            $valueId);
313
				$specialPrice += $optionPrice['price'];
314
				$normalPrice += $optionPrice['price'];
315
			}
316
		}
317
		
318
		$isSpecial = false;
319
		
320
		if($specialPrice < $normalPrice && $specialPrice > 0)
321
		{
322
			$discount  = ceil(round((1 - ($specialPrice / $normalPrice)) * -100, 1));
323
			$isSpecial = true;
324
		}
325
		
326
		if($isSpecial)
327
		{
328
			$discount = '<div class="ribbon-special"><span>' . $discount . '%</span></div>';
329
			
330
			return $discount;
331
		}
332
		
333
		return $discount;
334
	}
335
	
336
	
337
	protected function _getAttributesWeight(AttributesAjaxHandler $ajaxHandler)
338
	{
339
		$getArray = array(
340
			'action'       => 'calculate_weight',
341
			'products_qty' => $this->_getQueryParameter('products_qty'),
342
			'products_id'  => $this->_getQueryParameter('products_id')
343
		);
344
		
345
		if(!is_null($this->_getQueryParameter('id')))
346
		{
347
			$getArray['id'] = $this->_getQueryParameter('id');
348
		}
349
		
350
		$ajaxHandler->set_data('GET', $getArray);
351
		$ajaxHandler->set_data('POST', $this->_getQueryParametersCollection()->getArray());
352
		$ajaxHandler->proceed();
353
		
354
		$weight = $ajaxHandler->get_response();
355
		
356
		$ajaxHandler->v_output_buffer = null;
357
		
358
		return $weight;
359
	}
360
	
361
	
362
	protected function _getAttributesPrice(AttributesAjaxHandler $ajaxHandler)
363
	{
364
		$getArray = array(
365
			'action'       => 'calculate_price',
366
			'products_qty' => str_replace(',', '.', (string)$this->_getQueryParameter('products_qty')),
367
			'products_id'  => $this->_getQueryParameter('products_id')
368
		);
369
		
370
		if(!is_null($this->_getQueryParameter('id')))
371
		{
372
			$getArray['id'] = $this->_getQueryParameter('id');
373
		}
374
		
375
		if(is_null($this->_getPostData('properties_values_ids')))
376
		{
377
			$propertiesControl = MainFactory::create_object('PropertiesControl');
378
			if((int)$propertiesControl->count_properties_to_product((int)$this->_getQueryParameter('products_id')) > 0)
379
			{
380
				$_POST['properties_values_ids'] = array();
381
				$_POST['products_id']           = (int)$this->_getQueryParameter('products_id');
382
			}
383
		}
384
		
385
		if(!isset($ajaxHandler->v_data_array['POST']['products_qty']))
386
		{
387
			$ajaxHandler->v_data_array['POST']['products_qty'] = '1';
388
		}
389
		
390
		$ajaxHandler->set_data('GET', $getArray);
391
		$ajaxHandler->proceed();
392
		$price = $ajaxHandler->get_response();
393
		
394
		$ajaxHandler->v_output_buffer = null;
395
		
396
		return $price;
397
	}
398
	
399
	
400
	protected function _getAttributesImagesHtml(AttributesAjaxHandler $ajaxHandler)
401
	{
402
		$getArray = array(
403
			'action' => 'attribute_images'
404
		);
405
		
406
		if(!is_null($this->_getQueryParameter('id')))
407
		{
408
			$getArray['id'] = $this->_getQueryParameter('id');
409
		}
410
		
411
		$ajaxHandler->set_data('GET', $getArray);
412
		$ajaxHandler->proceed();
413
		
414
		return $ajaxHandler->get_response();
415
	}
416
	
417
	
418
	protected function _getAttributesImagesData()
419
	{
420
		$optionsIds = '';
421
		$valuesIds  = '';
422
		
423
		if(is_array($this->_getQueryParameter('id')))
424
		{
425
			foreach($this->_getQueryParameter('id') as $optionId => $valueId)
426
			{
427
				$optionsIds .= 'id[' . (int)$optionId . '],';
428
				$valuesIds .= (int)$valueId . ',';
429
			}
430
		}
431
		elseif(!is_null($this->_getQueryParameter('options_ids'))
432
		       && !is_null($this->_getQueryParameter('values_ids'))
433
		)
434
		{
435
			$optionsIds = $this->_getQueryParameter('options_ids');
436
			$valuesIds  = $this->_getQueryParameter('values_ids');
437
		}
438
		
439
		$attributes      = array();
440
		$optionsIdsArray = explode(',', substr($optionsIds, 0, -1));
441
		$valuesIdsArray  = explode(',', substr($valuesIds, 0, -1));
442
		$db              = StaticGXCoreLoader::getDatabaseQueryBuilder();
443
		
444
		foreach($optionsIdsArray as $key => $value)
445
		{
446
			$from             = strpos($value, '[');
447
			$productOptionsId = (int)substr($value, $from + 1, -1);
448
			
449
			$result = $db->select(array(
450
				                      'po.products_options_name',
451
				                      'pov.products_options_values_name',
452
				                      'pov.gm_filename'
453
			                      ))
454
			             ->from(array('products_options AS po', 'products_options_values AS pov'))
455
			             ->where(array(
456
				                     'po.products_options_id'         => $productOptionsId,
457
				                     'po.language_id'                 => $_SESSION['languages_id'],
458
				                     'pov.language_id'                => $_SESSION['languages_id'],
459
				                     'pov.products_options_values_id' => (int)$valuesIdsArray[$key]
460
			                     ))
461
			             ->limit(1)
462
			             ->get();
463
			if($result->num_rows() === 1)
464
			{
465
				$result = $result->row_array();
466
				if(!empty($result['gm_filename']))
467
				{
468
					$attributes[] = array(
469
						'src'   => DIR_WS_CATALOG . DIR_WS_IMAGES . 'product_images/attribute_images/'
470
						           . $result['gm_filename'],
471
						'title' => $result['products_options_name'] . ': ' . $result['products_options_values_name']
472
					);
473
				}
474
			}
475
		}
476
		
477
		return $attributes;
478
	}
479
	
480
	
481
	protected function _getQuantityChecker()
482
	{
483
		$productReadService = StaticGXCoreLoader::getService('ProductRead');
484
		$quantityChecker    = '';
485

    
486
		try{$product = $productReadService->getProductById(new IdType((int)$this->_getQueryParameter('products_id')));
487
		} catch (UnexpectedValueException $e) {$product = null;}
488
		
489
		if($product !== null
490
		&& $product->getSettings()->getPriceStatus() === 0
491
		&& $_SESSION['customers_status']['customers_status_show_price'] == '1'
492
		&& !$product->isFsk18())
493
		{
494
			$ajaxHandler = MainFactory::create('OrderAjaxHandler');
495
			
496
			$getArray = array(
497
				'action' => 'quantity_checker',
498
				'qty'    => $this->_getQueryParameter('products_qty'),
499
				'id'     => $this->_getQueryParameter('products_id')
500
			);
501
			
502
			$ajaxHandler->set_data('GET', $getArray);
503
			$ajaxHandler->proceed();
504
			$quantityChecker = $ajaxHandler->get_response();
505
		}
506
		
507
		return $quantityChecker;
508
	}
509
	
510
	
511
	protected function _getStockChecker()
512
	{
513
		$stockChecker = [
514
			'message' => '',
515
		    'canCheckout' => true,
516
		];
517
		
518
		# Check stock only if option ist action and controller is called on product details page or in listing
519
		# for a product without attributes and properties
520
		if(CHECK_STOCK_BEFORE_SHOPPING_CART === 'true'
521
		   && (!xtc_has_product_attributes((int)$this->_getQueryParameter('products_id'))
522
		       || (bool)$this->queryParametersArray['isProductInfo']))
523
		{
524
			$productReadService = StaticGXCoreLoader::getService('ProductRead');
525
			$product            = $productReadService->getProductById(new IdType((int)$this->_getQueryParameter('products_id')));
526
			
527
			if($product->getSettings()->getPriceStatus() === 0)
528
			{
529
				$ajaxHandler = MainFactory::create('OrderAjaxHandler');
530
				
531
				$getArray = array(
532
					'action' => 'stock_checker',
533
					'qty'    => $this->_getQueryParameter('products_qty'),
534
					'id'     => $this->_getQueryParameter('products_id')
535
				);
536
				
537
				$ajaxHandler->set_data('GET', $getArray);
538
				$ajaxHandler->proceed();
539
				$stockChecker = $ajaxHandler->get_response();
540
				
541
				# Clear stock message if checkout is possible and this controller is not called from the product details page
542
				if($stockChecker['canCheckout'] && !(bool)$this->queryParametersArray['isProductInfo'])
543
				{
544
					$stockChecker['message'] = '';
545
				}
546
			}
547
		}
548
		
549
		return $stockChecker;
550
	}
551
	
552
	
553
	/**
554
	 * @param                         $price
555
	 * @param \StoredProductInterface $product
556
	 *
557
	 * @return string
558
	 */
559
	protected function _formatPrice($price, StoredProductInterface $product)
560
	{
561
		if($price === GM_SHOW_PRICE_ON_REQUEST)
562
		{
563
			$seoBoost     = MainFactory::create_object('GMSEOBoost', [], true);
564
			$sefParameter = '';
565
			
566
			$query  = "SELECT
567
							content_id,
568
							content_title
569
						FROM " . TABLE_CONTENT_MANAGER . "
570
						WHERE
571
							languages_id = '" . (int)$_SESSION['languages_id'] . "' AND
572
							content_group = '7'";
573
			$result = xtc_db_query($query);
574
			if(xtc_db_num_rows($result))
575
			{
576
				$row                 = xtc_db_fetch_array($result);
577
				$contactContentId    = $row['content_id'];
578
				$contactContentTitle = $row['content_title'];
579
				
580
				if(defined('SEARCH_ENGINE_FRIENDLY_URLS') && SEARCH_ENGINE_FRIENDLY_URLS === 'false')
581
				{
582
					$sefParameter = '&content=' . xtc_cleanName($contactContentTitle);
583
				}
584
			}
585
			
586
			$subject           = GM_SHOW_PRICE_ON_REQUEST . ': '
587
			                     . $product->getName(new LanguageCode(new StringType($_SESSION['language_code'])));
588
			$propertySelection = [];
589
			
590
			$propertyValueIds = (array)$this->_getQueryParameter('properties_values_ids');
591
			
592
			foreach($propertyValueIds as $propertyValueId)
593
			{
594
				$propertyValueId = (int)$propertyValueId;
595
				
596
				$sql    = 'SELECT 
597
							`properties_name`, 
598
							`values_name` 
599
						FROM `products_properties_index` 
600
						WHERE 
601
							`products_id` = ' . (int)$this->_getQueryParameter('products_id') . ' AND
602
							`language_id` = ' . (int)$_SESSION['languages_id'] . ' AND
603
							`properties_values_id` = ' . $propertyValueId;
604
				$result = xtc_db_query($sql);
605
				if(xtc_db_num_rows($result))
606
				{
607
					$row                 = xtc_db_fetch_array($result);
608
					$propertySelection[] = $row['properties_name'] . ': ' . $row['values_name'];
609
				}
610
			}
611
			
612
			if(count($propertySelection))
613
			{
614
				$subject .= ' (' . implode(', ', $propertySelection) . ')';
615
			}
616
			
617
			if($seoBoost->boost_content)
618
			{
619
				$contactUrl = xtc_href_link($seoBoost->get_boosted_content_url($contactContentId,
620
				                                                               $_SESSION['languages_id']) . '?subject='
621
				                            . rawurlencode($subject));
622
			}
623
			else
624
			{
625
				$contactUrl = xtc_href_link(FILENAME_CONTENT,
626
				                            'coID=7&subject=' . rawurlencode($subject) . $sefParameter);
627
			}
628
			
629
			$price = '<a href="' . $contactUrl . '" class="price-on-request">' . GM_SHOW_PRICE_ON_REQUEST . '</a>';
630
		}
631
		
632
		return $price;
633
	}
634
}
    (1-1/1)