Blame view

src/Model/Table/DocumentsTable.php 5.51 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
4
5
6
7
8
<?php
namespace App\Model\Table;

use App\Model\Entity\Document;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
4dae83a2   Alexandre   Version: 2.5.1.0
9
use Cake\ORM\TableRegistry;
6c4edfa3   Alexandre   First Commit LabI...
10
11
12
13
14
15
16

/**
 * Documents Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Materiels
 * @property \Cake\ORM\Association\BelongsTo $Suivis
 */
0e5846aa   Alexandre   Css bouton valide...
17
class DocumentsTable extends AppTable
6c4edfa3   Alexandre   First Commit LabI...
18
19
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('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
42
43
44
45
        
        $this->belongsTo('TypeDocuments', [
        		'foreignKey' => 'type_document_id'
        ]);
6c4edfa3   Alexandre   First Commit LabI...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    }

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

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

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

6c4edfa3   Alexandre   First Commit LabI...
220
}