public function onMenuConfig($event) { $collection = Taxonomy::find()->sort()->asArray()->all(); $event->parameters->set($this->id, ["name" => "分类目录", "id" => $this->id, "tree" => NestedSetsTree::generateTree($collection, function ($item) { $item["id"] = $item['taxonomy_id']; // 这里还是用taxonomy_id的好..不用slug,因为slug很容易被更改 return $item; })]); }
public function init() { parent::init(); if ($this->slug == null) { throw new InvalidConfigException("slug不能为空"); } $this->currentAbsoluteUrl = \Yii::$app->getRequest()->getAbsoluteUrl(); $collection = Menu::findBySlug($this->slug); $createCallbacks = Hook::trigger(\hass\menu\Module::EVENT_MENU_LINK_CREATE)->parameters; $this->items = NestedSetsTree::generateTree($collection, function ($item) use($createCallbacks) { list($item['label'], $item["url"]) = call_user_func($createCallbacks[$item['module']], $item['name'], $item['original']); $item["options"] = Serializer::unserializeToArray($item["options"]); return $item; }, 'items'); }
/** * 根据rootid返回所有子链接 * @param unknown $id */ public function actionViewLinks($id) { $countries = Menu::findOne($id); $collection = $countries->children()->asArray()->all(); $controller = $this; $trees = NestedSetsTree::generateTree($collection, function ($item) use($controller) { //根据模块ID 获取模型 if ($item['module'] == $controller->module->id) { $item['islink'] = true; } $node = $controller->ensureNode($item, $item['module']); return $node; }); if (\Yii::$app->getRequest()->getIsAjax() == true) { return $this->renderJsonMessage(true, $trees); } }
/** * Get cached tree structure of category objects * * @return array */ public function getTaxonomytree() { if ($this->root != null) { $countries = Taxonomy::findOne(['slug' => $this->root]); $collection = $countries->children()->asArray()->all(); } else { $collection = Taxonomy::find()->sort()->asArray()->all(); } $indexs = TaxonomyIndex::find()->where(["entity_id" => $this->getEntityId(), "entity" => $this->getEntityClass()])->asArray()->all(); $indexs = ArrayHelper::getColumn($indexs, "taxonomy_id"); $trees = NestedSetsTree::generateTree($collection, function ($item) use($indexs) { $item["checked"] = false; if (in_array($item["taxonomy_id"], $indexs)) { $item["checked"] = true; } return $item; }); return $trees; }