gordonbanderson /
Silverstripe-Module-BreadcrumbNavigation
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @package BreadcrumbNavigation SS 3.0 |
||
| 5 | */ |
||
| 6 | |||
| 7 | class BreadcrumbNavigation extends DataExtension |
||
| 8 | { |
||
| 9 | private $initialised = false; |
||
| 10 | |||
| 11 | public static $includeHome = true; |
||
| 12 | public static $includeSelf = true; |
||
| 13 | public static $maxDepth = 20; |
||
| 14 | public static $stopAtPageType = false; |
||
| 15 | public static $showHidden = false; |
||
| 16 | public static $homeURLSegment = 'home'; |
||
| 17 | |||
| 18 | public $hasHome = false; |
||
| 19 | |||
| 20 | public $parentPages = null; |
||
| 21 | protected $isSelf = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Initialises the BreadcrumbNavigation class. Only called when Breadcrumbs are actually used. |
||
| 25 | * |
||
| 26 | * @return ArrayList of parent pages |
||
| 27 | */ |
||
| 28 | public function Pages() |
||
| 29 | { |
||
| 30 | if (!$this->initialised) { |
||
| 31 | $this->parentPages = array(); |
||
| 32 | $page = $this->owner; |
||
| 33 | $i = 0; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 34 | while ( |
||
| 35 | $page |
||
| 36 | && (!self::$maxDepth || sizeof($this->parentPages) < self::$maxDepth) |
||
| 37 | && (!self::$stopAtPageType || $page->ClassName != self::$stopAtPageType) |
||
| 38 | ) { |
||
| 39 | if (self::$showHidden || $page->ShowInMenus || ($page->ID == $this->owner->ID)) { |
||
| 40 | if ($page->URLSegment == self::$homeURLSegment) { |
||
| 41 | $this->hasHome = true; |
||
| 42 | } |
||
| 43 | if ($page->ID == $this->owner->ID) { |
||
| 44 | $page->isSelf = true; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ((!$page->isSelf) || ($page->isSelf) && (self::$includeSelf)) { |
||
| 48 | array_unshift($this->parentPages, $page); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | $page = $page->Parent; |
||
| 52 | } |
||
| 53 | if ((!$this->hasHome) && (self::$includeHome)) { |
||
| 54 | array_unshift($this->parentPages, DataObject::get_one('SiteTree', "`URLSegment` = '" . self::$homeURLSegment . "'")); |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->initialised = true; |
||
| 58 | } |
||
| 59 | return new ArrayList($this->parentPages); |
||
| 60 | } |
||
| 61 | |||
| 62 | public static function CreateBreadcrumb($menuTitle, $link, $isSelf) |
||
| 63 | { |
||
| 64 | $do = new DataObject(); |
||
| 65 | $do->Link = $link; |
||
| 66 | $do->MenuTitle = $menuTitle; |
||
| 67 | $do->isSelf = $isSelf; |
||
| 68 | return $do; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Adds one or more pages as child(ren) to end of parent pages. |
||
| 73 | * |
||
| 74 | * @param mixed $object array of or single object to add |
||
| 75 | * @param bool $unique Removes duplicate values from breadcrumb trail |
||
| 76 | */ |
||
| 77 | public function AddBreadcrumbAfter($object, $unique = false) |
||
| 78 | { |
||
| 79 | $this->Pages(); |
||
| 80 | foreach ($this->parentPages as $page) { |
||
| 81 | $page->isSelf = false; |
||
| 82 | } |
||
| 83 | if (is_array($object)) { |
||
| 84 | foreach ($object as $obj) { |
||
| 85 | array_push($this->parentPages, $obj); |
||
| 86 | } |
||
| 87 | } else { |
||
| 88 | array_push($this->parentPages, $object); |
||
| 89 | } |
||
| 90 | if ($unique) { |
||
| 91 | $this->parentPages = array_unique($this->parentPages); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds one or more pages as parent(s) to beginning of parent pages. |
||
| 97 | * |
||
| 98 | * @param mixed $object array of or single object to add |
||
| 99 | * @param bool $unique Removes duplicate values from breadcrumb trail |
||
| 100 | */ |
||
| 101 | public function AddBreadcrumbBefore($object, $unique = false) |
||
| 102 | { |
||
| 103 | $this->Pages(); |
||
| 104 | foreach ($this->parentPages as $page) { |
||
| 105 | $page->isSelf = false; |
||
| 106 | } |
||
| 107 | if (is_array($object)) { |
||
| 108 | foreach ($object as $obj) { |
||
| 109 | array_unshift($this->parentPages, $obj); |
||
| 110 | } |
||
| 111 | } else { |
||
| 112 | array_unshift($this->parentPages, $object); |
||
| 113 | } |
||
| 114 | if ($unique) { |
||
| 115 | $this->parentPages = array_unique($this->parentPages); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 |