Skip to content

Instantly share code, notes, and snippets.

@betaman
Last active December 13, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betaman/4973439 to your computer and use it in GitHub Desktop.
Save betaman/4973439 to your computer and use it in GitHub Desktop.
<!-- conf/config.php -->
...
[I18n]
; This is the method for determining which language to show the
; current visitor. Options are: url (e.g., /fr/), subdomain
; (e.g., fr.example.com), http (uses Accept-Language header),
; or cookie.
negotiation_method = url
; Switch this to On if you have more than one Language
multilingual = On
...
<!-- apps/navigation/lib/Functions.php -->
<?php
/**
* Print the site tree as an HTML list with the current
* context open as sub-lists up to the first-level children
* of the current page. The currently active page will have
* the class `current` and parents will have the class
* `parent`.
*
* Usage:
*
* $n = new Navigation;
* $path = $n->path ($page->id);
* $path = ($path) ? $path : array ();
* navigation_print_context ($n->tree, $path);
*/
function navigation_print_context ($tree, $path) {
echo '<ul>';
foreach ($tree as $item) {
if ($item->attr->id == $path[count ($path) - 1]) {
printf ('<li class="current"><a href="/%s">%s</a>', $item->attr->id, $item->data);
if (isset ($item->children)) {
navigation_print_context ($item->children, $path);
}
echo '</li>';
} elseif (in_array ($item->attr->id, $path)) {
printf ('<li class="parent"><a href="/%s">%s</a>', $item->attr->id, $item->data);
if (isset ($item->children)) {
navigation_print_context ($item->children, $path);
}
echo '</li>';
} else {
printf ('<li><a href="/%s">%s</a></li>', $item->attr->id, $item->data);
}
}
echo '</ul>';
}
/**
* Print the full site tree as an HTML list.
*/
function navigation_print_level ($tree) {
echo '<ul>';
foreach ($tree as $item) {
printf ('<li><a href="/%s">%s</a>', $item->attr->id, $item->data);
if (isset ($item->children)) {
navigation_print_level ($item->children);
}
echo '</li>';
}
echo '</ul>';
}
/**
* Clears all cache keys for the navigation app.
*/
function navigation_clear_cache () {
global $cache;
$cache->delete ('_navigation_top');
$cache->delete ('_c_navigation_map');
$cache->delete ('_c_navigation_dropmenu');
}
/**
* Returns a list of pages that are not in the navigation.
*/
function navigation_get_other_pages ($ids) {
$pages = array ();
$res = DB::fetch ('select id, title, menu_title from #prefix#webpage where access = "public"');
foreach ($res as $p) {
if (in_array ($p->id, $ids)) {
// skip if in tree
continue;
}
if (! empty ($p->menu_title)) {
$pages[$p->id] = $p->menu_title;
} else {
$pages[$p->id] = $p->title;
}
}
return $pages;
}
function navigation_print_admin_tree ($tree, $tree_root=true) {
echo ($tree_root) ?'<ul class="tdd-tree">' : "<ul>";
foreach ($tree as $item) {
printf ('<li id="%s"><i class="%s"></i> %s', $item->attr->id, $item->attr->classname, $item->data);
if (isset ($item->children)) {
navigation_print_admin_tree ($item->children, false);
}
echo '</li>';
}
echo '</ul>';
}
function navigation_add_class($navString, $pageId, $css){
$out = str_replace (
sprintf ('<li><a href="/%s">', $pageId),
sprintf ('<li class="%s"><a href="/%s">', $css, $pageId),
$navString
);
return $out;
}
function navigation_add_language_path($navString, $language, $languages){
$out = str_replace (
'href="/',
'href="/' . $language . '/',
$navString
);
$out = preg_replace (
'#"/' . $language . '/(' . join ('|', array_keys ($languages)) . ')"#',
'"/\1"',
$out
);
return $out;
}
?>
<!-- apps/admin/handlers/page.php -->
<?php
/**
* Renders a web page from the webpage table.
* This is the default handler in Elefant,
* allowing it to work as a web page-serving
* CMS by default.
*/
// determine page id
$id = count ($this->params)
? $this->params[
(conf ('General', 'page_url_style') === 'flat')
? 0
: count ($this->params) - 1
]
: 'index';
// check if cached
$res = $cache->get ('_admin_page_' . $id);
if ($res) {
$page = (is_object ($res)) ? $res : unserialize ($res);
// show admin edit buttons
if (User::is_valid () && User::is ('admin')) {
$lock = new Lock ('Webpage', $id);
$page->locked = $lock->exists ();
echo $tpl->render ('admin/editable', $page);
}
// output the page body
echo $page->body;
return;
}
// get it from the database
$wp = new Webpage ($id);
// page not found
if ($wp->error) {
echo $this->error (404, i18n_get ('Page not found'), '<p>' . i18n_get ('Hmm, we can\'t seem to find the page you wanted at the moment.') . '</p>');
return;
}
// access control
if ($wp->access !== 'public' && ! User::is ('admin')) {
if (! User::require_login ()) {
$page->title = i18n_get ('Login required');
echo $this->run ('user/login');
return;
}
if (! User::access ($wp->access)) {
$page->title = i18n_get ('Login required');
echo $this->run ('user/login');
return;
}
}
// set the page properties
$page->id = $id;
$page->title = $wp->title;
$page->_menu_title = $wp->menu_title;
$page->_window_title = $wp->window_title;
$page->description = $wp->description;
$page->keywords = $wp->keywords;
$page->layout = $wp->layout;
$page->head = $wp->head;
// show admin edit buttons
if (User::is_valid () && User::is('admin')) {
$lock = new Lock ('Webpage', $id);
$page->locked = $lock->exists ();
echo $tpl->render ('admin/editable', $page);
}
// execute any embedded includes
$out = $tpl->run_includes ($wp->body);
if ($wp->access == 'public' && $out === $wp->body) {
// public page, no includes, cacheable.
$page->body = $out;
$cache->set ('_admin_page_' . $id, serialize ($page));
}
// output the page body
echo $out;
?>
<!-- apps/navigation/handlers/section.php -->
<?php
/**
* Displays a single section of the navigation as a
* bulleted list, with `class="current"` added to
* the current page's `<li>` element for custom styling.
*/
global $i18n;
require_once ('apps/navigation/lib/Functions.php');
$n = new Navigation;
$section = $n->node ($data['section']);
//ob_start ();
$out = '<ul>';
if (conf ('I18n', 'multilingual') && $data['section'] === $i18n->language) {
$out .= sprintf ('<li><a href="/%s">%s</a></li>', $section->attr->id, $section->data);
}
if (is_array ($section->children)) {
foreach ($section->children as $item) {
$path = (conf ('General', 'page_url_style') === "flat") ? $item->attr->id : implode ("/",array_slice( $n->path ($item->attr->id), 1));
$out .= sprintf ('<li><a href="/%s">%s</a></li>', $path, $item->data);
}
}
$out .= '</ul>';
//$out = ob_get_clean ();
$out = navigation_add_class($out, $page->id, "current");
if($data['setActiveClass']){
foreach ( array_slice( $n->path ($page->id), 1) as $active) {
$out = navigation_add_class($out, $active, "active");
}
}
if ($i18n->url_includes_lang) {
$out = navigation_add_language_path ($out, $i18n->language, $i18n->languages);
}
echo $out;
?>
<!-- apps/navigation/handlers/top.php -->
<?php
/**
* Displays the top-level navigation as a bulleted list,
* with `class="current"` added to the current page's
* `<li>` element for custom styling.
*/
global $i18n;
if (conf ('I18n', 'multilingual')) {
$this->run ('navigation/section?section='.$i18n->language);
return;
}
require_once ('apps/navigation/lib/Functions.php');
$res = $cache->get ('_navigation_top');
if ($res) {
$out = navigation_add_class($res, $page->id, "current");
if($data['setActiveClass']){
foreach ( array_slice( $n->path ($page->id), 1) as $active) {
$out = navigation_add_class($out, $active, "active");
}
}
if ($i18n->url_includes_lang) {
$out = navigation_add_language_path ($out, $i18n->language, $i18n->languages);
}
echo $out;
return;
}
$n = new Navigation;
$out = '<ul>';
foreach ($n->tree as $item) {
$out .= sprintf ('<li><a href="/%s">%s</a></li>', $item->attr->id, $item->data);
}
$out .= '</ul>';
$cache->set ('_navigation_top', $out);
$out = navigation_add_class($out, $i18n->url_includes_lang.$page->id, "current");
if($data['setActiveClass']){
foreach ( array_slice( $n->path ($page->id), 1) as $active) {
$out = navigation_add_class($out, $active, "active");
}
}
if ($i18n->url_includes_lang) {
$out = navigation_add_language_path ($out, $i18n->language, $i18n->languages);
}
echo $out;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment