2022-01-14 10:09:02 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\models\Document;
|
|
|
|
use common\models\Template;
|
|
|
|
use Yii;
|
2022-01-14 17:05:29 +03:00
|
|
|
use yii\filters\auth\CompositeAuth;
|
2022-01-14 10:09:02 +03:00
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
2022-01-14 17:05:29 +03:00
|
|
|
use yii\filters\ContentNegotiator;
|
2022-01-14 10:09:02 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
2022-01-14 17:05:29 +03:00
|
|
|
use yii\web\Response;
|
2022-01-14 10:09:02 +03:00
|
|
|
|
2022-01-14 17:05:29 +03:00
|
|
|
class TemplateController extends ApiController
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'get-template-list' => ['get'],
|
|
|
|
'get-template-fields' => ['get'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionGetTemplateList(): array
|
|
|
|
{
|
2022-01-14 17:34:11 +03:00
|
|
|
$document_type = Yii::$app->request->get('document_type');
|
|
|
|
|
|
|
|
if (!empty($document_type)) {
|
|
|
|
$template = Template::find()->where(['document_type' => $document_type])->asArray()->all();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$template = Template::find()->asArray()->all();
|
|
|
|
}
|
2022-01-14 10:09:02 +03:00
|
|
|
|
2022-01-14 17:05:29 +03:00
|
|
|
if (empty($template)) {
|
2022-01-14 10:09:02 +03:00
|
|
|
throw new NotFoundHttpException('Documents are not assigned');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionGetTemplateFields(): array
|
|
|
|
{
|
|
|
|
$template_id = Yii::$app->request->get('template_id');
|
2022-01-14 17:05:29 +03:00
|
|
|
if (empty($template_id) or !is_numeric($template_id)) {
|
2022-01-14 10:09:02 +03:00
|
|
|
throw new NotFoundHttpException('Incorrect template ID');
|
|
|
|
}
|
|
|
|
|
|
|
|
$templates = Template::find()
|
|
|
|
->joinWith('templateDocumentFields.field')
|
|
|
|
->where(['template.id' => $template_id])
|
|
|
|
->asArray()
|
|
|
|
->all();
|
|
|
|
|
2022-01-14 17:05:29 +03:00
|
|
|
if (empty($templates)) {
|
2022-01-14 10:09:02 +03:00
|
|
|
throw new NotFoundHttpException('Documents are not assigned');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templates;
|
|
|
|
}
|
|
|
|
}
|