/** * Returns a tree of menu-items that belong to the menu * * @param array $settings A settings array that holds the parent-id and level * @return array */ public function getTree($settings = ['subMenu' => true, 'parentId' => 0, 'level' => 0, 'includeLanguage' => true, 'convertBr' => false]) { $items = []; $menuItems = $this->getItems($settings)->all(); foreach ($menuItems as $menuItem) { // First we need to check if the item has a non-public page attached // If so, and no user is logged in, the item is skipped if ($menuItem->entity == Page::className() && Yii::$app->user->isGuest) { $menuItemEntity = $menuItem->entityModel; if (isset($menuItemEntity->public) && $menuItemEntity->public == false) { continue; } } $item = ['label' => isset($settings['convertBr']) ? str_replace("\n", '<br>', $menuItem->name) : str_replace("\n", ' ', $menuItem->name), 'url' => $menuItem->getUrl($settings['includeLanguage']), 'entity' => $menuItem->entity, 'entity_id' => $menuItem->entity_id, 'active' => in_array($menuItem->entity == MenuItem::className() ? $menuItem->entity_id : $menuItem->id, Yii::$app->page->linkedMenuItemsIds), 'options' => ['class' => "menu-item-{$menuItem->id}"]]; if (isset($settings['includeObject']) && $settings['includeObject'] == true) { $item['object'] = $menuItem; } // A menu-item that links to an url has to open in a new window if ($menuItem->entity == MenuItem::ENTITY_URL) { $item['linkOptions'] = ['target' => '_blank']; } if ($settings['subMenu'] == true) { // Get the item's children $children = $this->getTree(['parentId' => $menuItem->id, 'level' => $menuItem->level + 1, 'includeLanguage' => $settings['includeLanguage'], 'subMenu' => $settings['subMenu'], 'convertBr' => isset($settings['convertBr'])]); if ($children) { $item['items'] = $children; } } $items[$menuItem->id] = $item; } return $items; }
/** * Parent menu item * @return null|static */ public function getParent() { return $this->hasOne(MenuItem::className(), ['id' => 'parent_id']); }
public function setEventHandlers() { // Set eventhandlers for the 'Menu' model Event::on(Menu::className(), ActiveRecord::EVENT_AFTER_DELETE, function ($event) { // Delete the children if (!$event->sender->deleteChildren()) { throw new \yii\base\Exception(Yii::t('app', 'There was an error while deleting this item')); } }); // Set eventhandlers for the 'MenuItem' model Event::on(MenuItem::className(), ActiveRecord::EVENT_AFTER_DELETE, function ($event) { // Delete the children if (!$event->sender->deleteChildren()) { throw new \yii\base\Exception(Yii::t('app', 'There was an error while deleting this item')); } }); }
/** * Performs validation on the provided model and $_POST data * * @param \infoweb\pages\models\Page $model The page model * @param array $post The $_POST data * @return array */ protected function validateModel($model, $post) { $languages = Yii::$app->params['languages']; // Populate the model with the POST data $model->load($post); // Parent is root if (empty($post[StringHelper::basename(MenuItem::className())]['parent_id'])) { $model->parent_id = 0; $model->level = 0; } else { $parent = MenuItem::findOne($post[StringHelper::basename(MenuItem::className())]['parent_id']); $model->parent_id = $parent->id; $model->level = $parent->level + 1; } // Create an array of translation models and populate them $translationModels = []; // Insert if ($model->isNewRecord) { foreach ($languages as $languageId => $languageName) { $translationModels[$languageId] = new MenuItemLang(['language' => $languageId]); } // Update } else { $translationModels = ArrayHelper::index($model->getTranslations()->all(), 'language'); } Model::loadMultiple($translationModels, $post); // Validate the model and translation $response = array_merge(ActiveForm::validate($model), ActiveForm::validateMultiple($translationModels)); // Return validation in JSON format Yii::$app->response->format = Response::FORMAT_JSON; return $response; }
/** * @return \yii\db\ActiveQuery */ public function getChildren() { return $this->hasMany(MenuItem::className(), ['menu_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getMenu() { return $this->hasOne(MenuItem::className(), ['id' => 'menu_item_id']); }