SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Pagenode.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\Helpers;
  15.  
  16. use Exception;
  17.  
  18. use Kohana;
  19.  
  20.  
  21. /**
  22. * Represents a page in a page tree
  23. **/
  24. class Pagenode extends Treenode
  25. {
  26.  
  27. /**
  28.   * Not valid
  29.   **/
  30. public static function loadTree($table, array $where = ['1'], $order = 'record_order')
  31. {
  32. throw new Exception("Method not supported for page trees, use Navigation::loadPageTree instead.");
  33. }
  34.  
  35. /**
  36.   * Returns the friendly url for a node
  37.   * @return string
  38.   **/
  39. public function getFriendlyUrl()
  40. {
  41. if (!empty($this->data['redirect'])) {
  42. return Lnk::url($this->data['redirect']);
  43. }
  44.  
  45. $anc = $this->findAncestors();
  46.  
  47. $parts = array();
  48. foreach ($anc as $node) {
  49. $parts[] = $node->getUrlName();
  50. }
  51.  
  52. return Kohana::config('core.site_domain', TRUE) . SubsiteSelector::$url_prefix . implode('/', $parts);
  53. }
  54.  
  55. /**
  56.   * Returns the friendly url for a node - but don't include a directory prefix
  57.   * @return string
  58.   **/
  59. public function getFriendlyUrlNoprefix()
  60. {
  61. $anc = $this->findAncestors();
  62.  
  63. $parts = array();
  64. foreach ($anc as $node) {
  65. $parts[] = $node->getUrlName();
  66. }
  67.  
  68. return implode('/', $parts);
  69. }
  70.  
  71. /**
  72.   * Returns the name that should be used to represent this node when rendering navigation elements
  73.   * on the front-end
  74.   * @return string N.B. NOT HTML-safe
  75.   */
  76. public function getNavigationName()
  77. {
  78. $node_name = $this->data['name'];
  79. if ($this->data['alt_nav_title']) $node_name = $this->data['alt_nav_title'];
  80.  
  81. return $node_name;
  82. }
  83.  
  84. /**
  85.   * Returns the name of this page only, when constructing a URL
  86.   * @return string
  87.   **/
  88. public function getUrlName()
  89. {
  90. if ($this->data['slug']) {
  91. $name = $this->data['slug'];
  92. } else {
  93. $name = Enc::urlname($this->data['name']);
  94. }
  95.  
  96. if (in_array($name, Constants::$conflict_page_urls)) {
  97. return '_' . $name;
  98. } else {
  99. return $name;
  100. }
  101. }
  102.  
  103. /**
  104.   * Returns the filename to use for the banner image.
  105.   * Returns NULL if no banner has been defined for this page and a generic banner should be used instead.
  106.   * @return string|null
  107.   **/
  108. public function getBanner()
  109. {
  110. if ($this->data['banner']) {
  111. return $this->data['banner'];
  112. }
  113.  
  114. $anc = $this->findAncestors ();
  115. $anc = array_reverse ($anc);
  116.  
  117. foreach ($anc as $node) {
  118. if ($node->data['banner']) {
  119. return $node->data['banner'];
  120. }
  121. }
  122.  
  123. return null;
  124. }
  125.  
  126. }
  127.  
  128.  
  129.