Blame view

src/Model/Table/SuivisTable.php 4.58 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
namespace App\Model\Table;

use App\Model\Entity\Suivi;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Suivis Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Materiels
ebe38bef   Alexandre   #3586 Ajout assoc...
14
 * @property \Cake\ORM\Association\BelongsTo $TypeSuivis
e9a0cc56   Alexandre   Version: 2.4.6.0
15
16
 * @property \Cake\ORM\Association\BelongsTo $GroupesThematiques
 * @property \Cake\ORM\Association\BelongsTo $GroupesMetiers
6c4edfa3   Alexandre   First Commit LabI...
17
18
 * @property \Cake\ORM\Association\HasMany $Documents
 */
0e5846aa   Alexandre   Css bouton valide...
19
class SuivisTable extends AppTable
6c4edfa3   Alexandre   First Commit LabI...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('suivis');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Materiels', [
            'foreignKey' => 'materiel_id',
            'joinType' => 'INNER'
        ]);
ebe38bef   Alexandre   #3586 Ajout assoc...
42
43
44
45
46
        
        $this->belongsTo('TypeSuivis', [
        		'foreignKey' => 'type_suivi_id'
        ]);
        
e9a0cc56   Alexandre   Version: 2.4.6.0
47
48
49
50
51
52
53
54
        $this->belongsTo('GroupesMetiers', [
        		'foreignKey' => 'groupes_metier_id'
        ]);
        
        $this->belongsTo('GroupesThematiques', [
        		'foreignKey' => 'groupes_thematique_id'
        ]);
        
6c4edfa3   Alexandre   First Commit LabI...
55
56
57
58
59
60
61
62
63
64
65
66
67
        $this->hasMany('Documents', [
            'foreignKey' => 'suivi_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
19798ef9   Alexandre   Mode_install, maj...
68
    	$validator
6c4edfa3   Alexandre   First Commit LabI...
69
70
            ->integer('id')
            ->allowEmpty('id', 'create');
19798ef9   Alexandre   Mode_install, maj...
71
    	
6c4edfa3   Alexandre   First Commit LabI...
72
        $validator
19798ef9   Alexandre   Mode_install, maj...
73
74
75
            ->integer('materiel_id');
    	
    	$validator
6c4edfa3   Alexandre   First Commit LabI...
76
77
78
            ->allowEmpty('date_controle');

        $validator
0e5846aa   Alexandre   Css bouton valide...
79
          ->allowEmpty('date_prochain_controle');
6c4edfa3   Alexandre   First Commit LabI...
80
81

        $validator
19798ef9   Alexandre   Mode_install, maj...
82
83
            ->notEmpty('organisme')
            ->add('organisme', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
6c4edfa3   Alexandre   First Commit LabI...
84
85

        $validator
19798ef9   Alexandre   Mode_install, maj...
86
87
88
89
            ->numeric('frequence', 'Veuillez saisir des chiffres uniquement.')
            ->allowEmpty('frequence')
            ->maxLength('frequence', 8, '8 Chiffres maximum');
        
6c4edfa3   Alexandre   First Commit LabI...
90
        $validator
e55ca961   Alexandre   Version: 2.4.2.8
91
            ->allowEmpty('type_frequence');
9cfb4997   Alexandre   Version: 2.4.3.10
92
93
94
        
        $validator
            ->allowEmpty('panne_resolu');
e55ca961   Alexandre   Version: 2.4.2.8
95
96
            
        $validator
19798ef9   Alexandre   Mode_install, maj...
97
98
            ->allowEmpty('commentaire')
            ->add('commentaire', 'valid', ['rule' => ['check_string_with_some_special_cars'], 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
6c4edfa3   Alexandre   First Commit LabI...
99

6c4edfa3   Alexandre   First Commit LabI...
100
101
102
103

        return $validator;
    }

0e5846aa   Alexandre   Css bouton valide...
104

19798ef9   Alexandre   Mode_install, maj...
105
    
6c4edfa3   Alexandre   First Commit LabI...
106
107
108
109
110
111
112
113
114
    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
0e5846aa   Alexandre   Css bouton valide...
115
116
117
118
    	
    	$checkNextDateControlIsAfterDateControl = function($entity) {
    		
    		 $controle = $entity->date_controle;
ebe38bef   Alexandre   #3586 Ajout assoc...
119
120
121
    		 $p_controle = $entity->date_prochain_controle;
    		 if (empty($controle) || empty($p_controle)) return true;
    		 $prochainControle = $p_controle;
0e5846aa   Alexandre   Css bouton valide...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    		 $controle = explode("/", $controle);
    		 $prochainControle = explode("/", $prochainControle);
    		 $controle = $controle[2].$controle[1].$controle[0];
    		 $prochainControle = $prochainControle[2].$prochainControle[1].$prochainControle[0];
    		 if ($controle > $prochainControle) {
    			 return false;
    		 }
    		 return true;

    	};
    	
    	$rules->add($checkNextDateControlIsAfterDateControl, [
    			'errorField' => 'date_prochain_controle',
    			'message' => 'La date de la prochaine intervention dois être postérieure à la date de l\'intervention précédente.'
    	]);
    	
6c4edfa3   Alexandre   First Commit LabI...
138
        $rules->add($rules->existsIn(['materiel_id'], 'Materiels'));
ebe38bef   Alexandre   #3586 Ajout assoc...
139
        $rules->add($rules->existsIn(['type_suivi_id'], 'TypeSuivis'));
e9a0cc56   Alexandre   Version: 2.4.6.0
140
141
        $rules->add($rules->existsIn(['groupes_thematique_id'], 'GroupesThematiques'));
        $rules->add($rules->existsIn(['groupes_metier_id'], 'GroupesMetiers'));
6c4edfa3   Alexandre   First Commit LabI...
142
143
        return $rules;
    }
3e24b686   Alexandre   Version: 2.4.2.20
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    
    
    public function beforeSave($event, $entity, $options)
    {
    	if(empty($entity->get('date_controle'))){
    		$entity->set('date_controle', null);
    	}
    	
    	if(empty($entity->get('date_prochain_controle'))){
    		$entity->set('date_prochain_controle', null);
    	}
    	 
    	return true;
    }
    
    
    
6c4edfa3   Alexandre   First Commit LabI...
161
}