2022-01-11 13:37:30 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\models\Document;
|
2022-01-14 10:09:02 +03:00
|
|
|
use common\models\DocumentFieldValue;
|
2022-01-16 23:54:13 +03:00
|
|
|
use common\services\DocumentService;
|
2022-01-11 13:37:30 +03:00
|
|
|
use Yii;
|
2022-01-14 10:09:02 +03:00
|
|
|
use yii\web\BadRequestHttpException;
|
2022-01-11 13:37:30 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
2022-01-14 10:09:02 +03:00
|
|
|
use yii\web\ServerErrorHttpException;
|
2022-01-11 13:37:30 +03:00
|
|
|
|
2022-01-14 17:05:29 +03:00
|
|
|
class DocumentController extends ApiController
|
2022-01-11 13:37:30 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'get-document-list' => ['get'],
|
2022-01-16 23:54:13 +03:00
|
|
|
'get-document' => ['get'],
|
2022-01-14 10:09:02 +03:00
|
|
|
'create-document' => ['post'],
|
2022-01-11 13:37:30 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-01-16 23:54:13 +03:00
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetDocumentList($document_type = null): array
|
2022-01-11 13:37:30 +03:00
|
|
|
{
|
2022-01-16 23:54:13 +03:00
|
|
|
$documents = DocumentService::getDocumentList($document_type);
|
2022-01-11 13:37:30 +03:00
|
|
|
|
|
|
|
if(empty($documents)) {
|
2022-01-16 23:54:13 +03:00
|
|
|
throw new NotFoundHttpException('Documents not found');
|
2022-01-11 13:37:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2022-01-16 23:54:13 +03:00
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetDocument($document_id): array
|
2022-01-11 13:37:30 +03:00
|
|
|
{
|
|
|
|
if(empty($document_id) or !is_numeric($document_id))
|
|
|
|
{
|
|
|
|
throw new NotFoundHttpException('Incorrect document ID');
|
|
|
|
}
|
|
|
|
|
2022-01-16 23:54:13 +03:00
|
|
|
$document = DocumentService::getDocument($document_id);
|
2022-01-11 13:37:30 +03:00
|
|
|
|
|
|
|
if(empty($document)) {
|
|
|
|
throw new NotFoundHttpException('There is no such document');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $document;
|
|
|
|
}
|
2022-01-14 10:09:02 +03:00
|
|
|
|
|
|
|
public function actionCreateDocument()
|
|
|
|
{
|
|
|
|
$document = Yii::$app->getRequest()->getBodyParams();
|
2022-01-16 23:54:13 +03:00
|
|
|
$documentFieldValues = $document['documentFieldValues'];
|
2022-01-14 10:09:02 +03:00
|
|
|
|
|
|
|
$modelDocument = new Document();
|
|
|
|
if ($modelDocument->load($document, '') && $modelDocument->save()) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->createDocimentFields($documentFieldValues, $modelDocument->id, $modelDocument->template_id);
|
|
|
|
}
|
|
|
|
catch (ServerErrorHttpException $e) {
|
|
|
|
$modelDocument->delete();
|
|
|
|
throw new BadRequestHttpException(json_encode($e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new BadRequestHttpException(json_encode($modelDocument->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
Yii::$app->getResponse()->setStatusCode(201);
|
2022-01-16 23:54:13 +03:00
|
|
|
return DocumentService::getDocument($modelDocument->id);
|
2022-01-14 10:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createDocimentFields($documentFieldValues , $document_id, $template_id)
|
|
|
|
{
|
|
|
|
if (!empty($documentFieldValues)) {
|
|
|
|
|
|
|
|
$modelFieldsArray = array();
|
|
|
|
|
|
|
|
foreach ($documentFieldValues as $docFieldValue) {
|
|
|
|
$tmpModelField = new DocumentFieldValue();
|
|
|
|
|
|
|
|
if ($tmpModelField->load($docFieldValue, '')) {
|
|
|
|
$modelFieldsArray[] = $tmpModelField;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new ServerErrorHttpException(
|
|
|
|
'Failed to load document field value where modelField: field_id=' . $tmpModelField->field_id . ' value=' . $tmpModelField->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($modelFieldsArray as $modelField) {
|
|
|
|
|
|
|
|
$modelField->document_id = $document_id;
|
|
|
|
if (!$modelField->save()) {
|
|
|
|
throw new ServerErrorHttpException(
|
|
|
|
'Failed to save document field value where modelField: field_id=' . $modelField->field_id . ' value=' . $modelField->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 13:37:30 +03:00
|
|
|
}
|