public function __invoke($menuId) { if (!isset(static::$menus[$menuId])) { $db = app('db'); if (!$db) { return new NavigationContainer(); } $cache = app('cache'); $languageCode = 'no'; try { $language = app('language'); $languageCode = $language ? $language->getCode() : 'no'; } catch (\Exception $e) { } $cacheId = 'Menu_' . $menuId . '_' . $languageCode; $cacheId = preg_replace('~[^a-zA-Z0-9_]~', '_', $cacheId); if ($cache === \null || ($container = $cache->load($cacheId)) === \false) { $tree = new Helper\Tree($menuId); $container = new NavigationContainer(); $container->setPages($this->buildPages($tree->getTree())); if ($cache !== \null) { $cache->save($container, $cacheId, ['Navigation_Model_Items']); } } static::$menus[$menuId] = $container; } return static::$menus[$menuId]; }
public function addItemAction() { $menuId = $this->request->getParam('menuId'); $id = $this->request->getParam('id'); $menu = $this->getModel()->find((int) $menuId); if ($menu === null) { return new RedirectResponse(route(\null, array('action' => 'index', 'menuId' => \null))); } $model = new Model\Items(); if ($id) { $item = $model->find((int) $id); if ($item === null) { return new RedirectResponse(route(\null, array('action' => 'items', 'id' => \null))); } } else { $item = $model->createEntity(); } if ($item instanceof Model\Entity\Item) { } $form = new Form(package_path('Navigation', 'Resources/forms/admin/index-add-item.php')); $tree = new Helper\Tree($menu->getAlias()); $form->parentId->setMultiOptions($tree->flat($tree->getTree(null), '---', array((int) $id))); $form->populate($item->toArray()); if ($this->request->isPost()) { $post = $this->request->getPost(); if (isset($post['btnBack'])) { return new RedirectResponse(route(\null, array('action' => 'items', 'id' => \null))); } $form->isValid($post); if (isset($post['alias']) && $post['alias']) { $m = new Model\Table\Items(); $where = array('alias = ?' => $post['alias']); if ($item->getId()) { $where['id <> ?'] = $item->getId(); } if ($m->fetchRow($where)) { $form->alias->addError('Псевдонимът се използва'); $form->markAsError(); } } if (!$form->hasErrors()) { if (isset($post['routeData'])) { $routeData = $post['routeData']; } else { $routeData = array(); } foreach ($routeData as $k => $v) { if (empty($v)) { unset($routeData[$k]); } } $post = Utils::arrayMapRecursive('trim', $post, true); $item->setFromArray($post); $item->setMenuId($menuId); if ($item->getRoute() === \null) { if ($item->getUrl() === \null) { $item->setUrl('#'); } } else { $item->setUrl(\null); } if ($item->getRoute()) { if (($navigationHelper = $this->getNavigationHelper($item->getRoute())) !== \null) { if (method_exists($navigationHelper, 'decode')) { $navigationHelper->decode($routeData, $item); } } } $item->setRouteData(empty($routeData) ? \null : json_encode($routeData)); if ($item->getOrder() === \null) { if ($item->getParentId()) { $and = ' AND parentId = ' . (int) $item->getParentId(); } else { $and = ' AND parentId IS NULL'; } $item->setOrder($model->getTable()->getAdapter()->fetchOne('SELECT IFNULL(MAX(`order`), 0) + 1 FROM MenuItems WHERE menuId = ' . (int) $menuId . $and)); } try { $model->save($item); if (isset($post['btnApply'])) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add-item', 'id' => $item->getId()])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'items', 'id' => \null])); } return $redirectResponse->withFlash('Информацията е записана'); } catch (\Exception $e) { if ($item->getId()) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add-item', 'id' => $item->getId()])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'items', 'id' => \null])); } return $redirectResponse->withFlash(env('development') ? $e->getMessage() : 'Възникна грешка. Опитайте по-късно', 'danger'); } } } $this->view->assign('menu', $menu); $this->view->assign('item', $item); $this->view->assign('form', $form); }