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

/**
 * OptionExporter
 * 
 * @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 OptionExporter extends FormModel
{
    private $_categoryName = 'system.exporter';

    public $records_per_file = 500;
    
    public $process_at_once = 50; // from a batch of 500, how many to process at once
    
    public $pause = 1; // pause between the batches
    
    public $memory_limit;
    
    public function rules()
    {
        $rules = array(
            array('records_per_file, process_at_once, pause', 'required'),
            array('records_per_file, process_at_once, pause', 'numerical', 'integerOnly' => true),
            array('records_per_file', 'numerical', 'min' => 50, 'max' => 100000),
            array('process_at_once', 'numerical', 'min' => 5, 'max' => 10000),
            array('process_at_once', 'compare', 'compareAttribute' => 'records_per_file', 'operator' => '<='),
            array('pause', 'numerical', 'min' => 0, 'max' => 60),
            array('memory_limit', 'in', 'range' => array_keys($this->getMemoryLimitOptions())),
        );
        
        return CMap::mergeArray($rules, parent::rules());    
    }
    
    public function attributeLabels()
    {
        $labels = array(
            'records_per_file'  => Yii::t('settings', 'Records per file'),
            'process_at_once'   => Yii::t('settings', 'Process at once'),
            'pause'             => Yii::t('settings', 'Pause'),
            'memory_limit'      => Yii::t('settings', 'Memory limit'),
        );
        
        return CMap::mergeArray($labels, parent::attributeLabels());    
    }
    
    public function attributePlaceholders()
    {
        $placeholders = array(
            'records_per_file'  => null,
            'process_at_once'   => null,
            'pause'             => null,
            'memory_limit'      => null,
        );
        
        return CMap::mergeArray($placeholders, parent::attributePlaceholders());
    }
    
    public function attributeHelpTexts()
    {
        $texts = array(
            'records_per_file'  => Yii::t('settings', 'Maximum number of records in a single export file (please note, the export is a multi-file archive).'),
            'process_at_once'   => Yii::t('settings', 'How many subscribers to process at once for each file.'),
            'pause'             => Yii::t('settings', 'How many seconds the script should "sleep" after each batch of subscribers.'),
            'memory_limit'      => Yii::t('settings', 'The maximum memory amount the export process is allowed to use while processing one batch of subscribers.'),
        );
        
        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);
        }
    }
    
    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 getMemoryLimitOptions()
    {
        return array(
            ''      => Yii::t('settings', 'System default'),
            '64M'   => Yii::t('settings', '{n} Megabytes', 64),
            '128M'  => Yii::t('settings', '{n} Megabytes', 128),
            '256M'  => Yii::t('settings', '{n} Megabytes', 256),
            '512M'  => Yii::t('settings', '{n} Megabytes', 512),
            '768M'  => Yii::t('settings', '{n} Megabytes', 768),
            '1G'    => Yii::t('settings', '{n} Gigabyte|{n} Gigabytes', 1),
            '2G'    => Yii::t('settings', '{n} Gigabyte|{n} Gigabytes', 2),
            '3G'    => Yii::t('settings', '{n} Gigabyte|{n} Gigabytes', 3),
            '4G'    => Yii::t('settings', '{n} Gigabyte|{n} Gigabytes', 4),
            '5G'    => Yii::t('settings', '{n} Gigabyte|{n} Gigabytes', 5),
        );
    }
    
    public function getFileSizeOptions()
    {
        $options = array();
        $size = 1024 * 1024 * 1;
        $options[$size] = Yii::t('settings', '{n} Megabyte|{n} Megabytes', 1);
        for ($i = 2; $i <= 5; ++$i) {
            $size = 1024 * 1024 * $i;
            $options[$size] = Yii::t('settings', '{n} Megabyte|{n} Megabytes', $i);
        }
        for ($i = 10; $i <= 50; ++$i) {
            if ($i % 5 == 0) {
                $size = 1024 * 1024 * $i;
                $options[$size] = Yii::t('settings', '{n} Megabyte|{n} Megabytes', $i);
            }
        }
        $size = 1024 * 1024 * 100;
        $options[$size] = Yii::t('settings', '{n} Megabyte|{n} Megabytes', 100);
        return $options;
    }
}
