<?php
    
    namespace common\models;
    
    use yii\web\UploadedFile;
    use Yii;
    
    /**
     * This is the model class for table "certificate".
     * @property integer $certificate_id
     * @property string  $name
     * @property string  $link
     */
    class Certificate extends \yii\db\ActiveRecord
    {
        
        /**
         * @var UploadedFile $file
         */
        public $file;
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'certificate';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'name',
                    ],
                    'required',
                ],
                [
                    [ 'name' ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'file' ],
                    'file',
                    'extensions' => 'pdf',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'certificate_id' => Yii::t('app', 'certificate_id'),
                'name'           => Yii::t('app', 'certname'),
                'link'           => Yii::t('app', 'certlink'),
                'file'           => Yii::t('app', 'certfile'),
            ];
        }
        
        public function beforeDelete()
        {
            if(parent::beforeDelete()) {
                $this->deleteFile();
                return true;
            } else {
                return false;
            }
            
        }
        
        public function deleteFile()
        {
            $link = $this->getCertificateFile();
            if(file_exists($link)) {
                unlink($link);
            }
        }
        
        public function upload()
        {
            if($this->validate()) {
                if(!empty($this->file)) {
                    $certDir = \Yii::getAlias('@storage/certificates');
                    $this->file->saveAs($certDir . '/' . $this->file->baseName . '.' . $this->file->extension);
                    $this->link = $this->file->baseName.'.'.$this->file->extension;
                }
                return true;
            } else {
                return false;
            }
        }
        
        public function getCertificateUrl()
        {
            if(!empty( $link = $this->link )) {
                return '/storage/certificates/' . $link;
            } else {
                return false;
            }
        }
        
        public function getCertificateFile()
        {
            if(!empty( $link = $this->link )) {
                $certDir = \Yii::getAlias('@storage/certificates');
                return $certDir . '/' . $link;
            } else {
                return false;
            }
        }
    }
