custom fields have been added to the documents

This commit is contained in:
iIronside
2022-12-09 16:18:57 +03:00
parent aad1b14473
commit 5b455b59f9
25 changed files with 904 additions and 106 deletions

View File

@ -4,9 +4,11 @@ namespace backend\modules\document\controllers;
use backend\modules\company\models\CompanyManager;
use backend\modules\document\models\Document;
use backend\modules\document\models\DocumentFieldValue;
use backend\modules\document\models\DocumentSearch;
use common\services\DocumentService;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
@ -55,8 +57,17 @@ class DocumentController extends Controller
*/
public function actionView($id)
{
$model = $this->findModel($id);
$documentFieldValuesDataProvider = new ActiveDataProvider([
'query' => $model->getDocumentFieldValues(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'model' => $model,
'documentFieldValuesDataProvider' => $documentFieldValuesDataProvider
]);
}
@ -68,11 +79,24 @@ class DocumentController extends Controller
public function actionCreate()
{
$model = new Document();
$model->scenario = Document::SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->load(Yii::$app->request->post())) {
DocumentService::generateDocumentBody($model);
$model->save(false);
return $this->redirect(['view', 'id' => $model->id]);
if ($model->validate()) {
$model->save(false);
if (!$model->getBlankFields()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->redirect([
'document-field-value/create-multiple',
'document_id' => $model->id,
'fieldNames' => $model->getBlankFields(),
]);
}
}
return $this->render('create', [
@ -90,9 +114,24 @@ class DocumentController extends Controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->scenario = Document::SCENARIO_PARTICIPANTS_OF_THE_TRANSACTION;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
if ($model->load(Yii::$app->request->post())) {
if ($model->isAttributeChanged('template_id', false)) {
DocumentService::generateDocumentBody($model);
}
if ($model->save()) {
if (!$model->getBlankFields()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->redirect([
'document-field-value/create-multiple',
'document_id' => $model->id,
'fieldNames' => $model->getBlankFields(),
]);
}
}
return $this->render('update', [
@ -130,54 +169,20 @@ class DocumentController extends Controller
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionDownload($id): string
{
return $this->render('download', [
'model' => Document::findOne($id)
]);
}
/**
* @param integer $id
* @throws NotFoundHttpException
*/
public function actionUpdateDocumentBody($id): string
{
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_UPDATE_DOCUMENT_BODY;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->updated_at = date('Y-m-d h:i:s');
$model->save();
}
return $this->render('download', [
'model' => $model
]);
}
public function actionDownloadPdf($id): string
public function actionDownload($id, $fileType = null): string
{
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_DOWNLOAD_DOCUMENT;
if ($model->validate()) {
DocumentService::downloadPdf($id);
}
Yii::$app->session->setFlash('error', $model->getFirstError('body'));
return $this->render('download', [
'model' => Document::findOne($id)
]);
}
public function actionDownloadDocx($id): string
{
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_DOWNLOAD_DOCUMENT;
if ($model->validate()) {
DocumentService::downloadDocx($id);
if ($fileType && $model->validate()) {
switch ($fileType) {
case 'pdf':
DocumentService::downloadPdf($id);
break;
case 'docx':
DocumentService::downloadDocx($id);
break;
}
}
Yii::$app->session->setFlash('error', $model->getFirstError('body'));
@ -207,4 +212,36 @@ class DocumentController extends Controller
}
return ['output'=>'', 'selected'=>''];
}
public function actionWriteFields($id)
{
$model = $this->findModel($id);
/** @var DocumentFieldValue[] $documentFieldsValue */
$documentFieldsValue = $model->getDocumentFieldValues()->all();
foreach ($documentFieldsValue as $fieldValue) {
$model->body = str_replace($fieldValue->documentField->field_template, $fieldValue->value, $model->body);
}
$model->save(false);
return $this->redirect([
'document/view',
'id' => $id,
]);
}
public function actionUpdateBody($documentId, $oldValue, $newValue)
{
$model = $this->findModel($documentId);
$model->body = str_replace($oldValue, $newValue, $model->body);
$model->save(false);
return $this->redirect([
'document/view',
'id' => $documentId,
]);
}
}

View File

@ -0,0 +1,203 @@
<?php
namespace backend\modules\document\controllers;
use backend\modules\document\models\DocumentField;
use backend\modules\document\models\DocumentFieldValue;
use backend\modules\document\models\DocumentFieldValueSearch;
use Yii;
use yii\base\Model;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* DocumentFieldValueController implements the CRUD actions for DocumentFieldValue model.
*/
class DocumentFieldValueController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all DocumentFieldValue models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new DocumentFieldValueSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single DocumentFieldValue model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new DocumentFieldValue model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new DocumentFieldValue();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Creates a new DocumentFieldValue model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreateMultiple()
{
$document_id = Yii::$app->request->get('document_id');
$fieldNames = Yii::$app->request->get('fieldNames');
// fields ID that should be saved in the document
$documentFieldsIdList = ArrayHelper::getColumn(
DocumentField::find()
->where(['in', 'field_template', $fieldNames])
->all(),
'id');
//already saved fields ID
$fieldsValuesIdList = ArrayHelper::getColumn(
DocumentFieldValue::find()->where(['document_id' => $document_id]) ->all(), 'document_field_id'
);
$fieldsIdList = array_diff($documentFieldsIdList, $fieldsValuesIdList);
$fieldsWithError = [];
$documentFieldValues = [];
if (empty($fieldsIdList)) {
return $this->redirect([
'document/view',
'id' => $document_id,
]);
}
else {
foreach ($fieldsIdList as $id){
$docFieldValue = new DocumentFieldValue();
$docFieldValue->document_id = $document_id;
$docFieldValue->document_field_id = $id;
$documentFieldValues[] = $docFieldValue;
}
if (Model::loadMultiple($documentFieldValues, Yii::$app->request->post())) {
foreach ($documentFieldValues as $documentFieldValue) {
if (!$documentFieldValue->save()) {
$fieldsWithError[] = $documentFieldValue;
}
}
if (!$fieldsWithError) {
return $this->redirect([
'document/write-fields',
'id' => $document_id,
]);
}
$documentFieldValues = $fieldsWithError;
}
}
return $this->render('_form_multiple', [
'documentFieldValues' => $documentFieldValues,
]);
}
/**
* Updates an existing DocumentFieldValue model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id, $fromDocument = null)
{
$model = $this->findModel($id);
$oldValue = $model->value;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($fromDocument) {
return $this->redirect([
'document/update-body',
'documentId' => $model->document_id,
'oldValue' => $oldValue,
'newValue' => $model->value
]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'fromDocument' =>$fromDocument,
]);
}
/**
* Deletes an existing DocumentFieldValue model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the DocumentFieldValue model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return DocumentFieldValue the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = DocumentFieldValue::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace backend\modules\document\models;
class DocumentFieldValue extends \common\models\DocumentFieldValue
{
}

View File

@ -0,0 +1,70 @@
<?php
namespace backend\modules\document\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\document\models\DocumentFieldValue;
/**
* DocumentFieldValueSearch represents the model behind the search form of `backend\modules\document\models\DocumentFieldValue`.
*/
class DocumentFieldValueSearch extends DocumentFieldValue
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'document_field_id', 'document_id'], 'integer'],
[['value'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = DocumentFieldValue::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'document_field_id' => $this->document_field_id,
'document_id' => $this->document_id,
]);
$query->andFilterWhere(['like', 'value', $this->value]);
return $dataProvider;
}
}

View File

@ -0,0 +1,40 @@
<?php
use backend\modules\document\models\Document;
use backend\modules\document\models\DocumentField;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldValue */
/* @var $form yii\widgets\ActiveForm */
/* @var $fromDocument bool */
?>
<div class="document-field-value-form">
<?php $form = ActiveForm::begin(); ?>
<?php
if (!$fromDocument) {
echo $form->field($model, 'document_field_id')->dropDownList(
ArrayHelper::map(DocumentField::find()->all(), 'id', 'title'),
['prompt' => '...']
);
echo $form->field($model, 'document_id')->dropDownList(
ArrayHelper::map(Document::find()->all(), 'id', 'title'),
['prompt' => '...']
);
}
?>
<?= $form->field($model, 'value')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,36 @@
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $documentFieldValues backend\modules\document\models\DocumentFieldValue[] */
/* @var $form yii\widgets\ActiveForm */
$model = $documentFieldValues[0];
$this->title = 'Заполнение полей документа: ' . $model->document->title;
$this->params['breadcrumbs'][] = ['label' => 'Значения полей документа', 'url' => ['index']];
$this->params['breadcrumbs'][] = $model->document->title;
?>
<div class="document-field-value-form">
<?php $form = ActiveForm::begin(); ?>
<?php foreach ($documentFieldValues as $index => $documentFieldValue) { ?>
<?= $form->field($documentFieldValue, "[$index]value")
->textInput(['maxlength' => true])
->label(ArrayHelper::getValue($documentFieldValue,'documentField.title')
) ?>
<?php } ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,33 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldValueSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="document-field-value-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'document_field_id') ?>
<?= $form->field($model, 'document_id') ?>
<?= $form->field($model, 'value') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,20 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldValue */
$this->title = 'Create Document Field Value';
$this->params['breadcrumbs'][] = ['label' => 'Document Field Values', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="document-field-value-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,36 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\document\models\DocumentFieldValueSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Document Field Values';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="document-field-value-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Document Field Value', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'document_field_id',
'document_id',
'value',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,21 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldValue */
/* @var $fromDocument bool */
$this->title = 'Изменить значение поля документа: ' . $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Document Field Values', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="document-field-value-update">
<?= $this->render('_form', [
'model' => $model,
'fromDocument' => $fromDocument
]) ?>
</div>

View File

@ -0,0 +1,39 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldValue */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Document Field Values', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="document-field-value-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'document_field_id',
'document_id',
'value',
],
]) ?>
</div>

