Soustraitant.php 5.95 KB
Newer Older
tguillon's avatar
tguillon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php

/**
 * Model Soustraitant
 *
 * 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');
24
App::uses('LinkedOrganisationInterface', 'Model/Interface');
tguillon's avatar
tguillon committed
25

26
class Soustraitant extends AppModel implements LinkedOrganisationInterface {
tguillon's avatar
tguillon committed
27 28 29

    public $name = 'Soustraitant';
    
30
    public $validationDomain = 'validation';
31

tguillon's avatar
tguillon committed
32 33 34 35 36 37 38 39 40 41 42 43
    public $actsAs = [
        'Database.DatabaseFormattable' => [
            'Database.DatabaseDefaultFormatter' => [
                'formatTrim' => [
                    'NOT' => ['binary']
                ],
                'formatNull' => true,
                'formatNumeric' => [
                    'float',
                    'integer'
                ],
                'formatSuffix'  => '/_id$/',
44
                'formatStripNotAlnum' => '/^(telephoneresponsable|telephonestructure|faxstructure|siretstructure|telephonefixe_dpo|telephoneportable_dpo)$/'
tguillon's avatar
tguillon committed
45
            ]
46 47
        ],
        'FieldAsFilterOption',
48 49 50 51
        'LettercaseFormattable' => [
            'upper_noaccents' => ['nomresponsable', 'nom_dpo'],
            'title' => ['prenomresponsable', 'prenom_dpo']
        ]
tguillon's avatar
tguillon committed
52 53 54 55 56 57 58 59 60 61 62 63
    ];
    
    /**
     * validate associations
     *
     * @var array
     *
     * @access public
     * @created 12/04/2018
     * @version v1.0.0
     */
    public $validate = [
64
        'siretstructure' => [
tguillon's avatar
tguillon committed
65 66 67 68 69
            'luhn' => [
                'rule' => [
                    'luhn',
                    true
                ],
70
                'message' => 'validation.numeroSIRETNonValide'
71 72 73
            ],
            [
                'rule' => 'numeric', 
74
                'message' => 'validation.numeroSIRETUniquementComposeChiffre'
75 76 77 78 79 80
            ],
            [
                'rule' => [
                    'minLength',
                    '14'
                ],
81
                'message' => 'validation.numeroSIRETAvoir14Caracteres'
82 83 84 85 86 87
            ],
            [
                'rule' => [
                    'maxLength',
                    '14'
                ],
88
                'message' => 'validation.numeroSIRETDoitPasDepasser14Caracteres' 
tguillon's avatar
tguillon committed
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
        ],
        'apestructure' => [
            'minLength' => [
                'rule' => ['minLength', 5],
                'allowEmpty' => true
            ],
            'maxLength' => [
                'rule' => ['maxLength', 5],
                'allowEmpty' => true
            ],
        ],
        'emailstructure' => [
            [
                'rule' => ['custom', REGEXP_EMAIL_FR],
                'message' => 'validation.adresseMailNonValide',
                'allowEmpty' => true
            ]
        ],
        'emailresponsable' => [
            [
                'rule' => ['custom', REGEXP_EMAIL_FR],
                'message' => 'validation.adresseMailNonValide',
                'allowEmpty' => true
            ]
        ],
        'email_dpo' => [
            [
                'rule' => ['custom', REGEXP_EMAIL_FR],
                'message' => 'validation.adresseMailNonValide',
                'allowEmpty' => true
            ]
tguillon's avatar
tguillon committed
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
        ]
    ];
    
    /**
     * hasMany associations
     *
     * @var array
     *
     * @access public
     * @created 11/04/2018
     * @version v1.0.0
     */
    public $hasAndBelongsToMany = [
        'Organisation' => [
            'className' => 'Organisation',
            'joinTable' => 'soustraitants_organisations',
            'foreignKey' => 'soustraitant_id',
            'associationForeignKey' => 'organisation_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => 'SoustraitantOrganisation'
147 148 149 150 151 152 153 154 155 156 157 158 159 160
        ],
        'Fiche' => [
            'className' => 'Fiche',
            'joinTable' => 'soustraitances',
            'foreignKey' => 'soustraitant_id',
            'associationForeignKey' => 'fiche_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => 'Soustraitance'
tguillon's avatar
tguillon committed
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

    /**
     * Retourne les id des entités liées au sous-traitant.
     *
     * @param int $id
     * @return array
     */
    public function getLinkedOrganisationsIds($id) {
        $query = [
            'fields' => ['organisation_id'],
            'conditions' => ['soustraitant_id' => $id],
        ];
        return Hash::extract(
            $this->SoustraitantOrganisation->find('all', $query),
            '{n}.SoustraitantOrganisation.organisation_id'
        );
    }

    /**
     * Retourne une condition permettant de limiter aux sous-traitants liés à une ou plusieurs entités.
     *
     * @param array|int $organisation_id
     * @return string
     */
    public function getConditionOrganisation($organisation_id) {
        $query = [
            'alias' => 'soustraitants_organisations',
            'fields' => [
                'soustraitants_organisations.soustraitant_id'
            ],
            'conditions' => [
                'soustraitants_organisations.organisation_id' => $organisation_id
            ]
        ];
        $sql = $this->SoustraitantOrganisation->sql($query);
        return "{$this->alias}.{$this->primaryKey} IN ( {$sql} )";
    }
tguillon's avatar
tguillon committed
200
}