589dd2e979e79ec2302f86092e271b98e5b61590
fenris foo

fenris authored 7 years ago

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

Christian Fraß authored 7 years ago

2) include_once("misc.php");
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

3) include_once("list.php");
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

4) 
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

5) /**
6)  */
fenris foo

fenris authored 7 years ago

7) class class_column
8) 	{
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

9) 		
10) 		/**
11) 		 */
12) 		public $title;
13) 		
14) 		
15) 		/**
16) 		 */
17) 		public $field;
fenris foo

fenris authored 7 years ago

18) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

19) 		
20) 		/**
21) 		 */
22) 		public $format_;
fenris foo

fenris authored 7 years ago

23) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

24) 		
25) 		/**
26) 		 */
27) 		public function __construct($title, $field, $format_ = null)
28) 			{
29) 				if ($format_ == null) $format_ = function ($x) {return $x;};
30) 				$this->title = $title;
31) 				$this->field = $field;
32) 				$this->format_ = $format_;
33) 			}
34) 		
35) 		
36) 		/**
37) 		 */
38) 		public function extract($row)
39) 			{
40) 				// return call_user_func($this->extract_, $row);
41) 				return $row[$this->field];
42) 				// return $this->extract_($row);
43) 			}
44) 		
45) 		
46) 		/**
47) 		 */
48) 		public function format($value)
49) 			{
50) 				return call_user_func($this->format_, $value);
51) 				// return $this->format_($value);
52) 			}
53) 		
fenris foo

fenris authored 7 years ago

54) 	}
55) 
56) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

57) /**
58)  */
59) class class_table
fenris foo

fenris authored 7 years ago

60) 	{
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

61) 		
62) 		/**
63) 		 */
64) 		private $columns;
65) 		
66) 		
67) 		/**
68) 		 */
69) 		private $rows;
70) 		
71) 		
72) 		/**
73) 		 */
74) 		public function __construct($columns, $rows = [])
75) 			{
76) 				$this->columns = $columns;
77) 				$this->rows = [];
78) 				$this->fill($rows);
79) 			}
80) 		
81) 		
82) 		/**
83) 		 */
84) 		public function columns_get()
85) 			{
86) 				return $this->columns;
87) 			}
88) 		
89) 		
90) 		/**
91) 		 */
92) 		private function add($row)
93) 			{
94) 				array_push($this->rows, $row);
95) 			}
96) 		
97) 		
98) 		/**
99) 		 */
100) 		private function fill($rows)
101) 			{
102) 				array_map
103) 					(
104) 						function ($row) {$this->add($row);},
105) 						$rows
106) 					)
107) 				;
108) 			}
109) 		
110) 		/*
111) 		+------+------+------+
112) 		|  xA  |  xB  |  xC  |
113) 		+------+------+------+
114) 		|  a2  |  b1  |  c3  |
115) 		+------+------+------+
116) 		|  a1  |  b1  |  c0  |
117) 		+------+------+------+
118) 		|  a1  |  b3  |  c2  |
119) 		+------+------+------+
120) 		|  a2  |  b2  |  c4  |
121) 		+------+------+------+
122) 		|  a1  |  b2  |  c1  |
123) 		+------+------+------+
124) 		|  a2  |  b4  |  c5  |
125) 		+------+------+------+
Christian Fraß minor changes; move to kora...

Christian Fraß authored 7 years ago

126) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

127) 		+------+------+------+
128) 		|  xA  |  xB  |  xC  |
129) 		+------+------+------+
130) 		|  a1  |  b1  |  c0  |
131) 		+------+------+------+
132) 		|  a1  |  b2  |  c1  |
133) 		+------+------+------+
134) 		|  a1  |  b3  |  c2  |
135) 		+------+------+------+
136) 		|  a2  |  b1  |  c3  |
137) 		+------+------+------+
138) 		|  a2  |  b2  |  c4  |
139) 		+------+------+------+
140) 		|  a2  |  b4  |  c5  |
141) 		+------+------+------+
Christian Fraß minor changes; move to kora...

Christian Fraß authored 7 years ago

142) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

143) 		+------+------+------+
144) 		|  xA  |  xB  |  xC  |
145) 		+------+------+------+
146) 		|  a1  |  b1  |  c0  |
147) 		|      +------+------+
148) 		|      |  b2  |  c1  |
149) 		|      +------+------+
150) 		|      |  b3  |  c2  |
151) 		+------+------+------+
152) 		|  a2  |  b1  |  c3  |
153) 		|      +------+------+
154) 		|      |  b2  |  c4  |
155) 		|      +------+------+
156) 		|      |  b4  |  c5  |
157) 		+------+------+------+
Christian Fraß minor changes; move to kora...

Christian Fraß authored 7 years ago

158) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

159) 		+------+------+------+------+------+
160) 		|  xA  | xB:b1| xB:b2| xB:b3| xB:b4|
161) 		+------+------+------+------+------+
162) 		|  a1  |  c0  |  c1  |  c2  |  --  |
163) 		+------+------+------+------+------+
164) 		|  a2  |  c3  |  c4  |  --  |  c5  |
165) 		+------+------+------+------+------+
166) 		 */
167) 		public function snap($configuration)
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

