1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jovis\HTMLTable; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A controller for creating a html table from a movie model. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class TableController implements \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use \Anax\DI\TInjectable; |
12
|
|
|
|
13
|
|
|
private $model; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Lists all instances of the model, (databasetable) except for noListing. |
17
|
|
|
* |
18
|
|
|
* @param $noListing params not to be listed |
19
|
|
|
* @param $model model object to list (create an html table from) |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function listAction($noListing = NULL, $model) |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
$this->model = $model; |
27
|
|
|
$this->model->setDI($this->di); |
28
|
|
|
|
29
|
|
|
$all = $this->model->findAll(); |
30
|
|
|
|
31
|
|
|
$aContent = array(); |
32
|
|
|
|
33
|
|
|
//gör om arrayen av objekt till en array av arrayer |
34
|
|
View Code Duplication |
foreach ($all as $key1=>$value) { |
|
|
|
|
35
|
|
|
foreach ($value as $key2=>$v){ |
36
|
|
|
if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
37
|
|
|
$aContent[$key1][$key2] = $v; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
$aHeading = []; |
44
|
|
|
//hittar objektens parametrar/tabellens kolumnnamn och skapar |
45
|
|
|
//en ny array av rubriker |
46
|
|
View Code Duplication |
foreach ($all as $key1=>$value) { |
|
|
|
|
47
|
|
|
foreach ($value as $key2=>$v){ |
48
|
|
|
if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
49
|
|
|
$aHeading[] = $key2; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
break; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$chtml = new \Jovis\HTMLTable\CHTMLTable($aHeading, $aContent); |
56
|
|
|
|
57
|
|
|
$htmltable = $chtml->getTable(); |
58
|
|
|
$source = $this->model->getSource(); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->theme->setTitle("Visa alla användare"); |
|
|
|
|
62
|
|
|
$this->views->add('table/list-all', [ |
|
|
|
|
63
|
|
|
'title' => $source, |
64
|
|
|
'htmltable' => $htmltable, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function initAction($model){ //används för att befolka databasen, bara för att testa |
69
|
|
|
$model->setDI($this->di); |
70
|
|
|
$model->init(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.