1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Fields; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Riclep\Storyblok\Field; |
9
|
|
|
use Riclep\Storyblok\Page; |
10
|
|
|
|
11
|
|
|
class Table extends Field |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array|string caption for the table |
15
|
|
|
*/ |
16
|
|
|
protected $caption; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string a class to apply to the <table> tag |
20
|
|
|
*/ |
21
|
|
|
protected $cssClass; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array|int the column numbers to convert to headers |
25
|
|
|
*/ |
26
|
|
|
protected $headerColumns; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __toString() |
30
|
|
|
{ |
31
|
|
|
return $this->toHtml($this->content); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function toHtml($table) { |
35
|
|
|
$html = '<table ' . ($this->cssClass ? 'class="' . $this->cssClass . '"' : null) . '>'; |
36
|
|
|
|
37
|
|
|
if ($this->caption) { |
38
|
|
|
if (is_array($this->caption)) { |
39
|
|
|
$html .= '<caption class="' . $this->caption[1] . '">' . $this->caption[0] . '</caption>'; |
40
|
|
|
} else { |
41
|
|
|
$html .= '<caption>' . $this->caption . '</caption>'; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$html .= '<thead><tr>'; |
46
|
|
|
|
47
|
|
|
foreach ($table['thead'] as $header) { |
48
|
|
|
$html .= '<th>' . nl2br($header['value']) . '</th>'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$html .= '</tr></thead><tbody>'; |
52
|
|
|
|
53
|
|
|
foreach ($table['tbody'] as $row) { |
54
|
|
|
$html .= '<tr>'; |
55
|
|
|
|
56
|
|
|
foreach ($row['body'] as $column => $cell) { |
57
|
|
|
if ($this->headerColumns && in_array(($column + 1), Arr::wrap($this->headerColumns))) { |
58
|
|
|
$html .= '<th>' . nl2br($cell['value']) . '</th>'; |
59
|
|
|
} else { |
60
|
|
|
$html .= '<td>' . nl2br($cell['value']) . '</td>'; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$html .= '</tr>'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $html . '</tbody></table>'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function caption($caption) { |
71
|
|
|
$this->caption = $caption; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function cssClass($cssClass) { |
77
|
|
|
$this->cssClass = $cssClass; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
} |