Blame view

src/Model/Table/DocumentsTable.php 6.1 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
<?php
namespace App\Model\Table;

6c4edfa3   Alexandre   First Commit LabI...
4
5
6
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
4dae83a2   Alexandre   Version: 2.5.1.0
7
use Cake\ORM\TableRegistry;
6c4edfa3   Alexandre   First Commit LabI...
8
9
10
11
12
13
14

/**
 * Documents Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Materiels
 * @property \Cake\ORM\Association\BelongsTo $Suivis
 */
0e5846aa   Alexandre   Css bouton valide...
15
class DocumentsTable extends AppTable
6c4edfa3   Alexandre   First Commit LabI...
16
17
18
19
20
{

    /**
     * Initialize method
     *
63c3cb16   epallier   Nombreux petits b...
21
22
     * @param array $config
     *            The configuration for the Table.
6c4edfa3   Alexandre   First Commit LabI...
23
24
25
26
27
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);
63c3cb16   epallier   Nombreux petits b...
28
        
6c4edfa3   Alexandre   First Commit LabI...
29
30
31
        $this->table('documents');
        $this->displayField('id');
        $this->primaryKey('id');
63c3cb16   epallier   Nombreux petits b...
32
        
6c4edfa3   Alexandre   First Commit LabI...
33
34
35
36
37
38
39
40
        $this->belongsTo('Materiels', [
            'foreignKey' => 'materiel_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Suivis', [
            'foreignKey' => 'suivi_id',
            'joinType' => 'INNER'
        ]);
9b4da83b   Alexandre   Version: 2.5.0.0
41
42
        
        $this->belongsTo('TypeDocuments', [
63c3cb16   epallier   Nombreux petits b...
43
            'foreignKey' => 'type_document_id'
9b4da83b   Alexandre   Version: 2.5.0.0
44
        ]);
6c4edfa3   Alexandre   First Commit LabI...
45
46
47
48
49
    }

    /**
     * Default validation rules.
     *
63c3cb16   epallier   Nombreux petits b...
50
51
     * @param \Cake\Validation\Validator $validator
     *            Validator instance.
6c4edfa3   Alexandre   First Commit LabI...
52
53
54
55
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
63c3cb16   epallier   Nombreux petits b...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        $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');
        
6c4edfa3   Alexandre   First Commit LabI...
74
        return $validator;
63c3cb16   epallier   Nombreux petits b...
75
76
    }

6c4edfa3   Alexandre   First Commit LabI...
77
78
79
80
    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
63c3cb16   epallier   Nombreux petits b...
81
82
     * @param \Cake\ORM\RulesChecker $rules
     *            The rules object to be modified.
6c4edfa3   Alexandre   First Commit LabI...
83
     * @return \Cake\ORM\RulesChecker
9b4da83b   Alexandre   Version: 2.5.0.0
84
     */
6c4edfa3   Alexandre   First Commit LabI...
85
86
    public function buildRules(RulesChecker $rules)
    {
63c3cb16   epallier   Nombreux petits b...
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
        $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.'
        ]);
        
6c4edfa3   Alexandre   First Commit LabI...
154
        return $rules;
9b4da83b   Alexandre   Version: 2.5.0.0
155
156
    }

9b4da83b   Alexandre   Version: 2.5.0.0
157
158
159
    /**
     * Custom Validation Rules
     */
63c3cb16   epallier   Nombreux petits b...
160
161
162
163
164
165
166
167
    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);
9b4da83b   Alexandre   Version: 2.5.0.0
168
    }
63c3cb16   epallier   Nombreux petits b...
169

9b4da83b   Alexandre   Version: 2.5.0.0
170
171
    /**
     * CakePHP Model Functions
63c3cb16   epallier   Nombreux petits b...
172
     */
9b4da83b   Alexandre   Version: 2.5.0.0
173
174
    public function beforeSave($event, $entity, $options)
    {
63c3cb16   epallier   Nombreux petits b...
175
176
177
178
179
180
181
        $file = $entity->get('chemin_file');
        if (! empty($file['tmp_name'])) {
            $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
            $entity->set('type_doc', $extension);
        }
        
        return true;
9b4da83b   Alexandre   Version: 2.5.0.0
182
    }
63c3cb16   epallier   Nombreux petits b...
183

9b4da83b   Alexandre   Version: 2.5.0.0
184
185
    /**
     * CakePHP Model Functions
63c3cb16   epallier   Nombreux petits b...
186
187
188
189
190
191
192
193
194
195
196
197
     */
    public function afterSave($event, $entity, $options)
    {
        $file = $entity->get('chemin_file');
        if (! empty($file['tmp_name'])) {
            $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
            if ($entity->get('photo')) {
                move_uploaded_file($file['tmp_name'], 'img' . DS . 'photos' . DS . $entity->get('id') . '.' . $extension);
            } else {
                move_uploaded_file($file['tmp_name'], 'files' . DS . $entity->get('id') . '.' . $extension);
            }
        }
9b4da83b   Alexandre   Version: 2.5.0.0
198
    }
63c3cb16   epallier   Nombreux petits b...
199

9b4da83b   Alexandre   Version: 2.5.0.0
200
201
    /**
     * CakePHP Model Functions
63c3cb16   epallier   Nombreux petits b...
202
203
204
205
206
207
208
209
210
     */
    public function afterDelete($event, $entity, $options)
    {
        $nomFichier = $entity->get('id') . '.' . $entity->get('type_doc');
        if ($entity->get('photo')) {
            unlink('img' . DS . 'photos' . DS . $nomFichier);
        } else {
            unlink('files' . DS . $nomFichier);
        }
9b4da83b   Alexandre   Version: 2.5.0.0
211
    }
6c4edfa3   Alexandre   First Commit LabI...
212
}