1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use Zend\View\Model\ViewModel; |
7
|
|
|
use Zend\Paginator\Paginator; |
8
|
|
|
use Zend\Paginator\Adapter\ArrayAdapter as ArrayAdapter; |
9
|
|
|
use Zend\Stdlib\Hydrator\ClassMethods as Hydrator; |
10
|
|
|
|
11
|
|
|
class CatalogManagerController extends AbstractActionController |
12
|
|
|
{ |
13
|
|
|
protected $partialDir = '/speck-catalog/catalog-manager/partial/'; |
14
|
|
|
|
15
|
|
|
public function init() |
16
|
|
|
{ |
17
|
|
|
$this->subLayout('layout/catalog-manager'); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
//find categories/products that match search terms |
21
|
|
|
public function categorySearchChildrenAction() |
22
|
|
|
{ |
23
|
|
|
$type = $this->params('type'); |
24
|
|
|
$children = $this->getService($type)->getAll(); |
25
|
|
|
|
26
|
|
|
$viewVars = array('children' => $children, 'type' => $type); |
27
|
|
|
return $this->partialView('category-search-children', $viewVars); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function newProductAction() |
31
|
|
|
{ |
32
|
|
|
$this->init(); |
33
|
|
|
$product = $this->getService('product')->getModel(); |
34
|
|
|
return new ViewModel(array('product' => $product)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function categoryTreePreviewAction() |
38
|
|
|
{ |
39
|
|
|
$siteId = $this->params('siteid'); |
40
|
|
|
$categoryService = $this->getService('category'); |
41
|
|
|
$categories = $categoryService->getCategoriesForTreePreview($siteId); |
42
|
|
|
|
43
|
|
|
$viewVars = array('categories' => $categories); |
44
|
|
|
return $this->partialView('category-tree', $viewVars); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function productsAction() |
48
|
|
|
{ |
49
|
|
|
$service = $this->getService('product'); |
50
|
|
|
|
51
|
|
|
$this->init(); |
52
|
|
|
$config = array( |
53
|
|
|
'p' => $this->params('p') ?: 1, |
54
|
|
|
'n' => 40, |
55
|
|
|
); |
56
|
|
|
$service->usePaginator($config); |
57
|
|
|
$query = $this->params()->fromQuery('query'); |
58
|
|
|
if ($query) { |
59
|
|
|
$products = $service->search($query); |
60
|
|
|
} else { |
61
|
|
|
$products = $service->getAll(); |
62
|
|
|
} |
63
|
|
|
return new ViewModel(array('products' => $products, 'query' => $query)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function categoriesAction() |
67
|
|
|
{ |
68
|
|
|
$this->init(); |
69
|
|
|
$sites = $this->getService('sites')->getAll(); |
70
|
|
|
return new ViewModel(array('sites' => $sites)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function productAction() |
74
|
|
|
{ |
75
|
|
|
$this->init(); |
76
|
|
|
$productService = $this->getService('product'); |
77
|
|
|
$product = $productService->getFullProduct($this->params('id')); |
78
|
|
|
|
79
|
|
|
$vars = array('product' => $product); |
80
|
|
|
return new ViewModel($vars); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
//returns main view variable(product/option/etc) |
85
|
|
|
protected function persist($class, $form) |
86
|
|
|
{ |
87
|
|
|
$service = $this->getService($class); |
88
|
|
|
|
89
|
|
|
if (method_exists($service, 'persist')) { |
90
|
|
|
return $service->persist($form); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$originalData = $form->getOriginalData(); |
94
|
|
|
$data = $form->getData(); |
95
|
|
|
|
96
|
|
|
if (count($originalData) && $service->find($originalData)) { |
97
|
|
|
$service->update($data, $originalData); |
98
|
|
|
return $service->find($data, true, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $service->insert($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function updateProductAction() |
105
|
|
|
{ |
106
|
|
|
$formData = $this->params()->fromPost(); |
107
|
|
|
$form = $this->getService('form')->getForm('product', null, $formData); |
108
|
|
|
|
109
|
|
|
if ($form->isValid()) { |
110
|
|
|
$product = $this->persist('product', $form); |
111
|
|
|
return $this->getResponse()->setContent($product->getProductId()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->partialView('product', array('product' => $product)); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function updateRecordAction() |
118
|
|
|
{ |
119
|
|
|
$class = $this->params('class'); |
120
|
|
|
$formData = $this->params()->fromPost(); |
121
|
|
|
$form = $this->getService('form')->getForm($class, null, $formData); |
122
|
|
|
|
123
|
|
|
if ($form->isValid()) { |
124
|
|
|
$entity = $this->persist($class, $form); |
125
|
|
|
} else { |
126
|
|
|
$entity = $this->getService($class)->getEntity($formData); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$partial = $this->dash($class); |
130
|
|
|
$viewVars = array(lcfirst($this->camel($class)) => $entity); |
131
|
|
|
return $this->partialView($partial, $viewVars); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function updateFormAction() |
135
|
|
|
{ |
136
|
|
|
$data = $this->params()->fromPost(); |
137
|
|
|
$form = $this->getService('form')->getForm($this->params('class'), null, $data); |
138
|
|
|
$helper = $this->getViewHelper('speckCatalogForm'); |
139
|
|
|
|
140
|
|
|
$html = $helper->renderFormMessages($form); |
141
|
|
|
return $this->getResponse()->setContent($html); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getViewHelper($helperName) |
145
|
|
|
{ |
146
|
|
|
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function findAction() |
150
|
|
|
{ |
151
|
|
|
$post = $this->params()->fromPost(); |
152
|
|
|
|
153
|
|
|
if (!isset($post['query'])) { |
154
|
|
|
$search = $this->partialDir . 'search/' . $this->dash($post['child_name']); |
155
|
|
|
$view = new ViewModel(array( |
156
|
|
|
'fields' => $post, |
157
|
|
|
'searchPartial' => $search |
158
|
|
|
)); |
159
|
|
|
return $view->setTemplate($this->partialDir . 'search/index')->setTerminal(true); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$response = array( |
163
|
|
|
'html' => '', |
164
|
|
|
); |
165
|
|
|
$result = $this->getService($post['parent_name'])->search($post); |
166
|
|
|
if (count($result) > 0) { |
167
|
|
|
$partial = $this->getViewHelper('partial'); |
168
|
|
|
$rowPartial = $this->partialDir . 'search/row/' . $this->dash($post['child_name']); |
169
|
|
|
foreach ($result as $row) { |
170
|
|
|
$response['html'] .= $partial($rowPartial, array('model' => $row, 'params' => $post)); |
171
|
|
|
} |
172
|
|
|
} else { |
173
|
|
|
$response['html'] = 'no result'; |
174
|
|
|
} |
175
|
|
|
$json_resp = json_encode($response); |
176
|
|
|
return $this->getResponse()->setContent($json_resp); |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
return $view; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function foundAction() |
188
|
|
|
{ |
189
|
|
|
$postParams = $this->params()->fromPost(); |
190
|
|
|
|
191
|
|
|
$objects = array(); |
192
|
|
|
|
193
|
|
|
if ($postParams['child_name'] === 'builder_product') { |
194
|
|
|
$parentProductId = $postParams['parent']['product_id']; |
195
|
|
|
$productIds = array_keys($postParams['check']); |
196
|
|
|
foreach ($productIds as $productId) { |
197
|
|
|
$objects[] = $this->getService('builder_product')->newBuilderForProduct($productId, $parentProductId); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$helper = $this->getViewHelper('speckCatalogRenderChildren'); |
202
|
|
|
$content = $helper->__invoke($postParams['child_name'], $objects); |
203
|
|
|
$response = $this->getResponse()->setContent($content); |
204
|
|
|
return $response; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function sortAction() |
208
|
|
|
{ |
209
|
|
|
$postParams = $this->params()->fromPost(); |
210
|
|
|
$childName = $this->params('type'); |
211
|
|
|
$parentName = $this->params('parent'); |
212
|
|
|
$parent = $postParams['parent_key']; |
213
|
|
|
|
214
|
|
|
$order = explode(',', $postParams['order']); |
215
|
|
|
foreach ($order as $i => $val) { |
216
|
|
|
if (!trim($val)) { |
217
|
|
|
unset($order[$i]); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
$parentService = $this->getService($parentName); |
221
|
|
|
$sortChildren = 'sort' . $this->camel($childName) . 's'; |
222
|
|
|
$result = $parentService->$sortChildren($parent, $order); |
|
|
|
|
223
|
|
|
|
224
|
|
|
return $this->getResponse(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function removeChildAction() |
228
|
|
|
{ |
229
|
|
|
$postParams = $this->params()->fromPost(); |
230
|
|
|
$parentName = $postParams['parent_name']; |
231
|
|
|
$childName = $postParams['child_name']; |
232
|
|
|
$parent = $postParams['parent']; |
233
|
|
|
$child = $postParams['child']; |
234
|
|
|
|
235
|
|
|
$parentService = $this->getService($parentName); |
236
|
|
|
|
237
|
|
|
$removeChildMethod = 'remove' . $this->camel($childName); |
238
|
|
|
$result = $parentService->$removeChildMethod($parent, $child); |
239
|
|
|
|
240
|
|
|
if (true === $result) { |
241
|
|
|
return $this->getResponse()->setContent('true'); |
242
|
|
|
} |
243
|
|
|
return $this->getResponse()->setContent('false'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getService($name) |
247
|
|
|
{ |
248
|
|
|
$serviceName = 'speckcatalog_' . $name . '_service'; |
249
|
|
|
return $this->getServiceLocator()->get($serviceName); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
//return the partial for a new record. |
253
|
|
|
public function newPartialAction() |
254
|
|
|
{ |
255
|
|
|
$postParams = $this->params()->fromPost(); |
256
|
|
|
$parentName = $postParams['parent_name']; |
257
|
|
|
$childName = $postParams['child_name']; |
258
|
|
|
$parent = $postParams['parent']; |
259
|
|
|
|
260
|
|
|
$parent = $this->getService($parentName)->find($parent); |
261
|
|
|
$child = $this->getService($childName)->getModel(); |
262
|
|
|
|
263
|
|
|
$child->setParent($parent); |
264
|
|
|
|
265
|
|
|
$partial = $this->dash($childName); |
266
|
|
|
$viewVars = array(lcfirst($this->camel($childName)) => $child); |
267
|
|
|
return $this->partialView($partial, $viewVars); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function partialView($partial, array $viewVars = null) |
271
|
|
|
{ |
272
|
|
|
$view = new ViewModel($viewVars); |
273
|
|
|
$view->setTemplate($this->partialDir . $partial); |
274
|
|
|
$view->setTerminal(true); |
275
|
|
|
|
276
|
|
|
return $view; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
protected function dash($name) |
280
|
|
|
{ |
281
|
|
|
$dash = new \Zend\Filter\Word\UnderscoreToDash; |
282
|
|
|
return $dash->__invoke($name); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function camel($name) |
286
|
|
|
{ |
287
|
|
|
$camel = new \Zend\Filter\Word\UnderscoreToCamelCase; |
288
|
|
|
return $camel->__invoke($name); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.