Browse files

Initial commit

  • Loading branch information...
0 parents commit 9ddb100515cc7e96751acfdb46c98028d46065b2 @marijnkampf committed Oct 12, 2011
46 README.md
@@ -0,0 +1,46 @@
+# Breadcrumb Navigation
+
+## Maintainers
+
+ * Marijn Kampf (Nickname: marijnkampf)
+ <marijn at exadium dot com>
+
+ http://www.exadium.com/tools/silverstripe/modules/breadcrumb-navigation/
+
+ Sponsored by Exadium Web Development
+
+## Introduction
+
+Self contained Breadcrum Navigation module, allowing you to control breadcrumbs using templates, rather than from code.
+
+## Requirements
+
+ * SilverStripe Trunk
+
+## Install and setup
+
+ * BreadcrumbNavigation should be in your sites root folder.
+ * Set options in your mysite/_config.php
+ BreadcrumbNavigation::$includeHome = false;
+ BreadcrumbNavigation::$includeSelf = true;
+ BreadcrumbNavigation::$maxDepth = 20;
+ BreadcrumbNavigation::$stopAtPageType = false;
+ BreadcrumbNavigation::$showHidden = false;
+ BreadcrumbNavigation::$homeURLSegment = 'home';
+
+ * In your template include either:
+ <% include BreadcrumbNavigationTemplate %>
+ or
+ <% include BreadcrumbNavigationTemplateAllLinked %>
+
+## Advanced use
+If you would like to add additional items to the Breadcrumb trail (e.g. for URL parameter actions) you can use AddAfter($object) and AddBefore($object) functions.
+You only need to define the attributes you use in your templates. These are Link and MenuTitle for the supplied templates.
+Define isSelf if you are not linking the current page.
+
+ $do = new DataObject();
+ $do->Link = $this->Link() . "show";
+ $do->MenuTitle = "Menu title";
+ $do->isSelf = true;
+ $this->AddAfter($do);
+
23 _config.php
@@ -0,0 +1,23 @@
+<?php
+
+/**
+ * BreadcrumbNavigation config file
+ *
+ * Maintained by Marijn Kampf (Nickname: marijnkampf) <marijn at exadium dot com>
+ * http://www.exadium.com/tools/silverstripe/modules/breadcrumb-navigation/
+ *
+ * Sponsored by Exadium Web Development
+ */
+
+ Object::add_extension('SiteTree', 'BreadcrumbNavigation');
+
+/*
+ Set the following options in mysite/_config.php.
+
+ BreadcrumbNavigation::$includeHome = false; // Include homepage in breadcrumbs.
+ BreadcrumbNavigation::$includeSelf = true; // Include the current page in breadcrumbs. See README.md for option to link / not link.
+ BreadcrumbNavigation::$maxDepth = 20; // Maximum depth to traverse
+ BreadcrumbNavigation::$stopAtPageType = false; // ClassName of a page to stop the upwards traversal.
+ BreadcrumbNavigation::$showHidden = false; // Include pages marked with the attribute ShowInMenus = 0.
+ BreadcrumbNavigation::$homeURLSegment = 'home'; // URL Segment to use for homepage.
+*/
95 code/BreadcrumbNavigation.php
@@ -0,0 +1,95 @@
+<?php
+
+/**
+ * @package BreadcrumbNavigation
+ */
+
+class BreadcrumbNavigation extends DataObjectDecorator {
+ private $initialised = false;
+
+ static $includeHome = true;
+ static $includeSelf = true;
+ static $maxDepth = 20;
+ static $stopAtPageType = false;
+ static $showHidden = false;
+ static $homeURLSegment = 'home';
+
+ public $hasHome = false;
+
+ public $parentPages = null;
+ protected $isSelf = false;
+
+ function __construct($record = null, $isSingleton = false) {
+ parent::__construct($record, $isSingleton);
+ }
+
+ /**
+ * Initialises the BreadcrumbNavigation class. Only called when Breadcrumbs are actually used.
+ *
+ * @return the number of parent pages
+ */
+ private function initialise() {
+ if (!$this->initialised) {
+ $this->parentPages = new DataObjectSet();
+ $page = $this->owner;
+ $i = 0;
+ while(
+ $page
+ && (!BreadcrumbNavigation::$maxDepth || sizeof($this->parentPages) < BreadcrumbNavigation::$maxDepth)
+ && (!BreadcrumbNavigation::$stopAtPageType || $page->ClassName != BreadcrumbNavigation::$stopAtPageType)
+ ) {
+ if(BreadcrumbNavigation::$showHidden || $page->ShowInMenus || ($page->ID == $this->owner->ID)) {
+ if($page->URLSegment == BreadcrumbNavigation::$homeURLSegment) $this->hasHome = true;
+ if ($page->ID == $this->owner->ID) $page->isSelf = true;
+
+ if ((!$page->isSelf) || ($page->isSelf) && (BreadcrumbNavigation::$includeSelf))
+ $this->parentPages->unshift($page);
+ }
+ $page = $page->Parent;
+ }
+ if ((!$this->hasHome) && (BreadcrumbNavigation::$includeHome)) $this->parentPages->unshift(DataObject::get_one('SiteTree', "`URLSegment` = '" . BreadcrumbNavigation::$homeURLSegment . "'"));
+
+ $this->initialised = true;
+ }
+ return count($this->parentPages);
+ }
+
+
+ /**
+ * Return parent pages for Breadcrumb Navigation.
+ *
+ * @return DataObjectSet of parent pages
+ */
+ function BreadcrumbNavigation() {
+ $this->initialise();
+ return $this->parentPages;
+ }
+
+ /**
+ * Adds one or more pages as child(ren) to end of parent pages.
+ *
+ * @param mixed $object array of or single object to add
+ */
+ function AddAfter($object) {
+ $this->initialise();
+ foreach($this->parentPages as $page) {
+ $page->isSelf = false;
+ }
+ if (is_array($object)) foreach ($object as $obj) $this->parentPages->push($obj);
+ else $this->parentPages->push($object);
+ }
+
+ /**
+ * Adds one or more pages as parent(s) to beginning of parent pages.
+ *
+ * @param mixed $object array of or single object to add
+ */
+ function AddBefore($object) {
+ $this->initialise();
+ foreach($this->parentPages as $page) {
+ $page->isSelf = false;
+ }
+ if (is_array($object)) foreach ($object as $obj) $this->parentPages->unshift($obj);
+ else $this->parentPages->unshift($object);
+ }
+}
1 templates/BreadcrumbNavigationTemplate.ss
@@ -0,0 +1 @@
+<% if BreadcrumbNavigation %><div class="breadcrumbnavigation"><% control BreadcrumbNavigation %><span class="$FirstLast"><% if isSelf %>$MenuTitle<% else %><a href="$Link">$MenuTitle</a><% end_if %></span> <% if Last %><% else %> &raquo; <% end_if %><% end_control %></div><% end_if %>
1 templates/BreadcrumbNavigationTemplateAllLinked.ss
@@ -0,0 +1 @@
+<% if BreadcrumbNavigation %><div class="breadcrumbnavigation"><% control BreadcrumbNavigation %><span class="$FirstLast"><a href="$Link">$MenuTitle</a></span> <% if Last %><% else %> &raquo; <% end_if %><% end_control %></div><% end_if %>

0 comments on commit 9ddb100

Please sign in to comment.