You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchTemplate.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * FullTextSearch - Full text search framework for Nextcloud
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later. See the COPYING file.
  8. *
  9. * @author Maxence Lange <maxence@artificial-owl.com>
  10. * @copyright 2018
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCP\FullTextSearch\Model;
  28. use JsonSerializable;
  29. use OCP\FullTextSearch\IFullTextSearchProvider;
  30. /**
  31. * Class SearchTemplate
  32. *
  33. * This is a data transfer object that should be created by Content Provider
  34. * when the getSearchTemplate() method is called.
  35. *
  36. * The object will contain templates to be displayed, and the list of the different
  37. * options to be available to the user when he start a new search.
  38. *
  39. * The display of the Options is generated by the FullTextSearch app and Options
  40. * can be displayed in 2 places:
  41. *
  42. * - the navigation page of the app that generate the indexed content.
  43. * (files, bookmarks, deck, mails, ...)
  44. * - the navigation page of the FullTextSearch app.
  45. *
  46. * Both pages will have different Options, and only the first one can integrate
  47. * a specific template.
  48. *
  49. * @see IFullTextSearchProvider::getSearchTemplate
  50. *
  51. * @since 15.0.0
  52. *
  53. * @package OCP\FullTextSearch\Model
  54. */
  55. final class SearchTemplate implements JsonSerializable {
  56. /** @var string */
  57. private $icon = '';
  58. /** @var string */
  59. private $css = '';
  60. /** @var string */
  61. private $template = '';
  62. /** @var SearchOption[] */
  63. private $panelOptions = [];
  64. /** @var SearchOption[] */
  65. private $navigationOptions = [];
  66. /**
  67. * SearchTemplate constructor.
  68. *
  69. * the class of the icon and the css file to be loaded can be set during the
  70. * creation of the object.
  71. *
  72. * @since 15.0.0
  73. *
  74. * @param string $icon
  75. * @param string $css
  76. */
  77. public function __construct(string $icon = '', string $css = '') {
  78. $this->icon = $icon;
  79. $this->css = $css;
  80. }
  81. /**
  82. * Set the class of the icon to be displayed in the left panel of the
  83. * FullTextSearch navigation page, in front of the related Content Provider.
  84. *
  85. * @since 15.0.0
  86. *
  87. * @param string $class
  88. *
  89. * @return SearchTemplate
  90. */
  91. public function setIcon(string $class): SearchTemplate {
  92. $this->icon = $class;
  93. return $this;
  94. }
  95. /**
  96. * Get the class of the icon.
  97. *
  98. * @since 15.0.0
  99. *
  100. * @return string
  101. */
  102. public function getIcon(): string {
  103. return $this->icon;
  104. }
  105. /**
  106. * Set the path of a CSS file that will be loaded when needed.
  107. *
  108. * @since 15.0.0
  109. *
  110. * @param string $css
  111. *
  112. * @return SearchTemplate
  113. */
  114. public function setCss(string $css): SearchTemplate {
  115. $this->css = $css;
  116. return $this;
  117. }
  118. /**
  119. * Get the path of the CSS file.
  120. *
  121. * @since 15.0.0
  122. *
  123. * @return string
  124. */
  125. public function getCss(): string {
  126. return $this->css;
  127. }
  128. /**
  129. * Set the path of the file of a template that the HTML will be displayed
  130. * below the Options.
  131. * This should only be used if your Content Provider needs to set options in
  132. * a way not generated by FullTextSearch
  133. *
  134. * @since 15.0.0
  135. *
  136. * @param string $template
  137. *
  138. * @return SearchTemplate
  139. */
  140. public function setTemplate(string $template): SearchTemplate {
  141. $this->template = $template;
  142. return $this;
  143. }
  144. /**
  145. * Get the path of the template file.
  146. *
  147. * @since 15.0.0
  148. *
  149. * @return string
  150. */
  151. public function getTemplate(): string {
  152. return $this->template;
  153. }
  154. /**
  155. * Add an option in the Panel that is displayed when the user start a search
  156. * within the app that generate the content.
  157. *
  158. * @see SearchOption
  159. *
  160. * @since 15.0.0
  161. *
  162. * @param SearchOption $option
  163. *
  164. * @return SearchTemplate
  165. */
  166. public function addPanelOption(SearchOption $option): SearchTemplate {
  167. $this->panelOptions[] = $option;
  168. return $this;
  169. }
  170. /**
  171. * Get all options to be displayed in the Panel.
  172. *
  173. * @since 15.0.0
  174. *
  175. * @return SearchOption[]
  176. */
  177. public function getPanelOptions(): array {
  178. return $this->panelOptions;
  179. }
  180. /**
  181. * Add an option in the left panel of the FullTextSearch navigation page.
  182. *
  183. * @see SearchOption
  184. *
  185. * @since 15.0.0
  186. *
  187. * @param SearchOption $option
  188. *
  189. * @return SearchTemplate
  190. */
  191. public function addNavigationOption(SearchOption $option): SearchTemplate {
  192. $this->navigationOptions[] = $option;
  193. return $this;
  194. }
  195. /**
  196. * Get all options to be displayed in the FullTextSearch navigation page.
  197. *
  198. * @since 15.0.0
  199. *
  200. * @return array
  201. */
  202. public function getNavigationOptions(): array {
  203. return $this->navigationOptions;
  204. }
  205. /**
  206. * @since 15.0.0
  207. *
  208. * @return array
  209. */
  210. public function jsonSerialize(): array {
  211. return [
  212. 'icon' => $this->getIcon(),
  213. 'css' => $this->getCss(),
  214. 'template' => $this->getTemplate(),
  215. 'panel' => $this->getPanelOptions(),
  216. 'navigation' => $this->getNavigationOptions()
  217. ];
  218. }
  219. }