table('documents'); $this->displayField('id'); $this->primaryKey('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'); 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(isset($size)) { 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)); 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); } } } /** * CakePHP Model Functions **/ 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); } } }