SearchTemplate.php 4.7 KB

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