improved snap-function
Christian Fraß

Christian Fraß commited on 2017-07-25 08:22:29
Zeige 2 geänderte Dateien mit 156 Einfügungen und 40 Löschungen.

... ...
@@ -0,0 +1,52 @@
1
+<?php
2
+
3
+/**
4
+ */
5
+function list_group($list, $collation = null)
6
+	{
7
+		if ($collation == null)
8
+			{
9
+				$collation = (function ($x, $y) {return ($x == $y);});
10
+			}
11
+		$groups = [];
12
+		foreach ($list as $element)
13
+			{
14
+				$found = false;
15
+				$group = null;
16
+				foreach ($groups as $group_)
17
+					{
18
+						if ($collation($group_[0], $element))
19
+							{
20
+								$group = $group_;
21
+								$found = true;
22
+								break;
23
+							}
24
+					}
25
+				if (! $found)
26
+					{
27
+						$group = [];
28
+					}
29
+				array_push($group, $element);
30
+				if (! $found)
31
+					{
32
+						array_push($groups, $group);
33
+					}
34
+			}
35
+		return $groups;
36
+	}
37
+
38
+
39
+/**
40
+ */
41
+function list_clean($list, $collation = null)
42
+	{
43
+		return (
44
+			array_map
45
+				(
46
+					function ($group) {return $group[0];},
47
+					list_group($list, $collation)
48
+				)
49
+		);
50
+	}
51
+
52
+ ?>
... ...
@@ -1,14 +1,29 @@
1 1
 <?php
2 2
 include_once("misc.php");
3
+include_once("list.php");
3 4
 
5
+/**
6
+ */
4 7
 class class_column
5 8
 	{
9
+		
10
+		/**
11
+		 */
6 12
 		public $title;
7 13
 		
14
+		
15
+		/**
16
+		 */
8 17
 		public $field;
9 18
 	
19
+		
20
+		/**
21
+		 */
10 22
 		public $format_;
11 23
 	
24
+		
25
+		/**
26
+		 */
12 27
 		public function __construct($title, $field, $format_ = null)
13 28
 			{
14 29
 				if ($format_ == null) $format_ = function ($x) {return $x;};
... ...
@@ -17,6 +32,9 @@ class class_column
17 32
 				$this->format_ = $format_;
18 33
 			}
19 34
 		
35
+		
36
+		/**
37
+		 */
20 38
 		public function extract($row)
21 39
 			{
22 40
 				// return call_user_func($this->extract_, $row);
... ...
@@ -24,19 +42,35 @@ class class_column
24 42
 				// return $this->extract_($row);
25 43
 			}
26 44
 		
45
+		
46
+		/**
47
+		 */
27 48
 		public function format($value)
28 49
 			{
29 50
 				return call_user_func($this->format_, $value);
30 51
 				// return $this->format_($value);
31 52
 			}
53
+		
32 54
 	}
33 55
 
56
+	
57
+/**
58
+ */
34 59
 class class_table
35 60
 	{
61
+		
62
+		/**
63
+		 */
36 64
 		private $columns;
37 65
 		
66
+		
67
+		/**
68
+		 */
38 69
 		private $rows;
39 70
 		
71
+		
72
+		/**
73
+		 */
40 74
 		public function __construct($columns, $rows = [])
41 75
 			{
42 76
 				$this->columns = $columns;
... ...
@@ -44,23 +78,33 @@ class class_table
44 78
 				$this->fill($rows);
45 79
 			}
46 80
 		
81
+		
82
+		/**
83
+		 */
47 84
 		public function columns_get()
48 85
 			{
49 86
 				return $this->columns;
50 87
 			}
51 88
 		
89
+		
90
+		/**
91
+		 */
52 92
 		private function add($row)
53 93
 			{
54 94
 				array_push($this->rows, $row);
55 95
 			}
56 96
 		
97
+		
98
+		/**
99
+		 */
57 100
 		private function fill($rows)
58 101
 			{
59 102
 				array_map
60 103
 					(
61 104
 						function ($row) {$this->add($row);},
62 105
 						$rows
63
-		);
106
+					)
107
+				;
64 108
 			}
65 109
 		
