alias = $alias; $this->theme = Theme::getEditTheme(); $this->dataIdPrefix = 'page-'.$this->theme->getDirName(); parent::__construct($controller, []); $this->bindToController(); } /** * Renders the widget. * @return string */ public function render() { return $this->makePartial('body', [ 'data' => $this->getData() ]); } /* * Event handlers */ public function onReorder() { $structure = json_decode(Input::get('structure'), true); if (!$structure) { throw new SystemException('Invalid structure data posted.'); } $pageList = new StaticPageList($this->theme); $pageList->updateStructure($structure); } public function onUpdate() { $this->extendSelection(); return $this->updateList(); } public function onSearch() { $this->setSearchTerm(Input::get('search')); $this->extendSelection(); return $this->updateList(); } /* * Methods for internal use */ protected function getData() { $pageList = new StaticPageList($this->theme); $pages = $pageList->getPageTree(true); $searchTerm = Str::lower($this->getSearchTerm()); if (strlen($searchTerm)) { $words = explode(' ', $searchTerm); $iterator = function($pages) use (&$iterator, $words) { $result = []; foreach ($pages as $page) { if ($this->textMatchesSearch($words, $this->subtreeToText($page))) { $result[] = (object) [ 'page' => $page->page, 'subpages' => $iterator($page->subpages) ]; } } return $result; }; $pages = $iterator($pages); } return $pages; } protected function getThemeSessionKey($prefix) { return $prefix.$this->theme->getDirName(); } protected function updateList() { return ['#'.$this->getId('page-list') => $this->makePartial('items', ['items' => $this->getData()])]; } protected function subtreeToText($page) { $result = $this->pageToText($page->page); $iterator = function($pages) use (&$iterator, &$result) { foreach ($pages as $page) { $result .= ' '.$this->pageToText($page->page); $iterator($page->subpages); } }; $iterator($page->subpages); return $result; } protected function pageToText($page) { $viewBag = $page->getViewBag(); return $page->getViewBag()->property('title').' '.$page->getViewBag()->property('url'); } protected function getSession($key = null, $default = null) { $key = strlen($key) ? $this->getThemeSessionKey($key) : $key; return parent::getSession($key, $default); } protected function putSession($key, $value) { return parent::putSession($this->getThemeSessionKey($key), $value); } }