Current Path : /home/vmanager/www/common/models/ |
Linux 9dbcd5f6333d 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 |
Current File : /home/vmanager/www/common/models/CompaniesDocumentsFiles.php |
<?php namespace common\models; use Yii; use common\models\types\CompanyDocumentFileType; use yii\behaviors\TimestampBehavior; use yii\db\Expression; /** * This is the model class for table "companies_documents_files". * * @property int $id_file * @property int $id_document * @property string $filename * @property string $file_label * @property string $file_type * @property string $created_at * * @property CompaniesDocuments $document * @property CompaniesDocumentsTasksHasCompaniesDocumentsFiles[] $relationsWithTasks * @property CompaniesDocumentsTasks[] $tasks */ class CompaniesDocumentsFiles extends \yii\db\ActiveRecord { public $file; /** * {@inheritdoc} */ public static function tableName() { return 'companies_documents_files'; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'value' => new Expression('LOCALTIMESTAMP'), 'updatedAtAttribute' => false, ], ]; } /** * @inheritdoc */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios['create'] = ['id_document', 'filename', 'file_label', 'file', 'file_type']; return $scenarios; } /** * {@inheritdoc} */ public function rules() { return [ ['id_document', 'required'], ['id_document', 'number', 'integerOnly' => true, 'min' => 1], ['id_document', 'exist', 'targetClass' => CompaniesDocuments::className(), 'targetAttribute' => 'id_document'], ['filename', 'required'], ['filename', 'string', 'min' => 5, 'max' => 255], ['file', 'file', 'extensions' => ['doc', 'docx', 'odt', 'wpd', 'wps', 'rtf', 'txt', 'csv', 'uot', 'psw', 'xls', 'xlt', 'xlsx', 'xltx', 'uos', 'pxl', 'ppt', 'pps', 'pptx', 'uop', 'pdf', 'jpg', 'png', 'webp', 'gif', 'psd', 'odf', 'cdr', 'zip', 'rar', '7z', 'iso', 'tar', 'bz2', 'gz', 'apk'], 'maxFiles' => 1, 'maxSize' => 5242880, 'skipOnEmpty' => false], ['file_label', 'default'], ['file_label', 'string', 'min' => 5, 'max' => 255], ['file_type', 'default', 'value' => CompanyDocumentFileType::CONTRACT], ['file_type', 'required'], ['file_type', 'in', 'range' => CompanyDocumentFileType::$values], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id_file' => Yii::t('common-models', 'ID pliku'), 'id_document' => Yii::t('common-models', 'ID dokumentu'), 'filename' => Yii::t('common-models', 'Plik'), 'file_label' => Yii::t('common-models', 'Etykieta'), 'file_type' => Yii::t('common-models', 'Typ'), 'created_at' => Yii::t('common-models', 'Data utworzenia'), 'file' => Yii::t('common-models', 'Plik'), ]; } /** * @inheritdoc */ public function afterDelete() { parent::afterDelete(); if(!empty($this->filename)) { $file = Yii::getAlias('@companyDocumentsRealPath').'/'.$this->id_document.'/'.$this->filename; if(file_exists($file)) { @unlink($file); } } } public function getFilePath() { return Yii::getAlias('@companyDocumentsRealPath').'/'.$this->id_document.'/'.$this->filename; } public static function createFileName($baseName, $extension) { $baseName = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $baseName)); $baseName = preg_replace('/\s+/i', '-', $baseName); $baseName = preg_replace('/[^a-zA-Z0-9_\-]/i', '', $baseName); return $baseName.'-'.str_replace(' ', '', substr(microtime(), 2)).'.'. strtolower($extension); } public function loadFile() { $oldFilePath = null; if($this->isNewRecord) { $this->file = \yii\web\UploadedFile::getInstance($this, 'file'); if($this->file) { $this->filename = self::createFileName($this->file->baseName, $this->file->extension); } } return $oldFilePath; } public function saveFile() { $newFilePath = null; if($this->file) { $dir = Yii::getAlias('@companyDocumentsRealPath').'/'.$this->id_document.'/'; if(!file_exists($dir)) { mkdir($dir, 0777, true); } $newFilePath = $dir.$this->filename; if(!$this->file->saveAs($newFilePath)) { return false; } } return $newFilePath; } /** * @return \yii\db\ActiveQuery */ public function getDocument() { return $this->hasOne(CompaniesDocuments::className(), ['id_document' => 'id_document']); } /** * @return \yii\db\ActiveQuery */ public function getRelationsWithTasks() { return $this->hasMany(CompaniesDocumentsTasksHasCompaniesDocumentsFiles::className(), ['id_file' => 'id_file'])->inverseOf('file'); } /** * @return \yii\db\ActiveQuery */ public function getTasks() { return $this->hasMany(CompaniesDocumentsTasks::className(), ['id_task' => 'id_task'])->via('relationsWithTasks'); } }