--- /dev/null
+<?php
+
+namespace yii_app\actions\weeklyReport;
+
+use Yii;
+use yii\base\Action;
+use yii\helpers\Json;
+use yii_app\records\WeeklyReportSubscription;
+
+class SubscriptionAction extends Action
+{
+ public function run() {
+
+ if (Yii::$app->request->isPost) {
+ $action = Yii::$app->request->post('action');
+ if ($action == "addNew") {
+ $name = Yii::$app->request->post('name');
+ $chatId = Yii::$app->request->post('chat_id');
+
+ $sub = new WeeklyReportSubscription;
+ $sub->name = $name;
+ $sub->chat_id = $chatId;
+ $sub->active = 0;
+
+ $sub->save();
+ if ($sub->getErrors()) {
+ throw new \Exception(Json::encode($sub->getErrors()));
+ }
+ return $this->controller->redirect('subscription');
+ }
+ if ($action == "changeSub") {
+ $subId = Yii::$app->request->post('id');
+ $name = Yii::$app->request->post('name');
+ $chatId = Yii::$app->request->post('chatId');
+ $active = Yii::$app->request->post('active');
+
+ $sub = WeeklyReportSubscription::findOne($subId);
+ $sub->name = $name;
+ $sub->chat_id = $chatId;
+ $sub->active = $active;
+ $sub->save();
+ if ($sub->getErrors()) {
+ throw new \Exception(Json::encode($sub->getErrors()));
+ }
+
+ return 'ok';
+ }
+ }
+
+ $subscriptions = WeeklyReportSubscription::find()->orderBy(['id' => SORT_DESC])->all();
+
+ return $this->controller->render('subscription', compact('subscriptions'));
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace yii_app\api3\modules\v1\controllers;
+
+use yii_app\records\WeeklyReportSubscription;
+
+class WeeklyReportController extends \yii_app\api3\controllers\NoActiveController
+{
+ public function actionSubscription() {
+ return WeeklyReportSubscription::find()->where(['active' => 1])->all();
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace app\controllers;
+
+use yii\web\Controller;
+
+class WeeklyReportController extends Controller
+{
+ public function actions()
+ {
+ return [
+ 'subscription' => \yii_app\actions\weeklyReport\SubscriptionAction::class,
+ ];
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m241010_150710_create_table_weekly_report_subscribtion
+ */
+class m241010_150710_create_table_weekly_report_subscribtion extends Migration
+{
+ const TABLE_NAME = 'erp24.weekly_report_subscription';
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->createTable(self::TABLE_NAME, [
+ 'id' => $this->primaryKey(),
+ 'name' => $this->string(36)->notNull()->comment('Имя пользователя'),
+ 'chat_id' => $this->integer()->notNull()->comment('chat_id пользователя'),
+ 'active' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('0 - не активен, 1 - активен')
+ ]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $this->dropTable(self::TABLE_NAME);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "weekly_report_subscription".
+ *
+ * @property int $id
+ * @property string $name Имя пользователя
+ * @property int $chat_id chat_id пользователя
+ * @property int $active 0 - не активен, 1 - активен
+ */
+class WeeklyReportSubscription extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'weekly_report_subscription';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['name', 'chat_id'], 'required'],
+ [['chat_id', 'active'], 'default', 'value' => null],
+ [['chat_id', 'active'], 'integer'],
+ [['name'], 'string', 'max' => 36],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'name' => 'Name',
+ 'chat_id' => 'Chat ID',
+ 'active' => 'Active',
+ ];
+ }
+}
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+use yii_app\records\WeeklyReportSubscription;
+
+/** @var $subscriptions []WeeklyReportSubscription */
+
+$this->registerJsFile('/js/weekly-report/subscription.js', ['position' => \yii\web\View::POS_END]);
+
+?>
+
+<div class="weeklyReportSubscription m-5">
+
+ <?php ActiveForm::begin() ?>
+
+ <div class="row">
+ <div class="col-3">
+ Имя: <?= Html::textInput('name') ?>
+ chat_id: <?= Html::textInput('chat_id', null, ['type' => 'number'])?>
+ </div>
+ <div class="col-1">
+ <?= Html::submitButton('Добавить', ['class' => 'btn btn-success btn-sm', 'name' => 'action', 'value' => 'addNew'])?>
+ </div>
+ </div>
+
+ <?php ActiveForm::end() ?>
+
+ <div class="row mt-2">
+ <div class="col-4">
+ <table id="subscriptionTable">
+ <thead>
+ <tr>
+ <th>Имя</th>
+ <th>chat_id</th>
+ <th>Активен</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php foreach($subscriptions as $sub): /** @var $sub WeeklyReportSubscription */ ?>
+ <tr data-id="<?= $sub->id ?>">
+ <td><input type="text" value="<?= $sub->name ?>" onchange="saveChanges(this)" /></td>
+ <td><input type="number" value="<?= $sub->chat_id ?>" onchange="saveChanges(this)" /></td>
+ <td><input type="checkbox" <?= $sub->active == 1 ? 'checked="checked"' : '' ?> onclick="saveChanges(this)" /> </td>
+ </tr>
+ <?php endforeach; ?>
+ </tbody>
+ </table>
+ </div>
+ </div>
+
+</div>
--- /dev/null
+/* jshint esversion: 6 */
+
+const param23 = $('meta[name=csrf-param]').attr('content');
+const token23 = $('meta[name=csrf-token]').attr('content');
+
+$(document).ready(() => {
+ $("#subscriptionTable").DataTable({
+ sorting: false,
+ info: false,
+ paging: true,
+ searching: true,
+ language: data_table_language
+ })
+})
+
+function saveChanges(self) {
+ const root = $(self).parent().parent();
+ const id = root.data('id');
+ const name = root.find('input[type=text]');
+ const chatId = root.find('input[type=number]');
+ const active = root.find('input[type=checkbox]');
+ $.ajax({
+ url: window.location.href,
+ method: 'post',
+ data: {
+ action: 'changeSub',
+ id,
+ name: name.val(),
+ chatId: chatId.val(),
+ active: (active.is(':checked') ? 1 : 0),
+ [param23]: token23
+ },
+ dataType: 'text',
+ success: (data) => {
+ console.log(data);
+ }
+ })
+}
\ No newline at end of file