<?php
namespace App\Model\Table;

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

/**
 * Imports Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Schooltypes
 * @property \Cake\ORM\Association\BelongsTo $Examinations
 *
 * @method \App\Model\Entity\Import get($primaryKey, $options = [])
 * @method \App\Model\Entity\Import newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Import[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Import|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Import patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Import[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Import findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class ImportsTable extends Table
{

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

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

        $this->addBehavior('Timestamp');

        $this->belongsTo('Schooltypes', [
            'foreignKey' => 'schooltype_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Examinations', [
            'foreignKey' => 'examination_id',
            'joinType' => 'INNER'
        ]);
    }

    /**
     * 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
            ->requirePresence('title', 'create')
            ->notEmpty('title');

        $validator
            ->requirePresence('filename', 'create')
            ->allowEmpty('filename');

        $validator
            ->requirePresence('tablename', 'create')
            ->notEmpty('tablename');

        $validator
            ->requirePresence('field_separator', 'create')
            ->notEmpty('field_separator');

        $validator
            ->requirePresence('mapping', 'create')
            ->notEmpty('mapping');

        $validator
            ->integer('skip')
            ->requirePresence('skip', 'create')
            ->notEmpty('skip');

        $validator
            ->boolean('clear')
            ->requirePresence('clear', 'create')
            ->notEmpty('clear');

        $validator
            ->boolean('update_table')
            ->requirePresence('update_table', 'create')
            ->notEmpty('update_table');

        $validator
            ->boolean('overwrite')
            ->requirePresence('overwrite', 'create')
            ->notEmpty('overwrite');

        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)
    {
        $rules->add($rules->existsIn(['schooltype_id'], 'Schooltypes'));
        $rules->add($rules->existsIn(['examination_id'], 'Examinations'));

        return $rules;
    }
}
