1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests to generate specified HTML table from an array of objects and |
4
|
|
|
* specification arrays in an Anax MVC framework. |
5
|
|
|
* |
6
|
|
|
* Generates an HTML table fron an array of objects, where each object contains |
7
|
|
|
* a number of data, according to the specifications in the table- and the |
8
|
|
|
* column specification array. |
9
|
|
|
* |
10
|
|
|
* In the table specification it is possible to set the CSS id and/or class for |
11
|
|
|
* the table and the table caption. |
12
|
|
|
* |
13
|
|
|
* In the column specification the arrays order corresponds to the columns order |
14
|
|
|
* in the table. The name of the key in the column specifiation should |
15
|
|
|
* correspond to the key in the object, where the data should be fetched from. |
16
|
|
|
* |
17
|
|
|
* It is possible to specify a title of the column in the column specification. |
18
|
|
|
* If no title is specified, the key name in the object of the first row is used. |
19
|
|
|
* The same name as the key in the column specification. |
20
|
|
|
* |
21
|
|
|
* It is possible to define a function for the column. The function could be |
22
|
|
|
* used to use an algorithm for the table data in the object or add a string |
23
|
|
|
* of HTML tags for the column. It is also possible to fetch the whole object |
24
|
|
|
* by naming the key in the column specifiation so it starts with object... |
25
|
|
|
* This makes it possible to use all the data in the object if it is wanted. |
26
|
|
|
* |
27
|
|
|
* The type could be set to footer and colspan could also be used, if wanted. |
28
|
|
|
* |
29
|
|
|
* @author Gunnar Eriksson <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
// Get environment & autoloader. |
33
|
|
|
require __DIR__.'/config.php'; |
34
|
|
|
|
35
|
|
|
// Create services and inject into the app. |
36
|
|
|
$di = new \Anax\DI\CDIFactoryDefault(); |
37
|
|
|
|
38
|
|
|
$app = new \Anax\Kernel\CAnax($di); |
39
|
|
|
|
40
|
|
|
// Home route |
41
|
|
|
$app->router->add('', function() use ($app) { |
42
|
|
|
$app->theme->addStylesheet('css/html-table.css'); |
43
|
|
|
$app->theme->setTitle("Using CHTMLTable in ANAX-MVC"); |
44
|
|
|
|
45
|
|
|
// Create data objects. |
46
|
|
|
$row0 = new stdClass(); |
47
|
|
|
$row0->column1 = "Table Cell 1"; |
48
|
|
|
$row0->column2 = "Table Cell 2"; |
49
|
|
|
$row0->column3 = "Table Cell 3"; |
50
|
|
|
$row0->column4 = "https://www.google.se"; |
51
|
|
|
$row0->column5 = "Table Cell 5"; |
52
|
|
|
$row0->column6 = "Table Cell 6"; |
53
|
|
|
|
54
|
|
|
$row1 = new stdClass(); |
55
|
|
|
$row1->column1 = "Table Cell 7"; |
56
|
|
|
$row1->column2 = "Table Cell 8"; |
57
|
|
|
$row1->column3 = "Table Cell 9"; |
58
|
|
|
$row1->column4 = "https://www.google.se"; |
59
|
|
|
$row1->column5 = "Table Cell 11"; |
60
|
|
|
$row1->column6 = ""; |
61
|
|
|
|
62
|
|
|
$row2 = new stdClass(); |
63
|
|
|
$row2->column1 = "Table Cell 13"; |
64
|
|
|
$row2->column2 = "Table Cell 14"; |
65
|
|
|
$row2->column3 = "Table Cell 15"; |
66
|
|
|
$row2->column4 = "https://www.google.se"; |
67
|
|
|
$row2->column5 = "Table Cell 17"; |
68
|
|
|
$row2->column6 = "Table Cell 18"; |
69
|
|
|
|
70
|
|
|
// Create table data, which is an array of data objects. |
71
|
|
|
$data = [ |
72
|
|
|
0 => $row0, |
73
|
|
|
1 => $row1, |
74
|
|
|
2 => $row2, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
// Create table specifiation. |
78
|
|
|
$tableSpecification = [ |
79
|
|
|
//'id' => 'test-table', |
|
|
|
|
80
|
|
|
//'class' => 'test-table', |
|
|
|
|
81
|
|
|
'caption' => 'The table' |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
$table = new \Guer\HTMLTable\CHTMLTable(); |
85
|
|
|
|
86
|
|
|
// Create table with table specification, table data and the column |
87
|
|
|
// specification. |
88
|
|
|
$table = $table->create($tableSpecification, $data, [ |
89
|
|
|
'column1' => [ |
90
|
|
|
'title' => 'Table Header 1', |
91
|
|
|
], |
92
|
|
|
'object1' => [ |
93
|
|
|
'title' => 'Table Header 2', |
94
|
|
|
'function' => function($object) { |
95
|
|
|
return htmlentities($object->column2, null, 'UTF-8'); |
96
|
|
|
} |
97
|
|
|
], |
98
|
|
|
'column4' => [ |
99
|
|
|
'title' => 'Table Header 4', |
100
|
|
View Code Duplication |
'function' => function($link) { |
|
|
|
|
101
|
|
|
return '<a href="'. htmlentities($link, null, 'UTF-8') . '">' . "Google" . '</a>'; |
102
|
|
|
} |
103
|
|
|
], |
104
|
|
|
'column6' => [ |
105
|
|
|
'title' => 'Table Header 6', |
106
|
|
|
'function' => function($isPresent) { |
107
|
|
|
return empty($isPresent) ? 'Not present' : 'Present'; |
108
|
|
|
} |
109
|
|
|
], |
110
|
|
|
'tablefoot1' => [ |
111
|
|
|
'type' => 'footer', |
112
|
|
|
'colspan' => '2', |
113
|
|
|
'value' => 'Footer Cell 1', |
114
|
|
|
], |
115
|
|
|
'tablefoot2' => [ |
116
|
|
|
'type' => 'footer', |
117
|
|
|
'colspan' => 2, |
118
|
|
|
'function' => function() { |
119
|
|
|
return '<a href="https://www.google.se">Link</a>'; |
120
|
|
|
} |
121
|
|
|
], |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
$app->views->add('default/page', [ |
126
|
|
|
'title' => "Example on using specified HTML table with CHTMLTable", |
127
|
|
|
'content' => $table->getHTMLTable(), |
128
|
|
|
]); |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
// Check for matching routes and dispatch to controller/handler of route |
133
|
|
|
$app->router->handle(); |
134
|
|
|
|
135
|
|
|
// Render the page |
136
|
|
|
$app->theme->render(); |
137
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.