Last active
August 29, 2015 13:56
-
-
Save WaltRiceJr/9270330 to your computer and use it in GitHub Desktop.
Custom Omeka Exhibit code to skip empty pages / section headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if the provided exhibit page is empty (layout type = text, no text) | |
* | |
* @param ExhibitPage $exhibitPage | |
* @return boolean | |
**/ | |
function my_exhibit_builder_exhibit_page_is_empty($exhibitPage) | |
{ | |
if ($exhibitPage->layout == 'text') { | |
$entries = $exhibitPage->getPageEntries(); | |
if ($entry = $entries[1]) { | |
if (trim($entry->text) == '') { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Return the first non-empty page in an exhibit, beginning with provided page | |
* If provided page is non-empty, it will be immediately returned; otherwise a search | |
* proceeds through the page structure to find the next non-empty page. | |
* | |
* @param Exhibit $exhibit | |
* @param ExhibitPage|null $exhibitPage | |
* @return ExhibitPage|null | |
**/ | |
function my_exhibit_builder_get_first_nonempty_page($exhibit, $exhibitPage = null) | |
{ | |
// if a page is passed in, start there, otherwise load the first top page | |
if (!$exhibitPage) | |
{ | |
$topPages = $exhibit->getTopPages(); | |
if (count($topPages) > 0) | |
{ | |
$exhibitPage = $topPages[0]; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
// check if page is text layout, and empty | |
if (my_exhibit_builder_exhibit_page_is_empty($exhibitPage)) | |
{ | |
// page is empty, look for the next page | |
$targetPage = null; | |
if ($nextPage = $exhibitPage->firstChildOrNext()) | |
{ | |
$targetPage = $nextPage; | |
} | |
elseif ($exhibitPage->parent_id) | |
{ | |
$parentPage = $exhibitPage->getParent(); | |
$nextParentPage = $parentPage->next(); | |
if ($nextParentPage) | |
{ | |
$targetPage = $nextPage; | |
} | |
} | |
return my_exhibit_builder_get_first_nonempty_page($exhibit, $targetPage); | |
} | |
// page is not empty, so return it | |
return $exhibitPage; | |
} | |
/** | |
* Return a link to the next exhibit page | |
* Customized from ExhibitBuilder 2.0 code | |
* | |
* @param string $text Link text | |
* @param array $props Link attributes | |
* @param ExhibitPage $exhibitPage If null, will use the current exhibit page | |
* @return string | |
*/ | |
function my_exhibit_builder_link_to_next_page($text = null, $props = array(), $exhibitPage = null) | |
{ | |
if (!$exhibitPage) { | |
$exhibitPage = get_current_record('exhibit_page'); | |
} | |
$exhibit = get_record_by_id('Exhibit', $exhibitPage->exhibit_id); | |
$targetPage = null; | |
// if page object exists, grab link to the first child page if exists. If it doesn't, grab | |
// a link to the next page | |
if ($nextPage = $exhibitPage->firstChildOrNext()) { | |
$targetPage = $nextPage; | |
} elseif ($exhibitPage->parent_id) { | |
$parentPage = $exhibitPage->getParent(); | |
$nextParentPage = $parentPage->next(); | |
if ($nextParentPage) { | |
$targetPage = $nextPage; | |
} | |
} | |
// CUSTOM | |
// IF PAGE IS EMPTY, SKIP IT! | |
// THIS ALLOWS SECTION HEADERS | |
if ($targetPage && my_exhibit_builder_exhibit_page_is_empty($targetPage)) { | |
return my_exhibit_builder_link_to_next_page($text, $props, $targetPage); | |
} | |
if ($targetPage) { | |
if (!isset($props['class'])) { | |
$props['class'] = 'next-page'; | |
} | |
if ($text === null) { | |
$text = metadata($targetPage, 'title') . ' »'; | |
} | |
return exhibit_builder_link_to_exhibit($exhibit, $text, $props, $targetPage); | |
} | |
return null; | |
} | |
/** | |
* Return a link to the previous exhibit page | |
* Customized from ExhibitBuilder 2.0 code | |
* | |
* @param string $text Link text | |
* @param array $props Link attributes | |
* @param ExhibitPage $exhibitPage If null, will use the current exhibit page | |
* @return string | |
*/ | |
function my_exhibit_builder_link_to_previous_page($text = null, $props = array(), $exhibitPage = null) | |
{ | |
if (!$exhibitPage) { | |
$exhibitPage = get_current_record('exhibit_page'); | |
} | |
$exhibit = get_record_by_id('Exhibit', $exhibitPage->exhibit_id); | |
// If page object exists, grab link to previous exhibit page if exists. If it doesn't, grab | |
// a link to the last page on the previous parent page, or the exhibit if at top level | |
if ($previousPage = $exhibitPage->previousOrParent()) { | |
// CUSTOM | |
// IF PAGE IS EMPTY, SKIP IT! | |
// THIS ALLOWS SECTION HEADERS | |
if (my_exhibit_builder_exhibit_page_is_empty($previousPage)){ | |
return my_exhibit_builder_link_to_previous_page($text, $props, $previousPage); | |
} | |
if(!isset($props['class'])) { | |
$props['class'] = 'previous-page'; | |
} | |
if ($text === null) { | |
$text = '« ' . metadata($previousPage, 'title'); | |
} | |
return exhibit_builder_link_to_exhibit($exhibit, $text, $props, $previousPage); | |
} | |
return null; | |
} | |
function my_exhibit_builder_is_page_ancestor($page, $parent) | |
{ | |
if (!$page) return false; | |
while ($page->parent_id) | |
{ | |
$page = $page->getParent(); | |
if ($page->id == $parent->id) | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Returns the HTML for a nested navigation for exhibit sections and pages | |
* Customized for my from ExhibitBuilder 1.x code, updated for Omeka 2.0 | |
* Top level pages are treated as "sections" (eliminated in Omeka 2.0) | |
* | |
* @param Exhibit|null $exhibit If null, will use the current exhibit | |
* @param boolean $showAllPages | |
* @param integer $cols The number of columns to split the menu | |
* @param ExhibitPage $exhibitPage Page to mark as selected | |
* @return string | |
**/ | |
function my_exhibit_builder_nested_nav($exhibit = null, $showAllPages = false, $cols = 2, $exhibitPage = null) | |
{ | |
if (!$exhibit) { | |
if (!($exhibit = get_current_record('exhibit'))) { | |
return; | |
} | |
} | |
if (!$exhibitPage) { | |
$exhibitPage = get_current_record('exhibit_page', 0); | |
} | |
$tot = count($exhibit->getTopPages()); | |
if ($tot <= $cols) $cols = 1; | |
$ctr = 0; | |
$html = '<ul class="exhibit-section-nav">'; | |
foreach ($exhibit->getTopPages() as $exhibitTopPage) | |
{ | |
if ($ctr == ceil($tot / $cols)) | |
{ | |
$html .= '</ul><ul class="exhibit-section-nav">'; | |
$ctr = 1; | |
} | |
else | |
{ | |
$ctr = $ctr + 1; | |
} | |
// note that the link is to the next non-empty page if the page is empty | |
$html .= '<li class="exhibit-nested-section' . ($exhibitPage->id == $exhibitTopPage->id || my_exhibit_builder_is_page_ancestor($exhibitPage, $exhibitTopPage) ? ' current' : '') . '"><a class="exhibit-section-title" href="' . html_escape(exhibit_builder_exhibit_uri($exhibit, my_exhibit_builder_get_first_nonempty_page($exhibit, $exhibitTopPage))) . '">' . str_replace('/', '/<wbr>', html_escape($exhibitTopPage->title)) . '</a>'; | |
if ($showAllPages || $exhibitPage->id == $exhibitTopPage->id || my_exhibit_builder_is_page_ancestor($exhibitPage, $exhibitTopPage)) | |
{ | |
$html .= my_exhibit_builder_child_page_nav($exhibitTopPage); | |
} | |
$html .= '</li>'; | |
} | |
$html .= '</ul>'; | |
// $html = apply_filters('exhibit_builder_nested_nav', $html, $exhibit, $showAllPages); | |
return $html; | |
} | |
/** | |
* Return the markup for the exhibit child page navigation. | |
* | |
* @param ExhibitPage|null $exhibitPage If null, uses the current exhibit page | |
* @return string | |
*/ | |
function my_exhibit_builder_child_page_nav($exhibitPage = null, $currentPage = null) | |
{ | |
if (!$exhibitPage) { | |
$exhibitPage = get_current_record('exhibit_page'); | |
} | |
if (!$currentPage) { | |
$currentPage = get_current_record('exhibit_page', 0); | |
} | |
$exhibit = $exhibitPage->getExhibit(); | |
$children = exhibit_builder_child_pages($exhibitPage); | |
$html = '<ul class="exhibit-child-nav navigation">' . "\n"; | |
foreach ($children as $child) { | |
$html .= '<li class="' . ((@$currentPage->id == $child->id) ? 'current' : '') . '"><a href="' . html_escape(exhibit_builder_exhibit_uri($exhibit, $child)) . '">' . html_escape($child->title) . '</a></li>'; | |
} | |
$html .= '</ul>' . "\n"; | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment