Blame view

src/Template/Common/index_gen.ctp 9.4 KB
f63eabb2   Etienne Pallier   Gestion plus simp...
1
2
<?php

087b29e7   Etienne Pallier   GROSSE REFACTORIS...
3
4
5
6
7
8
9
/*
 * ***************************
 * VUE LISTE GÉNÉRIQUE (index)
 * ***************************
 */


f63eabb2   Etienne Pallier   Gestion plus simp...
10
11
12
13
$DEBUG=false;
//$DEBUG=true;

// Variables passées par le contrôleur
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
14
15
16
17
18

// pas bien, mais pratique
$controller = $controller;

$CAN_ADD = $CAN_ADD;
f63eabb2   Etienne Pallier   Gestion plus simp...
19
$role = $role;
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
20
$nb_entities = $nb_entities;
f63eabb2   Etienne Pallier   Gestion plus simp...
21
22
23
$entities = $entities;
$entity_plural = $entity_plural;
$fields_names = $fields_names;
1b901ee9   Etienne Pallier   Refactorisation e...
24
25
$HAS_ADD_BUTTON = $HAS_ADD_BUTTON;
$HAS_EDIT_DEL_BUTTONS = $HAS_EDIT_DEL_BUTTONS;
3f9becf8   Etienne Pallier   Refactorisation :...
26
27
$sortWhitelist = $sortWhitelist;

f63eabb2   Etienne Pallier   Gestion plus simp...
28

1b901ee9   Etienne Pallier   Refactorisation e...
29
/*
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
30
// Ajout du champ "nom" automatique
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
31
32
foreach ($fields_names as $f=>$infos) $new_fields_names[$f] = $infos;
$fields_names = $new_fields_names;
1b901ee9   Etienne Pallier   Refactorisation e...
33
34
35
36
// Si champ 'nom', ajout auto du nice name
debug($fields_names);
if (empty($fields_names['nom'])) $fields_names['nom'] = ['nice_name'=>'Nom'];
*/
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
37
38
//debug($fields_names);

1b901ee9   Etienne Pallier   Refactorisation e...
39
//<div class="stats index large-9 medium-8 columns content">
f63eabb2   Etienne Pallier   Gestion plus simp...
40
41
?>

1b901ee9   Etienne Pallier   Refactorisation e...
42
43
44



f63eabb2   Etienne Pallier   Gestion plus simp...
45
46
<div class="entities index">

1b901ee9   Etienne Pallier   Refactorisation e...
47
48
49
50
51
52
53
54
55
56
57
	<?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');
f63eabb2   Etienne Pallier   Gestion plus simp...
58
	
1b901ee9   Etienne Pallier   Refactorisation e...
59
60
    //<table cellpadding="0" cellspacing="0" 
	?>
f63eabb2   Etienne Pallier   Gestion plus simp...
61
62
    <table style="border-collapse: separate; border-spacing: 0;width: 270px;">
		
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
63
		<!-- EN-TETES -->
f63eabb2   Etienne Pallier   Gestion plus simp...
64
65
		<thead>
			<tr>
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
66
				<!-- Boutons action -->
f63eabb2   Etienne Pallier   Gestion plus simp...
67
				<th class="actions"><?= __('') ?></th>
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
68
69

				<!--  Noms des champs -->
f63eabb2   Etienne Pallier   Gestion plus simp...
70
				<?php 
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
71
72
73
				//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) {
43c3d804   Etienne Pallier   Bugfix label (con...
74
			        // Joli nom pour le titre de la colonne (nice name)
1b901ee9   Etienne Pallier   Refactorisation e...
75
			        $nice_name = isset($infos['nice_name']) ? $infos['nice_name'] : $fname;
3f9becf8   Etienne Pallier   Refactorisation :...
76
			        if ( in_array($fname, array_keys($sortWhitelist)) ) $fname = $sortWhitelist[$fname];
06938694   Etienne Pallier   Vue liste projets...
77
78
79
80
81
			        /*
			        $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>";
3f9becf8   Etienne Pallier   Refactorisation :...
82
			    }
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
83
		        ?>
f63eabb2   Etienne Pallier   Gestion plus simp...
84
85
86
			</tr>
		</thead>
		
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
87
		<!--  DATA -->
f63eabb2   Etienne Pallier   Gestion plus simp...
88
		<tbody>
3f9becf8   Etienne Pallier   Refactorisation :...
89
90
91
            <?php foreach ($entities as $e): 
            //debug($e);
            ?>
f63eabb2   Etienne Pallier   Gestion plus simp...
92
93
94
            <tr>
            
            	<!-- 
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
95
96
            	Boutons edit / delete 
            	-->
f63eabb2   Etienne Pallier   Gestion plus simp...
97
				<td class="actions" style="padding: 6px 0; text-align: left;">   
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
98
                	<?php
1b901ee9   Etienne Pallier   Refactorisation e...
99
                	if ($HAS_EDIT_DEL_BUTTONS) {
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
100
101
102
103
104
105
106
107
108
                	//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'));
1b901ee9   Etienne Pallier   Refactorisation e...
109
                	}
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
110
               		?>
f63eabb2   Etienne Pallier   Gestion plus simp...
111
112
113
                </td>
				
				<!--
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
114
115
116
				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>
f63eabb2   Etienne Pallier   Gestion plus simp...
117
				 -->
f63eabb2   Etienne Pallier   Gestion plus simp...
118
				<?php
1b901ee9   Etienne Pallier   Refactorisation e...
119
120
121
122
123
124
125
126
127
				$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);
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
128
129
		            //<td class="smallText"><?= $this->Html->link($e->nom, ['action' => 'view', $e->id]) ></td>
		            echo "<td class='smallText'>";
404b35d8   Etienne Pallier   Refactorisations,...
130
		                
1b901ee9   Etienne Pallier   Refactorisation e...
131
132
133
134
135
136
137
138
		                // - 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);
		                }
404b35d8   Etienne Pallier   Refactorisations,...
139
		                
06938694   Etienne Pallier   Vue liste projets...
140
141
		                // - Si c'est le champ "nom" ou "id", on l'affiche comme un lien
		                elseif ($fname == 'nom' || $fname == 'id') 
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
142
		                    echo $this->Html->link($fval, ['action' => 'view', $e->id]);
404b35d8   Etienne Pallier   Refactorisations,...
143
		                
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
144
		                // - Si le champ est boolean => on affiche oui/non
06938694   Etienne Pallier   Vue liste projets...
145
146
147
148
149
150
151
152
153
154
	                    //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];
                        }
404b35d8   Etienne Pallier   Refactorisations,...
155
		                
06938694   Etienne Pallier   Vue liste projets...
156
157
158
159
160
		                // - 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);
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
161
    	                    //$fval = $e->has('sur_category') ? $e->sur_category->nom : $fval;
06938694   Etienne Pallier   Vue liste projets...
162
    	                    // ex: materiel
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
163
    	                    $contained_entity_name = $fields_names[$fname]['contained_entity_name'];
06938694   Etienne Pallier   Vue liste projets...
164
    	                    // ex: Materiels
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
165
    	                    $controller_name = $fields_names[$fname]['controller_name'];
06938694   Etienne Pallier   Vue liste projets...
166
167
168
169
    	                    //$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>
404b35d8   Etienne Pallier   Refactorisations,...
170
171
172
173
174
175
176
177
178
179
    	                    //$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;
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    	                    //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
    	                        ])
	                        );
    	                    */    
06938694   Etienne Pallier   Vue liste projets...
203
204
205
206
207
    	                } // FK
    	                // - Sinon, champ quelconque => on l'affiche directement
    	                else
    	                    echo $fval;
    	                    
087b29e7   Etienne Pallier   GROSSE REFACTORIS...
208
	                echo "</td>";
1b901ee9   Etienne Pallier   Refactorisation e...
209
		        } // foreach entity fields
f63eabb2   Etienne Pallier   Gestion plus simp...
210
211
212
				?>
			
			</tr>
1b901ee9   Etienne Pallier   Refactorisation e...
213
214
215
            <?php 
            //break;
            endforeach; // foreach entities ?>
f63eabb2   Etienne Pallier   Gestion plus simp...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        </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>
-->