public function indexAction() { $this->view->setTitle('Карты миграции данных'); if (!empty($_POST['type'])) { foreach ($_POST['type'] as $pathId => $objectType) { $mapPath = Migrations\Migration\Map\Path::get($pathId); if (is_numeric($objectType)) { $mapPath->object_id = $objectType; $mapPath->type = 'object'; } else { if ($objectType == 'object') { $object = new Migrations\Migration\Object(); $object->model = !empty($_POST['typeOptions'][$pathId]) ? $_POST['typeOptions'][$pathId] : ''; $object->migration_id = $mapPath->map->migration_id; $object->code = $object->name = $mapPath->item; $object->save(); $mapPath->type = 'object'; $mapPath->object_id = $object->id; } else { $mapPath->type = $objectType; } } $mapPath->save(); } } if (!empty($_POST['param'])) { foreach ($_POST['param'] as $paramId => $type) { $param = \Migrations\Migration\Object\Param::get($paramId); if ($type == 'newObject') { $object = new Migrations\Migration\Object(); $object->model = !empty($_POST['paramOptions'][$paramId]) ? $_POST['paramOptions'][$paramId] : ''; $object->migration_id = $param->object->migration_id; $object->code = $object->name = $param->code; $object->save(); $param->type = 'object'; $param->value = $object->id; } else { $param->type = $type; $param->value = !empty($_POST['paramOptions'][$paramId]) ? $_POST['paramOptions'][$paramId] : ''; } $param->save(); } } $models = $this->modules->getSelectListModels(); $map = \Migrations\Migration\Map::get($_GET['item_pk']); $objects = $map->migration->objects(['forSelect' => true]); $this->view->page(['data' => compact('map', 'models', 'objects')]); }
function drawParam($param, $form, $models, $objects, $parent = 0) { $selectArrays = []; $objectsCols = []; if (!isset($selectArrays[$param->object->migration_id])) { $selectArrays[$param->object->migration_id] = Migrations\Migration\Object::getList(['where' => ['migration_id', $param->object->migration_id], 'forSelect' => true]); $selectArrays[$param->object->migration_id] = ['' => 'Выберите', 'continue' => 'Пропустить', 'container' => 'Контейнер'] + $selectArrays[$param->object->migration_id]; } if (empty($objectsCols[$param->object_id])) { $modelName = $param->object->model; foreach (array_keys($modelName::$cols) as $colName) { $objectsCols[$param->object_id][$colName] = !empty($modelName::$labels[$colName]) ? $modelName::$labels[$colName] : $colName; } } $modelName = $param->object->model; $relations = []; foreach ($modelName::relations() as $relName => $relation) { $relations[$relName] = $relName; } if ($parent) { $parserName = '\\Migrations\\Parser\\Object\\' . ucfirst($parent->type); $parser = new $parserName(); $parser->param = $parent; $values = $parser->editor(); } else { $values = ['' => 'Выберите', 'continue' => 'Пропустить', 'item_key' => 'Ключ элемента', 'paramsList' => 'Список параметров', 'param' => 'Параметр', 'value' => ['text' => 'Значение', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'select', 'source' => 'array', 'sourceArray' => $objectsCols[$param->object_id], 'options' => ['value' => $param->type == 'value' ? $param->value : '']]], 'relation' => ['text' => 'Зависимость', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'select', 'source' => 'array', 'sourceArray' => $relations, 'options' => ['value' => $param->type == 'relation' ? $param->value : '']]], 'object' => ['text' => 'Объект', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'select', 'source' => 'array', 'sourceArray' => $selectArrays[$param->object->migration_id], 'options' => ['value' => $param->type == 'object' ? $param->value : '']]], 'objectLink' => ['text' => 'Ссылка на объект', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'select', 'source' => 'array', 'sourceArray' => $selectArrays[$param->object->migration_id], 'options' => ['value' => $param->type == 'objectLink' ? $param->value : '']]], 'newObject' => ['text' => 'Новый объект', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'select', 'source' => 'array', 'sourceArray' => $models, 'options' => ['value' => $param->type == 'newObject' ? $param->value : '']]], 'custom' => ['text' => 'Свой класс обработки', 'input' => ['name' => 'paramOptions[' . $param->id . ']', 'type' => 'text', 'options' => ['value' => $param->value]]]]; } $form->input('select', 'param[' . $param->id . ']', $param->code, ['values' => $values, 'value' => $param->type]); foreach ($param->childs as $child) { echo '<div class="col-xs-offset-1">'; drawParam($child, $form, $models, $objects, $param); echo '</div>'; } }
public function parse() { $options = $this->param->options ? json_decode($this->param->options, true) : []; $modelName = $this->object->object->model; $relation = $modelName::getRelation($this->param->value); $object = \Migrations\Migration\Object::get([['model', $relation['model']], ['migration_id', $this->object->object->migration_id]]); if (!$object) { $object = new \Migrations\Migration\Object(['model' => $relation['model'], 'code' => $this->param->code, 'name' => $this->param->code, 'migration_id' => $this->object->object->migration_id]); $object->save(); } if (!empty($relation['type']) && $relation['type'] == 'many') { $ids = []; if ($this->data) { foreach ($this->data as $code => &$item) { if (!\Tools::isAssoc($this->data)) { foreach ($this->data as &$item) { $objectParser = new \Migrations\Parser\Object(); $objectParser->object = $object; $objectParser->parentObject = $this->object; $objectParser->parentModel = $this->model; $objectParser->walker = $this->object->walker; $objectParser->parentParam = $this; $objectParser->data =& $item; if (!$this->model->pk()) { $this->model->save(); } $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); } } else { $objectParser = new \Migrations\Parser\Object(); $objectParser->object = $object; $objectParser->parentObject = $this->object; $objectParser->parentModel = $this->model; $objectParser->walker = $this->object->walker; $objectParser->parentParam = $this; $objectParser->data =& $item; if (!$this->model->pk()) { $this->model->save(); } $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); } } } if (!empty($options['clearMissing']) && $this->model->pk()) { $where = []; $where[] = [$relation['col'], $this->model->pk()]; if ($ids) { $where[] = ['id', implode(',', $ids), 'NOT IN']; } $modelName = $relation['model']; $objects = $modelName::getList(['where' => $where]); foreach ($objects as $delObject) { $objectId = \App::$cur->migrations->findParse($delObject->id, get_class($delObject)); if ($objectId) { unset(\App::$cur->migrations->ids['objectIds'][get_class($delObject)][$delObject->id]); unset(\App::$cur->migrations->ids['parseIds'][get_class($delObject)][$objectId->parse_id]); $objectId->delete(); } $delObject->delete(); } } } else { $objectParser = new \Migrations\Parser\Object(); $objectParser->object = $object; $objectParser->parentObject = $this->object; $objectParser->parentModel = $this->model; $objectParser->parentParam = $this; $objectParser->data =& $this->data; $ids = []; if (!\Tools::isAssoc($this->data)) { foreach ($this->data as &$data) { $model = $objectParser->setModel($this->data); if ($model && $model->id) { $ids[] = $model->id; } } } else { $model = $objectParser->setModel($this->data); if ($model && $model->id) { $ids[] = $model->id; } } if ($ids) { $this->model->{$relation['col']} = $ids[0]; } } }