1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 11.02.16 at 14:15 |
5
|
|
|
*/ |
6
|
|
|
namespace samsoncms\api\renderable; |
7
|
|
|
|
8
|
|
|
use samson\activerecord\dbQuery; |
9
|
|
|
use samsoncms\api\Entity; |
10
|
|
|
use samsonframework\core\ViewInterface; |
11
|
|
|
use samsonframework\orm\QueryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Renderable fields table. |
15
|
|
|
* Class should be used to simplify SamsonCMS generated entities outputting. |
16
|
|
|
* |
17
|
|
|
* @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering |
18
|
|
|
* |
19
|
|
|
* @package samsoncms\api\renderable |
20
|
|
|
*/ |
21
|
|
|
class Collection extends \samsoncms\api\query\Entity |
22
|
|
|
{ |
23
|
|
|
/** Name of the items variable in index view */ |
24
|
|
|
const ITEMS_VIEW_VARIABLE = 'items'; |
25
|
|
|
|
26
|
|
|
/** Name of the item prefix for variable in item view */ |
27
|
|
|
const ITEM_VIEW_VARIABLE = 'item'; |
28
|
|
|
|
29
|
|
|
/** @var string|callable Block view file or callback */ |
30
|
|
|
protected $indexView; |
31
|
|
|
|
32
|
|
|
/** @var string|callable Item view file or callback */ |
33
|
|
|
protected $itemView ; |
34
|
|
|
|
35
|
|
|
/** @var string|callable Empty view file or callback */ |
36
|
|
|
protected $emptyView = 'www/empty'; |
37
|
|
|
|
38
|
|
|
/** @var ViewInterface View render object */ |
39
|
|
|
protected $renderer; |
40
|
|
|
|
41
|
|
|
/** @var int Count of entities on one page */ |
42
|
|
|
protected $pageSize; |
43
|
|
|
|
44
|
|
|
/** @var int Current page number */ |
45
|
|
|
protected $pageNumber; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Collection constructor. |
49
|
|
|
* |
50
|
|
|
* @param ViewInterface $renderer Instance for rendering views |
51
|
|
|
* @param QueryInterface $query Instance for querying database |
52
|
|
|
* @param string|null $locale Localization language |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ViewInterface $renderer, QueryInterface $query = null, $locale = null) |
55
|
|
|
{ |
56
|
|
|
$this->renderer = $renderer; |
57
|
|
|
|
58
|
|
|
parent::__construct(null === $query ? new dbQuery() : $query, $locale); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set index view path. |
63
|
|
|
* @param string|callable $indexView Index view path or callback |
64
|
|
|
* @return $this Chaining |
65
|
|
|
*/ |
66
|
|
|
public function indexView($indexView) |
67
|
|
|
{ |
68
|
|
|
$this->indexView = $indexView; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set item view path. |
74
|
|
|
* @param string|callable $itemView Item view path or callback |
75
|
|
|
* @return $this Chaining |
76
|
|
|
*/ |
77
|
|
|
public function itemView($itemView) |
78
|
|
|
{ |
79
|
|
|
$this->itemView = $itemView; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set empty view path. |
86
|
|
|
* @param string|callable $emptyView Empty view path or callback |
87
|
|
|
* @return $this Chaining |
88
|
|
|
*/ |
89
|
|
|
public function emptyView($emptyView) |
90
|
|
|
{ |
91
|
|
|
$this->emptyView = $emptyView; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Render Entity collection item. |
97
|
|
|
* |
98
|
|
|
* @param Entity $item SamsonCMS entity for rendering |
99
|
|
|
* |
100
|
|
|
* @return string Rendered HTML |
101
|
|
|
*/ |
102
|
|
|
public function renderItem(Entity $item) |
103
|
|
|
{ |
104
|
|
|
return $this->renderer |
105
|
|
|
->view($this->itemView) |
|
|
|
|
106
|
|
|
->set($item, self::ITEM_VIEW_VARIABLE) |
107
|
|
|
->output(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Render empty collection item. |
112
|
|
|
* |
113
|
|
|
* @return string Rendered HTML |
114
|
|
|
*/ |
115
|
|
|
public function renderEmpty() |
116
|
|
|
{ |
117
|
|
|
return $this->renderer->view($this->emptyView)->output(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Render Entity collection index. |
122
|
|
|
* |
123
|
|
|
* @param string $items Collection of rendered items |
124
|
|
|
* |
125
|
|
|
* @return string Rendered HTML |
126
|
|
|
*/ |
127
|
|
|
public function renderIndex($items) |
128
|
|
|
{ |
129
|
|
|
return $this->renderer |
130
|
|
|
->view($this->indexView) |
|
|
|
|
131
|
|
|
->set($items, self::ITEMS_VIEW_VARIABLE) |
132
|
|
|
->output(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set pagination for SamsonCMS query. |
137
|
|
|
* |
138
|
|
|
* @param int $pageNumber Result page number |
139
|
|
|
* @param int|null $pageSize Results page size |
140
|
|
|
* @return $this Chaining |
141
|
|
|
*/ |
142
|
|
|
public function pager($pageNumber, $pageSize = null) |
143
|
|
|
{ |
144
|
|
|
$this->pageNumber = $pageNumber; |
145
|
|
|
$this->pageSize = null !== $pageSize ? $pageSize : $this->pageSize; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @return string Rendered fields table */ |
151
|
|
|
public function __toString() |
152
|
|
|
{ |
153
|
|
|
return $this->output(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @return string Rendered HTML for fields table */ |
157
|
|
|
public function output() |
158
|
|
|
{ |
159
|
|
|
// Perform SamsonCMS query |
160
|
|
|
$collection = $this->find($this->pageNumber, $this->pageSize); |
161
|
|
|
|
162
|
|
|
if (count($collection)) { |
163
|
|
|
// Render each entity view in collection |
164
|
|
|
$html = ''; |
165
|
|
|
foreach ($collection as $row) { |
166
|
|
|
$html .= $this->innerRender($row, $collection, 'itemView', 'renderItem'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Render collection main view with items |
170
|
|
|
return $this->innerRender($html, $collection, 'indexView', 'renderIndex'); |
171
|
|
|
} else { // Render empty entity view |
172
|
|
|
return $this->innerRender('', $collection, 'emptyView', 'renderEmpty'); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Generic view renderer. |
178
|
|
|
* |
179
|
|
|
* @param mixed $item |
180
|
|
|
* @param Entity[] $collection |
181
|
|
|
* @param string $variableName |
182
|
|
|
* @param string $methodName |
183
|
|
|
* |
184
|
|
|
* @return mixed Rendered view |
185
|
|
|
*/ |
186
|
|
|
protected function innerRender($item, $collection, $variableName, $methodName) |
187
|
|
|
{ |
188
|
|
|
return is_callable($this->$variableName) |
189
|
|
|
? call_user_func($this->$variableName, $item, $this->renderer, $this->query, $collection) |
190
|
|
|
: $this->$methodName($item); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: