completion of the document module
This commit is contained in:
@ -69,11 +69,4 @@ class DocumentFieldValue extends \yii\db\ActiveRecord
|
||||
{
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'field_id']);
|
||||
}
|
||||
|
||||
public function getFieldsArray()
|
||||
{
|
||||
return '44';
|
||||
// self::find()->select(['value', 'document_field.title', 'document_field.template'])
|
||||
// ->joinWith('field')->asArray()->all();
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,10 @@ use yii\db\Expression;
|
||||
*/
|
||||
class Template extends \yii\db\ActiveRecord
|
||||
{
|
||||
// public $template;
|
||||
const SCENARIO_UPDATE_TITLE = 'update';
|
||||
const SCENARIO_UPDATE_FILE = 'update';
|
||||
|
||||
public $template;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -52,13 +55,21 @@ class Template extends \yii\db\ActiveRecord
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['title'], 'unique'],
|
||||
[['template_file_name', 'title'], 'required'],
|
||||
// [['template'], 'required', 'message'=>'Укажите путь к файлу'],
|
||||
// [['template'], 'file', 'maxSize' => '10000'],
|
||||
// [['template'], 'file', 'skipOnEmpty' => false, 'extensions' => 'doc, docx, txt'],
|
||||
[['template'], 'required', 'message'=>'Укажите путь к файлу'],
|
||||
[['template'], 'file', 'maxSize' => '100000'],
|
||||
[['template'], 'file', 'skipOnEmpty' => true, 'extensions' => 'docx'],
|
||||
[['title', 'template_file_name'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace common\services;
|
||||
|
||||
|
||||
use common\models\Document;
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
@ -10,34 +11,64 @@ use Yii;
|
||||
|
||||
class DocumentService
|
||||
{
|
||||
private $file_title;
|
||||
private $model;
|
||||
private $document;
|
||||
private $template_title;
|
||||
private [] $fields;
|
||||
|
||||
private $file_title;
|
||||
private $documentFieldValuesArr;
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
public function __construct($file_title, $template_name, [] $fields)
|
||||
public function __construct($modelID)
|
||||
{
|
||||
$this->file_title = $file_title . 'docx';
|
||||
$this->document = new TemplateProcessor(Yii::getAlias('@templates') . "/$template_name");
|
||||
$this->model = Document::findOne($modelID);
|
||||
|
||||
$this->initDocument();
|
||||
}
|
||||
|
||||
public function setFields($fields )
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
private function initDocument()
|
||||
{
|
||||
foreach ($fields as $field) {
|
||||
$this->document->setValue('FIO', '8888888888' );
|
||||
$this->file_title = $this->model->title . '.docx';
|
||||
|
||||
$template_title = $this->model->template->template_file_name;
|
||||
$this->document = new TemplateProcessor(
|
||||
Yii::getAlias('@templates') . "/$template_title");
|
||||
|
||||
$this->documentFieldValuesArr = $this->model->documentFieldValues;
|
||||
}
|
||||
|
||||
public function setFields()
|
||||
{
|
||||
foreach ($this->documentFieldValuesArr as $docFieldValue) {
|
||||
$this->document->setValue(
|
||||
$docFieldValue->field->field_template,
|
||||
$docFieldValue->value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
public function downloadDocument()
|
||||
{
|
||||
$this->document->saveAs($this->file_title);
|
||||
}
|
||||
|
||||
public function creat3e()
|
||||
{}
|
||||
// Имя скачиваемого файла
|
||||
$downloadFile = $this->file_title;
|
||||
// Контент-тип означающий скачивание
|
||||
header("Content-Type: application/octet-stream");
|
||||
// Размер в байтах
|
||||
header("Accept-Ranges: bytes");
|
||||
// Размер файла
|
||||
header("Content-Length: ".filesize($downloadFile));
|
||||
// Расположение скачиваемого файла
|
||||
header("Content-Disposition: attachment; filename=".$downloadFile);
|
||||
|
||||
// Прочитать файл
|
||||
readfile($downloadFile);
|
||||
unlink($this->document);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user