SuivisTable.php 4.18 KB
<?php
namespace App\Model\Table;

use App\Model\Entity\Suivi;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Suivis Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Materiels
 * @property \Cake\ORM\Association\HasMany $Documents
 */
class SuivisTable extends Table
{

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

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

        $this->addBehavior('Timestamp');

        $this->belongsTo('Materiels', [
            'foreignKey' => 'materiel_id',
            'joinType' => 'INNER'
        ]);
        $this->hasMany('Documents', [
            'foreignKey' => 'suivi_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
            ->integer('materiel_id');
    	
    	$validator
            ->allowEmpty('date_controle');

        $validator
            ->allowEmpty('date_prochain_controle')
            ->add('date_prochain_controle', 'checkNextDateControlIsAfterDateControl', ['rule' => 'checkNextDateControlIsAfterDateControl', 'message' => 'La date de la prochaine intervention dois être postérieure à la date de l\'intervention précédente.', 'provider' => 'table']);

        $validator
            ->allowEmpty('type_intervention')
            ->add('type_intervention', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);

        $validator
            ->notEmpty('organisme')
            ->add('organisme', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);

        $validator
            ->numeric('frequence', 'Veuillez saisir des chiffres uniquement.')
            ->allowEmpty('frequence')
            ->maxLength('frequence', 8, '8 Chiffres maximum');
        
        $validator
            ->allowEmpty('commentaire')
            ->add('commentaire', 'valid', ['rule' => ['check_string_with_some_special_cars'], 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);


        return $validator;
    }

    
    // autoriser les caracteres habituels standards pour un ou plusieurs MOTs
    // accents + - _ / () . , \s (=space)
    private $string = "a-zA-Z0-9éèàùâêôîôûç%().,\/\s\+\-_";
    
    public function check_string($check) {
    	return (bool) preg_match('/^['.$this->string.']*$/', $check);
    }
    
    // autoriser les caracteres spéciaux (pour une PHRASE ou paragraphe) :
    // check_string PLUS ces symboles ====> & * > < ? % ! : , " '
    public function check_string_with_some_special_cars($check) {
    	return (bool) preg_match('/^['.$this->string.'?%!:,&*><\-\+\="\''.']*$/', $check);
    }
    
    function checkNextDateControlIsAfterDateControl() {
    	/*
    	$controle = $this->data [$this->name] ['date_controle'];
    	if (empty($controle)) return true;
    	$prochainControle = $this->data [$this->name] ['date_prochain_controle'];
    	$controle = explode("-", $controle);
    	$prochainControle = explode("-", $prochainControle);
    	$controle = $controle[2].$controle[1].$controle[0];
    	$prochainControle = $prochainControle[2].$prochainControle[1].$prochainControle[0];
    	if ($controle > $prochainControle) {
    		return false;
    	}
    	return true;
    	*/
    	return true;
    }
    
    /**
     * 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)
    {
        $rules->add($rules->existsIn(['materiel_id'], 'Materiels'));
        return $rules;
    }
}