<?php

/**
 * This is the model class for table "calllist".
 *
 * The followings are the available columns in table 'calllist':
 * @property integer $id
 * @property string $calluuid
 * @property string 'duration',$timestamp
 * @property string $user
 * @property string $remotecid
 * @property integer $duration
 * @property integer $state
 * @property integer $type
 * @property string $meta
 * @property integer $hidden
 * @property string $comment
 */
class Calllist extends PhonalisaRecord
{

    public $id;
    public $calluuid;
    public $timestamp;
    public $user;
    public $remotecid;
    public $duration;
    public $state;
    public $type;
    public $meta;
    public $hidden;
    public $comment;
    
     public static function myAttributes() {
        return array(
            'calluuid'=>'calluuid',
            'timestamp'=>'timestamp',
            'user'=>'user',
            'remotecid'=>'remotecid',
            'duration'=>'duration',
            'state'=>'state',
            'type'=>'type',
            'meta'=>'meta',
            'hidden'=>'hidden',
            'comment'=>'comment',
        );
    }


	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Calllist 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 'calllist';
	}

	/**
	 * @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('calluuid, user, remotecid, meta, comment', 'required'),
			array('duration, state, type, hidden', 'numerical', 'integerOnly'=>true),
			array('calluuid', 'length', 'max'=>100),
			array('user', 'length', 'max'=>20),
			array('remotecid, meta, comment', 'length', 'max'=>255),
			array('timestamp', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, calluuid, timestamp, user, remotecid, duration, state, type, meta, hidden, comment', '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' => __('ID'),
			'calluuid' => __('Calluuid'),
			'timestamp' => __('Timestamp'),
			'user' => __('User'),
			'remotecid' => __('Remotecid'),
			'duration' => __('Duration'),
			'state' => __('State'),
			'type' => __('Type'),
			'meta' => __('Meta'),
			'hidden' => __('Hidden'),
			'comment' => __('Comment'),
		);
	}

	/**
	 * 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()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('calluuid',$this->calluuid,true);
		$criteria->compare('timestamp',$this->timestamp,true);
		$criteria->compare('user',$this->user,true);
		$criteria->compare('remotecid',$this->remotecid,true);
		$criteria->compare('duration',$this->duration);
		$criteria->compare('state',$this->state);
		$criteria->compare('type',$this->type);
		$criteria->compare('meta',$this->meta,true);
		$criteria->compare('hidden',$this->hidden);
		$criteria->compare('comment',$this->comment,true);

		return new CPhonalisaDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        function renderState(PhonalisaRecord $data,$row) {
                $path=Yii::app()->request->baseUrl;
                if ($data->type == 1) {
                        switch ($data->state) {
                                case 4:
                                echo  CHtml::image($path.'/img/16x16/calllist/forwarded.png','eingehend Umgeleitet auf '.$data->meta); 
                                break;
                                case 8:
                                echo CHtml::image($path.'/img/16x16/calllist/in_success.png','eingehend Angenommen');
                                break;
                                case 7:
                                echo CHtml::image($path.'/img/16x16/calllist/in_missed.png','eingehend Verpass');
                                break;
                                case 9:
                                echo CHtml::image($path.'/img/16x16/calllist/change.png','Übernommen Durch: '.$data->meta);
                                break;
                                }
                        } 
                        else {
                                switch ($data->state) {
                                case 4:
                                echo CHtml::image($path.'/img/16x16/calllist/forwarded.png','ausgehend dann Umgeleitet auf '.$data->meta);
                                break;
                                case 8:
                                echo CHtml::image($path.'/img/16x16/calllist/out_success.png','ausgehend Angenommen');
                                break;
                                case 7:
                                echo CHtml::image($path.'/img/16x16/calllist/out_missed.png','ausgehend Verpasst');
                                break;
                                }
                        
                }
          
        }

        public function renderDateTime(PhonalisaRecord $data,$row) {

                $out = preg_split("/\./", $data->timestamp);
                if ($out[0] != "") {
                        return date("Y-m-d H:i:s", (int)$out[0]);
                } else {
                        return "n/a";
                }
        }
}