2809be596dcea0f46a219e32172bce03e4e19677
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);
128) 		
129) 		$columns_source =
130) 		[
131) 			new class_column("Vertical", "vertical"),
132) 			new class_column("Horizontal", "horizontal"),
133) 			new class_column("Data", "data"),
134) 		];
135) 		$rows_source = null;
136) 		{
137) 			$rows_source = $this->rows;
138) 			$rows_source = sql_condense
139) 			(
140) 				$rows_source,
141) 				array_map(function ($column) {return $column->field;}, $columns_vertical),
142) 				["vertical"],
143) 				["vertical" => function ($values) use (&$columns_vertical) {return implode("/", array_map(function ($column) use (&$values) {return $values[$column->field];}, $columns_vertical));}]
144) 			);
145) 			$rows_source = sql_condense
146) 			(
147) 				$rows_source,
148) 				array_map(function ($column) {return $column->field;}, $columns_horizontal),
149) 				["horizontal"],
150) 				["horizontal" => function ($values) use (&$columns_horizontal) {return implode("/", array_map(function ($column) use (&$values) {return $values[$column->field];}, $columns_horizontal));}]
151) 			);
152) 			$rows_source = sql_condense
153) 			(
154) 				$rows_source,
155) 				array_map(function ($column) {return $column->field;}, $columns_data),
156) 				["data"],
157) 				["data" => function ($values) use (&$configuration) {return fetch($configuration, "data_aggregator", function ($values) {return /*json_encode(*/$values/*)*/;}, 1)($values);}]
158) 			);
159) 		}
160) 		// return (new class_table($columns_source, $rows_source));
161) 		$columns_result = [];
162) 		{
163) 			array_push
164) 			(
165) 				$columns_result,
166) 				new class_column
167) 				(
168) 					fetch
169) 					(
170) 						$configuration,
171) 						"label_vertical",
172) 						function ($columns) {return implode("/", array_map(function ($column) {return $column->title;}, $columns));},
173) 						1
174) 					)($columns_vertical),
175) 					"vertical"
176) 				)
177) 			);
178) 		}
179) 		$values = [];
180) 		foreach ($rows_source as $row)
181) 		{
182) 			$value = $columns_source[1]->extract($row);
183) 			if (array_search($value, $values) === false)
184) 			{
185) 				array_push
186) 				(
187) 					$columns_result,
188) 					new class_column
189) 					(
190) 						fetch
191) 						(
192) 							$configuration,
193) 							"label_horizontal",
194) 							function ($columns, $value) {return implode("/", array_map(function ($column) {return $column->title;}, $columns)) . ":" . $value;},
195) 							1
196) 						)($columns_horizontal, $value),
197) 						sprintf("horizontal_%u", count($columns_result)-1),
198) 						fetch($configuration, "data_formatter", function ($x) {return json_encode($x);}, 1)
199) 					)
200) 				);
201) 				array_push($values, $value);
202) 			}
203) 		}
204) 		$groups = sql_groups($rows_source, "vertical");
205) 		$rows_result = array_map
206) 		(
207) 			function ($group) use (&$columns_vertical,&$columns_horizontal,&$columns_data,&$columns_source,&$columns_result,&$values)
208) 			{
209) 				$row = [];
210) 				{
211) 					$row["vertical"] = $group["value"];
212) 				}
213) 				for ($index = 0; $index < count($columns_result); ++$index)
214) 				{
215) 					$row[sprintf("horizontal_%u", $index)] = [];
216) 				}
217) 				foreach ($group["members"] as $member)
218) 				{
219) 					$value = $columns_source[1]->extract($member);
220) 					$data = $columns_source[2]->extract($member);
221) 					$index = array_search($value, $values);
222) 					if ($index === false)
223) 					{
224) 						throw ("fatal error");
225) 					}
226) 					else
227) 					{
228) 						$field = sprintf("horizontal_%u", $index);
229) 						array_push($row[$field], $data);
230) 					}
231) 				}
232) 				return $row;
233) 			},
234) 			$groups
235) 		);
236) 		return (new class_table($columns_result, $rows_result));
237) 	}
238) 	
fenris foo

fenris authored 8 years ago

239) 	public function generate()
240) 	{
241)  ?>
bfadmin-master advanced

bfadmin-master authored 8 years ago

242) <table class="datatable">