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.');
}
}