8794e319de05611d80218f8a3f07fa1992d0c228
fenris foo

fenris authored 8 years ago

1) <?php
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

2) include_once("misc.php");
3) 
fenris foo

fenris authored 8 years ago

4) class class_column
5) {
6) 	public $title;
7) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

8) 	public $field;
fenris foo

fenris authored 8 years ago

9) 	
10) 	public $format_;
11) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

12) 	public function __construct($title, $field, $format_ = null)
fenris foo

fenris authored 8 years ago

13) 	{
14) 		if ($format_ == null) $format_ = function ($x) {return $x;};
15) 		$this->title = $title;
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

16) 		$this->field = $field;
fenris foo

fenris authored 8 years ago

17) 		$this->format_ = $format_;
18) 	}
19) 	
20) 	public function extract($row)
21) 	{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

22) 		// return call_user_func($this->extract_, $row);
23) 		return $row[$this->field];
fenris foo

fenris authored 8 years ago

24) 		// return $this->extract_($row);
25) 	}
26) 	
27) 	public function format($value)
28) 	{
29) 		return call_user_func($this->format_, $value);
30) 		// return $this->format_($value);
31) 	}
32) }
33) 
34) class class_table
35) {
36) 	private $columns;
37) 	
38) 	private $rows;
39) 	
40) 	public function __construct($columns, $rows = [])
41) 	{
42) 		$this->columns = $columns;
43) 		$this->rows = [];
44) 		$this->fill($rows);
45) 	}
46) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

47) 	public function columns_get()
48) 	{
49) 		return $this->columns;
50) 	}
51) 	
fenris foo

fenris authored 8 years ago

52) 	private function add($row)
53) 	{
54) 		array_push($this->rows, $row);
55) 	}
56) 	
57) 	private function fill($rows)
58) 	{
59) 		array_map
60) 		(
61) 			function ($row) {$this->add($row);},
62) 			$rows
63) 		);
64) 	}
65) 	
Christian Fraß minor changes; move to kora...

Christian Fraß authored 7 years ago

66) 	/*
67) 	+------+------+------+
68) 	|  xA  |  xB  |  xC  |
69) 	+------+------+------+
70) 	|  a2  |  b1  |  c3  |
71) 	+------+------+------+
72) 	|  a1  |  b1  |  c0  |
73) 	+------+------+------+
74) 	|  a1  |  b3  |  c2  |
75) 	+------+------+------+
76) 	|  a2  |  b2  |  c4  |
77) 	+------+------+------+
78) 	|  a1  |  b2  |  c1  |
79) 	+------+------+------+
80) 	|  a2  |  b4  |  c5  |
81) 	+------+------+------+
82) 	
83) 	+------+------+------+
84) 	|  xA  |  xB  |  xC  |
85) 	+------+------+------+
86) 	|  a1  |  b1  |  c0  |
87) 	+------+------+------+
88) 	|  a1  |  b2  |  c1  |
89) 	+------+------+------+
90) 	|  a1  |  b3  |  c2  |
91) 	+------+------+------+
92) 	|  a2  |  b1  |  c3  |
93) 	+------+------+------+
94) 	|  a2  |  b2  |  c4  |
95) 	+------+------+------+
96) 	|  a2  |  b4  |  c5  |
97) 	+------+------+------+
98) 	
99) 	+------+------+------+
100) 	|  xA  |  xB  |  xC  |
101) 	+------+------+------+
102) 	|  a1  |  b1  |  c0  |
103) 	|      +------+------+
104) 	|      |  b2  |  c1  |
105) 	|      +------+------+
106) 	|      |  b3  |  c2  |
107) 	+------+------+------+
108) 	|  a2  |  b1  |  c3  |
109) 	|      +------+------+
110) 	|      |  b2  |  c4  |
111) 	|      +------+------+
112) 	|      |  b4  |  c5  |
113) 	+------+------+------+
114) 	
115) 	+------+------+------+------+------+
116) 	|  xA  | xB:b1| xB:b2| xB:b3| xB:b4|
117) 	+------+------+------+------+------+
118) 	|  a1  |  c0  |  c1  |  c2  |  --  |
119) 	+------+------+------+------+------+
120) 	|  a2  |  c3  |  c4  |  --  |  c5  |
121) 	+------+------+------+------+------+
122) 	 */
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

123) 	public function snap($configuration)
124) 	{
125) 		$columns_vertical = fetch($configuration, "columns_vertical", null, 2);
126) 		$columns_horizontal = fetch($configuration, "columns_horizontal", null, 2);
127) 		$columns_data = fetch($configuration, "columns_data", null, 2);
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

