1
0

SearchTemplate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\FullTextSearch\Model;
  8. use JsonSerializable;
  9. use OCP\FullTextSearch\IFullTextSearchProvider;
  10. use OCP\FullTextSearch\Model\ISearchOption;
  11. use OCP\FullTextSearch\Model\ISearchTemplate;
  12. /**
  13. * Class ISearchTemplate
  14. *
  15. * This is a data transfer object that should be created by Content Provider
  16. * when the getSearchTemplate() method is called.
  17. *
  18. * The object will contain templates to be displayed, and the list of the different
  19. * options to be available to the user when they start a new search.
  20. *
  21. * The display of the Options is generated by the FullTextSearch app and Options
  22. * can be displayed in 2 places:
  23. *
  24. * - the navigation page of the app that generate the indexed content.
  25. * (files, bookmarks, deck, mails, ...)
  26. * - the navigation page of the FullTextSearch app.
  27. *
  28. * Both pages will have different Options, and only the first one can integrate
  29. * a specific template.
  30. *
  31. * @see IFullTextSearchProvider::getSearchTemplate
  32. *
  33. * @since 15.0.0
  34. *
  35. * @package OC\FullTextSearch\Model
  36. */
  37. final class SearchTemplate implements ISearchTemplate, JsonSerializable {
  38. private string $template = '';
  39. /** @var SearchOption[] */
  40. private array $panelOptions = [];
  41. /** @var SearchOption[] */
  42. private array $navigationOptions = [];
  43. /**
  44. * ISearchTemplate constructor.
  45. *
  46. * the class of the icon and the css file to be loaded can be set during the
  47. * creation of the object.
  48. *
  49. * @since 15.0.0
  50. */
  51. public function __construct(
  52. private string $icon = '',
  53. private string $css = '',
  54. ) {
  55. }
  56. /**
  57. * Set the class of the icon to be displayed in the left panel of the
  58. * FullTextSearch navigation page, in front of the related Content Provider.
  59. *
  60. * @since 15.0.0
  61. */
  62. public function setIcon(string $class): ISearchTemplate {
  63. $this->icon = $class;
  64. return $this;
  65. }
  66. /**
  67. * Get the class of the icon.
  68. */
  69. public function getIcon(): string {
  70. return $this->icon;
  71. }
  72. /**
  73. * Set the path of a CSS file that will be loaded when needed.
  74. *
  75. * @since 15.0.0
  76. */
  77. public function setCss(string $css): ISearchTemplate {
  78. $this->css = $css;
  79. return $this;
  80. }
  81. /**
  82. * Get the path of the CSS file.
  83. *
  84. * @since 15.0.0
  85. */
  86. public function getCss(): string {
  87. return $this->css;
  88. }
  89. /**
  90. * Set the path of the file of a template that the HTML will be displayed
  91. * below the Options.
  92. * This should only be used if your Content Provider needs to set options in
  93. * a way not generated by FullTextSearch
  94. *
  95. * @since 15.0.0
  96. */
  97. public function setTemplate(string $template): ISearchTemplate {
  98. $this->template = $template;
  99. return $this;
  100. }
  101. /**
  102. * Get the path of the template file.
  103. *
  104. * @since 15.0.0
  105. */
  106. public function getTemplate(): string {
  107. return $this->template;
  108. }
  109. /**
  110. * Add an option in the Panel that is displayed when the user start a search
  111. * within the app that generate the content.
  112. *
  113. * @see ISearchOption
  114. *
  115. * @since 15.0.0
  116. */
  117. public function addPanelOption(ISearchOption $option): ISearchTemplate {
  118. $this->panelOptions[] = $option;
  119. return $this;
  120. }
  121. /**
  122. * Get all options to be displayed in the Panel.
  123. *
  124. * @since 15.0.0
  125. *
  126. * @return SearchOption[]
  127. */
  128. public function getPanelOptions(): array {
  129. return $this->panelOptions;
  130. }
  131. /**
  132. * Add an option in the left panel of the FullTextSearch navigation page.
  133. *
  134. * @see ISearchOption
  135. *
  136. * @since 15.0.0
  137. */
  138. public function addNavigationOption(ISearchOption $option): ISearchTemplate {
  139. $this->navigationOptions[] = $option;
  140. return $this;
  141. }
  142. /**
  143. * Get all options to be displayed in the FullTextSearch navigation page.
  144. *
  145. * @since 15.0.0
  146. */
  147. public function getNavigationOptions(): array {
  148. return $this->navigationOptions;
  149. }
  150. /**
  151. * @since 15.0.0
  152. */
  153. public function jsonSerialize(): array {
  154. return [
  155. 'icon' => $this->getIcon(),
  156. 'css' => $this->getCss(),
  157. 'template' => $this->getTemplate(),
  158. 'panel' => $this->getPanelOptions(),
  159. 'navigation' => $this->getNavigationOptions()
  160. ];
  161. }
  162. }