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'); //le nom du doc ne doit pas contenir de termes non alphanumérique, cela crée des problèmes pour les dl $validator->add('nom', 'alphaNumeric', [ 'rule' => 'alphaNumeric', 'message' => __('Le nom du document ne doit contenir que des chiffres et des lettres.'), ]); 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)); //nouvelle norme pour les noms de documents uploadés // 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 . $entity->get('id') . '.' . $extension); move_uploaded_file($file['tmp_name'], 'img' . DS . 'photos' . DS . $nom ); } else { //move_uploaded_file($file['tmp_name'], 'files' . DS . $entity->get('id') . '.' . $extension); move_uploaded_file($file['tmp_name'], 'files' . DS . $nom ); } } } /** * CakePHP Model Functions */ public function afterDelete($event, $entity, $options) { //nouvelle norme pour les noms de documents uploadés // 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); } } }