Browse code

improved snap-function

Christian Fraß authored on 25/07/2017 08:22:29
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -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,296 +1,361 @@
1 1
 <?php
2 2
 include_once("misc.php");
3
+include_once("list.php");
3 4
 
5
+/**
6
+ */
4 7
 class class_column
5
-{
6
-	public $title;
7
-	
8
-	public $field;
9
-	
10
-	public $format_;
11
-	
12
-	public function __construct($title, $field, $format_ = null)
13 8
 	{
14
-		if ($format_ == null) $format_ = function ($x) {return $x;};
15
-		$this->title = $title;
16
-		$this->field = $field;
17
-		$this->format_ = $format_;
18
-	}
9
+		
10
+		/**
11
+		 */
12
+		public $title;
13
+		
14
+		
15
+		/**
16
+		 */
17
+		public $field;
19 18
 	
20
-	public function extract($row)
21
-	{
22
-		// return call_user_func($this->extract_, $row);
23
-		return $row[$this->field];
24
-		// return $this->extract_($row);
25
-	}
19
+		
20
+		/**
21
+		 */
22
+		public $format_;
26 23
 	
27
-	public function format($value)
28
-	{
29
-		return call_user_func($this->format_, $value);
30
-		// return $this->format_($value);
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
+		
31 54
 	}
32
-}
33 55
 
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
-	
47
-	public function columns_get()
48
-	{
49
-		return $this->columns;
50
-	}
51
-	
52
-	private function add($row)
53
-	{
54
-		array_push($this->rows, $row);
55
-	}
56 56
 	
57
-	private function fill($rows)
57
+/**
58
+ */
59
+class class_table
58 60
 	{
59
-		array_map
60
-		(
61
-			function ($row) {$this->add($row);},
62
-			$rows
63
-		);
64
-	}
65
-	
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
-	+------+------+------+
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
+		+------+------+------+
82 126
 	
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
-	+------+------+------+
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
+		+------+------+------+
98 142
 	
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
-	+------+------+------+
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
+		+------+------+------+
114 158
 	
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
-	 */
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
-		$data_aggregator = fetch($configuration, "data_aggregator", function ($values) {return /*json_encode(*/$values/*)*/;}, 1);
129
-		
130
-		$columns_source =
131
-		[
132
-			new class_column("Vertical", "_vertical"),
133
-			new class_column("Horizontal", "_horizontal"),
134
-			new class_column("Data", "_data", function ($x) {return json_encode($x);}),
135
-		];
136
-		// gather data for the three columns
137
-		$rows_source = null;
138
-		{
139
-			$rows_source = array_map
140
-			(
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
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
-		}
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)
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)
181 168
 			{
182
-				array_push
183
-				(
184
-					$columns_result,
185
-					new class_column
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
186 212
 					(
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)
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
+							)
196 253
 					)
197
-				);
198
-				array_push($values, $value);
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));
199 289
 			}
200
-		}
201
-		$groups = sql_groups($rows_source, "_vertical");
202
-		$rows_result = array_map
203
-		(
204
-			function ($group) use (&$columns_vertical,&$columns_horizontal,&$columns_data,&$columns_source,&$columns_result,&$values)
205
-			{
206
-				$row = [];
207
-				{
208
-					$row["vertical"] = $group["value"];
209
-				}
210
-				for ($index = 0; $index < count($columns_result); ++$index)
211
-				{
212
-					$row[sprintf("horizontal_%u", $index)] = [];
213
-				}
214
-				foreach ($group["members"] as $member)
215
-				{
216
-					$value = $columns_source[1]->extract($member);
217
-					$data = $columns_source[2]->extract($member);
218
-					$index = array_search($value, $values);
219
-					if ($index === false)
220
-					{
221
-						throw ("fatal error");
222
-					}
223
-					else
224
-					{
225
-						$field = sprintf("horizontal_%u", $index);
226
-						array_push($row[$field], $data);
227
-					}
228
-				}
229
-				return $row;
230
-			},
231
-			$groups
232
-		);
233
-		return (new class_table($columns_result, $rows_result));
234
-	}
235 290
 	
236
-	public function generate()
237
-	{
291
+	
292
+		/**
293
+		 */
294
+		public function generate()
295
+			{
238 296
  ?>
239 297
 <table class="datatable">
240 298
 	<thead>
241 299
 		<tr>
242 300
 <?php
243
-	array_map
244
-	(
245
-		function ($column)
246
-		{
301
+			array_map
302
+				(
303
+					function ($column)
304
+						{
247 305
  ?>
248 306
  			<th>
249 307
 <?php
250
-			echo($column->title);
308
+							echo($column->title);
251 309
  ?>
252 310
 			</th>
253 311
 <?php
254
-		},
255
-		$this->columns
256
-	);
312
+						}
313
+					,
314
+					$this->columns
315
+				)
316
+			;
257 317
  ?>
258 318
 		</tr>
259 319
 	</thead>
260 320
 	<tbody>
261 321
 <?php
262
-	array_map
263
-	(
264
-		function ($row)
265
-		{
322
+			array_map
323
+				(
324
+					function ($row)
325
+					{
266 326
  ?>
267 327
 		<tr>
268 328
 <?php
269
-			array_map
270
-			(
271
-				function ($column) use (&$row)
272
-				{
329
+						array_map
330
+							(
331
+								function ($column) use (&$row)
332
+									{
273 333
  ?>
274 334
 		 			<td>
275 335
 <?php
276
-					echo($column->format($column->extract($row)));
336
+										echo($column->format($column->extract($row)));
277 337
  ?>
278 338
 		 			</td>
279 339
 <?php
280
-				},
281
-				$this->columns
282
-			);
340
+									}
341
+								,
342
+								$this->columns
343
+							)
344
+						;
283 345
  ?>
284 346
 		</tr>
285 347
 <?php
286
-		},
287
-		$this->rows
288
-	);
348
+						}
349
+					,
350
+					$this->rows
351
+				)
352
+			;
289 353
  ?>
290 354
 	</tbody>
291 355
 </table>
292 356
 <?php
357
+			}
293 358
 	}
294
-}
359
+	
295 360
  ?>
296 361