Initial commit
This commit is contained in:
211
vendor/houdini/classes/navigation.php
vendored
Normal file
211
vendor/houdini/classes/navigation.php
vendored
Normal file
@ -0,0 +1,211 @@
|
||||
<?php
|
||||
/**
|
||||
* core/template/navigation.php
|
||||
*
|
||||
* This class is for managing template navigation including menus, pagination, and breadcrumbs.
|
||||
*
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @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\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 getListItem( $link, $class = '' ) {
|
||||
$link = self::normalizeLinkArray( $link );
|
||||
if ( empty( $link ) ) {
|
||||
return '';
|
||||
}
|
||||
if ( ! empty( $class ) ) {
|
||||
$class = ' class="' . $class . '"';
|
||||
}
|
||||
// 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 = '<li'.$class.'><a href="' . $link['url'] . '">' . $link['text'] . '</a></li>';
|
||||
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 .= '<li>
|
||||
<a href="javascript:;" data-toggle="collapse" data-target="#menu-collapse-' . self::$collapse_count . '">
|
||||
' . $link['text'] . '<i class="fa fa-fw fa-caret-down"></i>
|
||||
</a>
|
||||
<ul id="menu-collapse-' . self::$collapse_count . '" class="collapse">' . $sub_list . '</ul>
|
||||
</li>';
|
||||
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 ) ) {
|
||||
$allCrumbs .= ' <b>></b> ';
|
||||
$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 .= '<b>' . ucfirst( $crumb ) . '</b>';
|
||||
break;
|
||||
}
|
||||
|
||||
$allCrumbs .= '<a href="' . $url . '">' . ucfirst( $crumb ) . '</a>';
|
||||
}
|
||||
Components::set( $breadcrumb_component, $allCrumbs );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = "#(.*)<li(?: class=\")?(.*?)(?:\")?>\s*?<a href=\"$url\"(.*)#is";
|
||||
if ( preg_match( $regex, $view ) ) {
|
||||
$regSelect = $url;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !empty( $regSelect ) ) {
|
||||
$regSelect = preg_quote($regSelect);
|
||||
$regActive = "$1<li class=\"$2 active\"><a href=\"$regSelect\"$3";
|
||||
|
||||
$view = preg_replace( $regex, $regActive, $view );
|
||||
$parentRegex = "#(.*)class=\"collapse(.*?)<li class=\"submenu active\">\s*<a href=\"$regSelect\"(.*)#is";
|
||||
|
||||
if ( preg_match( $parentRegex, $view ) ) {
|
||||
$expandRegex = "$1 class=\"expand$2<li class=\"submenu active\"><a href=\"$regSelect\"$3";
|
||||
$view = preg_replace( $parentRegex, $expandRegex, $view );
|
||||
}
|
||||
if ( !empty( $addToContent ) ) {
|
||||
self::$content .= $view;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return $view;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user