<?php

class Voicemail extends PhonalisaRecord
{
    public $red;
    public $ownerExtension;
    public $from;
    public $datetime;
    public $token;

    public static function myAttributes() {

        return array(
            'red'=>'red',
            'ownerExtension'=>'ownerExtension',
            'from'=>'from',
            'datetime'=>'datetime',
            'token' => 'token'
        );
    }

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Gateway the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @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('red, token', 'safe'),
		);
	}

	/**
	 * @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(
			'ownerExtension' => __('VOICEMAIL_MODEL_OWNER_EXTENSION'),
			'from' => __('VOICEMAIL_MODEL_FROM'),
            'datetime'=>__('VOICEMAIL_MODEL_DATE_TIME'),
            'red'=>__('VOICEMAIL_MODEL_RED'),
                    'token' => __('VOICEMAIL_MODEL_TOKEN')
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CPhonalisaDataProvider the data provider that can return the models based on the search/filter conditions.
	 */        
	public function search($order='id', $playlistID=NULL)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		if ($playlistID != NULL){
			$criteria=new CDbCriteria;
			$criteria->condition = 'type=moh AND playlistID='.$playlistID;
			return new CPhonalisaDataProvider($this, array(
					'criteria'=>$criteria,
			));	
		}

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
        $criteria->compare('user_id',$this->user_id,true);
        $criteria->compare('data',$this->data,true);
		$criteria->compare('name',$this->name,true);
        $criteria->compare('descr',$this->descr,true);
        $criteria->compare('duration',$this->duration,true);
        $criteria->compare('created',$this->created,true);

		return new CPhonalisaDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function beforeSave() {     
        return parent::beforeSave();    
    }       
    
    public function afterSave() {
    	return parent::afterSave();    
    }
    
    public function beforeDelete()
    {
        if (!$this->deleteBinaryField("data", '', 0)) {
            Log::w('Could not delete file "' . $this->valueForKey('data') . '.bin"', 'phonalisa-voicemail');
        }
        return parent::beforeDelete();
    }
}