2015-07-28 12:32:18 +00:00
< ? php
register_module ([
" name " => " Sidebar " ,
" version " => " 0.1 " ,
" author " => " Starbeamrainbowlabs " ,
2015-08-04 19:03:25 +00:00
" description " => " Adds a sidebar to the left hand side of every page. Add ' \$ settings->sidebar_show = true;' to your configuration, or append '&sidebar=yes' to the url to enable. Adding to the url sets a cookie to remember your setting. " ,
2015-07-28 12:32:18 +00:00
" id " => " extra-sidebar " ,
" code " => function () {
$show_sidebar = false ;
// Show the sidebar if it is enabled in the settings
if ( isset ( $settings -> sidebar_show ) && $settings -> sidebar_show === true )
$show_sidebar = true ;
// Also show and persist the sidebar if the special GET parameter
// sidebar is seet
if ( ! $show_sidebar && isset ( $_GET [ " sidebar " ]))
{
$show_sidebar = true ;
// Set a cookie to persist the display of the sidebar
2015-08-04 19:03:25 +00:00
setcookie ( " sidebar_show " , " true " , time () + ( 60 * 60 * 24 * 30 ));
2015-07-28 12:32:18 +00:00
}
// Show the sidebar if the cookie is set
if ( ! $show_sidebar && isset ( $_COOKIE [ " sidebar_show " ]))
$show_sidebar = true ;
// Delete the cookie and hide the sidebar if the special GET paramter
// nosidebar is set
if ( isset ( $_GET [ " nosidebar " ]))
{
$show_sidebar = false ;
unset ( $_COOKIE [ " sidebar_show " ]);
setcookie ( " sidebar_show " , null , time () - 3600 );
}
page_renderer :: register_part_preprocessor ( function ( & $parts ) use ( $show_sidebar ) {
2015-08-04 09:44:23 +00:00
global $settings , $pageindex ;
2015-07-28 12:32:18 +00:00
if ( $show_sidebar )
{
// Show the sidebar
2015-08-04 09:44:23 +00:00
$exec_start = microtime ( true );
$sidebar_contents = " " ;
$sidebar_contents .= render_sidebar ( $pageindex );
$parts [ " { body} " ] = " <aside class='sidebar'>
$sidebar_contents
<!-- Sidebar rendered in " . (microtime(true) - $exec_start ) . " s -->
</ aside >
< div class = 'main-container' > " . $parts["{body}"] . " </ div >
<!-------------->
< style >
body { display : flex ; }
. main - container { flex : 1 ; }
</ style > " ;
2015-07-28 12:32:18 +00:00
}
});
}
]);
2015-08-04 09:44:23 +00:00
/*
* @ summary Renders the sidebar for the given pageindex .
*
* @ param $pageindex { array } - The pageindex to renderthe sidebar for
* @ param $root_pagename { string } - The pagename that should be considered the root of the rendering . You don ' t usually need to use this , it is used by the algorithm itself since it is recursive .
*
* @ returns { string } A HTML rendering of the sidebar for the given pageindex
*/
function render_sidebar ( $pageindex , $root_pagename = " " )
{
global $settings ;
$result = " <ul " ;
// If this is the very root of the tree, add an extra class to it
if ( $root_pagename == " " ) $result .= " class='sidebar-tree' " ;
$result .= " > " ;
foreach ( $pageindex as $pagename => $details )
{
// If we have a valid root pagename, and it isn't present at the
// beginning of the current pagename, skip it
if ( $root_pagename !== " " && strpos ( $pagename , $root_pagename ) !== 0 )
continue ;
// The current page is the same as the root page, skip it
if ( $pagename == $root_pagename )
continue ;
// If the part of the current pagename that comes after the root
// pagename has a slash in it, skip it as it is a sub-sub page.
if ( strpos ( substr ( $pagename , strlen ( $root_pagename )), " / " ) !== false )
continue ;
$result .= " <li><a href='?action= $settings->defaultaction &page= $pagename '> $pagename </a> \n " ;
$result .= render_sidebar ( $pageindex , $pagename );
$result .= " </li> \n " ;
}
$result .= " </ul> \n " ;
2015-08-04 19:03:25 +00:00
return $result == " <ul></ul> \n " ? " " : $result ;
2015-08-04 09:44:23 +00:00
}
2015-07-28 12:32:18 +00:00
?>