66 110
 		/*
... ...
@@ -132,33 +176,41 @@ class class_table
132 176
 						new class_column("Vertical", "_vertical"),
133 177
 						new class_column("Horizontal", "_horizontal"),
134 178
 						new class_column("Data", "_data", function ($x) {return json_encode($x);}),
135
-		];
179
+					]
180
+				;
136 181
 				// gather data for the three columns
137
-		$rows_source = null;
138
-		{
139 182
 				$rows_source = array_map
140 183
 					(
141 184
 						function ($row) use (&$columns_vertical, &$columns_horizontal, &$columns_data, $data_aggregator)
142 185
 							{
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));
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);
145 188
 								$raw_data = []; foreach ($columns_data as $column) $raw_data[$column->field] = $column->extract($row);
146
-					
147 189
 								$row_ = [];
148 190
 								$row_["_vertical"] = implode("/", $raw_vertical);
149 191
 								$row_["_horizontal"] = implode("/", $raw_horizontal);
150 192
 								$row_["_data"] = $data_aggregator($raw_data);
151 193
 								return $row_;
152
-				},
153
-				$this->rows
154
-			);
155 194
 							}
195
+						,
196
+						$this->rows
197
+					)
198
+				;
156 199
 				// return (new class_table($columns_source, $rows_source));
157
-		$columns_result = [];
158
-		{
159
-			array_push
200
+				// find groups
201
+				$values = list_clean
160 202
 					(
161
-				$columns_result,
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
212
+					(
213
+						[
162 214
 							new class_column
163 215
 								(
164 216
 									fetch
... ...
@@ -167,21 +219,18 @@ class class_table
167 219
 											"label_vertical",
168 220
 											function ($columns) {return implode("/", array_map(function ($column) {return $column->title;}, $columns));},
169 221
 											1
170
-					)($columns_vertical),
222
+										)
223
+										($columns_vertical)
224
+									,
171 225
 									"vertical"
172 226
 								)
173
-			);
174
-		}
175
-		// find groups
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
227
+						],
228
+						array_map
183 229
 							(
184
-					$columns_result,
230
+								function ($value) use (&$configuration, &$columns_horizontal, &$count)
231
+									{
232
+										$count += 1;
233
+										return (
185 234
 											new class_column
186 235
 												(
187 236
 													fetch
... ...
@@ -190,15 +239,19 @@ class class_table
190 239
 															"label_horizontal",
191 240
 															function ($columns, $value) {return implode("/", array_map(function ($column) {return $column->title;}, $columns)) . ":" . $value;},
192 241
 															1
193
-						)($columns_horizontal, $value),
194
-						sprintf("horizontal_%u", count($columns_result)-1),
242
+														)
243
+														($columns_horizontal, $value)
244
+													,
245
+													sprintf("horizontal_%u", $count-1),
195 246
 													fetch($configuration, "data_formatter", function ($x) {return json_encode($x);}, 1)
196 247
 												)
197 248
 										);
198
-				array_push($values, $value);
199
-			}
200 249
 									}
201
-		$groups = sql_groups($rows_source, "_vertical");
250
+								,
251
+								$values
252
+							)
253
+					)
254
+				;
202 255
 				$rows_result = array_map
203 256
 					(
204 257
 						function ($group) use (&$columns_vertical, &$columns_horizontal, &$columns_data, &$columns_source, &$columns_result, &$values)
... ...
@@ -227,12 +280,17 @@ class class_table
227 280
 											}
228 281
 									}
229 282
 								return $row;
230
-			},
231
-			$groups
232
-		);
283
+							}
284
+						,
285
+						sql_groups($rows_source, "_vertical")
286
+					)
287
+				;
233 288
 				return (new class_table($columns_result, $rows_result));
234 289
 			}
235 290
 	
291
+	
292
+		/**
293
+		 */
236 294
 		public function generate()
237 295
 			{
238 296
  ?>
... ...
@@ -251,9 +309,11 @@ class class_table
251 309
  ?>
252 310
 			</th>
253 311
 <?php
254
-		},
312
+						}
313
+					,
255 314
 					$this->columns
256
-	);
315
+				)
316
+			;
257 317
  ?>
258 318
 		</tr>
259 319
 	</thead>
... ...
@@ -277,15 +337,19 @@ class class_table
277 337
  ?>
278 338
 		 			</td>
279 339
 <?php
280
-				},
340
+									}
341
+								,
281 342
 								$this->columns
282
-			);
343
+							)
344
+						;
283 345
  ?>
284 346
 		</tr>
285 347
 <?php
286
-		},
348
+						}
349
+					,
287 350
 					$this->rows
288
-	);
351
+				)
352
+			;
289 353
  ?>
290 354
 	</tbody>
291 355
 </table>
292 356