<?php

class RingtoneOwner
{

    const SYSTEM = 'system';

}

class Ringtone extends PhonalisaRecord
{

    public $fileName;
    public $userId;
    public $data;

    public static function myAttributes()
    {
        return array(
            'data' => 'data',
            'fileName' => 'fileName',
            'userId' => 'userId'
        );
    }

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

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

    /**
     * @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(
                'data',
                'ext.PhonalisaValidators.fileValidator',
                'useFlashError' => true,
                'mimeTypes' => 'audio/mpeg, audio/x-wav, audio/wav, audio/ogg, application/octet-stream',
                'wrongMimeType' => __('GLOBAL_FILE_UPLOAD_WRONG_MIME_TYPE_ERROR_MESSAGE_HTEK'),
                'on' => 'htek'
            ),
            array(
                'data',
                'ext.PhonalisaValidators.fileValidator',
                'useFlashError' => true,
                'mimeTypes' => 'audio/mpeg, audio/x-wav, audio/wav, audio/ogg',
                'wrongMimeType' => __('GLOBAL_FILE_UPLOAD_WRONG_MIME_TYPE_ERROR_MESSAGE'),
                'except' => 'htek'
            ),
            array('fileName, userId', '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(
            'data' => __('RINGTONE_MODEL_DATA'),
            'fileName' => __('RINGTONE_MODEL_FILE_NAME'),
            'userId' => __('RINGTONE_MODEL_USER_ID'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($order = 'fileName')
    {
        $criteria = new CDbCriteria;
        $criteria->condition = 'userId=system';
        $filter = $this->getFilter($criteria, $order);
        $filter['pagination'] = array(
            'pageSize' => getGridPageSize('ringtone-grid'),
        );

        if ($filter != null) {
            return new CPhonalisaDataProvider($this, $filter
            );
        }
    }

    public function userRingtonesSearch($order = 'id')
    {
        $data = $this->findAllByAttributes(array('userId' => app()->user->sudoUserInstance->id));
        return new CArrayDataprovider($data);
    }

    public function afterSave()
    {
        if (!empty($_FILES['Ringtone']['tmp_name']['data'])) {
            $file = CUploadedFile::getInstance($this, 'data');
            if ($file->tempName != '') {
                $this->saveFileToFieldByPath("data", $file->tempName);
            }
        }
        return parent::afterSave();
    }
}
