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

/**
 * OptionCampaignOptions
 * 
 * @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.3.3
 */
 
class OptionCampaignOptions extends FormModel
{
    const TEXT_YES = 'yes';
    
    const TEXT_NO = 'no';
    
    private $_categoryName = 'system.campaign.campaign_options';
    
    public $customer_select_delivery_servers = 'no';

    public function rules()
    {
        $rules = array(
            array('customer_select_delivery_servers', 'required'),
            array('customer_select_delivery_servers', 'in', 'range' => array_keys($this->getYesNoOptions())),
        );
        
        return CMap::mergeArray($rules, parent::rules());    
    }
    
    public function attributeLabels()
    {
        $labels = array(
            'customer_select_delivery_servers'   => Yii::t('settings', 'Customers can select delivery servers'),
        );
        
        return CMap::mergeArray($labels, parent::attributeLabels());    
    }
    
    public function attributePlaceholders()
    {
        $placeholders = array(
            'customer_select_delivery_servers' => '',
        );
        
        return CMap::mergeArray($placeholders, parent::attributePlaceholders());
    }
    
    public function attributeHelpTexts()
    {
        $texts = array(
            'customer_select_delivery_servers' => Yii::t('settings', 'Wheather the customers are able to select what delivery servers to use'),

        );
        
        return CMap::mergeArray($texts, parent::attributeHelpTexts());
    }
    
    protected function afterConstruct()
    {
        parent::afterConstruct();
        foreach ($this->getAttributes() as $attributeName => $attributeValue) {
            $this->$attributeName = Yii::app()->options->get($this->_categoryName . '.' . $attributeName, $this->$attributeName);
        }
    }
    
    protected function beforeValidate()
    {
        return parent::beforeValidate();
    }
    
    public function save()
    {
        if (!$this->validate()) {
            return false;
        }
        
        foreach ($this->getAttributes() as $attributeName => $attributeValue) {
            Yii::app()->options->set($this->_categoryName . '.' . $attributeName, $attributeValue);
        }
        
        return true;
    }
    
    public function getYesNoOptions()
    {
        return array(
            self::TEXT_NO  => Yii::t('app', 'No'),
            self::TEXT_YES => Yii::t('app', 'Yes'),
        );
    }
}
