-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedrRenderer.php
More file actions
382 lines (320 loc) · 10.5 KB
/
EmbedrRenderer.php
File metadata and controls
382 lines (320 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php namespace ProcessWire;
/**
* Embedr Renderer
*
* Renders items using visual card builder (no PHP templates needed)
*/
class EmbedrRenderer {
/**
* Type
*
* @var EmbedrType
*/
protected $type;
/**
* Items to render
*
* @var PageArray
*/
protected $items;
/**
* Config
*
* @var array
*/
protected $config;
/**
* Construct
*
* @param EmbedrType $type
* @param PageArray $items
*/
public function __construct(EmbedrType $type, $items) {
$this->type = $type;
$this->items = $items;
$this->config = $type->getConfig();
}
/**
* Render items
*
* @return string
*/
public function render() {
$layout = $this->config['layout'];
$mode = $this->type->mode ?: 'array';
// If mode is 'once', show only first item as single card
if($mode === 'once') {
if(!$this->items->count()) {
return "<!-- Embedr: No items found -->";
}
return $this->renderCard($this->items->first());
}
// Array mode - show multiple items
switch($layout) {
case 'grid':
return $this->renderGrid();
case 'list':
return $this->renderList();
case 'table':
return $this->renderTable();
default:
return $this->renderGrid();
}
}
/**
* Render grid layout
*
* @return string
*/
protected function renderGrid() {
$columns = (int) $this->config['columns'];
// UIKit supports 2, 3, 4, 6 - default to 6 for anything else
if(!in_array($columns, [2, 3, 4, 6])) {
$columns = 6;
}
// UIKit responsive grid classes
switch($columns) {
case 2:
$widthClass = 'uk-child-width-1-2@m';
break;
case 3:
$widthClass = 'uk-child-width-1-3@m';
break;
case 4:
$widthClass = 'uk-child-width-1-2@s uk-child-width-1-4@m';
break;
case 6:
$widthClass = 'uk-child-width-1-2@s uk-child-width-1-3@m uk-child-width-1-6@l';
break;
default:
$widthClass = 'uk-child-width-1-2@s uk-child-width-1-3@m uk-child-width-1-6@l';
}
$out = "<div class='uk-grid uk-grid-small {$widthClass} uk-grid-match' uk-grid>";
foreach($this->items as $item) {
$out .= "<div>{$this->renderCard($item)}</div>";
}
$out .= "</div>";
return $out;
}
/**
* Render list layout
*
* @return string
*/
protected function renderList() {
$out = "<div class='embedr-list space-y-4'>";
foreach($this->items as $item) {
$out .= $this->renderListItem($item);
}
$out .= "</div>";
return $out;
}
/**
* Render table layout
*
* @return string
*/
protected function renderTable() {
$out = "<table class='embedr-table w-full'>";
$out .= "<thead><tr>";
$fields = $this->config['fields'];
foreach($fields as $label => $fieldName) {
if(empty($fieldName)) continue;
$out .= "<th>" . ucfirst($label) . "</th>";
}
$out .= "</tr></thead><tbody>";
foreach($this->items as $item) {
$out .= $this->renderTableRow($item);
}
$out .= "</tbody></table>";
return $out;
}
/**
* Render single card
*
* @param Page $item
* @return string
*/
protected function renderCard($item) {
$fields = $this->config['fields'];
$link = $this->config['link'];
$out = "<div class='uk-card uk-card-default uk-card-hover' style='height: 100%;'>";
// Image
if(!empty($fields['image'])) {
$image = $this->getFieldValue($item, $fields['image']);
if($image) {
$imageUrl = $this->getImageUrl($image);
if($imageUrl) {
$out .= "<div class='uk-card-media-top' style='aspect-ratio: 1/1; overflow: hidden;'>";
if($link) $out .= "<a href='{$item->url}'>";
$out .= "<img src='{$imageUrl}' alt='{$item->title}' style='width: 100%; height: 100%; object-fit: cover;'>";
if($link) $out .= "</a>";
$out .= "</div>";
}
}
}
$out .= "<div class='uk-card-body'>";
// Title
if(!empty($fields['title'])) {
$title = $this->getFieldValue($item, $fields['title']);
$out .= "<h3 class='uk-card-title uk-text-small uk-margin-remove'>";
if($link) {
$out .= "<a href='{$item->url}'>{$title}</a>";
} else {
$out .= $title;
}
$out .= "</h3>";
}
// Description
if(!empty($fields['description'])) {
$description = $this->getFieldValue($item, $fields['description']);
$out .= "<p class='uk-text-small uk-text-muted'>{$description}</p>";
}
// Meta
if(!empty($fields['meta'])) {
$meta = $this->getFieldValue($item, $fields['meta']);
if($meta) {
$out .= "<p class='uk-text-meta'>{$meta}</p>";
}
}
// Price
if(!empty($fields['price'])) {
$price = $this->getFieldValue($item, $fields['price']);
if($price) {
$out .= "<p class='uk-text-bold uk-margin-small-top'>{$price}</p>";
}
}
$out .= "</div>";
$out .= "</div>";
return $out;
}
/**
* Render list item
*
* @param Page $item
* @return string
*/
protected function renderListItem($item) {
$fields = $this->config['fields'];
$link = $this->config['link'];
$out = "<div class='embedr-list-item flex gap-4 border-b pb-4'>";
// Image
if(!empty($fields['image'])) {
$image = $this->getFieldValue($item, $fields['image']);
if($image) {
$imageUrl = $this->getImageUrl($image);
if($imageUrl) {
$out .= "<div class='embedr-list-image flex-shrink-0'>";
if($link) $out .= "<a href='{$item->url}'>";
$out .= "<img src='{$imageUrl}' alt='{$item->title}' class='w-24 h-24 object-cover rounded'>";
if($link) $out .= "</a>";
$out .= "</div>";
}
}
}
$out .= "<div class='embedr-list-content flex-1'>";
// Title
if(!empty($fields['title'])) {
$title = $this->getFieldValue($item, $fields['title']);
$out .= "<h3 class='text-lg font-bold'>";
if($link) {
$out .= "<a href='{$item->url}' class='hover:underline'>{$title}</a>";
} else {
$out .= $title;
}
$out .= "</h3>";
}
// Description
if(!empty($fields['description'])) {
$description = $this->getFieldValue($item, $fields['description']);
$out .= "<p class='text-sm text-gray-600 mt-1'>{$description}</p>";
}
// Price
if(!empty($fields['price'])) {
$price = $this->getFieldValue($item, $fields['price']);
if($price) {
$out .= "<p class='text-lg font-bold mt-2'>{$price}</p>";
}
}
$out .= "</div>";
$out .= "</div>";
return $out;
}
/**
* Render table row
*
* @param Page $item
* @return string
*/
protected function renderTableRow($item) {
$fields = $this->config['fields'];
$out = "<tr>";
foreach($fields as $label => $fieldName) {
if(empty($fieldName)) continue;
$value = $this->getFieldValue($item, $fieldName);
if($label === 'image' && $value) {
$imageUrl = $this->getImageUrl($value);
$value = $imageUrl ? "<img src='{$imageUrl}' class='w-12 h-12 object-cover'>" : '';
}
$out .= "<td class='p-2'>{$value}</td>";
}
$out .= "</tr>";
return $out;
}
/**
* Get field value (supports dot notation)
*
* @param Page $item
* @param string $fieldPath
* @return mixed
*/
protected function getFieldValue($item, $fieldPath) {
if(empty($fieldPath)) return '';
// Support dot notation: category.title
if(strpos($fieldPath, '.') !== false) {
$parts = explode('.', $fieldPath);
$value = $item;
foreach($parts as $part) {
if(!$value) break;
$value = $value->get($part);
}
return $value;
}
return $item->get($fieldPath);
}
/**
* Get image URL
*
* @param mixed $image
* @return string
*/
protected function getImageUrl($image) {
$width = (int) $this->config['imageWidth'];
$height = (int) $this->config['imageHeight'];
// If it's a Pageimages field, get first image
if($image instanceof \ProcessWire\Pageimages && $image->count()) {
$image = $image->first();
}
// If it's a Pageimage
if($image instanceof \ProcessWire\Pageimage) {
if($width && $height) {
return $image->size($width, $height)->url;
} else if($width) {
return $image->width($width)->url;
}
return $image->url;
}
return '';
}
/**
* Get CSS column class for grid
*
* @param int $columns
* @return string
*/
protected function getColumnClass($columns) {
// UIKit uses uk-child-width-1-{n}@m format
// Already handled in renderGrid
return '';
}
}