DocumentsTable.php
7.05 KB
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
99
100
101
102
103
104
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
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
<?php
namespace App\Model\Table;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
/**
* Documents Model
*
* @property \Cake\ORM\Association\BelongsTo $Materiels
* @property \Cake\ORM\Association\BelongsTo $Suivis
*/
class DocumentsTable extends AppTable
{
/**
* Initialize method
*
* @param array $config
* The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('documents');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Materiels', [
'foreignKey' => 'materiel_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Suivis', [
'foreignKey' => 'suivi_id',
'joinType' => 'INNER'
]);
$this->belongsTo('TypeDocuments', [
'foreignKey' => 'type_document_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator
* Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator->integer('id')->allowEmpty('id', 'create');
$validator->allowEmpty('type_doc');
$validator->allowEmpty('chemin_file');
$validator->notEmpty('nom');
$validator->allowEmpty('type_document_id');
$validator->allowEmpty('description');
$validator->allowEmpty('materiel_id');
$validator->allowEmpty('photo');
$validator->allowEmpty('suivi_id');
//Mi regex pour mettre la règle alphaNumericDashUnderscore
$validator->add('nom', 'alphaNumericDashUnderscore', [
'rule' => ['custom', '|^[0-9a-zA-Z_-]*$|'],
'message' => __('Le nom du document ne doit contenir que des chiffres, des lettres, des tirets, ou des tirets.'),
]);
return $validator;
}
/**
* 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)
{
$config = TableRegistry::get('Configurations')->find()
->where([
'id =' => 1
])
->first();
$checkSizeDoc = function ($entity) {
if (! empty($entity->get('chemin_file')['tmp_name'])) {
$config = TableRegistry::get('Configurations')->find()
->where([
'id =' => 1
])
->first();
$size = $entity->get('chemin_file')['size'];
if ($size !== null) {
if ($size > $config->taille_max_doc)
return false;
else
return true;
} else
return false;
} else
return true;
};
$checkPhoto = function ($entity) {
if (! empty($entity->get('chemin_file')['tmp_name'])) {
if ($entity->get('photo')) {
$extension = strtolower(pathinfo($entity->get('chemin_file')['name'], PATHINFO_EXTENSION));
return in_array($extension, [
'png',
'jpg',
'jpeg'
]);
} else
return true;
} else
return true;
};
$checkEditFile = function ($entity) {
if (! $entity->get('edit')) {
if (empty($entity->get('chemin_file')['tmp_name'])) {
return false;
} else {
return true;
}
} else {
return true;
}
};
$rules->add($checkSizeDoc, [
'errorField' => 'chemin_file',
'message' => 'Le fichier ne peut pas avoir une taille supérieur à ' . substr($config->taille_max_doc / (1024 * 1024), 0, 4) . ' Mo.'
]);
$rules->add($checkPhoto, [
'errorField' => 'chemin_file',
'message' => 'La photo doit etre au format png, jpg (ou jpeg).'
]);
$rules->add($checkEditFile, [
'errorField' => 'chemin_file',
'message' => 'Un fichier doit être présent.'
]);
return $rules;
}
/**
* Custom Validation Rules
*/
public function fileExtension($check, $extensions, $allowEmpty = false)
{
$file = current($check);
if ($allowEmpty && empty($file['tmp_name'])) {
return true;
}
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
return in_array($extension, $extensions);
}
/**
* CakePHP Model Functions
*/
public function beforeSave($event, $entity, $options)
{
$file = $entity->get('chemin_file');
if (! empty($file['tmp_name'])) {
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$entity->set('type_doc', $extension);
}
return true;
}
/**
* CakePHP Model Functions
*/
public function afterSave($event, $entity, $options)
{
$file = $entity->get('chemin_file');
if (! empty($file['tmp_name'])) {
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
// MI nouvelle norme pour les noms de documents uploadés
// id(du matériel ou suivi associé)_NomDoc_id(Doc).extension
if(!$entity->get('materiel_id')== Null ) {
$id=$entity->get('materiel_id');
} else {
$id=$entity->get('suivi_id');
}
$nom=$id."_".$entity->get('nom')."_".$entity->get('id'). '.' . $extension;
if ($entity->get('photo')) {
move_uploaded_file($file['tmp_name'], 'img' . DS . 'photos' . DS . $nom );
} else {
move_uploaded_file($file['tmp_name'], 'files' . DS . $nom );
}
}
}
/**
* CakePHP Model Functions
*/
public function afterDelete($event, $entity, $options)
{
// MI nouvelle norme pour les noms de documents uploadés
// id(du matériel ou suivi associé)_NomDoc_id(Doc).extension
if(!$entity->get('materiel_id')== Null ) {
$id=$entity->get('materiel_id');
} else {
$id=$entity->get('suivi_id');
}
$nomFichier=$id."_".$entity->get('nom')."_".$entity->get('id').'.' . $entity->get('type_doc');
if ($entity->get('photo')) {
unlink('img' . DS . 'photos' . DS . $nomFichier);
} else {
unlink('files' . DS . $nomFichier);
}
}
}