summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Utils/HtmlTable.php
blob: 9a42386b9e1d9bff4277b219254fd6021b1b667a (plain)
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
<?php

namespace SMW\Utils;

use Html;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class HtmlTable {

	/**
	 * @var array
	 */
	private $headers = [];

	/**
	 * @var array
	 */
	private $cells = [];

	/**
	 * @var array
	 */
	private $rows = [];

	/**
	 * @since 3.0
	 *
	 * @param string $content
	 * @param array $attributes
	 */
	public function header( $content = '', $attributes = [] ) {
		if ( $content !== '' ) {
			$this->headers[] = [ 'content' => $content, 'attributes' => $attributes ];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $content
	 * @param array $attributes
	 */
	public function cell( $content = '', $attributes = [] ) {
		if ( $content !== '' ) {
			$this->cells[] = Html::rawElement( 'td', $attributes, $content );
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param array $attributes
	 *
	 * @return TableBuilder
	 */
	public function row( $attributes = [] ) {
		if ( $this->cells !== [] ) {
			$this->rows[] = [ 'cells' => $this->cells, 'attributes' => $attributes ];
			$this->cells = [];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param array $attributes
	 *
	 * @return string
	 */
	public function table( $attributes = [], $transpose = false, $htmlContext = false ) {

		$table = $this->buildTable( $transpose, $htmlContext );

		if ( $transpose ) {
			$attributes['data-transpose'] = true;
		}

		$this->headers = [];
		$this->rows = [];
		$this->cells = [];

		if ( $table !== '' ) {
			return Html::rawElement( 'table', $attributes, $table );
		}

		return '';
	}

	private function buildTable( $transpose, $htmlContext ) {

		if ( $transpose ) {
			return $this->transpose( $htmlContext );
		}

		$headers = [];
		$rows = [];

		foreach( $this->headers as $i => $header ) {
			$headers[] = Html::rawElement( 'th', $header['attributes'], $header['content'] );
		}

		foreach( $this->rows as $row ) {
			$rows[] = $this->createRow( implode( '', $row['cells'] ), $row['attributes'], count( $rows ) );
		}

		return $this->concatenateHeaders( $headers, $htmlContext ) . $this->concatenateRows( $rows, $htmlContext );
	}

	private function transpose( $htmlContext ) {

		$rows = [];

		foreach( $this->headers as $hIndex => $header ) {
			$cells = [];
			$headerItem = Html::rawElement( 'th', $header['attributes'], $header['content'] );

			foreach( $this->rows as $rIndex => $row ) {
				$cells[] = $this->getTransposedCell( $hIndex, $row );
			}

			// Collect new rows
			$rows[] = $this->createRow( $headerItem . implode( '', $cells ), [], count( $rows ) );
		}

		return $this->concatenateRows( $rows, $htmlContext );
	}

	private function createRow( $content = '', $attributes = [], $count ) {

		$alternate = $count % 2 == 0 ? 'row-odd' : 'row-even';

		if ( isset( $attributes['class'] ) ) {
			$attributes['class'] = $attributes['class'] . ' ' . $alternate;
		} else {
			$attributes['class'] = $alternate;
		}

		return Html::rawElement( 'tr', $attributes, $content );
	}

	private function concatenateHeaders( $headers, $htmlContext ) {

		if ( $htmlContext ) {
			return Html::rawElement( 'thead', [], implode( '', $headers ) );
		}

		return implode( '', $headers );
	}

	private function concatenateRows( $rows, $htmlContext ) {

		if ( $htmlContext ) {
			return Html::rawElement( 'tbody', [], implode( '', $rows ) );
		}

		return implode( '', $rows );
	}

	private function getTransposedCell( $index, $row ) {

		if ( isset( $row['cells'][$index] ) ) {
			return $row['cells'][$index];
		}

		$attributes = [];

		if ( isset( $row['attributes']['class'] ) && $row['attributes']['class'] === 'smwfooter' ) {
			$attributes = [ 'class' => 'footer-cell' ];
		}

		return Html::rawElement( 'td', $attributes, '' );
	}

}