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

/**
 * ListSubscriber
 * 
 * @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
 */
 
/**
 * This is the model class for table "list_subscriber".
 *
 * The followings are the available columns in table 'list_subscriber':
 * @property integer $subscriber_id
 * @property integer $list_id
 * @property string $unique_id
 * @property string $email
 * @property string $source
 * @property string $status
 * @property string $ip_address
 * @property string $date_added
 * @property string $last_updated
 *
 * The followings are the available model relations:
 * @property CampaignBounceLog[] $bounceLogs
 * @property CampaignDeliveryLog[] $deliveryLogs
 * @property CampaignTrackOpen[] $trackOpens
 * @property CampaignTrackUnsubscribe[] $trackUnsubscribes
 * @property CampaignTrackUrl[] $trackUrls
 * @property EmailBlacklist $emailBlacklist
 * @property ListFieldValue[] $fieldValues
 * @property Lists $list
 */
class ListSubscriber extends ActiveRecord
{
    const STATUS_CONFIRMED = 'confirmed';
    
    const STATUS_UNCONFIRMED = 'unconfirmed';
    
    const STATUS_UNSUBSCRIBED = 'unsubscribed';
    
    const SOURCE_WEB = 'web';
    
    const SOURCE_API = 'api';
    
    const BULK_SUBSCRIBE = 'subscribe';
    
    const BULK_UNSUBSCRIBE = 'unsubscribe';
    
    const BULK_DELETE = 'delete';
    
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{list_subscriber}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        $rules = array(
            array('status', 'in', 'range' => array_keys($this->getStatusesList())),
        );
        return CMap::mergeArray($rules, parent::rules());
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        $relations = array(
            'bounceLogs' => array(self::HAS_MANY, 'CampaignBounceLog', 'subscriber_id'),
            'deliveryLogs' => array(self::HAS_MANY, 'CampaignDeliveryLog', 'subscriber_id'),
            'trackOpens' => array(self::HAS_MANY, 'CampaignTrackOpen', 'subscriber_id'),
            'trackUnsubscribes' => array(self::HAS_MANY, 'CampaignTrackUnsubscribe', 'subscriber_id'),
            'trackUrls' => array(self::HAS_MANY, 'CampaignTrackUrl', 'subscriber_id'),
            'emailBlacklist' => array(self::HAS_ONE, 'EmailBlacklist', 'subscriber_id'),
            'fieldValues' => array(self::HAS_MANY, 'ListFieldValue', 'subscriber_id'),
            'list' => array(self::BELONGS_TO, 'Lists', 'list_id'),
        );

        return CMap::mergeArray($relations, parent::relations());
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $labels = array(
            'subscriber_id' => Yii::t('list_subscribers', 'Subscriber'),
            'list_id'       => Yii::t('list_subscribers', 'List'),
            'unique_id'     => Yii::t('list_subscribers', 'Unique id'),
            'email'         => Yii::t('list_subscribers', 'Email'),
            'source'        => Yii::t('list_subscribers', 'Source'),
            'ip_address'    => Yii::t('list_subscribers', 'Ip Address'),
        );
        
        return CMap::mergeArray($labels, parent::attributeLabels());
    }

    /**
     * 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 ListSubscriber the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    protected function beforeSave()
    {
        if ($this->isNewRecord) {
            $this->subscriber_uid = $this->generateUid();
        }
        
        return parent::beforeSave();
    }
    
    public function findByUid($subscriber_uid)
    {
        return $this->findByAttributes(array(
            'subscriber_uid' => $subscriber_uid,
        ));    
    }
    
    public function generateUid()
    {
        $unique = StringHelper::uniqid();
        $exists = $this->findByUid($unique);
        
        if (!empty($exists)) {
            return $this->generateUid();
        }
        
        return $unique;
    }
    
    public function getIsBlacklisted()
    {
        return EmailBlacklist::isBlacklisted($this->email);
    }
    
    public function addToBlacklist($reason)
    {
        $blacklist = new EmailBlacklist();
        $blacklist->subscriber_id = $this->subscriber_id;
        $blacklist->email = $this->email;
        $blacklist->reason = $reason;
        return $blacklist->save();
    }
    
    public function getUid()
    {
        return $this->subscriber_uid;
    }
    
    public function getStatusesList()
    {
        return array(
            self::STATUS_CONFIRMED      => Yii::t('list_subscribers', self::STATUS_CONFIRMED),
            self::STATUS_UNCONFIRMED    => Yii::t('list_subscribers', self::STATUS_UNCONFIRMED),
            self::STATUS_UNSUBSCRIBED   => Yii::t('list_subscribers', self::STATUS_UNSUBSCRIBED)
        );
    }
    
    public function getBulkActionsList()
    {
        return array(
            self::BULK_SUBSCRIBE    => Yii::t('list_subscribers', ucfirst(self::BULK_SUBSCRIBE)),
            self::BULK_UNSUBSCRIBE  => Yii::t('list_subscribers', ucfirst(self::BULK_UNSUBSCRIBE)),
            self::BULK_DELETE       => Yii::t('list_subscribers', ucfirst(self::BULK_DELETE))
        );
    }
}
