in api added methods: document created and get profile with reports permission
This commit is contained in:
@ -3,10 +3,16 @@
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\Document;
|
||||
use common\models\DocumentFieldValue;
|
||||
use common\models\Template;
|
||||
use common\models\TemplateDocumentField;
|
||||
use Exception;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
|
||||
class DocumentController extends Controller
|
||||
{
|
||||
@ -26,7 +32,7 @@ class DocumentController extends Controller
|
||||
return [
|
||||
// 'get-task' => ['get'],
|
||||
'get-document-list' => ['get'],
|
||||
// 'create-task' => ['post'],
|
||||
'create-document' => ['post'],
|
||||
// 'update-task' => ['put', 'patch'],
|
||||
];
|
||||
}
|
||||
@ -50,11 +56,7 @@ class DocumentController extends Controller
|
||||
throw new NotFoundHttpException('Incorrect document ID');
|
||||
}
|
||||
|
||||
$document = Document::find()
|
||||
->joinWith(['documentFieldValues.field'])
|
||||
->where(['document.id' => $document_id])
|
||||
->asArray()
|
||||
->all();
|
||||
$document = Document::getDocument($document_id);
|
||||
|
||||
if(empty($document)) {
|
||||
throw new NotFoundHttpException('There is no such document');
|
||||
@ -62,4 +64,60 @@ class DocumentController extends Controller
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
public function actionCreateDocument()
|
||||
{
|
||||
$document = Yii::$app->getRequest()->getBodyParams();
|
||||
$documentFieldValues = Yii::$app->getRequest()->getBodyParams()['documentFieldValues'];
|
||||
|
||||
$tmp = TemplateDocumentField::find()->select('field_id')
|
||||
->where(['template_id' => 94])->asArray()->all();
|
||||
|
||||
$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);
|
||||
return Document::getDocument($modelDocument->id);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user