1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 10.02.16 at 18:15 |
5
|
|
|
*/ |
6
|
|
|
namespace samsoncms\api\renderable; |
7
|
|
|
|
8
|
|
|
use samsoncms\api\field\Row; |
9
|
|
|
use samsonframework\core\RenderInterface; |
10
|
|
|
use samsonframework\core\ViewInterface; |
11
|
|
|
use samsonframework\orm\QueryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Renderable fields table. |
15
|
|
|
* Class should be used to simplify additional field tables rendering. |
16
|
|
|
* If your additional fields table has special logic just extend this class |
17
|
|
|
* and change any ot its method. |
18
|
|
|
* |
19
|
|
|
* @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering |
20
|
|
|
* |
21
|
|
|
* @package samsoncms\api\renderable |
22
|
|
|
*/ |
23
|
|
|
class FieldsTable extends \samsoncms\api\field\Table implements RenderInterface |
24
|
|
|
{ |
25
|
|
|
/** Name of the rows variable in index view */ |
26
|
|
|
const ROWS_VIEW_VARIABLE = 'rows'; |
27
|
|
|
|
28
|
|
|
/** Name of the row prefix for variable in row view */ |
29
|
|
|
const ROW_VIEW_VARIABLE = 'row'; |
30
|
|
|
|
31
|
|
|
/** @var string|callable Block view file or callback */ |
32
|
|
|
protected $indexView = 'index'; |
33
|
|
|
|
34
|
|
|
/** @var string|callable Row view file or callback */ |
35
|
|
|
protected $rowView = 'row'; |
36
|
|
|
|
37
|
|
|
/** @var ViewInterface */ |
38
|
|
|
protected $renderer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* GeneralInfo constructor. |
42
|
|
|
* |
43
|
|
|
* @param QueryInterface $query |
44
|
|
|
* @param ViewInterface $renderer |
45
|
|
|
* @param int[] $navigationID Collection of entity navigation identifiers |
46
|
|
|
* @param int $entityID Entity identifier |
47
|
|
|
* @param string|null $locale Table localization |
48
|
|
|
*/ |
49
|
|
|
public function __construct(QueryInterface $query, ViewInterface $renderer, $navigationID, $entityID, $locale = null) |
50
|
|
|
{ |
51
|
|
|
// Store renderer |
52
|
|
|
$this->renderer = $renderer; |
53
|
|
|
|
54
|
|
|
parent::__construct($query, $navigationID, $entityID, $locale); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set index view path. |
59
|
|
|
* @param string|callable $indexView Index view path or callback |
60
|
|
|
* @return $this Chaining |
61
|
|
|
*/ |
62
|
|
|
public function indexView($indexView) |
63
|
|
|
{ |
64
|
|
|
$this->indexView = $indexView; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
/** |
68
|
|
|
* Set row view path. |
69
|
|
|
* @param string|callable $rowView Row view path or callback |
70
|
|
|
* @return $this Chaining |
71
|
|
|
*/ |
72
|
|
|
public function rowView($rowView) |
73
|
|
|
{ |
74
|
|
|
$this->rowView = $rowView; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Render table row. |
80
|
|
|
* |
81
|
|
|
* @param Row $row Collection of column values. |
82
|
|
|
* |
83
|
|
|
* @return string Rendered HTML |
84
|
|
|
*/ |
85
|
|
|
public function renderRow(Row $row) |
86
|
|
|
{ |
87
|
|
|
return $this->renderer->view($this->rowView)->set(self::ROW_VIEW_VARIABLE, $row)->output(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Render table row. |
92
|
|
|
* |
93
|
|
|
* @param string $items Collection of rendered rows. |
94
|
|
|
* |
95
|
|
|
* @return string Rendered HTML |
96
|
|
|
*/ |
97
|
|
|
public function renderIndex($items) |
98
|
|
|
{ |
99
|
|
|
return $this->renderer->view($this->indexView) |
|
|
|
|
100
|
|
|
->set(self::ROWS_VIEW_VARIABLE, $items) |
101
|
|
|
->output(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @return string Rendered HTML for fields table */ |
105
|
|
|
public function render() |
106
|
|
|
{ |
107
|
|
|
$html = ''; |
108
|
|
View Code Duplication |
foreach ($this->collection as $row) { |
|
|
|
|
109
|
|
|
// Call external handler |
110
|
|
|
if (is_callable($this->rowView)) { |
111
|
|
|
$html .= call_user_func($this->rowView, $this->renderer, $this->query, $row); |
112
|
|
|
} else { // Call default renderer |
113
|
|
|
$html .= $this->renderRow($row); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Call external handler |
118
|
|
|
if (is_callable($this->indexView)) { |
119
|
|
|
$html = call_user_func($this->indexView, $html, $this->renderer, $this->query, $this->collection); |
120
|
|
|
} else { // Call default renderer |
121
|
|
|
$html = $this->renderIndex($html); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $html; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Prepare fields table for rendering in the view. |
129
|
|
|
* |
130
|
|
|
* @param string|null $prefix Prefix to be added to view variables |
131
|
|
|
* @param array $restricted Collection of fields to be restricted |
132
|
|
|
* |
133
|
|
|
* @return array Collection of view variables for rendering |
134
|
|
|
*/ |
135
|
|
|
public function toView($prefix = null, array $restricted = array()) |
136
|
|
|
{ |
137
|
|
|
return array_diff_key(array($prefix.'html' => $this->render()), $restricted); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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: