MyHelperHelper.php
15.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/* src/View/Helper/ButtonHelper.php */
namespace App\View\Helper;
use Cake\View\Helper;
use phpDocumentor\Reflection\Types\Void_;
//class MyButtonHelper extends Helper {
class MyHelperHelper extends Helper {
// On a besoin du HtmlHelper
public $helpers = ['Html', 'Form'];
public function initialize(array $config) {
//debug($config);
}
function getHourMnSecForDuration($duration_sec) {
//debug($duration_sec);
// PHP7 only (intdiv)
//$h = intdiv($duration_sec,3600);
// PHP5 !!!
$h = (int)($duration_sec/3600);
$m = (int)( ($duration_sec - $h*3600) / 60 );
$s = $duration_sec - $h*3600 - $m*60;
//return [$h,$m,$s];
return "$h h $m mn $s sec";
}
// (EP) Fonction utilisée dans la vue Materiels/add_or_edit
function isReadonlyField ($fieldName, $readonlyFields) {
// - Fonctionnement inversé : TOUS readonly (*) SAUF certains champs (listés)
//if (!empty($myReadonlyFields) && $myReadonlyFields[0]=='*') {
if (!empty($readonlyFields) && array_key_exists('*', $readonlyFields)) {
/*
$modifiableFields = $readonlyFields;
array_shift($modifiableFields);
*/
$modifiableFields = $readonlyFields['*'];
return ! in_array($fieldName, $modifiableFields);
}
// - Fonctionnement normal :
//return ( !empty($materiel->$fieldName) && in_array($fieldName, $myReadonlyFields) );
//return ( in_array($fieldName, $readonlyFields) );
return ( array_key_exists($fieldName, $readonlyFields) );
}
/*
private function getLabelForFieldName ($fieldName) {
if ( substr($fieldName, -3) === '_id' ) $fieldName = substr($fieldName,0,-3);
return ucfirst($fieldName);
}
*/
// (EP) Fonction utilisée dans la vue Materiels/add_or_edit
function control(
$IS_ADD, $readonlyFields, $fieldName, $label=null,
$empty=null, $default=null, $type=null, $class=null, $placeholder=null, $value=null
) {
$fieldValue = $this->_View->viewVars['entity']->$fieldName;
$options = [
//'type' => 'text',
//'label' => $label ? $label : $this->getLabelForFieldName($fieldName),
'label' => $label, // si null, c'est le framework qui génère un label auto
//'empty' => 'Choisir un domaine',
'empty' => $empty,
'default' => $default,
'type' => $type,
'class' => $class,
'placeholder' => $placeholder,
//'value' => $value,
//'style' => 'width: 260px',
// inutile car automatique si le nom de la variable passée par le controleur respecte la norme
//'options' => $surCategories,
//'default' => $Designation,
// ADD only
/////'default' => $materiel->designation,
//'default' => $designations,
//'default' => $designations->toArray(),
// EDIT only
// disabled ssi EDIT && readonly && pas vide (jamais disabled si mode ADD)
'disabled' => !$IS_ADD && $this->isReadonlyField($fieldName, $readonlyFields) && !empty($fieldValue)
//'disabled' => $IS_ADD ? false : $this->isReadonlyField($fieldName, $readonlyFields) && !empty($this->Form->getData($fieldName))
];
//if (!is_null($default)) $options['default'] = $default;
if (!is_null($value)) $options['value'] = $value;
return $this->Form->control($fieldName, $options);
}
function echoListToManageOrViewWithIcon($can_manage=false, $list_name, $controller_name, $action_name='index') {
//function echo_list($html, $verb, $list_name, $controller_name, $action_name) {
$verb = $can_manage ? 'Gérer' : 'Voir';
$icon_name = $can_manage ? 'config2.png' : 'see2.png';
$this->echoMenuItemWithIcon($icon_name, "$verb les $list_name", $controller_name, $action_name, 'nom');
}
function echoMenuItemPostLinkWithIcon($icon_name, $title, $controller_name, $action_name) {
$this->echoMenuItemLinkOrPostLinkWithIcon(true, $icon_name, $title, $controller_name, $action_name);
}
function echoMenuItemWithIcon($img_name, $title, $controller_name, $action_name=null, $sort_field=null) {
$this->echoMenuItemLinkOrPostLinkWithIcon(false, $img_name, $title, $controller_name, $action_name, $sort_field);
}
function echoMenuItemLinkOrPostLinkWithIcon($postlink, $img_name, $title, $controller_name, $action_name=null, $sort_field=null) {
$icon_size=40;
echo '<tr>';
echo "<td width=$icon_size>";
echo $this->Html->image("icons/$img_name", [
"alt" => $title,
'height' => $icon_size, 'width' => $icon_size,
'url' => ['controller' => $controller_name, 'action' => $action_name]
]);
echo '</td>';
echo '<td>';
if (! $postlink) {
echo $this->Html->link($title, [
'controller' => $controller_name,
'action' => $action_name,
'sort' => $sort_field,
]);
}
else {
echo $this->Form->postlink($title, [
'controller' => $controller_name,
'action' => $action_name,
], [
'confirm' => __('Êtes-vous sur ?')
]);
}
echo '</td>';
echo '</tr>';
}
public function getActionButton($icon_class, $buttonStyle, $title, $controller, $action, $id, $other_args=[], $tip='', $confirmMessage='', $moreButtonStyle='') {
if ($controller=='') $controller='materiels';
$controllerArgs = [];
$controllerArgs['controller'] = $controller;
$controllerArgs['action'] = $action;
$controllerArgs[] = $id;
foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;
return $this->Html->link(
__("<i class=$icon_class></i>$title"),
$controllerArgs,
/*
[
'controller' => $controller,
'action' => $action,
$id,
$other_args
],
*/
[
'title' => $tip,
'escape' => false,
'onclick' => 'return true;',
//'style' => 'margin-right: 10px'.$moreButtonStyle,
'style' => $buttonStyle,
'confirm' => $confirmMessage
]
);
}
public function displayTableRowLabelAndValue($label, $value, $params = "") { $this->displayElement($label, $value, $params); }
//@deprecated
public function displayElement($label, $value, $params = "") {
$TD = ($params=="") ? 'TD' : "TD $params";
//$TD = ($params=="") ? '<TD>' : '<TD '.$params.'>';
//$tdstyle = $params!="" ? $params : '';
// Ca c'est parce que sinon y'a au moins deux tests qui passent pas, a cause de l'espace dans la balise ...
//if ($valeur != "") echo '<tr> <td><strong>'.$nom.' </strong></td>' . $TD.$valeur.'</td></tr>';
//if ($valeur != "") echo '<tr><td><strong>' . $nom . ' </strong></td>' . $balise . $valeur . '</td></tr>';
//if ($valeur!="") echo "<TR> <TD><strong>".__($nom)."</strong></TD> <$TD>".h($valeur)."</TD> </TR>";
//if ($valeur!="") echo "<TR> <TD><strong>".__($nom)."</strong></TD> <$TD>".$valeur."</TD> </TR>";
if ($value!==null && $value!=='') {
$val = $value;
if ($value === true) $val = 'Oui';
if ($value === false) $val = 'Non';
echo "<TR> <TD><strong>".__($label)."</strong></TD> <$TD>".$val."</TD> </TR>";
}
//if ($valeur!="") echo "<TR> <TD><strong>$nom</strong></TD> <$TD>$valeur</TD> </TR>";
}
public function echoActionButton (
$icon_class, $buttonStyle, $title,
$action, $id, $controller=null, // si controleur null =>controleur par défaut
$other_args=[], $tip='',
$confirmMessage='', $moreButtonStyle='',
$with_confirm=false
) {
//if ($controller=='') $controller='materiels';
$controllerArgs = [];
if ($controller) $controllerArgs['controller'] = $controller;
$controllerArgs['action'] = $action;
$controllerArgs[] = $id;
foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;
// Avec confirmation (avant de déclencher l'action)
if ($with_confirm) {
if (!$confirmMessage) $confirmMessage = __("Êtes-vous sur de vouloir $title cette entité ?");
echo $this->Form->postLink(__("<i class='$icon_class'></i>$title"),
$controllerArgs,
[
'title' => $title,
//'style' => $buttonStyle,
'style' => 'margin-left: 10px',
'escape' => false,
'confirm' => $confirmMessage,
//'confirm' => $confirmMessage
//'confirm' => __("Êtes-vous sur de vouloir $title cette entité ?"),
//'confirm' => __('Êtes-vous sur de vouloir supprimer # {0}?', $suivi->id)
]);
}
// Sans confirmation (action directe)
else {
echo $this->Html->link(
__("<i class=$icon_class></i>$title"),
$controllerArgs,
/*
[
'controller' => $controller,
'action' => $action,
$id,
$other_args
],
*/
[
'title' => $tip,
'escape' => false,
'onclick' => 'return true;',
//'style' => 'margin-right: 10px'.$moreButtonStyle,
'style' => $buttonStyle,
//'confirm' => $confirmMessage ? $confirmMessage : $tip,
]
);
}
}
public function echoEditButton () { $this->echoButtonForAction('icon-pencil', 'edit', 'Editer', 'Modifier'); }
public function echoButtonForAction ($icon_class, $action, $label, $tooltip, $with_confirm=false) {
//$icon_class = 'icon-pencil';
$buttonStyle = 'margin-right: 10px';
echo '<div id="boutons" class="actions" style="margin-bottom:20px; width:100%; float:none; padding:5px 0;">';
$this->echoActionButton($icon_class, $buttonStyle,
" $label", $action, null, null, [], $tooltip,
'', '',
$with_confirm
);
echo "</div>";
}
public function echoDeleteButton (
$title,
$id, $controller=null, // si controleur null =>controleur par défaut
$other_args=[], $confirmMessage='', $moreButtonStyle=''
) {
//if ($controller=='') $controller='materiels';
$controllerArgs = [];
if ($controller) $controllerArgs['controller'] = $controller;
$controllerArgs['action'] = 'delete';
$controllerArgs[] = $id;
foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;
echo $this->Form->postLink(__("<i class='icon-trash'></i>$title"),
$controllerArgs,
[
'title' => 'Supprimer',
//'style' => $buttonStyle,
'style' => 'margin-left: 10px',
'escape' => false,
//'confirm' => $confirmMessage
'confirm' => __('Êtes-vous sur de vouloir supprimer cette entité ?'),
//'confirm' => __('Êtes-vous sur de vouloir supprimer # {0}?', $suivi->id)
]);
}
/* Pour src/Template/Configurations/view et edit */
public function echoSectionStart($title, $START_OPEN=true, $nice_title=null, $SPACE='') {
$icon_class = $START_OPEN ? 'icon-chevron-down' : 'icon-chevron-up';
if (!$nice_title) $nice_title = $title;
$WITH_TABLE=true;
//echo '<h3 id="t_'.$section.'" style="cursor: pointer;">';
//echo "<h3 id='t_$section' class='toggle' style='cursor: pointer;'>";
echo "<h3 id='t_$title' class='toggle' style='cursor: pointer;'>";
//echo '<i class="icon-chevron-down" style="font-size: 14px;" id="i_'.$section.'"></i>';
echo "<i class='$icon_class' style='font-size: 14px;' id='i_$title'></i>";
echo $SPACE.'<span style="text-decoration: underline;">'.$nice_title.'</span>';
echo '</h3>';
//echo '<div id="'.$section.'" style="margin-bottom: 20px;">';
echo '<div id="'.$title.'" style="margin-bottom: 20px;">';
if ($WITH_TABLE) {
echo '<table>';
echo '<tr><th style="width: 250px;"></th><th></th></tr>';
}
}
public function echoSectionStop() {
$WITH_TABLE=true;
if ($WITH_TABLE) echo '</table>';
echo '</div>';
}
//function displaySectionShowHide($controller_name, $entity_type_name, $title, $id_name, $is_masculine, $name_field_name, $entities) {
//function displaySectionShowHide($controller_name, $entity_type_name, $title, $is_masculine, $name_field_name, $entities) {
function displayAssociatedEntitiesAsSectionShowHide($controller_name, $entity_type_name, $title, $is_masculin, $name_field_name, $entities) {
$id_name = $controller_name;
$id_h3 = 't_'.$id_name;
$id_i = 'i_'.$id_name;
$title = ucfirst($title).' associé'. ($is_masculin ? 's' : 'es');
echo "<h3 id='$id_h3' class='toggle' style='cursor: pointer;'>";
echo "<i class='icon-chevron-down' style='font-size: 14px;' id='$id_i'></i>";
//echo " <span style='text-decoration: underline;'>$title (".$entities->count().")</span>";
echo " <span style='text-decoration: underline;'>$title (".count($entities).")</span>";
echo "</h3>";
echo "<div id='$id_name' style='margin-bottom: 20px;'>";
//if (true) {
$shift3 = ' ';
$shift5 = ' ';
//if ($entities->isEmpty()) {
if (empty($entities)) {
//echo "Aucune $entity_name liée à ce domaine.";
echo $shift5.($is_masculin ? "Aucun $entity_type_name associé" : "Aucune $entity_type_name associée");
}
else {
echo "<table>";
/*
<tr>
<th><= __('Nom') ?></th>
<th style="width: 50px;"><= __('Détail') ?></th>
</tr>
*/
foreach ($entities as $entity) {
echo "<tr>";
echo "<td>";
// Nom de l'entité (ou numéro de l'entité si nom vide, comme pour les emprunts par exemple...)
$name = $entity->$name_field_name ? $entity->$name_field_name : "$entity_type_name #".$entity->id;
echo $shift3.$this->Html->link(h($name), ['controller' => $controller_name, 'action' => 'view', h($entity->id)]);
echo "</td>";
/*
<td class="actions">
<= $this->Html->link(__('<i class="icon-search"></i>'), ['controller' => $controller, 'action' => 'view', $entity->id], ['escape' => false, 'style' => 'margin:0']) ?>
</td>
*/
echo "</tr>";
}
echo "</table>";
}
echo "</div>";
} // displaySectionShowHide()
}