Blame view

src/Model/Table/DocumentsTable.php 5.41 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{

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

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

        $this->belongsTo('Materiels', [
            'foreignKey' => 'materiel_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Suivis', [
            'foreignKey' => 'suivi_id',
            'joinType' => 'INNER'
        ]);
9b4da83b   Alexandre   Version: 2.5.0.0
40
41
42
43
        
        $this->belongsTo('TypeDocuments', [
        		'foreignKey' => 'type_document_id'
        ]);
6c4edfa3   Alexandre   First Commit LabI...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    }

    /**
     * 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
f4e6dc02   Alexandre   Version: 2.5.2.0
62
63
            ->allowEmpty('chemin_file');
            
9b4da83b   Alexandre   Version: 2.5.0.0
64
65
        $validator
            ->notEmpty('nom');
4fd23929   Alexandre   Version: 2.5.0
66
67
        
        $validator
9b4da83b   Alexandre   Version: 2.5.0.0
68
69
70
            ->allowEmpty('type_document_id');
            
        $validator
4fd23929   Alexandre   Version: 2.5.0
71
72
73
74
            ->allowEmpty('description');
            
        $validator
            ->allowEmpty('materiel_id');
4dae83a2   Alexandre   Version: 2.5.1.0
75
76
77
        
       $validator
            ->allowEmpty('photo');
4fd23929   Alexandre   Version: 2.5.0
78
79
80
            
        $validator
            ->allowEmpty('suivi_id');
6c4edfa3   Alexandre   First Commit LabI...
81
82

        return $validator;
9b4da83b   Alexandre   Version: 2.5.0.0
83
84
85
    }   
    
    
6c4edfa3   Alexandre   First Commit LabI...
86
87
88
89
90
91
    /**
     * 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
9b4da83b   Alexandre   Version: 2.5.0.0
92
     */
6c4edfa3   Alexandre   First Commit LabI...
93
94
    public function buildRules(RulesChecker $rules)
    {
4dae83a2   Alexandre   Version: 2.5.1.0
95
96
    	$config = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
    	
9b4da83b   Alexandre   Version: 2.5.0.0
97
    	$checkSizeDoc= function($entity) {
f4e6dc02   Alexandre   Version: 2.5.2.0
98
99
100
101
    		if(!empty($entity->get('chemin_file')['tmp_name'])) {
    			$config = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
    			$size = $entity->get('chemin_file')['size'];
    			if(isset($size)) {
aaf7558a   Thibaud Ajas   modifications de ...
102
    				if($size > $config->taille_max_doc)
f4e6dc02   Alexandre   Version: 2.5.2.0
103
    					return false;
aaf7558a   Thibaud Ajas   modifications de ...
104
    				else
f4e6dc02   Alexandre   Version: 2.5.2.0
105
    					return true;
aaf7558a   Thibaud Ajas   modifications de ...
106
    			} else
f4e6dc02   Alexandre   Version: 2.5.2.0
107
    				return false;
aaf7558a   Thibaud Ajas   modifications de ...
108
    		} else
f4e6dc02   Alexandre   Version: 2.5.2.0
109
    			return true;
9b4da83b   Alexandre   Version: 2.5.0.0
110
111
    	};
    	
4dae83a2   Alexandre   Version: 2.5.1.0
112
    	$checkPhoto= function($entity) {
f4e6dc02   Alexandre   Version: 2.5.2.0
113
114
115
116
117
    		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']);
				}
aaf7558a   Thibaud Ajas   modifications de ...
118
				else
f4e6dc02   Alexandre   Version: 2.5.2.0
119
					return true;
aaf7558a   Thibaud Ajas   modifications de ...
120
    		} else
f4e6dc02   Alexandre   Version: 2.5.2.0
121
    			return true;
4dae83a2   Alexandre   Version: 2.5.1.0
122
123
    	};
    	
f4e6dc02   Alexandre   Version: 2.5.2.0
124
125
126
127
128
129
130
131
132
133
134
135
    	$checkEditFile= function($entity) {
    		if(!$entity->get('edit')) {
				if(empty($entity->get('chemin_file')['tmp_name'])) {
					return false;
				}
				else {
					return true;
				}
    		} else {
    			return true;
    		}
    	};
4dae83a2   Alexandre   Version: 2.5.1.0
136
    	
9b4da83b   Alexandre   Version: 2.5.0.0
137
138
    	$rules->add($checkSizeDoc, [
    			'errorField' => 'chemin_file',
4dae83a2   Alexandre   Version: 2.5.1.0
139
    			'message' => 'Le fichier ne peut pas avoir une taille supérieur à '.substr($config->taille_max_doc/(1024*1024), 0, 4).' Mo.'
9b4da83b   Alexandre   Version: 2.5.0.0
140
141
    	]);
    	
4dae83a2   Alexandre   Version: 2.5.1.0
142
143
144
145
146
    	$rules->add($checkPhoto, [
    			'errorField' => 'chemin_file',
    			'message' => 'La photo doit etre au format png, jpg (ou jpeg).'
    	]);
    	 
f4e6dc02   Alexandre   Version: 2.5.2.0
147
148
149
150
    	$rules->add($checkEditFile, [
    			'errorField' => 'chemin_file',
    			'message' => 'Un fichier doit être présent.'
    	]);
4dae83a2   Alexandre   Version: 2.5.1.0
151
    	
6c4edfa3   Alexandre   First Commit LabI...
152
        return $rules;
9b4da83b   Alexandre   Version: 2.5.0.0
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    }

    
    /**
     * 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)
    {
f4e6dc02   Alexandre   Version: 2.5.2.0
173
    	
9b4da83b   Alexandre   Version: 2.5.0.0
174
    	$file = $entity->get('chemin_file');
f4e6dc02   Alexandre   Version: 2.5.2.0
175
176
177
178
    	if(!empty($file['tmp_name'])) {
    		$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    		$entity->set('type_doc', $extension);
    	}
9b4da83b   Alexandre   Version: 2.5.0.0
179
    	
9b4da83b   Alexandre   Version: 2.5.0.0
180
181
182
183
184
185
186
187
188
189
    	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));
4dae83a2   Alexandre   Version: 2.5.1.0
190
191
192
193
194
195
    		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
196
197
198
199
200
201
202
203
204
    	}
    
    }
    
    /**
     * CakePHP Model Functions
     **/
    public function afterDelete($event, $entity, $options) {
    	$nomFichier = $entity->get('id').'.'.$entity->get('type_doc');
4dae83a2   Alexandre   Version: 2.5.1.0
205
206
207
208
209
210
    	if($entity->get('photo')) {
    		unlink('img'.DS.'photos'.DS.$nomFichier);
    	}
    	else {
    		unlink('files'.DS.$nomFichier);
    	}
9b4da83b   Alexandre   Version: 2.5.0.0
211
212
    }

6c4edfa3   Alexandre   First Commit LabI...
213
}