<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * DeliveryServerSmtp
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class DeliveryServerSmtp extends DeliveryServer
{
    protected $serverType = 'smtp';
    
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        $rules = array(
            array('password, port, timeout', 'required'),
        );
        
        return CMap::mergeArray($rules, parent::rules());
    }
    
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return DeliveryServer the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    public function sendEmail(array $params = array())
    {
        Yii::app()->hooks->doAction('delivery_server_before_send_email', $this, $params);
        
        $this->markHourlyUsage();
        $sent = $this->getMailer()->send($this->getParamsArray($params));
        
        Yii::app()->hooks->doAction('delivery_server_after_send_email', $this, $params, $sent);
        
        return $sent;
    }
    
    public function attributePlaceholders()
    {
        $placeholders = array(
            'hostname'    => Yii::t('servers', 'smtp.your-server.com'),
            'username'    => Yii::t('servers', 'you@your-server.com'),
            'password'    => Yii::t('servers', 'your smtp account password')
        );
        
        return CMap::mergeArray($placeholders, parent::attributePlaceholders());
    }
    
}