128) 		$data_aggregator = fetch($configuration, "data_aggregator", function ($values) {return /*json_encode(*/$values/*)*/;}, 1);
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

129) 		
130) 		$columns_source =
131) 		[
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

132) 			new class_column("Vertical", "_vertical"),
133) 			new class_column("Horizontal", "_horizontal"),
134) 			new class_column("Data", "_data", function ($x) {return json_encode($x);}),
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

135) 		];
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

136) 		// gather data for the three columns
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

137) 		$rows_source = null;
138) 		{
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

139) 			$rows_source = array_map
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

140) 			(
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

141) 				function ($row) use (&$columns_vertical, &$columns_horizontal, &$columns_data, $data_aggregator)
142) 				{
143) 					$raw_vertical = []; foreach ($columns_vertical as $column) array_push($raw_vertical, $column->extract($row));
144) 					$raw_horizontal = []; foreach ($columns_horizontal as $column) array_push($raw_horizontal, $column->extract($row));
145) 					$raw_data = []; foreach ($columns_data as $column) $raw_data[$column->field] = $column->extract($row);
146) 					
147) 					$row_ = [];
148) 					$row_["_vertical"] = implode("/", $raw_vertical);
149) 					$row_["_horizontal"] = implode("/", $raw_horizontal);
150) 					$row_["_data"] = $data_aggregator($raw_data);
151) 					return $row_;
152) 				},
153) 				$this->rows
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

154) 			);
155) 		}
156) 		// return (new class_table($columns_source, $rows_source));
157) 		$columns_result = [];
158) 		{
159) 			array_push
160) 			(
161) 				$columns_result,
162) 				new class_column
163) 				(
164) 					fetch
165) 					(
166) 						$configuration,
167) 						"label_vertical",
168) 						function ($columns) {return implode("/", array_map(function ($column) {return $column->title;}, $columns));},
169) 						1
170) 					)($columns_vertical),
171) 					"vertical"
172) 				)
173) 			);
174) 		}
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

175) 		// find groups
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

176) 		$values = [];
177) 		foreach ($rows_source as $row)
178) 		{
179) 			$value = $columns_source[1]->extract($row);
180) 			if (array_search($value, $values) === false)
181) 			{
182) 				array_push
183) 				(
184) 					$columns_result,
185) 					new class_column
186) 					(
187) 						fetch
188) 						(
189) 							$configuration,
190) 							"label_horizontal",
191) 							function ($columns, $value) {return implode("/", array_map(function ($column) {return $column->title;}, $columns)) . ":" . $value;},
192) 							1
193) 						)($columns_horizontal, $value),
194) 						sprintf("horizontal_%u", count($columns_result)-1),
195) 						fetch($configuration, "data_formatter", function ($x) {return json_encode($x);}, 1)
196) 					)
197) 				);
198) 				array_push($values, $value);
199) 			}
200) 		}
Christian Fraß added transition-data and t...

Christian Fraß authored 7 years ago

201) 		$groups = sql_groups($rows_source, "_vertical");
202) // echo("<!-- " . json_encode($groups, JSON_PRETTY_PRINT) . " -->" . "\n");
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

203) 		$rows_result = array_map
204) 		(
205) 			function ($group) use (&$columns_vertical,&$columns_horizontal,&$columns_data,&$columns_source,&$columns_result,&$values)
206) 			{
207) 				$row = [];
208) 				{
209) 					$row["vertical"] = $group["value"];
210) 				}
211) 				for ($index = 0; $index < count($columns_result); ++$index)
212) 				{
213) 					$row[sprintf("horizontal_%u", $index)] = [];
214) 				}
215) 				foreach ($group["members"] as $member)
216) 				{
217) 					$value = $columns_source[1]->extract($member);
218) 					$data = $columns_source[2]->extract($member);
219) 					$index = array_search($value, $values);
220) 					if ($index === false)
221) 					{
222) 						throw ("fatal error");
223) 					}
224) 					else
225) 					{
226) 						$field = sprintf("horizontal_%u", $index);
227) 						array_push($row[$field], $data);
228) 					}
229) 				}
230) 				return $row;
231) 			},
232) 			$groups
233) 		);
234) 		return (new class_table($columns_result, $rows_result));
235) 	}
236) 	
fenris foo

fenris authored 8 years ago

237) 	public function generate()
238) 	{
239)  ?>
bfadmin-master advanced

bfadmin-master authored 8 years ago

240) <table class="datatable">