edit.ctp 6.34 KB
<?php

use App\Model\Table\MaterielsTable;

$DEBUG = false;
//$DEBUG = true;


// Variables générales cakephp
$_REQUEST = $_REQUEST;
$_COOKIE = $_COOKIE;
$_ENV = $_ENV;
//debug($_COOKIE);
/*
[
    'CAKEPHP' => 'hiraigc02eb46spa06iiog6tvh'
]
*/
//debug($_ENV);
//debug($_REQUEST);

// Variables passées par le controleur
$CAN_EDIT = true;
$CAN_EDIT = $CAN_EDIT; 
$READONLY = $READONLY;
$contact = $contact;
$fieldsets = $fieldsets;
//debug($fieldsets);

$icon = $READONLY ? '' : "<i class='icon-edit'></i>";
//$icon = $READONLY ? '' : "<i class='icon-pencil'></i>";
//$end = $READONLY ? "(Visualisation)" : "(Modification)";
//$title = "Configuration des champs obligatoires ou non modifiables de la fiche Matériel $end";
$title = "Configuration des champs obligatoires ou non modifiables de la fiche Matériel";
echo "<br><h2>$icon $title</h2><br>";
/*
    Autres icones possibles :
echo "<br><h2><i class='icon-edit'></i> $title</h2><br><br>";
echo "<br><h2><i class='icon-list'></i> $title</h2><br><br>";
echo "<br><h2><i class='icon-plus'></i> $title</h2><br><br>";
echo "<br><h2><i class='icon-pencil'></i> $title</h2><br><br>";
echo "<br><h2><i class='icon-trash'></i> $title</h2><br><br>";
*/

if ($READONLY && $CAN_EDIT) {
    $this->MyHelper->echoEditButton();
    $this->MyHelper->echoButtonForAction('icon-trash', 'reset-to-default', 'Remettre les valeurs par défaut', 'Remettre les valeurs par défaut', true);
    echo '<br><br>';
}

//echo $this->Html->icon('pencil');

echo $this->Form->create($contact);

    if (!$READONLY) {
        echo $this->Form->button('Enregistrer', ['class'=>'btn btn-outline-success', 'type'=>'submit']);
        //echo $this->Form->button('Submit');
        echo '<br><br>';
    }
    
    //echo $this->Form->button($this->Html->icon('pencil'), ['escape' => false]);
    // ...can be easily rewritten as:
    //echo $this->Form->button('i:pencil');

    foreach ($fieldsets as $fieldset_name => $fields) {
        $controls = [];
        
        //foreach ($fields as $field) {
        foreach ($fields as $field_name => $attributes) {

            if ($field_name=='fieldset_comment') {
                $controls["$fieldset_name.fieldset_comment"] = [
                    'hidden' => true,
                    //'type' => 'text',
                    'type' => 'hidden',
                    'label' => false,
                    'val' => $attributes,
                    //'size' => 20,
                    //'disabled' => $READONLY,
                    //'readonly' => $READONLY,
                ];
                continue;
            }
            
            // champ activé ou désactivé
            $checked = $attributes['selected'];
            $comment = $attributes['comment'];
            $except_roles = isset($attributes['except_roles']) ? $attributes['except_roles'] : [];
            
            $name = "$fieldset_name.$field_name";

            // - CHAMP de la table materiel : Checkbox pour le dé/sélectionner
            $label = $field_name;
            if ($READONLY) {
                if ($comment) $label .= ' ('.$comment.')';
                if ($except_roles) $label .= ' (sauf '.implode(', ',$except_roles).')';
            }
            $controls["$name.selected"] = [
                'type' => 'checkbox',
                //'hiddenField' => true,
                'label' => $label,
                'checked' => $checked,
                'disabled' => $READONLY,
                //'readonly' =>$READONLY,
                //'readonly' => 'readonly',
            ];

            // (EDIT ONLY)
            // - Label associé au champ (entre parenthèses) doit pouvoir être modifié
            if (!$READONLY) $controls["$name.comment"] = [
                //'type' => 'input',
                'label' => false,
                'val' => $comment,
                //'size' => 20,
                //'disabled' => $READONLY,
                //'readonly' => $READONLY,
            ];
            
            // (EDIT ONLY)
            $fieldsets_with_roles = ['UNEDITABLE_FIELDS','UNEDITABLE_FIELDS_AFTER_LOT0', 'UNEDITABLE_FIELDS_AFTER_LOT1', 'UNEDITABLE_FIELDS_AFTER_LOT2'];
            // - ROLES exceptés (select multiple list)
            //if (!$READONLY && $field_name!='HAS_ORDER_BUTTON')
            if ( !$READONLY && in_array($fieldset_name, $fieldsets_with_roles) ) $controls["$name.except_roles"] = [
                'type' => 'select',
                'label' => '(excepté pour les rôles)',
                //'options' => ['<aucune exception>', 'user', 'admin', 'resp'],
                //'options' => ['Utilisateur', 'Responsable', 'Administration'],
                'options' => [
                    ['text' => 'Utilisateur', 'value' => 'Utilisateur'],
                    ['text' => 'Responsable', 'value' => 'Responsable'],
                    ['text' => 'Administration', 'value' => 'Administration'],
                ],
                //'val' => ['Responsable', 'Utilisateur'],
                'val' => $except_roles,
                'multiple' => 'checkbox',
                //'size' => 20,
                'disabled' => $READONLY,
            ];
            
        } // foreach $fields
        
        // https://api.cakephp.org/3.5/class-Cake.View.Helper.FormHelper.html#_controls
        $DEBUG && debug($controls);
        $fieldset_name_with_comment = $fieldset_name; 
        if ( isset($fields['fieldset_comment']) ) $fieldset_name_with_comment .= ' ('. $fields['fieldset_comment'] .')';
        
        echo $this->MyHelper->echoSectionStart($fieldset_name);
            echo $this->Form->controls(
                // fields
                $controls,
                // options
                [
                    'legend' => $fieldset_name_with_comment.' :',
                    /*
                    // paramètres html
                    'id' => "t_$fieldset_name",
                    'class' => 'toggle',
                    */
                ]
            );
        echo $this->MyHelper->echoSectionStop();
        
    } // foreach $fieldsets
        
    /*
    echo $this->Form->control('name');
    echo $this->Form->control('email');
    echo $this->Form->control('body');
    */
    
    if (!$READONLY) 
        echo $this->Form->button('Enregistrer', ['class'=>'btn btn-outline-success', 'type'=>'submit']);
        //echo $this->Form->button('Submit');
    

echo $this->Form->end();

if ($READONLY && $CAN_EDIT) $this->MyHelper->echoEditButton();