SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RteLibraryPages.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. /**
  17. * Richtext library for pages
  18. **/
  19. class RteLibraryPages extends RteLibrary
  20. {
  21. protected $name = 'Pages';
  22. private $db;
  23.  
  24.  
  25. public function __construct()
  26. {
  27. }
  28.  
  29.  
  30. /**
  31.   * Do a library browse
  32.   *
  33.   * @return array of RteLibContainer and RteLibObject objects
  34.   **/
  35. public function browse($path)
  36. {
  37.  
  38. if ($path) {
  39. $path_parts = explode('/', $path);
  40. $parent_id = array_pop($path_parts);
  41. } else {
  42. $parent_id = 0;
  43. }
  44.  
  45. $root = Navigation::loadPageTree($_SESSION['admin']['active_subsite'], true);
  46. $node = $root->findNodeValue('id', $parent_id);
  47.  
  48. $out = array();
  49.  
  50. // This page
  51. if ($parent_id != 0) {
  52. $out[] = new RteLibObject(
  53. $node['id'],
  54. $node['name'],
  55. 'href' => 'page/view_by_id/' . $node['id'],
  56. 'title' => $node['name'],
  57. ),
  58. 'date' => $node['date_modified'],
  59. )
  60. );
  61. }
  62.  
  63. // Children pages
  64. foreach ($node->children as $row) {
  65. if (count($row->children)) {
  66. $out[] = new RteLibContainer(
  67. $row['id'],
  68. $row['name']
  69. );
  70.  
  71. } else {
  72. $out[] = new RteLibObject(
  73. $row['id'],
  74. $row['name'],
  75. 'href' => 'page/view_by_id/' . $row['id'],
  76. 'title' => $row['name'],
  77. ),
  78. 'date' => $row['date_modified'],
  79. )
  80. );
  81. }
  82. }
  83.  
  84. return $out;
  85. }
  86.  
  87.  
  88. /**
  89.   * Do a library search
  90.   *
  91.   * @return array of RteLibContainer and RteLibObject objects
  92.   **/
  93. public function search($term)
  94. {
  95. $conditions = array();
  96. $conditions[] = ['pages.name', 'CONTAINS', $term];
  97.  
  98. $params = [];
  99. $where = Pdb::buildClause($conditions, $params, 'OR');
  100.  
  101. $q = "SELECT pages.id, pages.name, pages.date_modified
  102. FROM ~pages AS pages
  103. WHERE pages.active = 1
  104. AND ({$where})
  105. ORDER BY pages.name";
  106. $res = Pdb::query($q, $params, 'pdo');
  107.  
  108. $out = array();
  109. foreach ($res as $row) {
  110. $out[] = new RteLibObject(
  111. $row['id'],
  112. $row['name'],
  113. 'href' => Page::url($row['id']),
  114. 'title' => $row['name'],
  115. ),
  116. 'date' => $row['date_modified'],
  117. )
  118. );
  119. }
  120. $res->closeCursor();
  121.  
  122. return $out;
  123. }
  124.  
  125. }
  126.