breadcrumb.snippet.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * BreadCrumb
  4. * Copyright 2011 Benjamin Vauchel <contact@omycode.fr>
  5. *
  6. * BreadCrumb is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option) any
  9. * later version.
  10. *
  11. * BreadCrumb is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  13. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * BreadCrumb; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  17. * Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * @package breadcrumb
  20. * @author Benjamin Vauchel <contact@omycode.fr>
  21. *
  22. * @version Version 1.4.3 pl
  23. * 07/03/15
  24. *
  25. * Breadcrumb is a snippet for MODx Revolution, inspired by the Jared's BreadCrumbs snippet.
  26. * It will create a breadcrumb navigation for the current resource or a specific resource.
  27. *
  28. * Optional properties:
  29. *
  30. * @property from - (int) Resource ID of the first crumb; [Default value : 0].
  31. * @property to - (int) Resource ID of the last crumb; [Default value : current resource id].
  32. * @property exclude - (string) Comma separated list of resources IDs not shown in breadcrumb. [Default value : []]
  33. * @property maxCrumbs - (int) Max crumbs shown in breadcrumb. Max delimiter template can be customize with property maxCrumbTpl ; [Default value : 100].
  34. * @property showHidden - (bool) Show hidden resources in breadcrumb; [Default value : true].
  35. * @property showContainer - (bool) Show container resources in breadcrumb; [Default value : true].
  36. * @property showUnPub - (bool) Show unpublished resources in breadcrumb; [Default value : true].
  37. * @property showCurrentCrumb - (bool) Show current resource as a crumb; [Default value : true].
  38. * @property showBreadCrumbAtHome - (bool) Show BreadCrumb on the home page; [Default value : true].
  39. * @property showHomeCrumb - (bool) Add the home page crumb at the start of the breadcrumb; [Default value : true].
  40. * @property useWebLinkUrl - (bool) Use the weblink url instead of the url to the weblink; [Default value : true].
  41. * @property direction - (string) Direction or breadcrumb : Left To Right (ltr) or Right To Left (rtl) for Arabic language for example; [Default value : ltr].
  42. * @property scheme - (string) URL Generation Scheme; [Default value : -1].
  43. *
  44. * Templates :
  45. *
  46. * @property containerTpl - (string) Container template for BreadCrumb; [Default value : BreadCrumbContainerTpl].
  47. * @property currentCrumbTpl - (string) Current crumb template for BreadCrumb; [Default value : BreadCrumbCurrentCrumbTpl].
  48. * @property linkCrumbTpl - (string) Default crumb template for BreadCrumb; [Default value : BreadCrumbLinkCrumbTpl].
  49. * @property categoryCrumbTpl - (string) Default category crumb template for BreadCrumb; [Default value : BreadCrumbCategoryCrumbTpl].
  50. * @property maxCrumbTpl - (string) Max delimiter crumb template for BreadCrumb; [Default value : BreadCrumbMaxCrumbTpl].
  51. */
  52. // Script Properties
  53. $from = !empty($from) ? $from : $modx->getOption('from', $scriptProperties, 0, true, true);
  54. $to = $currentResourceId = !empty($to) ? $to : $modx->getOption('to', $scriptProperties, $modx->resource->get('id'), true);
  55. $exclude = !empty($exclude) ? explode(',', $exclude) : array();
  56. $maxCrumbs = !empty($maxCrumbs) ? abs(intval($maxCrumbs)) : $modx->getOption('maxCrumbs', $scriptProperties, 100, true);
  57. $showHidden = isset($showHidden) ? (bool)$showHidden : (bool)$modx->getOption('showHidden', $scriptProperties, true, true);
  58. $showContainer = isset($showContainer) ? (bool)$showContainer : (bool)$modx->getOption('showContainer', $scriptProperties, true, true);
  59. $showUnPub = isset($showUnPub) ? (bool)$showUnPub : (bool)$modx->getOption('showUnPub', $scriptProperties, true, true);
  60. $showCurrentCrumb = isset($showCurrentCrumb) ? (bool)$showCurrentCrumb : (bool)$modx->getOption('showCurrentCrumb', $scriptProperties, true, true);
  61. $showBreadCrumbAtHome = isset($showBreadCrumbAtHome) ? (bool)$showBreadCrumbAtHome : (bool)$modx->getOption('showBreadCrumbAtHome', $scriptProperties, true, true);
  62. $showHomeCrumb = isset($showHomeCrumb) ? (bool)$showHomeCrumb : (bool)$modx->getOption('showHomeCrumb', $scriptProperties, true, true);
  63. $useWebLinkUrl = isset($useWebLinkUrl) ? (bool)$useWebLinkUrl : (bool)$modx->getOption('useWebLinkUrl', $scriptProperties, true, true);
  64. $direction = !empty($direction) ? $direction : $modx->getOption('direction', $scriptProperties, 'ltr', true);
  65. $scheme = !empty($scheme) ? $scheme : $modx->getOption('scheme', $scriptProperties, $modx->getOption('link_tag_scheme'), true);
  66. $containerTpl = !empty($containerTpl) ? $containerTpl : $modx->getOption('containerTpl', $scriptProperties, '@INLINE <ul id="breadcrumb" itemprop="breadcrumb">[[+crumbs]]</ul>');
  67. $homeCrumbTpl = !empty($homeCrumbTpl) ? $homeCrumbTpl : $modx->getOption('homeCrumbTpl', $scriptProperties, '@INLINE <li><a href="[[+link]]">[[+pagetitle]]</a></li>');
  68. $currentCrumbTpl = !empty($currentCrumbTpl) ? $currentCrumbTpl : $modx->getOption('currentCrumbTpl', $scriptProperties, '@INLINE <li>[[+pagetitle]]</li>');
  69. $linkCrumbTpl = !empty($linkCrumbTpl) ? $linkCrumbTpl : $modx->getOption('linkCrumbTpl', $scriptProperties, '@INLINE <li><a href="[[+link]]">[[+pagetitle]]</a></li>');
  70. $categoryCrumbTpl = !empty($categoryCrumbTpl) ? $categoryCrumbTpl : $modx->getOption('categoryCrumbTpl', $scriptProperties, '@INLINE <li><a href="[[+link]]">[[+pagetitle]]</a></li>');
  71. $maxCrumbTpl = !empty($maxCrumbTpl) ? $maxCrumbTpl : $modx->getOption('maxCrumbTpl', $scriptProperties, '@INLINE <li>...</li>');
  72. // include parseTpl
  73. include_once $modx->getOption('breadcrumb.core_path',null,$modx->getOption('core_path').'components/breadcrumb/includes/').'include.parsetpl.php';
  74. // Output variable
  75. $output = '';
  76. // We check if current resource is the homepage and if breadcrumb is shown for the homepage
  77. if (!$showBreadCrumbAtHome && $modx->resource->get('id') == $modx->getOption('site_start')) {
  78. return '';
  79. }
  80. // We get all the other crumbs
  81. $crumbs = array();
  82. $crumbsCount = 0;
  83. $resourceId = $to;
  84. while ($resourceId != $from && $crumbsCount < $maxCrumbs)
  85. {
  86. if (!$resource = $modx->getObject('modResource', $resourceId)) {
  87. break;
  88. }
  89. // We check the conditions to show crumb
  90. if (
  91. $resourceId != $modx->getOption('site_start') // ShowHomeCrumb
  92. && (($resource->get('hidemenu') && $showHidden) || !$resource->get('hidemenu')) // ShowHidden
  93. && (($resource->get('isfolder') && $showContainer) || !$resource->get('isfolder')) // ShowContainer
  94. && ((!$resource->get('published') && $showUnPub) || $resource->get('published')) // UnPub
  95. && (($resourceId == $currentResourceId && $showCurrentCrumb) || $resourceId != $currentResourceId) // ShowCurrent
  96. && !in_array($resourceId, $exclude) // Excluded resources
  97. ) {
  98. // If is LTR direction, we push resource at the beginning of the array
  99. if ($direction == 'ltr') {
  100. array_unshift($crumbs, $resource);
  101. }
  102. // Else we push it at the end
  103. else {
  104. $crumbs[] = $resource;
  105. }
  106. $crumbsCount++;
  107. }
  108. $resourceId = $resource->get('parent');
  109. }
  110. // Add home crumb
  111. if ($showHomeCrumb && $resource = $modx->getObject('modResource', $modx->getOption('site_start'))) {
  112. if ($direction == 'ltr') {
  113. array_unshift($crumbs, $resource);
  114. } else {
  115. $crumbs[] = $resource;
  116. }
  117. }
  118. // We build the output of crumbs
  119. foreach($crumbs as $key => $resource)
  120. {
  121. // Home crumb tpl ?
  122. if ($resource->get('id') == $modx->getOption('site_start'))
  123. {
  124. $tpl = $homeCrumbTpl;
  125. }
  126. // Current crumb tpl ?
  127. elseif ($showCurrentCrumb && ($resource->get('id') == $currentResourceId))
  128. {
  129. $tpl = $currentCrumbTpl;
  130. }
  131. // resource is a container only, calculated in a similar manner to Wayfinder
  132. elseif ($resource->get('isfolder')
  133. && ( $resource->get('template') == 0
  134. || strpos($resource->get('link_attributes'), 'rel="category"') !== false
  135. )
  136. ) {
  137. $tpl = $categoryCrumbTpl;
  138. }
  139. // or default crumb tpl ?
  140. else {
  141. $tpl = $linkCrumbTpl;
  142. }
  143. // Placeholders
  144. $placeholders = $resource->toArray();
  145. if ($resource->get('class_key') == 'modWebLink' && $useWebLinkUrl) {
  146. if (is_numeric($resource->get('content'))) {
  147. $link = $modx->makeUrl($resource->get('content'), '', '', $scheme);
  148. } else {
  149. $link = $resource->get('content');
  150. }
  151. } else {
  152. $link = $modx->makeUrl($resource->get('id'), '', '', $scheme);
  153. }
  154. $placeholders = array_merge($resource->toArray(), array('link' => $link));
  155. // Output
  156. $output .= parseTpl($tpl, $placeholders);
  157. }
  158. // We add the max delimiter to the crumbs output, if the max limit was reached
  159. if ($crumbsCount == $maxCrumbs) {
  160. // If is LTR direction, we push the max delimiter at the beginning of the crumbs
  161. if ($direction == 'ltr') {
  162. $output = parseTpl($maxCrumbTpl).$output;
  163. }
  164. // Else we push it at the end
  165. else {
  166. $output .= parseTpl($maxCrumbTpl);
  167. }
  168. }
  169. // We build the breadcrumb output
  170. $output = parseTpl($containerTpl, array(
  171. 'crumbs' => $output,
  172. ));
  173. return $output;
  174. ?>