View File

@ -1,5 +1,6 @@
<?php
use backend\modules\document\models\DocumentTemplate;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\widgets\DetailView;
@ -44,9 +45,8 @@ $this->params['breadcrumbs'][] = $this->title;
],
[
'attribute' => 'Переменные в шаблоне',
'value' => function($model){
preg_match_all('/(\${\w+})/', $model->template_body,$out);
return implode(",", $out[0]);
'value' => function(DocumentTemplate $model){
return implode(", ", $model->getFields());
},
],
],

View File

@ -53,15 +53,14 @@ use yii\widgets\ActiveForm;
]
); ?>
<?= $form->field($model, 'contractor_company_id')->widget(Select2::class,
<?= $form->field($model, 'contractor_company_id')->dropDownList(
Company::find()->select(['name', 'id'])->indexBy('id')->column(),
[
'data' => Company::find()->select(['name', 'id'])->indexBy('id')->column(),
'options' => ['id' => 'contractor-company-id','placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
'id' => 'contractor-company-id',
'prompt' => 'Выберите'
]
); ?>
);
?>
<?= $form->field($model, 'contractor_manager_id')->widget(DepDrop::className(),
[

View File

@ -24,7 +24,6 @@ $this->params['breadcrumbs'][] = 'Загрузить';
<?php $form = ActiveForm::begin([
'id' => 'update-resume-text-form',
'action' => Url::to(['document/update-document-body', 'id' => $model->id]),
'options' => ['method' => 'post']])
?>
@ -35,16 +34,12 @@ $this->params['breadcrumbs'][] = 'Загрузить';
],
]); ?>
<div class="form-group">
<?= Html::submitButton('Сохраниить изменения', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<div>
<p>
<?= Html::a('Скачать pdf', ['download-pdf', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
<?= Html::a('Скачать docx', ['download-docx', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
<?= Html::a('Скачать pdf', ['download', 'id' => $model->id, 'fileType' => 'docx'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Скачать docx', ['download', 'id' => $model->id, 'fileType' => 'pdf'], ['class' => 'btn btn-success']) ?>
</p>
</div>
</div>

View File

@ -1,6 +1,7 @@
<?php
use backend\modules\company\models\Company;
use backend\modules\company\models\CompanyManager;
use backend\modules\employee\models\Manager;
use yii\helpers\Html;
use yii\grid\GridView;
@ -25,7 +26,6 @@ $this->params['breadcrumbs'][] = $this->title;
['class' => 'yii\grid\SerialColumn'],
'title',
// 'body:ntext',
[
'attribute' => 'company_id',
'filter' => Company::find()->select(['name', 'id'])->indexBy('id')->column(),
@ -38,24 +38,18 @@ $this->params['breadcrumbs'][] = $this->title;
],
[
'attribute' => 'manager_id',
'filter' => Manager::find()->select(['fio', 'manager.id'])
->joinWith('userCard')->indexBy('manager.id')->column(),
'filter' => false,
'value' => 'manager.userCard.fio'
],
[
'attribute' => 'contractor_manager_id',
'filter' => Manager::find()->select(['fio', 'manager.id'])
->joinWith('userCard')->indexBy('manager.id')->column(),
'filter' => false,
'value' => 'manager.userCard.fio'
],
//'title',
//'body:ntext',
//'created_at',
//'updated_at',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {download}',
'template' => '{view} {update} {download} {delete}',
'buttons' => [
'download' => function($url, $model) {
return Html::a(
@ -67,4 +61,5 @@ $this->params['breadcrumbs'][] = $this->title;
]
],
]); ?>
</div>

View File

@ -1,13 +1,13 @@
<?php
use common\classes\Debug;
use common\models\Document;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\Document */
/* @var $documentFieldValuesDataProvider yii\data\ActiveDataProvider */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Documents', 'url' => ['index']];
@ -49,7 +49,7 @@ $this->params['breadcrumbs'][] = $this->title;
],
[
'attribute' => 'contractor_manager_id',
'value' => ArrayHelper::getValue($model, 'manager.userCard.fio'),
'value' => ArrayHelper::getValue($model, 'contractorManager.userCard.fio'),
],
[
'attribute' => 'body',
@ -61,4 +61,37 @@ $this->params['breadcrumbs'][] = $this->title;
],
]) ?>
<div>
<h2>
<?= 'Поля документа:'?>
</h2>
</div>
<?= GridView::widget([
'dataProvider' => $documentFieldValuesDataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'document_field_id',
'value' => 'documentField.title'
],
'attribute' => 'value',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update}',//'{view} {update} {delete}',
'controller' => 'document-field-value',
'buttons' => [
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['document-field-value/update', 'id' => $model['id'], 'fromDocument' => true]);
},
],
],
],
]); ?>
</div>

View File

@ -41,7 +41,7 @@
'visible' => Yii::$app->user->can('confidential_information')
],
[
'label' => 'Профили', 'icon' => 'address-book-o', 'url' => '#', //'active' => \Yii::$app->controller->id == 'user-card',
'label' => 'Профили', 'icon' => 'address-book-o', 'url' => '#',
'items' => $menuItems,
'visible' => Yii::$app->user->can('confidential_information')
],