168) 			{
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

169) 				$columns_vertical = fetch($configuration, "columns_vertical", null, 2);
170) 				$columns_horizontal = fetch($configuration, "columns_horizontal", null, 2);
171) 				$columns_data = fetch($configuration, "columns_data", null, 2);
172) 				$data_aggregator = fetch($configuration, "data_aggregator", function ($values) {return /*json_encode(*/$values/*)*/;}, 1);
173) 				
174) 				$columns_source =
175) 					[
176) 						new class_column("Vertical", "_vertical"),
177) 						new class_column("Horizontal", "_horizontal"),
178) 						new class_column("Data", "_data", function ($x) {return json_encode($x);}),
179) 					]
180) 				;
181) 				// gather data for the three columns
182) 				$rows_source = array_map
183) 					(
184) 						function ($row) use (&$columns_vertical, &$columns_horizontal, &$columns_data, $data_aggregator)
185) 							{
186) 								$raw_vertical = array_map(function ($column) use (&$row) {return $column->extract($row);}, $columns_vertical);
187) 								$raw_horizontal = array_map(function ($column) use (&$row) {return $column->extract($row);}, $columns_horizontal);
188) 								$raw_data = []; foreach ($columns_data as $column) $raw_data[$column->field] = $column->extract($row);
189) 								$row_ = [];
190) 								$row_["_vertical"] = implode("/", $raw_vertical);
191) 								$row_["_horizontal"] = implode("/", $raw_horizontal);
192) 								$row_["_data"] = $data_aggregator($raw_data);
193) 								return $row_;
194) 							}
195) 						,
196) 						$this->rows
197) 					)
198) 				;
199) 				// return (new class_table($columns_source, $rows_source));
200) 				// find groups
201) 				$values = list_clean
202) 					(
203) 						array_map
204) 							(
205) 								function ($row) use (&$columns_source) {return $columns_source[1]->extract($row);},
206) 								$rows_source
207) 							)
208) 					)
209) 				;
210) 				$count = 0;
211) 				$columns_result = array_merge
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

212) 					(
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

213) 						[
214) 							new class_column
215) 								(
216) 									fetch
217) 										(
218) 											$configuration,
219) 											"label_vertical",
220) 											function ($columns) {return implode("/", array_map(function ($column) {return $column->title;}, $columns));},
221) 											1
222) 										)
223) 										($columns_vertical)
224) 									,
225) 									"vertical"
226) 								)
227) 						],
228) 						array_map
229) 							(
230) 								function ($value) use (&$configuration, &$columns_horizontal, &$count)
231) 									{
232) 										$count += 1;
233) 										return (
234) 											new class_column
235) 												(
236) 													fetch
237) 														(
238) 															$configuration,
239) 															"label_horizontal",
240) 															function ($columns, $value) {return implode("/", array_map(function ($column) {return $column->title;}, $columns)) . ":" . $value;},
241) 															1
242) 														)
243) 														($columns_horizontal, $value)
244) 													,
245) 													sprintf("horizontal_%u", $count-1),
246) 													fetch($configuration, "data_formatter", function ($x) {return json_encode($x);}, 1)
247) 												)
248) 										);
249) 									}
250) 								,
251) 								$values
252) 							)
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

253) 					)
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

254) 				;
255) 				$rows_result = array_map
256) 					(
257) 						function ($group) use (&$columns_vertical, &$columns_horizontal, &$columns_data, &$columns_source, &$columns_result, &$values)
258) 							{
259) 								$row = [];
260) 								{
261) 									$row["vertical"] = $group["value"];
262) 								}
263) 								for ($index = 0; $index < count($columns_result); ++$index)
264) 									{
265) 										$row[sprintf("horizontal_%u", $index)] = [];
266) 									}
267) 								foreach ($group["members"] as $member)
268) 									{
269) 										$value = $columns_source[1]->extract($member);
270) 										$data = $columns_source[2]->extract($member);
271) 										$index = array_search($value, $values);
272) 										if ($index === false)
273) 											{
274) 												throw ("fatal error");
275) 											}
276) 										else
277) 											{
278) 												$field = sprintf("horizontal_%u", $index);
279) 												array_push($row[$field], $data);
280) 											}
281) 									}
282) 								return $row;
283) 							}
284) 						,
285) 						sql_groups($rows_source, "_vertical")
286) 					)
287) 				;
288) 				return (new class_table($columns_result, $rows_result));
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

289) 			}
290) 	
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

291) 	
292) 		/**
293) 		 */
294) 		public function generate()
295) 			{
fenris foo

fenris authored 7 years ago

296)  ?>
bfadmin-master advanced

bfadmin-master authored 7 years ago

297) <table class="datatable">
fenris foo

fenris authored 7 years ago

298) 	<thead>
299) 		<tr>
300) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

301) 			array_map
302) 				(
303) 					function ($column)
304) 						{
fenris foo

fenris authored 7 years ago

305)  ?>
306)  			<th>
307) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

308) 							echo($column->title);
fenris foo

fenris authored 7 years ago

309)  ?>
310) 			</th>
311) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

312) 						}
313) 					,
314) 					$this->columns
315) 				)
316) 			;
fenris foo

fenris authored 7 years ago

317)  ?>
318) 		</tr>
319) 	</thead>
320) 	<tbody>
321) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

322) 			array_map
323) 				(
324) 					function ($row)
325) 					{
fenris foo

fenris authored 7 years ago

326)  ?>
327) 		<tr>
328) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

329) 						array_map
330) 							(
331) 								function ($column) use (&$row)
332) 									{
fenris foo

fenris authored 7 years ago

333)  ?>
334) 		 			<td>
335) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

336) 										echo($column->format($column->extract($row)));
fenris foo

fenris authored 7 years ago

337)  ?>
338) 		 			</td>
339) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

340) 									}
341) 								,
342) 								$this->columns
343) 							)
344) 						;
fenris foo

fenris authored 7 years ago

345)  ?>
346) 		</tr>
347) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

348) 						}
349) 					,
350) 					$this->rows
351) 				)
352) 			;
fenris foo

fenris authored 7 years ago

353)  ?>
354) 	</tbody>
355) </table>
356) <?php
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

357) 			}
fenris foo

fenris authored 7 years ago

358) 	}
Christian Fraß improved snap-function

Christian Fraß authored 6 years ago

359)