<?php

App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email');

/**
 * MicDjJobsMsg Model
 *
 * @property Application $Application
 */
class DreamjobJobMsg extends AppModel {

    /**
     * Use database config
     *
     * @var string
     */
    public $useDbConfig = 'dreamjobMain';

    /**
     * Use table
     *
     * @var mixed False or table name
     */
    public $useTable = 'mic_dj_jobs_msg';

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'id';

    /**
     * Validation rules
     *
     * @var array
     */
    public static function sendMail($mail, $id, $data, $fromcompany = false) {
        $Email = new CakeEmail('dreamjobMain');
        $Email->to($mail);
        if ($fromcompany) {
            $Email->subject('Du hast eine neue Nachricht bei dreamJOB');
            $Email->template('job_application_msg_worker');
        } else {
            $Email->subject('Sie haben eine neue Nachricht auf dreamJOB');
            $Email->template('job_application_msg_company');
        }
        $Email->attachments(array('dak.jpg' => WWW_ROOT . DS . 'mail' . DS . 'dak.jpg'));
        $Email->viewVars(array('id' => $id, 'data' => $data));
        return $Email->send();
    }

    public function sendMessage() {
        $dataSource = $this->getDataSource();
        $dataSource->begin();
        $result = $this->save();
        if ($result) {
            $app = $this->DreamjobJobApplication->find('first', array('conditions' => array('DreamjobJobApplication.id' => $result["DreamjobJobMsg"]["application_id"])));

            if ($result["DreamjobJobMsg"]['fromcompany'])
                $result = self::sendMail($app['WorkerUser']['mail'], $result["DreamjobJobMsg"]['application_id'], array('User' => $app['WorkerUser'], 'AppUser' => $app['AppUserWorker']),true);
            else
                $result = self::sendMail($app['User']['mail'], $result["DreamjobJobMsg"]['application_id'], array('DreamjobCompany' => $app['DreamjobCompany']));
        }
        if ($result) {
            $dataSource->commit();
            return true;
        } else {
            $dataSource->rollback();
        }
        return false;
    }

    public $validate = array(
        'application_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'fromcompany' => array(
            'boolean' => array(
                'rule' => array('boolean'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'saw' => array(
            'boolean' => array(
                'rule' => array('boolean'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'text' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     *//*
      public $belongsTo = array(
      'Application' => array(
      'className' => 'DreamjobJobApplication',
      'foreignKey' => 'application_id',
      'conditions' => '',
      'fields' => '',
      'order' => ''
      )
      ); */
    public $hasOne = array(
        'DreamjobJobApplication' => array(
            'className' => 'DreamjobJobApplication',
            'foreignKey' => false,
            'conditions' => array('DreamjobJobApplication.id = DreamjobJobMsg.application_id'),
            'fields' => '',
            'order' => ''
        ),
        'Opening' => array(
            'className' => 'DreamjobJobOpening',
            'foreignKey' => false,
            'conditions' => array('Opening.id = DreamjobJobApplication.opening_id'),
            'limit' => 1
        ),
        'AppUserWorker' => array(
            'className' => 'AppUser',
            'foreignKey' => false,
            'conditions' => array('DreamjobJobApplication.worker_id = AppUserWorker.id'),
            'limit' => 1
        ),
        'Worker' => array(
            'className' => 'User',
            'foreignKey' => false,
            'conditions' => array('DreamjobJobApplication.worker_id = AppUserWorker.id  and Worker.id = AppUserWorker.user_id'),
            'limit' => 1
        ),
        'AppUserCompany' => array(
            'className' => 'AppUser',
            'foreignKey' => false,
            'conditions' => array('Opening.company_id = AppUserCompany.id'),
            'limit' => 1
        ),
        'Company' => array(
            'className' => 'User',
            'foreignKey' => false,
            'conditions' => array('Opening.company_id = AppUserCompany.id  and Company.id = AppUserWorker.user_id'),
            'limit' => 1
        ),
    );

}