WebdpoFiche.php 13.2 KB
Newer Older
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
<?php

/**
 * Model WebdpoFiche
 *
 * web-DPO : Outil de gestion de vos traitements dans le cadre de la 
 * réglementation relative à la protection des données personnelles (RGPD)
 * 
 * Copyright (c) Libriciel SCOP (https://www.libriciel.fr/)
 *
 * Licensed under The CeCiLL V2 License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright   Copyright (c) Libriciel SCOP (https://www.libriciel.fr/)
 * @link        https://www.libriciel.fr/web-cil/
 * @since       web-DPO v1.0.0
 * @license     http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html CeCiLL V2 License
 * @version     v1.0.0
 * @package     AppModel
 */

App::uses('AppModel', 'Model');

class WebdpoFiche extends AppModel {

    public $name = 'WebdpoFiche';

    public $useTable = false;
    
    protected $_schema = [];

    public $validate = [
        'declarantraisonsociale' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantadresse' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantemail' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantsiret' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantape' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declaranttelephone' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantservice' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'donneesSensible' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'personneresponsable' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'fonctionresponsable' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'emailresponsable' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'telephoneresponsable' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'transfertHorsUe' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
99
        'personnedpo' => [
100 101 102 103
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
104
        'emaildpo' => [
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
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantpersonnenom' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'declarantpersonneemail' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'outilnom' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        'finaliteprincipale' => [
            'notBlank' => [
                'rule' => ['notBlank']
            ]
        ],
        // ---------------------------------------------------------------------
130 131
        'descriptionDecisionAutomatisee' => [
            'checkNotBlankIf' => [
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                'rule' => ['checkNotBlankIf', 'decisionAutomatisee', true, ['Oui']]
            ]
        ],
        // ---------------------------------------------------------------------
        'organismeDestinataireHorsUe' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'transfertHorsUe', true, ['Oui']]
            ]
        ],
        'paysDestinataireHorsUe' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'transfertHorsUe', true, ['Oui']]
            ]
        ],
        'typeGarantieHorsUe' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'transfertHorsUe', true, ['Oui']]
149 150 151
            ]
        ],
        // ---------------------------------------------------------------------
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        'typeDonneeSensible' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'donneesSensible', true, ['Oui']]
            ]
        ],
        'descriptionDonneeSensible' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'donneesSensible', true, ['Oui']]
            ]
        ],
        'dureeConservationDonneeSensible' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'donneesSensible', true, ['Oui']]
            ]
        ],
        // ---------------------------------------------------------------------
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
        'soustraitantid' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'soustraitant', true, ['1']]
            ]
        ],
        'soustraitantsiret' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'soustraitant', true, ['1']]
            ]
        ],
        'soustraitantape' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'soustraitant', true, ['1']]
            ]
        ],
        // ---------------------------------------------------------------------
        'coresponsableid' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'nomcoresponsable' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'prenomcoresponsable' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'fonctioncoresponsable' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'emailcoresponsable' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'telephonecoresponsable' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'raisonsocialestructure' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'siretstructure' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
        'apestructure' => [
            'checkNotBlankIf' => [
                'rule' => ['checkNotBlankIf', 'coresponsable', true, ['1']]
            ]
        ],
    ];
    
    public function saveWithVirtualFields($data, $champs) {
        $Fiche = ClassRegistry::init('Fiche');

        foreach ($champs as $champ) {
            $details = json_decode(Hash::get($champ, 'Champ.details'));
            if ($details->obligatoire == true) {
                if (isset($this->validate[$details->name]) == false) {
                    $this->validate[$details->name] = [];
                }
tguillon's avatar
tguillon committed
240 241 242 243 244
                if (in_array(Hash::get($champ, 'Champ.type'), ['checkboxes', 'multi-select'])) {
                    $this->validate[$details->name]['multiple'] = ['rule' => ['multiple', ['min' => 1]]];
                } else {
                    $this->validate[$details->name]['notBlank'] = ['rule' => ['notBlank']];
                }
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            }
        }
        
        $data = $this->cleanupData($data, 'Fiche');
        
        $Fiche->create($data);
        $success = $Fiche->save(null, ['atomic' => false]);
        
        $this->data[$this->alias] = $data['Fiche'];
        $fieldsToKeep = ['id', 'coresponsable', 'soustraitantid', 'created', 'modified'];

        foreach (array_keys($Fiche->schema()) as $fieldName) {
            if (in_array($fieldName, $fieldsToKeep, true) === false) {
                unset($this->data[$this->alias][$fieldName]);
            }
        }

262 263 264
        $dynamicValidationErrors = [];
        $originalData = $this->data;
        $originalValidationErrors = $this->validationErrors;
265
        foreach (['horsue', 'donneessensibles'] as $fieldIndex) {
266 267 268 269 270 271 272 273 274
            if (isset($originalData[$this->alias][$fieldIndex]) === true && empty($originalData[$this->alias][$fieldIndex]) === false) {
                foreach ($originalData[$this->alias][$fieldIndex] as $key => $organisme) {
                    $dynamicData = [
                        $this->alias => $organisme +
                            [
                                'coresponsable' => $originalData[$this->alias]['coresponsable']
                            ]
                    ];

275 276 277 278 279 280 281 282 283 284
                    // Ajout de la valeur des champs pour les règles de validation dynamiques checkNotBlankIf
                    foreach (array_keys($organisme) as $tmp) {
                        $validate = (array)Hash::get($this->validate, $tmp);
                        foreach ($validate as $validationRule) {
                            if (isset($validationRule['rule'][0]) === true && $validationRule['rule'][0] === 'checkNotBlankIf') {
                                $dynamicData[$this->alias][$validationRule['rule'][1]] = $originalData[$this->alias][$validationRule['rule'][1]];
                            }
                        }
                    }

285 286 287 288 289 290 291 292 293 294 295
                    $this->create($dynamicData);
                    if ($this->validates() !== true) {
                        foreach ($this->validationErrors as $idx => $errors) {
                            $dynamicValidationErrors[$fieldIndex][$key][$idx] = $errors;
                        }
                    }
                }
            }
        }
        $this->data = $originalData;
        $this->validationErrors = $originalValidationErrors;
296
        $this->create([$this->alias => $this->data[$this->alias]]);
297
        $success = $this->validates() && $success && empty($dynamicValidationErrors) === true;
298 299

        $Fiche->validationErrors = $Fiche->validationErrors + $this->validationErrors;
300
        $Fiche->validationErrors = Hash::merge($Fiche->validationErrors, $dynamicValidationErrors);
301 302 303 304 305 306 307

        if ($success == true) {
            if ($data['Fiche']['coresponsable'] == '0') {
                $this->data[$this->alias]['coresponsable'] = 'Non';
            } else {
                $this->data[$this->alias]['coresponsable'] = 'Oui';
            }
308 309 310 311 312 313 314

            if (!empty($data['Fiche']['soustraitant'])) {
                if ($data['Fiche']['soustraitant'] == '0') {
                    $this->data[$this->alias]['soustraitant'] = 'Non';
                } else {
                    $this->data[$this->alias]['soustraitant'] = 'Oui';
                }
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
            $idsToDelete = array_keys($Fiche->Valeur->find('list', [
                'conditions' => [
                    'champ_name' => array_keys($this->data[$this->alias]),
                    'fiche_id' => $Fiche->id
                ],
                'contain' => false
            ]));
            
            if (empty($idsToDelete) == false) {
                $success = $Fiche->Valeur->deleteAll([
                    'Valeur.id' => $idsToDelete
                ]) && $success;
            }

            foreach($this->data[$this->alias] as $fieldName => $value) {
                if ($fieldName != 'formulaire_id' && ($value != '' || in_array($fieldName, $this->validate))) {
                    if (is_array($value)) {
                        $value = json_encode($value);
                    }

                    $Fiche->Valeur->create([
                        'champ_name' => $fieldName,
                        'fiche_id' => $Fiche->id,
                        'valeur' => $value
                    ]);

                    $success = false !== $Fiche->Valeur->save(null, ['atomic' => false]) && $success;
                    $Fiche->Valeur->validationErrors = [];
                }
            }
        }

        $this->validationErrors = [];
        return $success;
    }
    
    public function cleanupData(array $data, $alias = null) {
        $alias = $alias === null ? $this->alias : $alias;

        if ($data[$alias]['coresponsable'] == false) {
            $data[$alias]['coresponsableid'] = null;
            $data[$alias]['nomcoresponsable'] = null;
            $data[$alias]['prenomcoresponsable'] = null;
            $data[$alias]['fonctioncoresponsable'] = null;
            $data[$alias]['emailcoresponsable'] = null;
            $data[$alias]['telephonecoresponsable'] = null;
            $data[$alias]['raisonsocialestructure'] = null;
            $data[$alias]['siretstructure'] = null;
            $data[$alias]['apestructure'] = null;
            $data[$alias]['telephonestructure'] = null;
            $data[$alias]['faxstructure'] = null;
            $data[$alias]['adressestructure'] = null;
            $data[$alias]['emailstructure'] = null;
        }  
        
        return ($data);
    }

    public function beforeValidate($options = array()) {
        $this->data = $this->cleanupData($this->data);
        return parent::beforeValidate($options);
    }
}