1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use Zend\View\Model\ViewModel; |
7
|
|
|
|
8
|
|
|
class ProductController extends AbstractActionController |
9
|
|
|
{ |
10
|
|
|
protected $services = array( |
11
|
|
|
'product' => 'speckcatalog_product_service', |
12
|
|
|
'product_uom' => 'speckcatalog_product_uom_service', |
13
|
|
|
'builder' => 'speckcatalog_builder_product_service', |
14
|
|
|
'configure_buy' => 'speckcatalog_configure_buy_service', |
15
|
|
|
'cart' => 'catalog_cart_service', |
16
|
|
|
'renderer' => 'zendviewrendererphprenderer', |
17
|
|
|
'option' => 'speckcatalog_option_service', |
18
|
|
|
'product_image' => 'speckcatalog_product_image_service', |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
public function getService($name) |
22
|
|
|
{ |
23
|
|
|
if (!array_key_exists($name, $this->services)) { |
24
|
|
|
throw new \Exception('invalid service name'); |
25
|
|
|
} |
26
|
|
|
if (is_string($this->services[$name])) { |
27
|
|
|
$this->services[$name] = $this->getServiceLocator()->get($this->services[$name]); |
28
|
|
|
} |
29
|
|
|
return $this->services[$name]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function indexAction() |
33
|
|
|
{ |
34
|
|
|
$cartItemId = $this->params('cartItemId'); |
35
|
|
|
$cartService = $this->getService('cart'); |
36
|
|
|
$product = $this->getService('product') |
37
|
|
|
->setEnabledOnly(true) |
38
|
|
|
->getFullProduct($this->params('id')); |
39
|
|
|
if (!$product) { |
40
|
|
|
throw new \Exception('no product for that id'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->layout()->crumbs = $this->getService('product')->getCrumbs($product); |
44
|
|
|
|
45
|
|
|
$vars = array( |
46
|
|
|
'product' => $product, |
47
|
|
|
'editingCart' => ($cartItemId ? true : false), |
48
|
|
|
'cartItem' => ($cartItemId ? $cartService->findItemById($cartItemId) : false), |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return new ViewModel($vars); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function imagesAction() |
55
|
|
|
{ |
56
|
|
|
$productId = $this->params('id'); |
57
|
|
|
$images = $this->getService('product_image')->getImages('product', $productId); |
58
|
|
|
|
59
|
|
|
return new ViewModel(array('images' => $images)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getViewHelper($helperName) |
63
|
|
|
{ |
64
|
|
|
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function uomsPartialAction() |
68
|
|
|
{ |
69
|
|
|
$post = $this->params()->fromPost(); |
70
|
|
|
$pid = $post['product_id']; |
71
|
|
|
$builderPid = isset($post['builder_product_id']) ? $post['builder_product_id'] : null; |
72
|
|
|
|
73
|
|
|
$helper = $this->getViewHelper('speckCatalogUomsToCart'); |
74
|
|
|
$content = $helper->__invoke($pid, $builderPid); |
75
|
|
|
|
76
|
|
|
return $this->getResponse()->setContent($content); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function optionsPartialAction() |
80
|
|
|
{ |
81
|
|
|
$postParams = $this->params()->fromPost(); |
82
|
|
|
$productId = $postParams['product_id']; |
83
|
|
|
|
84
|
|
|
$options = $this->getService('option')->getByProductId($productId, true, true); |
85
|
|
|
|
86
|
|
|
$renderer = $this->getService('renderer'); |
87
|
|
|
foreach ($options as $option) { |
88
|
|
|
$vars = array('option' => $option); |
89
|
|
|
$html .= $renderer->render('/catalog/product/option', $vars); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $html; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: