guild/frontend/modules/api/controllers/TemplateController.php

70 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace frontend\modules\api\controllers;
2022-01-16 23:54:13 +03:00
use common\services\TemplateService;
use yii\web\NotFoundHttpException;
2022-01-14 17:05:29 +03:00
class TemplateController extends ApiController
{
public function verbs(): array
{
return [
'get-template-list' => ['get'],
'get-template-fields' => ['get'],
2022-01-16 23:54:13 +03:00
'get-template' => ['get'],
];
}
2022-01-16 23:54:13 +03:00
/**
* @throws NotFoundHttpException
*/
public function actionGetTemplateList($document_type = null): array
{
2022-01-16 23:54:13 +03:00
$templateList = TemplateService::getTemplateList($document_type);
2022-01-14 17:34:11 +03:00
2022-01-16 23:54:13 +03:00
if (empty($templateList)) {
throw new NotFoundHttpException('No templates found');
2022-01-14 17:34:11 +03:00
}
2022-01-16 23:54:13 +03:00
return $templateList;
}
/**
* @throws NotFoundHttpException
*/
public function actionGetTemplateFields($template_id): array
{
if (empty($template_id) or !is_numeric($template_id)) {
throw new NotFoundHttpException('Incorrect template ID');
2022-01-14 17:34:11 +03:00
}
2022-01-16 23:54:13 +03:00
$templateWithFields = TemplateService::getTemplateWithFields($template_id);
if (empty($templateWithFields)) {
throw new NotFoundHttpException('No template found');
}
2022-01-16 23:54:13 +03:00
return $templateWithFields;
}
2022-01-16 23:54:13 +03:00
/**
* @throws NotFoundHttpException
*/
public function actionGetTemplate($template_id): array
{
2022-01-14 17:05:29 +03:00
if (empty($template_id) or !is_numeric($template_id)) {
throw new NotFoundHttpException('Incorrect template ID');
}
2022-01-16 23:54:13 +03:00
$template = TemplateService::getTemplate($template_id);
2022-01-16 23:54:13 +03:00
if (empty($template)) {
throw new NotFoundHttpException('No template found');
}
2022-01-16 23:54:13 +03:00
return $template;
}
}