1
|
|
|
<?php |
2
|
|
|
ar_pinp::allow('ar_html_table'); |
3
|
|
|
|
4
|
|
|
class ar_html_table extends ar_htmlElement { |
5
|
|
|
|
6
|
|
|
private $rowHeaderAttributes = null; |
7
|
|
|
private $rowHeaders = null; |
8
|
|
|
|
9
|
|
|
public function __construct( $data= null, $attributes = null, $childNodes = null, $parentNode = null ) { |
10
|
|
|
parent::__construct( 'table', $attributes , $childNodes, $parentNode ); |
11
|
|
|
if ( isset($data) ) { |
12
|
|
|
$this->body( $data ); |
13
|
|
|
} |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function body( $data ) { |
17
|
|
|
if (!is_array($data) ) { |
18
|
|
|
$data = array( $data ); |
19
|
|
|
} |
20
|
|
|
$rows = $this->getRows($data)->setAttribute( 'class', array( |
21
|
|
|
'tableFirstLast' => ar::listPattern( 'tableFirst .*', '.* tableLast'), |
22
|
|
|
'tableOddEven' => ar::listPattern( '(tableOdd tableEven?)*' ) |
23
|
|
|
) ); |
24
|
|
|
$this->appendChild( |
25
|
|
|
ar_html::tag( |
26
|
|
|
'tbody', $rows |
27
|
|
|
) |
28
|
|
|
); |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function getRows( $list ) { |
33
|
|
|
$nodes = ar_html::nodes(); |
34
|
|
|
foreach ( $list as $key => $content ) { |
35
|
|
|
if ( !is_array( $content ) ) { |
36
|
|
|
$content = array( $content ); |
37
|
|
|
} |
38
|
|
|
$cells = $this->getCells( $content, 'td')->setAttribute('class', array( |
39
|
|
|
'tableOddEven' => ar::listPattern( '(tableOdd tableEven?)*' ), |
40
|
|
|
'tableFirstLast' => ar::listPattern( 'tableFirst .*', '.* tableLast') |
41
|
|
|
) ); |
42
|
|
|
$nodes[] = ar_html::tag( 'tr', ar_html::nodes( $header, $cells ) ); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
return $nodes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getCells( $list, $tag = 'td') { |
48
|
|
|
$nodes = ar_html::nodes(); |
49
|
|
|
foreach ($list as $key => $content ) { |
50
|
|
|
$nodes[] = ar_html::el($tag, $content); |
51
|
|
|
} |
52
|
|
|
return $nodes; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function head( $list, $attributes = null ) { |
56
|
|
|
if ( is_array($list) ) { |
57
|
|
|
$nodes = $this->getCells( $list, 'th' ); |
58
|
|
|
$head = $this->thead->firstChild; //current( $this->getElementsByTagName('thead') ); |
|
|
|
|
59
|
|
|
if ( !isset($head) ) { |
60
|
|
|
$head = ar_html::tag( 'thead', $attributes ); |
61
|
|
|
if ( $foot = $this->tfoot->firstChild ) { |
|
|
|
|
62
|
|
|
$this->insertBefore( $head, $foot ); |
63
|
|
|
} else if ( $body = $this->tbody->firstChild ) { |
|
|
|
|
64
|
|
|
$this->insertBefore( $head, $body ); |
65
|
|
|
} else { |
66
|
|
|
$this->appendChild( $head ); |
67
|
|
|
} |
68
|
|
|
} else if (isset($attributes)) { |
69
|
|
|
$head->setAttributes( $attributes ); |
70
|
|
|
} |
71
|
|
|
$head->appendChild( ar_html::tag( 'tr', $nodes ) ); |
72
|
|
|
} |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function foot( $list, $attributes = null ) { |
77
|
|
|
if ( is_array( $list ) ) { |
78
|
|
|
$nodes = $this->getCells( $list, 'td' ); |
79
|
|
|
$foot = $this->tfoot->lastChild; |
|
|
|
|
80
|
|
|
if ( !isset($foot) ) { |
81
|
|
|
$foot = ar_html::tag( 'tfoot', $attributes ); |
82
|
|
|
if ( $head = $this->thead->lastChild ) { |
|
|
|
|
83
|
|
|
$this->insertBefore( $foot, $head->nextSibling ); |
84
|
|
|
} else if ( $body = $this->tbody->firstChild ) { |
|
|
|
|
85
|
|
|
$this->insertBefore( $foot, $body ); |
86
|
|
|
} else { |
87
|
|
|
$this->appendChild( $foot ); |
88
|
|
|
} |
89
|
|
|
} else if (isset( $attributes ) ) { |
90
|
|
|
$foot->setAttributes( $attributes ); |
91
|
|
|
} |
92
|
|
|
$foot->appendChild( ar_html::tag( 'tr', $nodes ) ); |
93
|
|
|
} |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function rowHeaders( $list, $attributes = array() ) { |
98
|
|
|
foreach ( $list as $key => $value ) { |
99
|
|
|
if ( ! ($value instanceof ar_htmlNode ) || $value->tagName != 'th' ) { |
|
|
|
|
100
|
|
|
$list[$key] = ar_html::tag( 'th', |
101
|
|
|
array_merge( |
102
|
|
|
(array) $attributes, |
103
|
|
|
array( 'scope' => 'row' ) |
104
|
|
|
), |
105
|
|
|
$value |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
reset($list); |
110
|
|
|
foreach( $this->tbody->lastChild->childNodes as $row ) { |
|
|
|
|
111
|
|
|
$row->insertBefore( current($list), $row->firstChild ); |
112
|
|
|
next($list); |
113
|
|
|
} |
114
|
|
|
$this->rowHeaders = $list; |
115
|
|
|
$this->rowHeaderAttributes = $attributes; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function cols() { |
120
|
|
|
$args = func_get_args(); |
121
|
|
|
$cols = call_user_func_array( array('ar_html', 'nodes'), $args ); |
122
|
|
|
$this->removeChild( $this->colgroup ); |
|
|
|
|
123
|
|
|
$this->removeChild( $this->col ); |
|
|
|
|
124
|
|
|
$this->insertBefore( $cols, $this->firstChild ); |
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function caption($content, $attributes = null) { |
129
|
|
|
$newCaption = ar_html::tag( 'caption', $content, $attributes ); |
130
|
|
|
if ( $caption = $this->caption->firstChild ) { |
|
|
|
|
131
|
|
|
$this->replaceChild( $newCaption, $caption ); |
132
|
|
|
} else { |
133
|
|
|
$this->insertBefore( $newCaption, $this->firstChild ); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.