Commit 9b526379 authored by duke's avatar duke

Разработать API для страницы "Конфигураторы оборудования"

parent 28242fd6
......@@ -5,6 +5,7 @@ namespace common\modules\content\models;
use common\models\MetaTags;
use common\modules\languages\models\Languages;
use common\modules\messageTemplate\models\MessageTemplate;
use frontend\modules\api\models\resource\CoContentResource;
use frontend\modules\sitemap\behaviors\SitemapBehavior;
use Yii;
use yii\behaviors\TimestampBehavior;
......@@ -58,6 +59,34 @@ class CoContent extends \common\components\ActiveRecordModel
return 'co_content';
}
/**
* @return array
*/
public function fields()
{
return [
'id',
'url',
'name' => function ($model) {
/** @var CoContent $model */
return $model->lang ? $model->lang->name : null;
},
'text' => function ($model) {
/** @var CoContent $model */
return $model->lang ? $model->lang->text : null;
},
'meta_title' => function ($model) {
return $model->metaTag->title;
},
'meta_description' => function ($model) {
return $model->metaTag->description;
},
'meta_keywords' => function ($model) {
return $model->metaTag->keywords;
}
];
}
/**
* @inheritdoc
*/
......
......@@ -12,12 +12,13 @@ use common\modules\content\widgets\MetaTagsWidget;
/* @var $form yii\widgets\ActiveForm */
$blocks = \common\modules\content\models\CoBlocks::find()->all();
//$validation = $model->isNewRecord ? false : true;
?>
<div class="co-content-form">
<?php $form = ActiveForm::begin([
'enableClientValidation' => true,
'options' => [
'enctype' => 'multipart/form-data'
]
......
<?php
use yii\db\Migration;
/**
* Handles adding columns to table `{{%bids}}`.
*/
class m260130_091220_add_columnsto_bids_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('bids', 'ip', $this->string()->null()->after('text'));
$this->addColumn('bids', 'from_page', $this->string()->null()->after('text'));
$this->addColumn('bids', 'subscribe', $this->boolean());
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('bids', 'ip');
$this->dropColumn('bids', 'from_page');
$this->dropColumn('bids', 'subscribe');
}
}
......@@ -27,6 +27,7 @@ return [
'support' => ['class' => 'common\modules\support\Module'],
'sessions' => ['class' => 'common\modules\sessions\Module'],
'analyticsSchool' => ['class' => 'common\modules\analyticsSchool\Module'],
'api' => ['class' => 'frontend\modules\api\Module'],
'sitemap' => [
'class' => 'frontend\modules\sitemap\Sitemap',
'models' => [
......@@ -159,6 +160,24 @@ return [
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'api/static',
'pluralize' => false,
'only' => ['view', 'options']
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'api/settings',
'pluralize' => false,
'only' => ['index', 'options']
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'api/feedback',
'pluralize' => false,
'only' => ['create', 'options']
],
'contacts' => 'site/contacts',
['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
['pattern' => 'sitemapg', 'route' => 'sitemap/default/google', 'suffix' => '.xml'],
......
<?php
namespace frontend\modules\api;
use Yii;
use yii\web\Response;
/**
* Class Module
* @package common\modules\bids
*/
class Module extends \yii\base\Module
{
/**
* @inheritdoc
*/
public $controllerNamespace = 'frontend\modules\api\controllers';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Yii::$app->response->format = Response::FORMAT_JSON;
$contentType = Yii::$app->getRequest()->getContentType();
switch ($contentType) {
case 'application/json':
Yii::$app->response->format = Response::FORMAT_JSON;
break;
case 'application/xml':
Yii::$app->response->format = Response::FORMAT_XML;
break;
default:
Yii::$app->response->format = Response::FORMAT_JSON;
}
}
}
<?php
namespace frontend\modules\api\controllers;
use frontend\modules\api\models\form\FeedbackForm;
use yii\rest\Controller;
use yii\rest\CreateAction;
/**
* Class FeedbackController
* @package frontend\modules\api\controllers
*/
class FeedbackController extends Controller
{
/**
* @inheritDoc
*/
public function verbs()
{
return [
'create' => ['post']
];
}
/**
* @inheritDoc
*/
public function actions()
{
return [
'create' => [
'class' => CreateAction::className(),
'modelClass' => FeedbackForm::className()
]
];
}
}
\ No newline at end of file
<?php
namespace frontend\modules\api\controllers;
use frontend\modules\api\models\resource\SettingsResource;
use yii\rest\Controller;
/**
* Class SettingsController
* @package frontend\modules\api\controllers
*/
class SettingsController extends Controller
{
/**
* @inheritDoc
*/
public function verbs()
{
return [
'index' => ['get']
];
}
/**
* @return SettingsResource
*/
public function actionIndex()
{
$model = new SettingsResource();
return $model;
}
}
\ No newline at end of file
<?php
namespace frontend\modules\api\controllers;
use common\modules\content\models\CoContent;
use yii\rest\Controller;
use yii\rest\ViewAction;
/**
* Class StaticController
* @package frontend\modules\api\controllers
*/
class StaticController extends Controller
{
/**
* @inheritDoc
*/
public function verbs()
{
return [
'view' => ['get']
];
}
/**
* @inheritDoc
*/
public function actions()
{
return [
'view' => [
'class' => ViewAction::className(),
'modelClass' => CoContent::className()
]
];
}
}
\ No newline at end of file
# Уведомление с формы
Получение уведомлений из формы обратной связи
**URL** : `/api/feedback`
**Method** : `POST`
**Auth request** : NO
**Data example**
`/api/feedback`
**Data constraints**
| Параметр | Описание
| ----------------------|---------------------------------------------------------|
| **name** | Имя. Обязательное к заполнению |
| **phone** | Телефон. Обязательное к заполнению|
| **email** | E-mail |
| **form** | Тип формы. Обязательное к заполнению. Принимает значения `message` (Форма обратной связи) и `callback` (Обратный звонок)|
| **text** | Текст сообщения|
| **from_page** | С какой страницы был отправлен запрос|
| **confirm** | Согласие на обработку данных. Обязательное к заполнению|
| **subscribe** | Согласие на получение рассылок |
**Data example**
```json
{
"name": "Test",
"phone": "+7988799098",
"email": "test@test.ru",
"form": "message",
"text": "test",
"from_page": "index",
"confirm": 1,
"subscribe": 0
}
```
## Success Response
**Status** : `201 CREATED`
**Content**
```json
{
"email": "test@test.ru",
"name": "Тест",
"phone": "+489567",
"form": "message",
"ip": "127.0.0.1",
"confirm": true,
"created_at": 1770104803,
"updated_at": 1770104803,
"id": 681
}
```
**Status** : `422 Data Validation Failed`
**Content**
```json
[
{
"field": "name",
"message": "Необходимо заполнить «Имя»."
},
{
"field": "phone",
"message": "Необходимо заполнить «Телефон»."
},
{
"field": "form",
"message": "Необходимо заполнить «Форма отправки»."
}
]
```
\ No newline at end of file
# Получение настроек
Используется для получения настроек API.
**URL** : `/api/settings`
**Method** : `GET`
**Auth request** : NO
**Data example**
`/api/settings`
## Success Response
**Status** : `200 OK`
**Content**
```json
{
"email": null,
"footer_email": null,
"phone": null,
"telegram": null,
"policy": null,
"cookie": null
}
```
\ No newline at end of file
# Получение контента страницы
Используется для получения контента страницы API.
**URL** : `/api/static/{id}`
`{id}` - идентификатор страницы, можно узнать в ПУ http://taskon.task-on.com/content/content-admin/manage
**Method** : `GET`
**Auth request** : NO
**Data example**
`/api/static/9`
## Success Response
**Status** : `200 OK`
**Content**
```json
{
"id": 95,
"url": "api",
"name": "Лендинг",
"text": "<strong>test</strong>",
"meta_title": "мета заголовок",
"meta_description": "мета описание",
"meta_keywords": "мета ключевые слова"
}
```
\ No newline at end of file
<?php
namespace frontend\modules\api\models\form;
use common\modules\bids\models\Bid;
use Yii;
/**
* Class FeedbackForm
* @package frontend\modules\api\models\feedback
*/
class FeedbackForm extends Bid
{
/**
* @return array
*/
public function rules()
{
return [
[['name', 'phone', 'form', 'confirm'], 'required'],
[['ip', 'email', 'phone', 'text', 'from_page'], 'string'],
[['name'], 'string', 'min' => 2, 'max' => 100],
[['email'], 'email'],
[['confirm', 'subscribe'], 'boolean'],
[['form'], 'in', 'range' => [Bid::FORM_MESSAGE, Bid::FORM_CALLBACK]]
];
}
public function beforeValidate()
{
$this->ip = Yii::$app->getRequest()->getUserIP();
$this->confirm = true;
return parent::beforeValidate();
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$this->send();
}
}
\ No newline at end of file
<?php
namespace frontend\modules\api\models\resource;
use common\models\Settings;
/**
* Class SettingsResource
* @package frontend\modules\api\models\resource
*/
class SettingsResource extends \yii\base\Model
{
public $email;
public $footer_email;
public $phone;
public $telegram;
public $policy;
public $cookie;
/**
* @return array
*/
public function fields()
{
return [
'email',
'footer_email',
'phone',
'telegram',
'policy',
'cookie'
];
}
public function init()
{
parent::init();
$this->phone = Settings::getValue('api-phone');
$this->telegram = Settings::getValue('api-telegram');
$this->policy = Settings::getValue('api-policy');
$this->cookie = Settings::getValue('api-cookie');
$this->email = Settings::getValue('setting-info-email');
$this->footer_email = Settings::getValue('api-footer-email');
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment