SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/ChildrenPagesWidget.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. * Shows a list of pages that are related to this one
  22. **/
  23. class ChildrenPagesWidget extends Widget
  24. {
  25. protected $friendly_name = "Children pages";
  26. protected $friendly_desc = 'An list of the children pages as a textual list';
  27.  
  28.  
  29. /**
  30.   * Does the front-end rendering of this widget
  31.   *
  32.   * @param int $orientation The orientation of the widget
  33.   **/
  34. public function render($orientation)
  35. {
  36. $this->settings['max_depth'] = (int) @$this->settings['max_depth'];
  37. if ($this->settings['max_depth'] <= 0) $this->settings['max_depth'] = 1;
  38.  
  39. $root_node = Navigation::getRootNode();
  40. if ($root_node == null) return;
  41.  
  42. $matcher = Navigation::getPageNodeMatcher();
  43. if ($matcher == null) return;
  44.  
  45. $page_node = $root_node->findNode($matcher);
  46. if ($page_node == null) return;
  47.  
  48. $page_node->filterChildren(new TreenodeInMenuMatcher());
  49.  
  50. if (count($page_node->children) == 0) {
  51. $page_node->removeFilter();
  52. return null;
  53. }
  54.  
  55. $out = "<ul>";
  56. foreach ($page_node->children as $page) {
  57. $page_url = Enc::html($page->getFriendlyUrl());
  58. $page_title = Enc::html($page->getNavigationName());
  59. $out .= "<li><a href=\"{$page_url}\">{$page_title}</a></li>";
  60. }
  61. $out .= "</ul>";
  62.  
  63. $page_node->removeFilter();
  64.  
  65. return $out;
  66. }
  67.  
  68. }
  69.  
  70.  
  71.  
  72.