/** * Adds protected node types. */ public function onEnable($event, $module) { foreach ((array) $module->get('nodes') as $type => $route) { if (isset($route['protected']) and $route['protected'] and !Node::where(['type = ?'], [$type])->first()) { Node::create(['title' => $route['label'], 'slug' => App::filter($route['label'], 'slugify'), 'type' => $type, 'status' => 1, 'link' => $route['name']])->save(); } } }
/** * @param string $menu * @param array $parameters * @return Node|null */ public function getRoot($menu, $parameters = []) { $parameters = array_replace(['start_level' => 1, 'depth' => PHP_INT_MAX, 'mode' => 'all'], $parameters); $user = App::user(); $startLevel = (int) $parameters['start_level'] ?: 1; $maxDepth = $startLevel + ($parameters['depth'] ?: PHP_INT_MAX); $nodes = Node::where(['menu' => $menu, 'status' => 1])->orderBy('priority')->get(); $nodes[0] = new Node(['path' => '/']); $nodes[0]->parent_id = null; $node = App::node(); $path = $node->path; if (!isset($nodes[$node->id])) { foreach ($nodes as $node) { if ($node->getUrl('base') === $path) { $path = $node->path; break; } } } $path .= '/'; $segments = explode('/', $path); $rootPath = count($segments) > $startLevel ? implode('/', array_slice($segments, 0, $startLevel + 1)) . '/' : '/'; foreach ($nodes as $node) { $depth = substr_count($node->path, '/'); $parent = isset($nodes[$node->parent_id]) ? $nodes[$node->parent_id] : null; $node->set('active', 0 === strpos($path, $node->path . '/')); if ($depth >= $maxDepth || !$node->hasAccess($user) || $node->get('menu_hide') || !($parameters['mode'] == 'all' || $node->get('active') || 0 === strpos($node->path . '/', $rootPath) || $depth == $startLevel)) { continue; } $node->setParent($parent); if ($node->get('active') && $depth == $startLevel - 1) { $root = $node; } } if (!isset($root)) { return null; } $root->setParent(); return $root; }
/** * @Route("/{id}", methods="DELETE") * @Request({"id"}, csrf=true) */ public function deleteAction($id) { App::config('system/site')->remove('menus.' . $id); Node::where(['menu = :id'], [':id' => $id])->update(['menu' => 'trash', 'status' => 0]); return ['message' => 'success']; }