/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/widgets/model/SwitcheryForm.php
196 строк
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\widgets\model; use common\models\base\BaseModel; use common\models\base\BaseQuery; use common\models\base\IstBaseQuery; use common\widgets\BasePanelWidget; use Yii; /** * Отображение специфичных данных о модели * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ abstract class SwitcheryForm extends BasePanelWidget { /** * Текущая модель с которой работает виджет * @var null|BaseModel */ public $model = null; /** * Имя основного класса с которым работаем * @var null */ protected $modelClass = null; /** * Запрет на редактирование * @var boolean */ public $disabled = false; /** * @inheritdoc */ public $viewName = '@common/widgets/model/views/switchery.twig'; /** * @inheritdoc */ public $dependsAsset = [ 'common\assets\SwitcheryAsset', ]; /** * Имя класса внешнего справочника * @var null */ protected $foreignModel = null; /** * BaseQuery для получения списка * @var BaseQuery */ protected $foreignQuery = null; /** * BaseQuery для элементов отмеченных ранее * @var BaseQuery */ protected $checkedQuery = null; /** * Внешний ключ * @var null */ protected $foreign_key = null; /** * @var null */ protected $relation_key = null; /** * @var null */ protected $relation_name = null; /** * Конечная точка для сохранения данных * @var string */ protected $endpoint = '/modal/save'; /** * Показывать заголовок */ protected $show_title = true; /** * Формирование данных * @return array */ protected function initData() { $list = $this->foreignQuery ->all(); $checked = $this->checkedQuery ->select($this->foreign_key) ->andWhere([$this->relation_key => $this->model->id]); if ($this->checkedQuery instanceof IstBaseQuery) { $checked = $checked->prepareActive(); } $checked = $checked->column(); return [ 'list' => $list, 'checked' => array_flip($checked), 'disabled' => $this->disabled ]; } /** * Сохранение данных */ public function doSave() { $checked = strtolower(Yii::$app->request->post('checked')); $value = Yii::$app->request->post('value'); $id = Yii::$app->request->get('id'); if ($this->relation_name) { $model = $this->modelClass; /** @var BaseModel $obj */ $obj = $model::find() ->where(['id' => $id]) ->one(); if ($obj) { $foreign = $this->foreignModel ->find() ->where(['id' => $value]) ->one(); if ($checked === 'true') { $this->link($obj, $this->relation_name, $foreign); } else { $this->unlink($obj, $foreign); } return true; } } } /** * @inheritdoc */ public function doRun() { $result = $this->initData(); $result['endpoint'] = $this->endpoint; $result['model'] = $this->model; $result['class'] = $this->classNameUrl(); $result['name'] = $this->foreignQuery->alias . '-' . $this->checkedQuery->alias; $result['show_title'] = $this->show_title; return $result; } /** * Разрываем связь */ public function unlink($obj, $foreign) { if ($this->checkedQuery instanceof IstBaseQuery) { $checked_obj = $this->checkedQuery ->where([ $this->foreign_key => $value, $this->relation_key => $id, ]) ->prepareActive() ->one(); $checked_obj->close(); } else { $obj->unlink($this->relation_name, $foreign, true); } } /** * Создаем связь */ public function link($obj, $relation_name, $foreign) { $obj->link($relation_name, $foreign); } }