SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/SearchHandler.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.  
  19. /**
  20. * Defines a table which can be searched against using the front-end search
  21. **/
  22. class SearchHandler
  23. {
  24. private $table;
  25. private $ctlr_name;
  26. private $where;
  27. private $joins;
  28. private $having;
  29.  
  30.  
  31. /**
  32.   * @param string $table The name of the keywords table, e.g. page_keywords
  33.   * @param string $ctlr_name The name of a controller which implements the
  34.   * FrontEndSearch interface. Must be fully namespaced.
  35.   */
  36. public function __construct($table, $ctlr_name)
  37. {
  38. if (strpos($ctlr_name, '\\') === false) {
  39. throw new \InvalidArgumentException('Controller name must be fully namespaced');
  40. }
  41. $this->table = $table;
  42. $this->ctlr_name = $ctlr_name;
  43. $this->where = array();
  44. $this->joins = array();
  45. $this->having = array();
  46. }
  47.  
  48.  
  49. /**
  50.   * Gets the table name for the keywords table, e.g. page_keywords
  51.   **/
  52. public function getTable()
  53. {
  54. return $this->table;
  55. }
  56.  
  57. /**
  58.   * Gets the table name for the main table, e.g. pages
  59.   **/
  60. public function getMainTable()
  61. {
  62. return Inflector::plural(str_replace('_keywords', '', $this->table));
  63. }
  64.  
  65. /**
  66.   * Sets the table name for the keywords table, e.g. page_keywords
  67.   **/
  68. public function setTable($val)
  69. {
  70. $val = trim($val);
  71. if ($val == '') throw new Exception("No input value specified");
  72. $this->name = $val;
  73. }
  74.  
  75.  
  76. /**
  77.   * Gets the controller name, e.g. PageController
  78.   **/
  79. public function getCtlrName()
  80. {
  81. return $this->ctlr_name;
  82. }
  83.  
  84. /**
  85.   * Sets the controller name, e.g. PageController. The specified controller must implement the FrontEndSearch interface.
  86.   **/
  87. public function setCtlrName($val)
  88. {
  89. if (! class_exists($val)) throw new Exception("Specified controller class does not exist");
  90. $this->ctlr_name = $val;
  91. }
  92.  
  93.  
  94. /**
  95.   * Gets all where clauses which should be added to the search query
  96.   **/
  97. public function getWhere()
  98. {
  99. return $this->where;
  100. }
  101.  
  102. /**
  103.   * Adds a where clause to this search handler.
  104.   * Where clauses should refer to the main table using the alias 'main'
  105.   * e.g. $handler->addWhere("main.active = 1")
  106.   **/
  107. public function addWhere($val)
  108. {
  109. $this->where[] = $val;
  110. }
  111.  
  112.  
  113. /**
  114.   * Gets all joins which should be added to the search query
  115.   **/
  116. public function getJoins()
  117. {
  118. return $this->joins;
  119. }
  120.  
  121. /**
  122.   * Adds a join to this search handler.
  123.   * Inner joins should refer to the main table using the alias 'main'
  124.   * e.g. $handler->addJoin("INNER JOIN categories ON categories.item_id = main.id")
  125.   **/
  126. public function addJoin($val)
  127. {
  128. $this->joins[] = $val;
  129. }
  130.  
  131.  
  132. /**
  133.   * Gets all having clauses which should be added to the search query
  134.   **/
  135. public function getHaving()
  136. {
  137. return $this->having;
  138. }
  139.  
  140. /**
  141.   * Adds a having clause to this search handler.
  142.   * Having clauses should refer to the main table using the alias 'main'
  143.   * e.g. $handler->addHaving("main.active = 1")
  144.   **/
  145. public function addHaving($val)
  146. {
  147. $this->having[] = $val;
  148. }
  149.  
  150. }
  151.  
  152.  
  153.