|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Nip\Form\Renderer\AbstractRenderer; |
|
4
|
|
|
|
|
5
|
|
|
class Nip_Form_Renderer_Table extends AbstractRenderer |
|
6
|
|
|
{ |
|
7
|
|
|
protected $_table = []; |
|
8
|
|
|
protected $_tbody = []; |
|
9
|
|
|
protected $_data = []; |
|
10
|
|
|
protected $_rows = []; |
|
11
|
|
|
protected $_cols = []; |
|
12
|
|
|
|
|
13
|
8 |
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
8 |
|
parent::__construct(); |
|
16
|
8 |
|
$this->setTableAttrib('class', 'form horizontal'); |
|
17
|
8 |
|
$this->setTableAttrib('cellspacing', '0'); |
|
18
|
8 |
|
$this->setTableAttrib('cellpadding', '0'); |
|
19
|
8 |
|
} |
|
20
|
|
|
|
|
21
|
8 |
|
public function setTableAttrib($type, $value) |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->_table[$type] = $value; |
|
24
|
8 |
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function setTBodyAttrib($type, $value) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->_tbody[$type] = $value; |
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setRowAttrib($idRow, $type, $value) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->_rows[$idRow][$type] = $value; |
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function addClassName($name) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->_table['class'] .= ' ' . $name; |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function addCell($idRow, $idCol, $element, $type = 'text') |
|
46
|
|
|
{ |
|
47
|
|
|
$this->_data[$idRow][$idCol]['element'] = $element; |
|
48
|
|
|
$this->_data[$idRow][$idCol]['type'] = $type; |
|
49
|
|
|
if (!in_array($idCol, $this->_cols)) { |
|
50
|
|
|
$this->_cols[] = $idCol; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setCols() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->_cols = func_get_args(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function renderElements() |
|
61
|
|
|
{ |
|
62
|
|
|
$return = '<table'; |
|
63
|
|
|
foreach ($this->_table as $attrib => $value) { |
|
64
|
|
|
$return .= ' ' . $attrib . '="' . $value . '"'; |
|
65
|
|
|
} |
|
66
|
|
|
$return .= '>'; |
|
67
|
|
|
$renderRows = $this->renderRows(); |
|
68
|
|
|
$return .= '<tbody'; |
|
69
|
|
|
foreach ($this->_tbody as $attrib => $value) { |
|
70
|
|
|
$return .= ' ' . $attrib . '="' . $value . '"'; |
|
71
|
|
|
} |
|
72
|
|
|
$return .= '>'; |
|
73
|
|
|
if ($renderRows) { |
|
74
|
|
|
$return .= $renderRows; |
|
75
|
|
|
} |
|
76
|
|
|
$return .= '</tbody>'; |
|
77
|
|
|
$return .= '</table>'; |
|
78
|
|
|
return $return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function renderRows() |
|
82
|
|
|
{ |
|
83
|
|
|
$return = ''; |
|
84
|
|
|
foreach ($this->_data as $idRow=>$row) { |
|
85
|
|
|
$cell = reset($row); |
|
86
|
|
|
$element = $cell['element']; |
|
87
|
|
|
|
|
88
|
|
|
if (!$element->isRendered()) { |
|
89
|
|
|
$return .= '<tr'; |
|
90
|
|
|
if ($this->_rows[$idRow]) { |
|
91
|
|
|
foreach ($this->_rows[$idRow] as $attrib => $value) { |
|
92
|
|
|
$return .= ' ' . $attrib . '="' . $value . '"'; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$return .= '>'; |
|
97
|
|
|
foreach ($this->_cols as $idCol) { |
|
98
|
|
|
$cell = $row[$idCol]; |
|
99
|
|
|
switch ($cell['type']) { |
|
100
|
|
|
case 'label': |
|
101
|
|
|
$return .= '<td class="label' . ($element->isError() ? ' error' : '') . '">'; |
|
102
|
|
|
$return .= $element->getLabel(); |
|
103
|
|
|
if ($element->isRequired()) { |
|
104
|
|
|
$return .= '<span class="required">*</span>'; |
|
105
|
|
|
} |
|
106
|
|
|
$return .= ":"; |
|
107
|
|
|
break; |
|
108
|
|
|
|
|
109
|
|
|
case 'value': |
|
110
|
|
|
$return .= '<td class="value">'; |
|
111
|
|
|
$return .= $this->renderElement($element); |
|
112
|
|
|
$return .= '</td>'; |
|
113
|
|
|
break; |
|
114
|
|
|
|
|
115
|
|
|
case 'text': |
|
116
|
|
|
default: |
|
117
|
|
|
$return .= '<td>'; |
|
118
|
|
|
$return .= $cell['element']; |
|
119
|
|
|
$return .= '</td>'; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$return .= '</tr>'; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
return $return; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|