* @link https://TheTempusProject.com/Core * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Houdini\Classes; use TheTempusProject\Hermes\Functions\Route as Routes; use TheTempusProject\Canary\Bin\Canary as Debug; class Navigation extends Template { public static $menus_array = []; public static $collapse_count = 0; public static function normalizeLinkArray( $link ) { $normal_link = []; if ( !isset( $link['text'] ) ) { Debug::info( 'You must provide link text when extending the main menu.' ); return false; } $normal_link['text'] = Components::parse( $link['text'] ); if ( empty( $link['url'] ) ) { $normal_link['url'] = '#'; } elseif ( is_array( $link['url'] ) ) { $normal_link['url'] = $link['url']; } else { $normal_link['url'] = Components::parse( $link['url'] ); } if ( !isset( $link['filter'] ) ) { $normal_link['filter'] = ''; } else { $normal_link['filter'] = $link['filter']; } return $normal_link; } public static function addMenu( $name ) { if ( !isset( self::$menus_array[ $name ] ) ) { self::$menus_array[ $name ] = []; } } public static function addLink( $menuName, $link ) { if ( !isset( self::$menus_array[ $menuName ] ) ) { Debug::debug( 'menu link mot found, creating new:' . $menuName); self::addMenu( $menuName ); } self::$menus_array[ $menuName ][] = self::normalizeLinkArray( $link ); } public static function getMenuLinks( $menuName ) { if ( !isset( self::$menus_array[ $menuName ] ) ) { Debug::debug( 'menu link mot found, creating new:' . $menuName); return false; } $out = []; foreach ( self::$menus_array[ $menuName ] as $key => $item) { $filter = $item['filter']; if ( ! empty( $filter ) ) { $input = '{' . strtoupper($item['filter']) . '}testing{/' . strtoupper($item['filter']) . '}'; $output = Filters::apply( $input ); // 20 years later and a zero 'location' still gets me.... if ( ! stripos( $output, 'esting' ) ) { continue; } } $out[] = (object) $item; } return $out; } public static function getListItem( $link, $class = '' ) { $link = self::normalizeLinkArray( $link ); if ( empty( $link ) ) { return ''; } // this is a good idea but it breaks active page select because the regex is not equipped to deal with the inclusion of a 'class' field in the list items field // $list_item_class = ''; # the class for individual list items $data = ''; if ( !empty( $link['filter'] ) ) { $data = '{' . strtoupper($link['filter']) . '}'.$data.'{/' . strtoupper($link['filter']) . '}'; $data = Filters::apply( $data ); } return $data; } public static function getMenuView( $nav_view, $list_component_name, $menu_name, $should_set_active_page = true ) { if ( empty( self::$menus_array[$menu_name] ) ) { Debug::error( 'menu not found: ' . $menu_name ); return ''; } $list = ''; foreach ( self::$menus_array[$menu_name] as $key => $link ) { if ( ! empty( $link) && is_array( $link['url'] ) ) { self::$collapse_count++; $sub_list = ''; foreach ( $link['url'] as $key => $sub_link ) { $sub_list .= self::getListItem( $sub_link, 'submenu' ); } $list .= ''; continue; } $list .= self::getListItem( $link ); } Components::set( $list_component_name, $list ); if ( true === $should_set_active_page ) { $out = self::activePageSelect( $nav_view ); } else { $out = Views::simpleView( $nav_view ); } return $out; } public static function setCrumbComponent( $breadcrumb_component, $url ) { // @todo this should be some sort of other thing or not let url be empty $esplodade = explode( '/', $url ); $allCrumbs = ''; $previousCrumb = ''; foreach ( $esplodade as $key => $crumb ) { if ( 'delete' === substr( $crumb, 0, 6 ) ) { break; } if ( !empty( $previousCrumb ) ) { $previousCrumb = trim( $previousCrumb ) . '/' . $crumb; } else { $previousCrumb = $crumb; } $url = Routes::getAddress() . $previousCrumb; if ( 'create' === substr( $crumb, 0, 6 ) || 'view' === substr( $crumb, 0, 4 ) || $url === Routes::getRequestUrl() || 'edit' === substr( $crumb, 0, 4 ) ) { $allCrumbs .= ''; break; } $allCrumbs .= ''; } $nav = ''; $nav .= ''; Components::set( $breadcrumb_component, $nav ); } /** * This function parses either given html or the current page content and sets * the current active page to selected within an html list. * * @param string $menu - The name of the view you wish to add. can be any arbitrary value if $view is provided. * @param string $selectString - The string/url you are searching for, default model/controller is used if none is provided. * @param string $view - The html you want parsed, view is generated from menu name if $view is left blank * @return string|bool - returns bool if the menu was added to the page content or * returns the parsed view if one was provided with the * function call. */ public static function activePageSelect( $menu, $selectString = null, $addToContent = false, $use_included_html = false ) { $regSelect = null; if ( false === $use_included_html ) { $view = Views::simpleView( $menu ); } else { $view = $menu; } if ( $selectString == null ) { $selectString = ltrim( Routes::getUri(false), '/' ); } $explodedUrl = explode( '/', $selectString ); $variations = [ Routes::getRequestUrl(), Routes::getUri(), Routes::getUri(false), Routes::getAddress() . $selectString, Routes::getAddress() . $explodedUrl[0], $selectString, $explodedUrl[0], '/' . $explodedUrl[0], '/' . $explodedUrl[0] . '/', $explodedUrl[0] . '/index', '/' . $explodedUrl[0] . '/index', '/' . $explodedUrl[0] . '/index/', ]; foreach ( $variations as $url ) { $regex = "#(.*)\s*?\s*