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

use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Suivis Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Unites
 * @property \Cake\ORM\Association\BelongsTo $Materiels
 * @property \Cake\ORM\Association\BelongsTo $TypeSuivis
 * @property \Cake\ORM\Association\BelongsTo $GroupesThematiques
 * @property \Cake\ORM\Association\BelongsTo $GroupesMetiers
 * @property \Cake\ORM\Association\HasMany $Documents
 * @property \Cake\ORM\Association\HasMany $Fichemetrologiques
 */
class SuivisTable extends AppTable
{

    /**
     * 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->belongsTo('TypeSuivis', [
            'foreignKey' => 'type_suivi_id'
        ]);
        
        $this->belongsTo('GroupesMetiers', [
            'foreignKey' => 'groupes_metier_id'
        ]);
        
        $this->belongsTo('GroupesThematiques', [
            'foreignKey' => 'groupes_thematique_id'
        ]);
        
        $this->hasMany('Documents', [
            'foreignKey' => 'suivi_id'
        ]);
        $this->belongsTo('Unites', [
            'foreignKey' => 'unite_id'
        ]);
        $this->hasMany('Fichemetrologiques', [
            'foreignKey' => 'suivi_id'
        ]);
        /*
         * EP remarque 9/6/17 : il manque peut-etre ceci pour formule_id, sans doute en cours pour le LATMOS ?
         * $this->belongsTo('Table?', [
         * 'foreignKey' => 'formule_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');
        
        $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('type_frequence');
        
        $validator->allowEmpty('panne_resolu');
        
        $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;
    }

    /**
     * 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)
    {
        $checkNextDateControlIsAfterDateControl = function ($entity) {
            
            $controle = $entity->date_controle;
            $p_controle = $entity->date_prochain_controle;
            if (empty($controle) || empty($p_controle))
                return true;
            $prochainControle = $p_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;
        };
        
        $rules->add($checkNextDateControlIsAfterDateControl, [
            'errorField' => 'date_prochain_controle',
            'message' => 'La date de la prochaine intervention dois être postérieure à la date de l\'intervention précédente.'
        ]);
        
        $rules->add($rules->existsIn([
            'materiel_id'
        ], 'Materiels'));
        $rules->add($rules->existsIn([
            'type_suivi_id'
        ], 'TypeSuivis'));
        $rules->add($rules->existsIn([
            'groupes_thematique_id'
        ], 'GroupesThematiques'));
        $rules->add($rules->existsIn([
            'groupes_metier_id'
        ], 'GroupesMetiers'));
        return $rules;
    }

    public function beforeSave($event, $entity, $options)
    {
        if (empty($entity->get('date_controle'))) {
            $entity->set('date_controle', null);
        }
        
        if (empty($entity->get('date_prochain_controle'))) {
            $entity->set('date_prochain_controle', null);
        }
        
        return true;
    }
}