adddf67eec76f436fa75c82b648ce73a414d2a5c
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ß better table-snap

Christian Fraß authored 7 years ago

66) 	public function snap($configuration)
67) 	{
68) 		$columns_vertical = fetch($configuration, "columns_vertical", null, 2);
69) 		$columns_horizontal = fetch($configuration, "columns_horizontal", null, 2);
70) 		$columns_data = fetch($configuration, "columns_data", null, 2);
71) 		
72) 		$columns_source =
73) 		[
74) 			new class_column("Vertical", "vertical"),
75) 			new class_column("Horizontal", "horizontal"),
76) 			new class_column("Data", "data"),
77) 		];
78) 		$rows_source = null;
79) 		{
80) 			$rows_source = $this->rows;
81) 			$rows_source = sql_condense
82) 			(
83) 				$rows_source,
84) 				array_map(function ($column) {return $column->field;}, $columns_vertical),
85) 				["vertical"],
86) 				["vertical" => function ($values) use (&$columns_vertical) {return implode("/", array_map(function ($column) use (&$values) {return $values[$column->field];}, $columns_vertical));}]
87) 			);
88) 			$rows_source = sql_condense
89) 			(
90) 				$rows_source,
91) 				array_map(function ($column) {return $column->field;}, $columns_horizontal),
92) 				["horizontal"],
93) 				["horizontal" => function ($values) use (&$columns_horizontal) {return implode("/", array_map(function ($column) use (&$values) {return $values[$column->field];}, $columns_horizontal));}]
94) 			);
95) 			$rows_source = sql_condense
96) 			(
97) 				$rows_source,
98) 				array_map(function ($column) {return $column->field;}, $columns_data),
99) 				["data"],
100) 				["data" => function ($values) use (&$configuration) {return fetch($configuration, "data_aggregator", function ($values) {return /*json_encode(*/$values/*)*/;}, 1)($values);}]
101) 			);
102) 		}
103) 		// return (new class_table($columns_source, $rows_source));
104) 		$columns_result = [];
105) 		{
106) 			array_push
107) 			(
108) 				$columns_result,
109) 				new class_column
110) 				(
111) 					fetch
112) 					(
113) 						$configuration,
114) 						"label_vertical",
115) 						function ($columns) {return implode("/", array_map(function ($column) {return $column->title;}, $columns));},
116) 						1
117) 					)($columns_vertical),
118) 					"vertical"
119) 				)
120) 			);
121) 		}
122) 		$values = [];
123) 		foreach ($rows_source as $row)
124) 		{
125) 			$value = $columns_source[1]->extract($row);
126) 			if (array_search($value, $values) === false)
127) 			{
128) 				array_push
129) 				(
130) 					$columns_result,
131) 					new class_column
132) 					(
133) 						fetch
134) 						(
135) 							$configuration,
136) 							"label_horizontal",
137) 							function ($columns, $value) {return implode("/", array_map(function ($column) {return $column->title;}, $columns)) . ":" . $value;},
138) 							1
139) 						)($columns_horizontal, $value),
140) 						sprintf("horizontal_%u", count($columns_result)-1),
141) 						fetch($configuration, "data_formatter", function ($x) {return json_encode($x);}, 1)
142) 					)
143) 				);
144) 				array_push($values, $value);
145) 			}
146) 		}
147) 		$groups = sql_groups($rows_source, "vertical");
148) 		$rows_result = array_map
149) 		(
150) 			function ($group) use (&$columns_vertical,&$columns_horizontal,&$columns_data,&$columns_source,&$columns_result,&$values)
151) 			{
152) 				$row = [];
153) 				{
154) 					$row["vertical"] = $group["value"];
155) 				}
156) 				for ($index = 0; $index < count($columns_result); ++$index)
157) 				{
158) 					$row[sprintf("horizontal_%u", $index)] = [];
159) 				}
160) 				foreach ($group["members"] as $member)
161) 				{
162) 					$value = $columns_source[1]->extract($member);
163) 					$data = $columns_source[2]->extract($member);
164) 					$index = array_search($value, $values);
165) 					if ($index === false)
166) 					{
167) 						throw ("fatal error");
168) 					}
169) 					else
170) 					{
171) 						$field = sprintf("horizontal_%u", $index);
172) 						array_push($row[$field], $data);
173) 					}
174) 				}
175) 				return $row;
176) 			},
177) 			$groups
178) 		);
179) 		return (new class_table($columns_result, $rows_result));
180) 	}
181) 	
fenris foo

fenris authored 8 years ago

182) 	public function generate()
183) 	{
184)  ?>
bfadmin-master advanced

bfadmin-master authored 8 years ago

185) <table class="datatable">