SearchTemplate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 OC\FullTextSearch\Model;
  28. use JsonSerializable;
  29. use OCP\FullTextSearch\IFullTextSearchProvider;
  30. use OCP\FullTextSearch\Model\ISearchOption;
  31. use OCP\FullTextSearch\Model\ISearchTemplate;
  32. /**
  33. * Class ISearchTemplate
  34. *
  35. * This is a data transfer object that should be created by Content Provider
  36. * when the getSearchTemplate() method is called.
  37. *
  38. * The object will contain templates to be displayed, and the list of the different
  39. * options to be available to the user when he start a new search.
  40. *
  41. * The display of the Options is generated by the FullTextSearch app and Options
  42. * can be displayed in 2 places:
  43. *
  44. * - the navigation page of the app that generate the indexed content.
  45. * (files, bookmarks, deck, mails, ...)
  46. * - the navigation page of the FullTextSearch app.
  47. *
  48. * Both pages will have different Options, and only the first one can integrate
  49. * a specific template.
  50. *
  51. * @see IFullTextSearchProvider::getSearchTemplate
  52. *
  53. * @since 15.0.0
  54. *
  55. * @package OC\FullTextSearch\Model
  56. */
  57. final class SearchTemplate implements ISearchTemplate, JsonSerializable {
  58. /** @var string */
  59. private $icon = '';
  60. /** @var string */
  61. private $css = '';
  62. /** @var string */
  63. private $template = '';
  64. /** @var SearchOption[] */
  65. private $panelOptions = [];
  66. /** @var SearchOption[] */
  67. private $navigationOptions = [];
  68. /**
  69. * ISearchTemplate constructor.
  70. *
  71. * the class of the icon and the css file to be loaded can be set during the
  72. * creation of the object.
  73. *
  74. * @since 15.0.0
  75. *
  76. * @param string $icon
  77. * @param string $css
  78. */
  79. public function __construct(string $icon = '', string $css = '') {
  80. $this->icon = $icon;
  81. $this->css = $css;
  82. }
  83. /**
  84. * Set the class of the icon to be displayed in the left panel of the
  85. * FullTextSearch navigation page, in front of the related Content Provider.
  86. *
  87. * @since 15.0.0
  88. *
  89. * @param string $class
  90. *
  91. * @return ISearchTemplate
  92. */
  93. public function setIcon(string $class): ISearchTemplate {
  94. $this->icon = $class;
  95. return $this;
  96. }
  97. /**
  98. * Get the class of the icon.
  99. *
  100. * @since 15.0.0
  101. *
  102. * @return string
  103. */
  104. public function getIcon(): string {
  105. return $this->icon;
  106. }
  107. /**
  108. * Set the path of a CSS file that will be loaded when needed.
  109. *
  110. * @since 15.0.0
  111. *
  112. * @param string $css
  113. *
  114. * @return ISearchTemplate
  115. */
  116. public function setCss(string $css): ISearchTemplate {
  117. $this->css = $css;
  118. return $this;
  119. }
  120. /**
  121. * Get the path of the CSS file.
  122. *
  123. * @since 15.0.0
  124. *
  125. * @return string
  126. */
  127. public function getCss(): string {
  128. return $this->css;
  129. }
  130. /**
  131. * Set the path of the file of a template that the HTML will be displayed
  132. * below the Options.
  133. * This should only be used if your Content Provider needs to set options in
  134. * a way not generated by FullTextSearch
  135. *
  136. * @since 15.0.0
  137. *
  138. * @param string $template
  139. *
  140. * @return ISearchTemplate
  141. */
  142. public function setTemplate(string $template): ISearchTemplate {
  143. $this->template = $template;
  144. return $this;
  145. }
  146. /**
  147. * Get the path of the template file.
  148. *
  149. * @since 15.0.0
  150. *
  151. * @return string
  152. */
  153. public function getTemplate(): string {
  154. return $this->template;
  155. }
  156. /**
  157. * Add an option in the Panel that is displayed when the user start a search
  158. * within the app that generate the content.
  159. *
  160. * @see ISearchOption
  161. *
  162. * @since 15.0.0
  163. *
  164. * @param ISearchOption $option
  165. *
  166. * @return ISearchTemplate
  167. */
  168. public function addPanelOption(ISearchOption $option): ISearchTemplate {
  169. $this->panelOptions[] = $option;
  170. return $this;
  171. }
  172. /**
  173. * Get all options to be displayed in the Panel.
  174. *
  175. * @since 15.0.0
  176. *
  177. * @return SearchOption[]
  178. */
  179. public function getPanelOptions(): array {
  180. return $this->panelOptions;
  181. }
  182. /**
  183. * Add an option in the left panel of the FullTextSearch navigation page.
  184. *
  185. * @see ISearchOption
  186. *
  187. * @since 15.0.0
  188. *
  189. * @param ISearchOption $option
  190. *
  191. * @return ISearchTemplate
  192. */
  193. public function addNavigationOption(ISearchOption $option): ISearchTemplate {
  194. $this->navigationOptions[] = $option;
  195. return $this;
  196. }
  197. /**
  198. * Get all options to be displayed in the FullTextSearch navigation page.
  199. *
  200. * @since 15.0.0
  201. *
  202. * @return array
  203. */
  204. public function getNavigationOptions(): array {
  205. return $this->navigationOptions;
  206. }
  207. /**
  208. * @since 15.0.0
  209. *
  210. * @return array
  211. */
  212. public function jsonSerialize(): array {
  213. return [
  214. 'icon' => $this->getIcon(),
  215. 'css' => $this->getCss(),
  216. 'template' => $this->getTemplate(),
  217. 'panel' => $this->getPanelOptions(),
  218. 'navigation' => $this->getNavigationOptions()
  219. ];
  220. }
  221. }