intermediate commit
This commit is contained in:
@ -3,4 +3,5 @@ Yii::setAlias('@common', dirname(__DIR__));
|
||||
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
|
||||
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
|
||||
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
|
||||
Yii::setAlias('@templates', dirname(dirname(__DIR__)) . '/frontend/web/upload/templates');
|
||||
Yii::setAlias('@templates', dirname(dirname(__DIR__)) . '/backend/web/upload/templates');
|
||||
Yii::setAlias('@documents', dirname(dirname(__DIR__)) . '/backend/web/upload/documents');
|
||||
|
@ -28,6 +28,10 @@ return [
|
||||
'path' => 'media/upload',
|
||||
'name' => 'Изображения',
|
||||
],
|
||||
[
|
||||
'basePath' => '@templates',
|
||||
'name' => 'Шаблоны документов' //перевод Yii::t($category, $message)
|
||||
],
|
||||
],
|
||||
'watermark' => [
|
||||
'source' => __DIR__ . '/logo.png', // Path to Water mark image
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace common\helpers;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\Html;
|
||||
|
||||
class TimeHelper
|
||||
|
28
common/helpers/TransliteratorHelper.php
Normal file
28
common/helpers/TransliteratorHelper.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace common\helpers;
|
||||
|
||||
class TransliteratorHelper
|
||||
{
|
||||
private static $rus = [
|
||||
'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У',
|
||||
'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з',
|
||||
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь',
|
||||
'э', 'ю', 'я'
|
||||
];
|
||||
|
||||
private static $lat = [
|
||||
'A', 'B', 'V', 'G', 'D', 'E', 'Yo', 'Zh', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U',
|
||||
'F', 'H', 'C', 'Ch', 'Sh', 'Shch', 'Y', 'Y', 'Y', 'E', 'Yu', 'Ya','a', 'b', 'v', 'g', 'd', 'e', 'yo', 'zh',
|
||||
'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'shch', 'y',
|
||||
'y', 'y', 'e', 'yu', 'ya'
|
||||
];
|
||||
|
||||
public static function transliterate($source)
|
||||
{
|
||||
if ($source) {
|
||||
return str_replace(self::$rus, self::$lat, $source);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
use yii\db\StaleObjectException;
|
||||
|
||||
@ -87,7 +88,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getManager()
|
||||
{
|
||||
@ -95,7 +96,7 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
@ -103,9 +104,9 @@ class Document extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getDocumentFieldValues()
|
||||
public function getDocumentFieldValues(): ActiveQuery
|
||||
{
|
||||
return $this->hasMany(DocumentFieldValue::className(), ['document_id' => 'id']);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use common\helpers\TransliteratorHelper;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
@ -10,6 +11,7 @@ use yii\helpers\ArrayHelper;
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $field_template
|
||||
*
|
||||
* @property DocumentFieldValue[] $documentFieldValues
|
||||
* @property TemplateDocumentField[] $templateDocumentFields
|
||||
@ -30,10 +32,16 @@ class DocumentField extends \yii\db\ActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['title'], 'string', 'max' => 255],
|
||||
[['title', 'field_template'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
$this->field_template = TransliteratorHelper::transliterate($this->title);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -42,6 +50,7 @@ class DocumentField extends \yii\db\ActiveRecord
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название',
|
||||
'field_template' => 'Шаблон поля',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\db\ActiveQuery;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document_field_value".
|
||||
@ -54,7 +55,7 @@ class DocumentFieldValue extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getDocument()
|
||||
{
|
||||
@ -62,10 +63,17 @@ class DocumentFieldValue extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getField()
|
||||
public function getField(): ActiveQuery
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,9 @@
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
@ -20,7 +22,7 @@ use yii\db\Expression;
|
||||
*/
|
||||
class Template extends \yii\db\ActiveRecord
|
||||
{
|
||||
public $template;
|
||||
// public $template;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -50,9 +52,9 @@ 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' => '10000'],
|
||||
// [['template'], 'file', 'skipOnEmpty' => false, 'extensions' => 'doc, docx, txt'],
|
||||
[['title', 'template_file_name'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
@ -78,7 +80,7 @@ class Template extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
if (!empty($this->template_file_name)) {
|
||||
$template_path = Yii::getAlias('@templates') . '/' . $this->template_file_name;
|
||||
$template_path = Yii::getAlias('@templates') . $this->template_file_name;
|
||||
|
||||
if(file_exists($template_path)) {
|
||||
unlink($template_path);
|
||||
@ -88,7 +90,7 @@ class Template extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getDocuments()
|
||||
{
|
||||
@ -96,10 +98,27 @@ class Template extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getTemplateDocumentFields()
|
||||
{
|
||||
return $this->hasMany(TemplateDocumentField::className(), ['template_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
//TODO no need, delete
|
||||
public function getDocumentFields()
|
||||
{
|
||||
$fieldsArray = [];
|
||||
|
||||
foreach ($this->templateDocumentFields as $templateDocField) {
|
||||
$fieldsArray[] = $templateDocField->field;
|
||||
}
|
||||
|
||||
return $fieldsArray;
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ class TemplateDocumentField extends \yii\db\ActiveRecord
|
||||
*/
|
||||
public function getField()
|
||||
{
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'field_id']);
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'field_id'])->asArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
43
common/services/DocumentService.php
Normal file
43
common/services/DocumentService.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
use Yii;
|
||||
|
||||
class DocumentService
|
||||
{
|
||||
private $file_title;
|
||||
private $document;
|
||||
private $template_title;
|
||||
private [] $fields;
|
||||
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
public function __construct($file_title, $template_name, [] $fields)
|
||||
{
|
||||
$this->file_title = $file_title . 'docx';
|
||||
$this->document = new TemplateProcessor(Yii::getAlias('@templates') . "/$template_name");
|
||||
}
|
||||
|
||||
public function setFields($fields )
|
||||
{
|
||||
foreach ($fields as $field) {
|
||||
$this->document->setValue('FIO', '8888888888' );
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->document->saveAs($this->file_title);
|
||||
}
|
||||
|
||||
public function creat3e()
|
||||
{}
|
||||
}
|
Reference in New Issue
Block a user