<?php

/**
 * This is the model class for "OneTimeLink".
 *
 * @property string $confirmed_id
 * @property string $time
 * @property string $type
 *
 */
class OneTimeLink extends PhonalisaRecord
{

    public $user_id;
    public $time;
    public $type;

    const TYPE_PASSWORD_RECOVERY = 0;

    public static function myAttributes()
    {

        return array(
            "user_id" => "user_id",
            "time" => "time",
            "type" => "type",
        );
    }

    /**
     * @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('user_id, time', 'required'),
            array('type', 'numerical', 'integerOnly' => true),
            array('user_id, time', '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(
            'user_id' => __('user_id'),
            'time' => __('time'),
            'type' => __('type'),
        );
    }

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

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

    public function deleteOldOneTimeLinks()
    {
        $oldOTL = $this->find(array(), 'time');
        $currentTimeStamp = time();
        foreach ($oldOTL as $oldie) {
            $date = DateTime::createFromFormat('Y-m-d H:i', $oldie->time);
            $createdTimeStamp = date_timestamp_get($date);
            $timeDifference = $currentTimeStamp - $createdTimeStamp;
            if (date('i', $timeDifference > Yii::app()->params['oneTimeLinkExpireTime'])) {
                $oldie->delete();
            }
        }
    }

    public function getLink($asHtml = true)
    {
        $link = null;

        if ($this->type == OneTimeLink::TYPE_PASSWORD_RECOVERY) {
            $path = Yii::app()->getBaseUrl(true) . url('/site/resetPassword', array('id' => $this->id));
            if ($asHtml) {
                $link = l($path, $path);
            } else {
                $link = $path;
            }
        }

        return $link;
    }

    public function beforeSave()
    {
        parent::beforeSave();
        if ($this->isNewRecord) {
            $this->time = date('Y-m-d H:i');
        }
        return true;
    }
}
