SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RteLibraryHasCategories.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. /**
  18. * Abstract richtext library for bog-standard "has categories" modules, such as news articles, blog posts, etc.
  19. **/
  20. abstract class RteLibraryHasCategories extends RteLibrary
  21. {
  22. protected $name;
  23.  
  24. /**
  25.   * @var string Main table name
  26.   */
  27. protected $main_table;
  28.  
  29. /**
  30.   * @var array Columns to pull when fetching records from the main table
  31.   */
  32. protected $main_columns = ['id', 'name'];
  33.  
  34. /**
  35.   * @var array Columns for the ORDER BY clause
  36.   */
  37. protected $main_order = ['name'];
  38.  
  39. /**
  40.   * @var array Columns to search against
  41.   */
  42. protected $search_columns = ['name'];
  43.  
  44.  
  45. /**
  46.   * For a given database row, return the display identifier of the record
  47.   */
  48. protected function getIdentifier(array $row)
  49. {
  50. return $row['name'];
  51. }
  52.  
  53.  
  54. /**
  55.   * For a given database row, return an array of link attributes
  56.   *
  57.   * @param array $row From the database
  58.   * @return array HTML attributes, e.g. 'href' or 'title'
  59.   */
  60. protected abstract function getLinkAttrs(array $row);
  61.  
  62.  
  63. /**
  64.   * Validates configuration
  65.   */
  66. public function __construct()
  67. {
  68. Pdb::validateIdentifier($this->main_table);
  69.  
  70. foreach ($this->main_columns as $col) {
  71. Pdb::validateIdentifier($col);
  72. }
  73. foreach ($this->main_order as $col) {
  74. Pdb::validateIdentifier($col);
  75. }
  76. foreach ($this->search_columns as $col) {
  77. Pdb::validateIdentifier($col);
  78. }
  79.  
  80. if (!in_array('id', $this->main_columns)) {
  81. $this->main_columns[] = 'id';
  82. }
  83. if (!in_array('date_modified', $this->main_columns)) {
  84. $this->main_columns[] = 'date_modified';
  85. }
  86. }
  87.  
  88.  
  89. /**
  90.   * Do a library browse
  91.   *
  92.   * @return array of RteLibContainer and RteLibObject objects
  93.   */
  94. public function browse($path)
  95. {
  96. $cat_table = Category::tableMain2cat($this->main_table);
  97. $joiner_table = Category::tableMain2joiner($this->main_table);
  98. $joiner_col = Category::columnMain2joiner($this->main_table);
  99.  
  100. if ($path == '') {
  101. // An empty "path", so return the category list
  102. $q = "SELECT cat.id, cat.name
  103. FROM ~{$cat_table} AS cat
  104. INNER JOIN ~{$joiner_table} AS joiner ON joiner.cat_id = cat.id
  105. INNER JOIN ~{$this->main_table} AS main ON joiner.{$joiner_col} = main.id
  106. GROUP BY cat.id
  107. ORDER BY cat.name";
  108. $res = Pdb::query($q, [], 'pdo');
  109.  
  110. $out = array();
  111. foreach ($res as $row) {
  112. $out[] = new RteLibContainer($row['id'], $row['name']);
  113. }
  114. $res->closeCursor();
  115.  
  116. return $out;
  117.  
  118. } else {
  119. // Return the item list
  120. $columns = implode(',', $this->main_columns);
  121. $order = implode(',', $this->main_order);
  122. $q = "SELECT {$columns}
  123. FROM ~{$this->main_table} AS main
  124. INNER JOIN ~{$joiner_table} AS joiner ON joiner.{$joiner_col} = main.id
  125. WHERE joiner.cat_id = ?
  126. ORDER BY {$order}";
  127. $res = Pdb::query($q, [$path], 'pdo');
  128.  
  129. $out = array();
  130. foreach ($res as $row) {
  131. $out[] = new RteLibObject(
  132. $row['id'],
  133. $this->getIdentifier($row),
  134. $this->getLinkAttrs($row),
  135. 'date' => $row['date_modified'],
  136. )
  137. );
  138. }
  139. $res->closeCursor();
  140.  
  141. return $out;
  142. }
  143. }
  144.  
  145.  
  146. /**
  147.   * Do a library search
  148.   *
  149.   * @return array of RteLibObject objects
  150.   */
  151. public function search($term)
  152. {
  153. $conditions = [];
  154. foreach ($this->search_columns as $col) {
  155. $conditions[] = [$col, 'CONTAINS', $term];
  156. }
  157.  
  158. $params = [];
  159. $where = Pdb::buildClause($conditions, $params, 'OR');
  160.  
  161. $columns = implode(',', $this->main_columns);
  162. $order = implode(',', $this->main_order);
  163. $q = "SELECT {$columns}
  164. FROM ~{$this->main_table} AS main
  165. WHERE {$where}
  166. ORDER BY {$order}";
  167. $res = Pdb::query($q, $params, 'pdo');
  168.  
  169. $out = array();
  170. foreach ($res as $row) {
  171. $out[] = new RteLibObject(
  172. $row['id'],
  173. $this->getIdentifier($row),
  174. $this->getLinkAttrs($row),
  175. 'date' => $row['date_modified'],
  176. )
  177. );
  178. }
  179. $res->closeCursor();
  180.  
  181. return $out;
  182. }
  183.  
  184. }
  185.