index_gen.ctp
9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
* ***************************
* VUE LISTE GÉNÉRIQUE (index)
* ***************************
*/
$DEBUG=false;
//$DEBUG=true;
// Variables passées par le contrôleur
// pas bien, mais pratique
$controller = $controller;
$CAN_ADD = $CAN_ADD;
$role = $role;
$nb_entities = $nb_entities;
$entities = $entities;
$entity_plural = $entity_plural;
$fields_names = $fields_names;
$HAS_ADD_BUTTON = $HAS_ADD_BUTTON;
$HAS_EDIT_DEL_BUTTONS = $HAS_EDIT_DEL_BUTTONS;
$sortWhitelist = $sortWhitelist;
/*
// Ajout du champ "nom" automatique
foreach ($fields_names as $f=>$infos) $new_fields_names[$f] = $infos;
$fields_names = $new_fields_names;
// Si champ 'nom', ajout auto du nice name
debug($fields_names);
if (empty($fields_names['nom'])) $fields_names['nom'] = ['nice_name'=>'Nom'];
*/
//debug($fields_names);
//<div class="stats index large-9 medium-8 columns content">
?>
<div class="entities index">
<?php
echo "<h2><i class='icon-list'></i> Liste des $entity_plural ($nb_entities)</h2>";
if ($this->fetch('list_header')): ?>
<div class="list_header">
<?= $this->fetch('list_header') ?>
</div>
<?php
endif;
if ($HAS_ADD_BUTTON && $CAN_ADD) echo $this->element('button_add');
//<table cellpadding="0" cellspacing="0"
?>
<table style="border-collapse: separate; border-spacing: 0;width: 270px;">
<!-- EN-TETES -->
<thead>
<tr>
<!-- Boutons action -->
<th class="actions"><?= __('') ?></th>
<!-- Noms des champs -->
<?php
//foreach ($fields_names as $fname=>$fnicename) echo "<th>".$this->Paginator->sort($fname, $fnicename)."</th>";
//foreach ($fields_names as $fname=>$infos) {
foreach ($fields_names as $fname=>$infos) {
// Joli nom pour le titre de la colonne (nice name)
$nice_name = isset($infos['nice_name']) ? $infos['nice_name'] : $fname;
if ( in_array($fname, array_keys($sortWhitelist)) ) $fname = $sortWhitelist[$fname];
/*
$sort_name = isset($infos['sort_name']) ? $infos['sort_name'] : $fname;
echo "<th>".$this->Paginator->sort($sort_name, ucfirst($nice_name))."</th>";
*/
echo "<th>".$this->Paginator->sort($fname, ucfirst($nice_name))."</th>";
}
?>
</tr>
</thead>
<!-- DATA -->
<tbody>
<?php foreach ($entities as $e):
//debug($e);
?>
<tr>
<!--
Boutons edit / delete
-->
<td class="actions" style="padding: 6px 0; text-align: left;">
<?php
if ($HAS_EDIT_DEL_BUTTONS) {
//debug(compact('id','CAN_EDIT','CAN_DELETE'));
$id = $e->id;
//$CAN_EDIT = in_array($role, ['Administration', 'Super Administrateur']);
$CAN_EDIT = $controller->isAuthorizedAction('edit', $id);
$CAN_DELETE = $controller->isAuthorizedAction('delete', $id);
//$CAN_DELETE=true;
$style_num=1;
$WITH_DIV = false;
echo $this->element('buttons_edit_del', compact('id','CAN_EDIT','CAN_DELETE', 'style_num', 'WITH_DIV'));
}
?>
</td>
<!--
DATA : affichage de chaque champ dans une colonne
<td class="smallText"><= $this->Html->link($e->nom, ['action' => 'view', $e->id]) ?></td>
<td class="smallText"><= $e->has('sur_category') ? $this->Html->link($e->sur_category->nom, ['controller' => 'SurCategories', 'action' => 'view', $e->sur_category->id]) : '' ?></td>
-->
<?php
$entity = $e->toArray();
//debug($entity);
//foreach ($e->toArray() as $fname=>$fval) {
foreach ($fields_names as $fname=>$infos) {
// $fval est soit un champ physique soit un champ virtuel (calculé par le Model)
$fval = isset($entity[$fname]) ? $entity[$fname] : $e->connexDurAvg;
//debug($fname);
//if (! in_array($fname, array_keys($fields_names)) ) continue;
//debug($fname);
//<td class="smallText"><?= $this->Html->link($e->nom, ['action' => 'view', $e->id]) ></td>
echo "<td class='smallText'>";
// - S'il y a une fonction de transformation à appliquer, ben... on l'applique
//var_dump($fname);
if ( isset($fields_names[$fname]['f']) ) {
$f = $fields_names[$fname]['f'];
//debug($f); debug($fval); exit;
//var_dump("Y");
echo $this->MyHelper->$f($fval);
}
// - Si c'est le champ "nom" ou "id", on l'affiche comme un lien
elseif ($fname == 'nom' || $fname == 'id')
echo $this->Html->link($fval, ['action' => 'view', $e->id]);
// - Si le champ est boolean => on affiche oui/non
//elseif ( (substr($fname,0,3)=='is_') && ($fval===false || $fval===true) )
elseif (
( substr($fname,0,3)=='is_' || isset($infos['bool_values']) )
&&
( $fval==false || $fval==true )
) {
$bool_values = isset($infos['bool_values']) ? $infos['bool_values'] : ['Oui','Non'];
//echo $fval ? "Oui" : "Non";
echo $fval ? $bool_values[0] : $bool_values[1];
}
// - Si le champ est une FK => on récupère la valeur du 'contained'
//elseif ( strpos($fname,'_id') !== strlen($fname)-3 )
elseif ( substr($fname,-3) == '_id' ) {
//var_dump($fname);
//$fval = $e->has('sur_category') ? $e->sur_category->nom : $fval;
// ex: materiel
$contained_entity_name = $fields_names[$fname]['contained_entity_name'];
// ex: Materiels
$controller_name = $fields_names[$fname]['controller_name'];
//$linkval = $e->has($contained_entity_name) ? $e->$contained_entity_name->nom : $fval;
$name_field_label = $infos['name_field_label']; // 'nom' ou 'designation' ou ...
//$projet->has('chef_science_id') ? $this->Html->link($projet->chef_science->nom, ['controller' => 'Users', 'action' => 'view', $projet->chef_science_id]) : '' ></td>
//$linkval = $e->has($contained_entity_name) ? $e->$contained_entity_name->$name_field_label : $fval;
if ($e->has($contained_entity_name)) {
if ( isset($fields_names[$fname]['improved']) ) {
$fk_field = $fields_names[$fname]['improved'];
$linkval = $e->$fk_field;
}
else
$linkval = $e->$contained_entity_name->$name_field_label;
}
else $linkval = $fval;
//echo $this->Html->link($fval, ['controller' => $controller_name, 'action' => 'view', $e->sur_category->id]);
echo $this->Html->link($linkval, ['controller' => $controller_name, 'action' => 'view', $fval]);
/*
//debug($fname);
$parent_entity_infos = &$parent_entities_infos[$fname];
//debug($parent_entity_infos);
$fk_contained_name = $parent_entity_infos['fk_contained_name'];
//debug($fk_contained_name);
//$fvalue = $associated_entity_infos['name'];
$fvalue = $entity->$fk_contained_name->nom;
$this->MyHelper->displayElement(
__(ucfirst($parent_entity_infos['type_name'])),
//$category->has('sur_category') ? $this->Html->link($category->sur_category->nom, [
$this->Html->link($fvalue, [
'controller' => $parent_entity_infos['controller_name'],
//'controller' => 'SurCategories',
'action' => 'view',
//$associated_entity_infos['id']
$fval
//$category->sur_category->id
])
);
*/
} // FK
// - Sinon, champ quelconque => on l'affiche directement
else
echo $fval;
echo "</td>";
} // foreach entity fields
?>
</tr>
<?php
//break;
endforeach; // foreach entities ?>
</tbody>
</table>
<?php echo $this->element('pagination_with_first_and_last'); ?>
</div>
<!--
<div class="actions">
<php echo $this->element('menu') ?>
<php
echo $this->element('menu_index', [
'pluralHumanName' => 'Catégories',
'singularHumanName' => 'Catégorie'
])?>
</div>
-->