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 samsoncms\api\Entity; |
9
|
|
|
use samsonframework\core\ViewInterface; |
10
|
|
|
use samsonframework\orm\QueryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Renderable fields table. |
14
|
|
|
* Class should be used to simplify SamsonCMS generated entities outputting. |
15
|
|
|
* |
16
|
|
|
* @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering |
17
|
|
|
* |
18
|
|
|
* @package samsoncms\api\renderable |
19
|
|
|
*/ |
20
|
|
|
class Collection extends \samsoncms\api\query\Entity |
21
|
|
|
{ |
22
|
|
|
/** Name of the items variable in index view */ |
23
|
|
|
const ITEMS_VIEW_VARIABLE = 'items'; |
24
|
|
|
|
25
|
|
|
/** Name of the item prefix for variable in item view */ |
26
|
|
|
const ITEM_VIEW_VARIABLE = 'item'; |
27
|
|
|
|
28
|
|
|
/** @var string Block view file */ |
29
|
|
|
protected $indexView = 'www/index'; |
30
|
|
|
|
31
|
|
|
/** @var string Item view file */ |
32
|
|
|
protected $itemView = 'www/item'; |
33
|
|
|
|
34
|
|
|
/** @var string Empty view file */ |
35
|
|
|
protected $emptyView = 'www/empty'; |
36
|
|
|
|
37
|
|
|
/** @var ViewInterface View render object */ |
38
|
|
|
protected $renderer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Collection constructor. |
42
|
|
|
* |
43
|
|
|
* @param QueryInterface $query Instance for querying database |
44
|
|
|
* @param ViewInterface $renderer Instance for rendering views |
45
|
|
|
* @param string|null $locale Localization language |
46
|
|
|
*/ |
47
|
|
|
public function __construct(QueryInterface $query, ViewInterface $renderer, $locale = null) |
48
|
|
|
{ |
49
|
|
|
$this->renderer = $renderer; |
50
|
|
|
|
51
|
|
|
parent::__construct($query, $locale); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set index view path. |
56
|
|
|
* @param string $indexView Index view path |
57
|
|
|
* @return self Chaining |
58
|
|
|
*/ |
59
|
|
|
public function indexView($indexView) |
60
|
|
|
{ |
61
|
|
|
$this->indexView = $indexView; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set item view path. |
67
|
|
|
* @param string $itemView Item view path |
68
|
|
|
* @return self Chaining |
69
|
|
|
*/ |
70
|
|
|
public function itemView($itemView) |
71
|
|
|
{ |
72
|
|
|
$this->itemView = $itemView; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set empty view path. |
79
|
|
|
* @param string $emptyView Empty view path |
80
|
|
|
* @return self Chaining |
81
|
|
|
*/ |
82
|
|
|
public function emptyView($emptyView) |
83
|
|
|
{ |
84
|
|
|
$this->emptyView = $emptyView; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Render Entity collection item. |
90
|
|
|
* |
91
|
|
|
* @param Entity $item SamsonCMS entity for rendering |
92
|
|
|
* |
93
|
|
|
* @return string Rendered HTML |
94
|
|
|
*/ |
95
|
|
|
public function renderItem(Entity $item) |
96
|
|
|
{ |
97
|
|
|
return $this->renderer |
98
|
|
|
->view($this->itemView) |
99
|
|
|
->set(self::ITEM_VIEW_VARIABLE, $item) |
100
|
|
|
->output(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Render empty collection item. |
105
|
|
|
* |
106
|
|
|
* @return string Rendered HTML |
107
|
|
|
*/ |
108
|
|
|
public function renderEmpty() |
109
|
|
|
{ |
110
|
|
|
return $this->renderer->view($this->emptyView)->output(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render Entity collection index. |
115
|
|
|
* |
116
|
|
|
* @param string $items Collection of rendered items |
117
|
|
|
* |
118
|
|
|
* @return string Rendered HTML |
119
|
|
|
*/ |
120
|
|
|
public function renderIndex($items) |
121
|
|
|
{ |
122
|
|
|
return $this->renderer->view($this->indexView) |
123
|
|
|
->set(self::ITEMS_VIEW_VARIABLE, $items) |
124
|
|
|
->output(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @return string Rendered HTML for fields table */ |
128
|
|
|
public function render($page, $count) |
129
|
|
|
{ |
130
|
|
|
// Perform SamsonCMS query |
131
|
|
|
$collection = $this->find($page, $count); |
132
|
|
|
|
133
|
|
|
$html = ''; |
134
|
|
|
if (count($collection)) { |
135
|
|
|
// Render each entity view in collection |
136
|
|
|
foreach ($collection as $row) { |
137
|
|
|
$html .= $this->renderItem($row); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Render collection main view with items |
141
|
|
|
$html = $this->renderIndex($html); |
142
|
|
|
} else { // Render empty entity view |
143
|
|
|
$html .= $this->renderEmpty(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $html; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|