coresponsable.ctp 10.7 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
echo $this->Html->script([
    'smalot-bootstrap-datetimepicker/js/bootstrap-datetimepicker.min',
    'smalot-bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.fr.js'
]);
echo $this->Html->css('/js/smalot-bootstrap-datetimepicker/css/bootstrap-datetimepicker.min');
?>

9 10 11 12
<!-- Onglet Co-responsable -->
<div id="ongletComplementaireCoresponsable" class="tab-pane">
    <br>

13 14 15 16 17 18 19 20 21 22
    <!-- Information sur le rédacteur -->
    <div class="col-md-12">
    <span class='labelFormulaire'>
        <?php
        echo __d('fiche', 'fiche.textInfoConcernantCoresponsable');
        ?>
    </span>
        <div class="row row35"></div>
    </div>

23 24 25
    <?php
    if (!empty($responsables)) {
        ?>
26 27 28
        <!-- Champs du formulaire -->
        <div class="row">
            <div class="col-md-6">
29
                <?php
30 31 32
                echo $this->WebcilForm->input('Coresponsable.coresponsables', [
                    'id' => 'coresponsables',
                    'label' => [
33
                        'text' => __d('fiche', 'fiche.champCoresponsables')
34 35 36 37 38 39 40 41
                    ],
                    'class' => 'form-control',
                    'options' => Hash::combine($responsables, '{n}.Responsable.id', '{n}.Responsable.raisonsocialestructure'),
                    'empty' => true,
                    'multiple' => true,
                    'required' => true,
                    'data-placeholder' => __d('fiche', 'fiche.placeholderChampCoresponsable')
                ]);
42 43
                ?>
            </div>
44 45 46 47
        </div>
        <?php
    }
    ?>
48

49 50 51 52 53 54 55 56 57 58 59 60 61
    <div id="coresponsableCards"></div>

    <?php
    if (!empty($coresponsables)) {
        foreach ($coresponsables as $key => $coresponsable) {
            echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.responsable_id", [
                'value' => $coresponsable['Coresponsable']['responsable_id'],
                'id' => false
            ]);
            echo $this->Form->hidden("Coresponsable.currentCoresponsable.{$coresponsable['Coresponsable']['responsable_id']}.coresponsable_id", [
                'value' => $coresponsable['Coresponsable']['id'],
                'id' => false
            ]);
62
        }
63 64 65
    }
    ?>

66 67 68
</div>
<!-- Fin onglet Co-responsable -->

69
<?php
70
$coresponsablesRequestData = null;
71 72 73 74
if (isset($this->request->data['Coresponsable']['coresponsables'])) {
    $coresponsablesRequestData = $this->request->data['Coresponsable']['coresponsables'];
}

75 76
$coresponsabilitefieldsRequestData = null;
if (isset($this->request->data['WebdpoCoresponsable']) && !empty($this->request->data['WebdpoCoresponsable'])) {
77
    $coresponsabilitefieldsRequestData = $this->request->data['WebdpoCoresponsable'];
78 79 80 81 82
}

$validationErrors = null;
if (isset($this->validationErrors['WebdpoCoresponsable']) && !empty($this->validationErrors['WebdpoCoresponsable'])) {
    $validationErrors = $this->validationErrors['WebdpoCoresponsable'];
83 84 85
}
?>

86 87
<script type="text/javascript">

88
    $(document).ready(function () {
89 90 91 92 93 94 95
        let eventSelect = $("#coresponsables");
        $(eventSelect).select2({
            width: '100%',
        });

        let infoCoresponsables = <?php echo json_encode(Hash::combine($responsables, '{n}.Responsable.id', '{n}.Responsable'))?>;

96
        <?php
97
        if (isset($fields['coresponsable']) && !empty($fields['coresponsable'])) {
98 99 100 101 102 103 104 105 106 107 108 109 110 111
            $templateFieldsCoresponsable = $this->element('Fiches/tabs/formulaire', [
                'champs' => $fields['coresponsable']
            ]);
            $arrayFieldsId = [];
            foreach ($fields['coresponsable'] as $field) {
                $arrayFieldsId[] = json_decode($field['Champ']['details'], true)['name'];
            }
        } else {
            $arrayFieldsId = [];
            $templateFieldsCoresponsable = '';
        }
        ?>

        let templateFieldsCoresponsable = <?php echo json_encode($templateFieldsCoresponsable);?>;
112 113
        let coresponsablesRequestData = <?php echo json_encode($coresponsablesRequestData);?>;
        let coresponsabilitefieldsRequestData = <?php echo json_encode($coresponsabilitefieldsRequestData);?>;
114
        let validationErrors = <?php echo json_encode($validationErrors);?>;
115

116 117 118 119
        if (coresponsablesRequestData) {
            $(coresponsablesRequestData).each(function (key, val) {
                createCard(templateFieldsCoresponsable, infoCoresponsables, val, coresponsabilitefieldsRequestData);
            });
120 121 122 123 124 125 126 127 128

            // Mise en évidence des champs en erreur
            if (validationErrors !== null) {
                $.each(validationErrors, function (responsableId, fieldError) {
                    $.each(fieldError, function (field, errorMessage) {
                        $('#cr_' + responsableId + '_' + field).closest('div').after('<div class="error-message">'+errorMessage.toString()+'</div>');
                    });
                });
            }
129
        }
130

131
        // Quand on sélectionne dans le champ Co-responsable(s)
132 133
        $(eventSelect).on("select2:select", function (e) {
            let idSelect = e.params.data.id;
134
            createCard(templateFieldsCoresponsable, infoCoresponsables, idSelect);
135 136 137 138 139 140 141 142

            $('.calendar').datetimepicker({
                viewMode: 'year',
                startView: 'decade',
                format: 'dd/mm/yyyy',
                minView: 2,
                language: 'fr'
            });
143 144
        });

145
        // Quand on déselectionne dans le champ Co-responsable(s)
146
        $(eventSelect).on("select2:unselect", function (e) {
147
            let id = e.params.data.id
148
            let idRemove = '#card_coresponsable_' + id;
149 150

            $('#coresponsableCards').find(idRemove).remove();
151 152 153

            $('input[type="hidden"][name="data[Coresponsable][currentCoresponsable]['+id+'][responsable_id]"]').remove();
            $('input[type="hidden"][name="data[Coresponsable][currentCoresponsable]['+id+'][coresponsable_id]"]').remove();
154 155
        });

156 157 158 159 160 161 162 163
        $('.calendar').datetimepicker({
            viewMode: 'year',
            startView: 'decade',
            format: 'dd/mm/yyyy',
            minView: 2,
            language: 'fr'
        });

164 165
    });

166
    function createCard(templateFieldsCoresponsable, infoCoresponsables, idSelect, coresponsabilitefieldsRequestData = null) {
167 168 169 170
        let coresponsable = infoCoresponsables[idSelect];
        let colLeft = '';
        let formCoreponsableFields = '';

171
        if (templateFieldsCoresponsable) {
172
            let fields = templateFieldsCoresponsable.replace(/data\[WebdpoFiche\]/g, 'data[WebdpoCoresponsable]['+idSelect+']');
173 174 175 176 177 178 179
            fields = fields.replace(/(for|id)="(<?php echo implode('|', $arrayFieldsId);?>)"/g, '$1="cr_'+idSelect+'_$2"');

            if (fields) {
                formCoreponsableFields = '<div id="form-container-coresponsable">';
                formCoreponsableFields = formCoreponsableFields + fields;
                formCoreponsableFields = formCoreponsableFields + '</div>';
            }
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
        }

        if (coresponsable['adressestructure'] != null) {
            colLeft = colLeft + '<p>' +
                '<u>Adresse :</u>' +
                coresponsable['adressestructure'] +
                '</p>';
        }

        if (coresponsable['telephonestructure'] != null) {
            colLeft = colLeft + '<p>' +
                '<u>Téléphone :</u>' +
                coresponsable['telephonestructure'] +
                '</p>';
        }

        if (coresponsable['faxstructure'] != null) {
            colLeft = colLeft + '<p>' +
                '<u>Fax :</u>' +
                coresponsable['faxstructure'] +
                '</p>';
        }

        if (coresponsable['emailstructure'] != null) {
            colLeft = colLeft + '<p>' +
                '<u>E-mail :</u>' +
                coresponsable['emailstructure'] +
                '</p>';
        }

        let nomComplet = coresponsable['prenomresponsable'] + ' ' + coresponsable['nomresponsable'];

        $('#coresponsableCards').append('<div id="card_coresponsable_'+idSelect+'" class="row">' +
            '<div class="col-md-12">' +
            '   <div class="panel panel-default ">' +
            '       <div class="panel-body">' +
            '           <div class="col-md-6">' +
            '               <h4>' + coresponsable['raisonsocialestructure'] + '</h4>' +
            '               <p>' +
            '                   <u>N° SIRET :</u>' +
                                coresponsable['siretstructure'] +
            '               </p>' +
            '               <p>' +
            '                   <u>Code APE :</u>' +
                                coresponsable['apestructure'] +
            '               </p>' +
                            colLeft +
            '           </div>' +
            '           <div class="col-md-6">' +
            '               <h4>' + nomComplet + '</h4>' +
            '               <p>' +
            '                   <u>Fonction :</u>' +
                                coresponsable['fonctionresponsable'] +
            '               </p>' +
            '               <p>' +
            '                   <u>Téléphone :</u>' +
                                coresponsable['telephoneresponsable'] +
            '               </p>' +
            '               <p>' +
            '                   <u>E-mail :</u>' +
                                coresponsable['emailresponsable'] +
            '               </p>' +
            '           </div>' +
                        formCoreponsableFields +
            '       </div>' +
            '   </div>' +
            '</div>' +
        '</div>');
248 249 250 251

        // On attribut la valeur au champ si il y en a une.
        if (coresponsabilitefieldsRequestData !== null) {
            $.each(coresponsabilitefieldsRequestData[idSelect], function (key, val) {
252 253
                let fieldId = '#cr_' + idSelect + '_' + key;

254
                if (jQuery.isArray(val)) {
255 256
                    let ifMultiSelect = $(fieldId + '[name="data[WebdpoCoresponsable]['+idSelect+']['+key+'][]"]');

257
                    $.each(val, function (k, v) {
258 259 260 261 262
                        if (ifMultiSelect.length) {
                            $(fieldId + ' option[value="' + v + '"]').prop("selected", true);
                        } else {
                            $('#' + key + v).prop("checked", true);
                        }
263 264
                    });
                } else {
265 266 267 268 269 270 271
                    let ifRadio = $('input[type="radio"][name="data[WebdpoCoresponsable]['+idSelect+']['+key+']"][value="'+val+'"]');

                    if (ifRadio.length) {
                        $(ifRadio).prop("checked",true);
                    } else {
                        $(fieldId).val(val);
                    }
272
                }
273 274
            });
        }
275 276
    }

277
</script>