/** * Recursively creates a tree from the pages array * * @param MenuItem parent MenuItem object under which the tree is build * @param array pageIdx indices of pages for this tree */ private function buildTree(MenuItem &$parent, $pageIdx) { foreach ($pageIdx as $i) { if ($this->pages[$i]->parent == $parent->get('id')) { $child = $parent->addChild($this->pages[$i]); $this->buildTree($child, array_diff($pageIdx, array($i))); if ($this->pid == $this->pages[$i]->id) { $parent->getRoot()->setCurrentItem($child); } } } }
public function testSomething() { Config::inst()->remove('NavigationScraper', 'MenuSets'); Config::inst()->update('NavigationScraper', 'MenuSets', array('Test' => array('PageToScrape' => 'https://www.example.com', 'CSSSelector' => '.test a'))); $updateService = new MenuSetUpdateService(); $updateService->scrapeConfiguredMenuSets(); $menuItems = MenuItem::get(); $this->assertEquals(1, count($menuItems)); $menuItem = $menuItems->first(); $this->assertEquals('Test', $menuItem->MenuSet); $this->assertEquals('Hello world!', $menuItem->LinkText); $this->assertEquals('https://www.example.com/hello-world', $menuItem->LinkHref); }
/** * Scrapes all configured MenuSets under the NavigationScraper configuration item */ public function scrapeConfiguredMenuSets() { $menuSetsToScrape = Config::inst()->get('NavigationScraper', 'MenuSets'); $scraperService = Injector::inst()->create('NavigationScraperService'); foreach ($menuSetsToScrape as $menuSet => $details) { $menuItems = $scraperService->scrape($details['PageToScrape'], $details['CSSSelector']); if (count($menuItems) == 0) { continue; } MenuItem::get()->filter(array('MenuSet' => $menuSet))->removeAll(); foreach ($menuItems as $menuItemData) { $menuItem = new MenuItem(array('MenuSet' => $menuSet, 'LinkText' => $menuItemData['html'], 'LinkHref' => $menuItemData['href'])); $menuItem->write(); } } }
function getMenuItem() { $items = \MenuItem::get(); foreach ($items as $item) { $item_price[] = $item->menu_item_price; $item_desc[] = $item->menu_item_short; $item_name[] = $item->menu_item_name; $item_fk[] = $item->menu_item_category_fk; } if (!isset($item)) { $items = null; return $items; } $items = array($item_name, $item_desc, $item_fk, $item_price); return $items; }
function getMenuItem($name, $menu) { return MenuItem::get($name, $menu); }
public static function ScrapedMenu($menuSetName) { return MenuItem::get()->filter(array('MenuSet' => $menuSetName)); }