1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use Zend\View\Model\ViewModel; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class CategoryController extends AbstractActionController |
10
|
|
|
{ |
11
|
|
|
protected $catalogService; |
12
|
|
|
|
13
|
|
|
public function indexAction() |
14
|
|
|
{ |
15
|
|
|
$paginatorVars = $this->getPaginatorVars(); |
16
|
|
|
|
17
|
|
|
$id = $this->params('id'); |
18
|
|
|
$service = $this->getServiceLocator()->get('speckcatalog_category_service'); |
19
|
|
|
$category = $service->getCategoryForView($id, $paginatorVars); |
20
|
|
|
|
21
|
|
|
$crumbs = $service->getCrumbs($category); |
22
|
|
|
$this->layout()->crumbs = array( |
23
|
|
|
'type' => 'category', |
24
|
|
|
'crumbs' => $crumbs, |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
if (null === $category) { |
28
|
|
|
throw new \Exception('fore oh fore'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$paginatorVars['categoryId'] = $category->getCategoryId(); |
32
|
|
|
|
33
|
|
|
return new ViewModel(array( |
34
|
|
|
'category' => $category, |
35
|
|
|
'paginatorVars' => $paginatorVars, |
36
|
|
|
)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setCatalogService($catalogService) |
40
|
|
|
{ |
41
|
|
|
$this->catalogService = $catalogService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCatalogService() |
45
|
|
|
{ |
46
|
|
|
return $this->catalogService; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getPaginatorVars($stringify = false) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
if (isset($_GET['nn'])) { |
52
|
|
|
$this->perPageAction(); |
53
|
|
|
} |
54
|
|
|
$keys = array('n', 'p', 'o', 's'); |
55
|
|
|
$paginatorVars = array(); |
56
|
|
|
foreach ($keys as $key) { |
57
|
|
|
if (isset($_GET[$key])) { |
58
|
|
|
$paginatorVars[$key] = $_GET[$key]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $paginatorVars; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function perPageAction() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$keys = array('o', 's'); |
68
|
|
|
foreach ($keys as $key) { |
69
|
|
|
if (isset($_GET[$key])) { |
70
|
|
|
$paginatorVars[$key] = $_GET[$key]; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$page = (isset($_GET['p']) ? $_GET['p'] : 1); |
75
|
|
|
$perPage = (isset($_GET['n']) ? $_GET['n'] : 10); |
76
|
|
|
|
77
|
|
|
$offset = ((($page * $perPage) - $perPage) + 1); //this is the first item on the page |
78
|
|
|
|
79
|
|
|
$paginatorVars['p'] = floor($offset / $_GET['nn']) + 1; //new page number |
|
|
|
|
80
|
|
|
$paginatorVars['n'] = $_GET['nn']; //new items per page |
81
|
|
|
|
82
|
|
|
$query = '?' . http_build_query($paginatorVars); |
83
|
|
|
|
84
|
|
|
return $this->redirect()->toUrl('/category/' . $this->params('id') . $query); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.