SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/RelatedLinksWidget.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Widgets;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Helpers\Enc;
  19. use Sprout\Helpers\Navigation;
  20. use Sprout\Helpers\TreenodeInMenuMatcher;
  21. use Sprout\Helpers\UserPerms;
  22. use Sprout\Helpers\Url;
  23.  
  24.  
  25. /**
  26. * Shows a list of pages that are related to this one
  27. **/
  28. class RelatedLinksWidget extends Widget
  29. {
  30. protected $friendly_name = "Related links";
  31.  
  32.  
  33. /**
  34.   * Does the front-end rendering of this widget
  35.   *
  36.   * @param int $orientation The orientation of the widget
  37.   **/
  38. public function render($orientation)
  39. {
  40. $root_node = Navigation::getRootNode();
  41. if ($root_node == null) return;
  42.  
  43. $matcher = Navigation::getPageNodeMatcher();
  44. if ($matcher == null) return;
  45.  
  46. $page_node = $root_node->findNode($matcher);
  47. if ($page_node == null) return;
  48.  
  49. $ancestors = $page_node->findAncestors();
  50. $top_anc = $ancestors[0];
  51.  
  52. $top_anc->filterChildren(new TreenodeInMenuMatcher());
  53.  
  54. if (count($top_anc->children) == 0) {
  55. $top_anc->removeFilter();
  56. return null;
  57. }
  58.  
  59. $out = Kohana::config('sprout.related_heading');
  60. $out = str_replace('SECTION', Enc::html($top_anc->getNavigationName()), $out);
  61.  
  62. $out .= "<ul class=\"depth1\">";
  63.  
  64. // Top-parent, either page name (TRUE) or custom text (any string)
  65. $top = Kohana::config('sprout.related_top');
  66. if ($top) {
  67. $classes = 'depth1';
  68. if ($page_node === $top_anc) $classes .= ' on';
  69.  
  70. $page_title = Enc::html($top_anc->getNavigationName());
  71. $page_url = Enc::html($top_anc->getFriendlyUrl());
  72.  
  73. if ($top === true) {
  74. $out .= "<li class=\"{$classes}\"><a href=\"{$page_url}\">{$page_title}</a></li>";
  75. } else {
  76. $out .= "<li class=\"{$classes}\"><a href=\"{$page_url}\">" . Enc::html($top) . "</a></li>";
  77. }
  78. }
  79.  
  80. foreach ($top_anc->children as $page) {
  81. if (! UserPerms::checkPermissionsTree('pages', $page['id'])) continue;
  82.  
  83. $out .= self::drawnode ($page, 1, $ancestors);
  84. }
  85. $out .= "</ul>";
  86.  
  87. $top_anc->removeFilter();
  88.  
  89. return $out;
  90. }
  91.  
  92.  
  93. /**
  94.   * Draws a single item, and its sub-items
  95.   *
  96.   * @param TreeNode $node The node to draw
  97.   * @param int $depth The depth of the current node
  98.   * @param array $ancestors The ancestors of the current page node, for highlighting.
  99.   **/
  100. static private function drawnode($node, $depth, &$ancestors)
  101. {
  102. $classes = 'depth' . $depth;
  103.  
  104. $max_depth = Kohana::config('sprout.related_max_depth');
  105. $new_depth = $depth + 1;
  106.  
  107. $node_title = Enc::html($node->getNavigationName());
  108. $node_url = Enc::html($node->getFriendlyUrl());
  109.  
  110. // If the page is the current item
  111. if (Url::current() === $node->getFriendlyUrlNoprefix()) {
  112. $classes .= ' on';
  113. } else if (in_array($node, $ancestors, true)) {
  114. $classes .= ' ancestor';
  115. }
  116.  
  117. if (in_array($node, $ancestors, true)) {
  118.  
  119. // Items that are part of the ancestory of the current page
  120. $out = "<li class=\"{$classes}\"><a href=\"{$node_url}\">{$node_title}</a>";
  121.  
  122. if (($max_depth === null or $new_depth <= $max_depth) and count($node->children)) {
  123. $out .= "<ul class=\"depth{$new_depth}\">";
  124. foreach ($node->children as $node) {
  125. if (! UserPerms::checkPermissionsTree('pages', $node['id'])) continue;
  126.  
  127. $out .= self::drawnode($node, $new_depth, $ancestors);
  128. }
  129. $out .= "</ul>";
  130. }
  131.  
  132. $out .= "</li>";
  133.  
  134. } else {
  135. // Everything else
  136. $out = "<li class=\"{$classes}\"><a href=\"{$node_url}\">{$node_title}</a></li>";
  137. }
  138.  
  139. return $out;
  140. }
  141.  
  142. }
  143.  
  144.  
  145.  
  146.