guild/common/models/Template.php

127 lines
3.3 KiB
PHP
Raw Normal View History

2021-12-27 16:50:17 +03:00
<?php
namespace common\models;
use Yii;
2022-01-07 15:04:11 +03:00
use yii\base\InvalidConfigException;
2021-12-27 16:50:17 +03:00
use yii\behaviors\TimestampBehavior;
2022-01-07 15:04:11 +03:00
use yii\db\ActiveQuery;
2021-12-27 16:50:17 +03:00
use yii\db\Expression;
/**
* This is the model class for table "template".
*
* @property int $id
* @property string $title
* @property string $created_at
* @property string $updated_at
2022-01-05 17:11:31 +03:00
* @property string $template_file_name
* @property int $document_type
2021-12-27 16:50:17 +03:00
*
* @property Document[] $documents
* @property TemplateDocumentField[] $templateDocumentFields
*/
class Template extends \yii\db\ActiveRecord
{
2022-01-10 15:22:51 +03:00
const SCENARIO_UPDATE_TITLE = 'update';
const SCENARIO_UPDATE_FILE = 'update';
public $template;
2021-12-27 16:50:17 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'template';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['created_at', 'updated_at'], 'safe'],
2022-01-05 17:11:31 +03:00
[['title'], 'unique'],
[['document_type'], 'integer'],
[['template_file_name', 'title', 'document_type'], 'required'],
2022-01-10 15:22:51 +03:00
[['template'], 'required', 'message'=>'Укажите путь к файлу'],
[['template'], 'file', 'maxSize' => '100000'],
[['template'], 'file', 'skipOnEmpty' => true, 'extensions' => 'docx'],
2022-01-05 17:11:31 +03:00
[['title', 'template_file_name'], 'string', 'max' => 255],
2021-12-27 16:50:17 +03:00
];
}
2022-01-10 15:22:51 +03:00
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[static::SCENARIO_UPDATE_TITLE] = ['created_at', 'updated_at', 'title', 'template_file_name'];
$scenarios[static::SCENARIO_UPDATE_FILE] = ['template'];
return $scenarios;
}
2021-12-27 16:50:17 +03:00
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Название',
'created_at' => 'Дата создания',
'updated_at' => 'Дата изменения',
2022-01-05 17:11:31 +03:00
'template_file_name' => 'Файл шаблона',
'document_type' => 'Тип документа',
2021-12-27 16:50:17 +03:00
];
}
public function beforeDelete()
{
foreach ($this->templateDocumentFields as $templateDocumentField){
$templateDocumentField->delete();
}
2022-01-05 17:11:31 +03:00
if (!empty($this->template_file_name)) {
2022-01-10 16:05:51 +03:00
$template_path = Yii::getAlias('@templates') . '/' . $this->template_file_name;
2022-01-05 17:11:31 +03:00
if(file_exists($template_path)) {
unlink($template_path);
}
}
2021-12-27 16:50:17 +03:00
return parent::beforeDelete();
}
/**
2022-01-07 15:04:11 +03:00
* @return ActiveQuery
2021-12-27 16:50:17 +03:00
*/
public function getDocuments()
{
return $this->hasMany(Document::className(), ['template_id' => 'id']);
}
/**
2022-01-07 15:04:11 +03:00
* @return ActiveQuery
2021-12-27 16:50:17 +03:00
*/
public function getTemplateDocumentFields()
{
return $this->hasMany(TemplateDocumentField::className(), ['template_id' => 'id']);
}
2022-01-07 15:04:11 +03:00
public function getTitle()
{
return $this->title;
}
2021-12-27 16:50:17 +03:00
}