<?php

/**
 * This is the model class for table "fax".
 *
 * The followings are the available columns in table 'fax':
 * @property integer $id
 * @property integer $user_id
 * @property string $filename
 * @property integer $date
 * @property string $sender
 * @property integer $duration
 * @property integer $size
 * @property integer $pages
 * @property string $resolution
 * @property integer $state // (0) = inbox, (1) = outbox, (2) = sent
 * @property integer $direction // (0) = inbound, (1) = outbound 
 * @property string $errortext
 */
class Fax extends PhonalisaRecord
{
	public $number;
	public $file;
	public $faxfilecontent;	 //NON Base64 File content, can be multiple format!
	public $id;
	public $user_id;
	public $filename;
	public $date;
	public $sender;
	public $duration;
	public $size;
	public $pages;
	public $resolution;
	public $state; // (0) = inbox, (1) = outbox, (2) = sent
	public $direction; // (0) = inbound, (1) = outbound 
	public $errortext;
	
	public static function myAttributes() {
        return array(
            'user_id'=>'user_id',
            'filename'=>'filename',
            'date'=>'date',
            'sender'=>'sender',
            'duration'=>'duration',
            'size'=>'size',
            'pages'=>'pages',
            'resolution'=>'resolution',
            'state'=>'state',
            'direction'=>'direction',
            'errortext'=>'errortext',
        );
    }
	
	
	
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Inbox 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 'fax';
	}

	/**
	 * @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('date, duration, size, pages, state, direction', 'numerical', 'integerOnly'=>true),
			array('filename, date, sender, duration, resolution', 'safe'),
			array('file', 'file', 'types'=>'jpg, gif, png, tiff, pdf, txt'),
			array('user_id,', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, filename, date, sender, duration, size, pages, resolution', 'safe', 'on'=>'search'),
		);
	}
	
	public function renderDateTime(PhonalisaRecord $data,$row) {

		$out = preg_split("/\./", $data->date);
		if ($out[0] != "") {
			return PhonalisaDate::userDateTime((int)$out[0]);
		} else {
			return __("n/a");
		}
		
	}
	
	public function renderDuration(PhonalisaRecord $data,$row) {

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

	/**
	 * @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'),
			'user_id' => __('User ID'),
			'filename' => __('Filename'),
			'date' => __('Date'),
			'sender' => __('Sender'),
			'duration' => __('Duration'),
			'size' => __('Size'),
			'pages' => __('Pages'),
			'resolution' => __('Resolution'),
			'faxfilecontent' => __('Filecontent')
			
		);
	}

	/**
	 * 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('user_id',$this->user_id);
		$criteria->compare('filename',$this->filename,true);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('sender',$this->sender,true);
		$criteria->compare('duration',$this->duration,true);
		$criteria->compare('size',$this->size);
		$criteria->compare('pages',$this->pages);
		$criteria->compare('resolution',$this->resolution,true);

		return new CPhonalisaDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'date DESC',
				),
		));
	}
	
	public function renderFaxFileContent(PhonalisaRecord $data,$row) {
		return CHtml::image("/faxInbox/default/faxshow/id/".$data->id);
	}
	
	public function getFaxFileContentPng() {
		
		if ($this->faxfilecontent == "") {
			$this->loadFaxFileContent();
		}
		return $this->faxfilecontent;
		
	}
	
	
		
	public function loadFaxFileContent() {
		$data = CoreRESTfulBridge::getFaxData($this);
		if ($data === false) {
			return;
		}

		if ($data->result == "ok") {
			$this->faxfilecontent = base64_decode($data->base64data);			
		}
		
	}
	
	
}