public function onMenuConfig($event) { $models = Page::find()->select(["id", "parent", "title"])->asArray()->all(); array_unshift($models, Page::getAppDefaultPage()); $data = []; $tree = new Tree($models); $nodes = $tree->getRootNodes(); $data = []; foreach ($nodes as $node) { $node = $node->toArray(); $data[] = call_user_func([$this, "repalceKey"], $node); } $event->parameters->set($this->id, ["name" => "页面", "id" => $this->id, "tree" => $data]); }
public function getCanParentNodes() { $models = Page::find()->select(["id", "parent", "title"])->asArray()->all(); $tree = new Tree($models); $nodes = $tree->getNodes(); // 去处当前节点和子节点 if ($this->primaryKey) { $node = $tree->getNodeById($this->primaryKey); $ancestors = $node->getDescendantsAndSelf(); $nodes = array_diff($nodes, $ancestors); } $result = []; foreach ($nodes as $node) { $result[$node->get("id")] = str_repeat("--", $node->getLevel() - 1) . $node->get("title"); } return $result; }
/** * 添加模块的链接到菜单里 * @throws HttpException */ public function actionCreateLinksFromModule() { if (!Yii::$app->request->isAjax) { throw new HttpException("这里只允许ajax"); } $items = Yii::$app->request->post("menu-item", null); $module = Yii::$app->request->post("module"); if (!$items) { return; } $nodes = []; foreach ($items as $item) { $item = $this->ensureNode($item, $module); $item['id'] = $item['original']; $nodes[] = $item; } $tree = new Tree($nodes); $nodes = []; foreach ($tree->getRootNodes() as $node) { $nodes[] = $node->toArray(); } return $this->renderJsonMessage(true, $nodes); }