<?php

/**
 * This is the model class for table "newsletter_subscriber".
 *
 * The followings are the available columns in table 'newsletter_subscriber':
 * @property integer $id
 * @property string $email
 * @property integer $create_datetime
 * @property integer $have_previous_experience
 * @property string $previous_experience_role
 * @property string $phonenumber
 */
class NewsletterSubscriber extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'newsletter_subscriber';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email', 'required'),
            array('create_datetime, have_previous_experience', 'numerical', 'integerOnly' => true),
            array('email', 'email'),
            array('email', 'unique'),
            array('email', 'length', 'max' => 50),
            array('previous_experience_role', 'length', 'max' => 300),
            array('phonenumber', 'length', 'max' => 45),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, email, create_datetime, have_previous_experience, previous_experience_role, phonenumber', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'email' => Yii::t('admin', 'Email'),
            'create_datetime' => Yii::t('admin', 'Create Datetime'),
            'have_previous_experience' => Yii::t('admin', 'Do you have any previous experience in volunteering field?'),
            'previous_experience_role' => Yii::t('admin', 'If Yes, can you tell us more?'),
            'phonenumber' => Yii::t('admin', 'Phone Number'),
            'fullname' => Yii::t('admin', 'Full Name'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('create_datetime', $this->create_datetime);
        $criteria->compare('have_previous_experience', $this->have_previous_experience);
        $criteria->compare('previous_experience_role', $this->previous_experience_role, true);
        $criteria->compare('phonenumber', $this->phonenumber, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function beforeValidate() {
        if ($this->isNewRecord) {
            // set the create date, last updated date and the user doing the creating  
            $this->create_datetime = time();
        }
        return parent::beforeValidate();
    }

    /**
     * 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 NewsletterSubscriber the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
