public function actionNewChildren()
{
/**
* @var Tree $parent
*/
$parent = $this->model;
if (\Yii::$app->request->isPost) {
$post = \Yii::$app->request->post();
$childTree = new Tree();
$parent = Tree::find()->where(['id' => $post["pid"]])->one();
$childTree->load($post);
if (!$childTree->priority) {
$childTree->priority = Tree::PRIORITY_STEP;
//Элемент с большим приоритетом
if ($treeChildrens = $parent->getChildren()->orderBy(['priority' => SORT_DESC])->one()) {
$childTree->priority = $treeChildrens->priority + Tree::PRIORITY_STEP;
}
}
$response = ['success' => false];
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
try {
if ($parent && $parent->processAddNode($childTree)) {
$response['success'] = true;
}
} catch (\Exception $e) {
$response['success'] = false;
$response['message'] = $e->getMessage();
}
if (!$post["no_redirect"]) {
$this->redirect(Url::to(["new-children", "id" => $parent->primaryKey]));
} else {
return $response;
}
} else {
$tree = new Tree();
$search = new Search(Tree::className());
$dataProvider = $search->search(\Yii::$app->request->queryParams);
$searchModel = $search->getLoadedModel();
$dataProvider->query->andWhere(['pid' => $parent->primaryKey]);
$controller = \Yii::$app->cms->moduleCms->createControllerByID("admin-tree");
return $this->render('new-children', ['model' => new Tree(), 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'controller' => $controller]);
}
}