SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/SitemapWidget.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 Sprout\Helpers\Enc;
  17. use Sprout\Helpers\Navigation;
  18. use Sprout\Helpers\TreenodeInMenuMatcher;
  19.  
  20.  
  21. /**
  22. * Shows a site map
  23. **/
  24. class SitemapWidget extends Widget
  25. {
  26. protected $friendly_name = "Sitemap";
  27. protected $friendly_desc = 'A complete sitemap of the website';
  28.  
  29.  
  30. /**
  31.   * Does the front-end rendering of this widget
  32.   *
  33.   * @param int $orientation The orientation of the widget
  34.   **/
  35. public function render($orientation)
  36. {
  37. $root_node = Navigation::getRootNode();
  38. if ($root_node == null) return;
  39.  
  40. $root_node->filterChildren(new TreenodeInMenuMatcher());
  41.  
  42. $out = "<ul class=\"depth1\">";
  43. foreach ($root_node->children as $page) {
  44. $out .= self::drawnode ($page, 1, $ancestors);
  45. }
  46. $out .= "</ul>";
  47.  
  48. $root_node->removeFilter();
  49.  
  50. return $out;
  51. }
  52.  
  53.  
  54. /**
  55.   * Draws a single item, and its sub-items
  56.   *
  57.   * @param TreeNode $node The node to draw
  58.   * @param int $depth The depth of the current node
  59.   * @param array $ancestors The ancestors of the current page node, for highlighting.
  60.   **/
  61. static private function drawnode($node, $depth, &$ancestors)
  62. {
  63. $classes = 'depth' . $depth;
  64.  
  65. $node_title = Enc::html($node->getNavigationName());
  66. $node_url = Enc::html($node->getFriendlyUrl());
  67.  
  68. $out = "<li class=\"{$classes}\"><a href=\"{$node_url}\">{$node_title}</a>";
  69.  
  70. if (count($node->children)) {
  71. $new_depth = $depth + 1;
  72. $out .= "<ul class=\"depth{$new_depth}\">";
  73. foreach ($node->children as $node) {
  74. $out .= self::drawnode($node, $new_depth, $ancestors);
  75. }
  76. $out .= "</ul>";
  77. }
  78.  
  79. $out .= "</li>";
  80.  
  81. return $out;
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88.