1
0

SearchTemplate.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /** @var string */
  56. private $icon = '';
  57. /** @var string */
  58. private $css = '';
  59. /** @var string */
  60. private $template = '';
  61. /** @var SearchOption[] */
  62. private $panelOptions = [];
  63. /** @var SearchOption[] */
  64. private $navigationOptions = [];
  65. /**
  66. * ISearchTemplate constructor.
  67. *
  68. * the class of the icon and the css file to be loaded can be set during the
  69. * creation of the object.
  70. *
  71. * @since 15.0.0
  72. *
  73. * @param string $icon
  74. * @param string $css
  75. */
  76. public function __construct(string $icon = '', string $css = '') {
  77. $this->icon = $icon;
  78. $this->css = $css;
  79. }
  80. /**
  81. * Set the class of the icon to be displayed in the left panel of the
  82. * FullTextSearch navigation page, in front of the related Content Provider.
  83. *
  84. * @since 15.0.0
  85. *
  86. * @param string $class
  87. *
  88. * @return ISearchTemplate
  89. */
  90. public function setIcon(string $class): ISearchTemplate {
  91. $this->icon = $class;
  92. return $this;
  93. }
  94. /**
  95. * Get the class of the icon.
  96. *
  97. * @since 15.0.0
  98. *
  99. * @return string
  100. */
  101. public function getIcon(): string {
  102. return $this->icon;
  103. }
  104. /**
  105. * Set the path of a CSS file that will be loaded when needed.
  106. *
  107. * @since 15.0.0
  108. *
  109. * @param string $css
  110. *
  111. * @return ISearchTemplate
  112. */
  113. public function setCss(string $css): ISearchTemplate {
  114. $this->css = $css;
  115. return $this;
  116. }
  117. /**
  118. * Get the path of the CSS file.
  119. *
  120. * @since 15.0.0
  121. *
  122. * @return string
  123. */
  124. public function getCss(): string {
  125. return $this->css;
  126. }
  127. /**
  128. * Set the path of the file of a template that the HTML will be displayed
  129. * below the Options.
  130. * This should only be used if your Content Provider needs to set options in
  131. * a way not generated by FullTextSearch
  132. *
  133. * @since 15.0.0
  134. *
  135. * @param string $template
  136. *
  137. * @return ISearchTemplate
  138. */
  139. public function setTemplate(string $template): ISearchTemplate {
  140. $this->template = $template;
  141. return $this;
  142. }
  143. /**
  144. * Get the path of the template file.
  145. *
  146. * @since 15.0.0
  147. *
  148. * @return string
  149. */
  150. public function getTemplate(): string {
  151. return $this->template;
  152. }
  153. /**
  154. * Add an option in the Panel that is displayed when the user start a search
  155. * within the app that generate the content.
  156. *
  157. * @see ISearchOption
  158. *
  159. * @since 15.0.0
  160. *
  161. * @param ISearchOption $option
  162. *
  163. * @return ISearchTemplate
  164. */
  165. public function addPanelOption(ISearchOption $option): ISearchTemplate {
  166. $this->panelOptions[] = $option;
  167. return $this;
  168. }
  169. /**
  170. * Get all options to be displayed in the Panel.
  171. *
  172. * @since 15.0.0
  173. *
  174. * @return SearchOption[]
  175. */
  176. public function getPanelOptions(): array {
  177. return $this->panelOptions;
  178. }
  179. /**
  180. * Add an option in the left panel of the FullTextSearch navigation page.
  181. *
  182. * @see ISearchOption
  183. *
  184. * @since 15.0.0
  185. *
  186. * @param ISearchOption $option
  187. *
  188. * @return ISearchTemplate
  189. */
  190. public function addNavigationOption(ISearchOption $option): ISearchTemplate {
  191. $this->navigationOptions[] = $option;
  192. return $this;
  193. }
  194. /**
  195. * Get all options to be displayed in the FullTextSearch navigation page.
  196. *
  197. * @since 15.0.0
  198. *
  199. * @return array
  200. */
  201. public function getNavigationOptions(): array {
  202. return $this->navigationOptions;
  203. }
  204. /**
  205. * @since 15.0.0
  206. *
  207. * @return array
  208. */
  209. public function jsonSerialize(): array {
  210. return [
  211. 'icon' => $this->getIcon(),
  212. 'css' => $this->getCss(),
  213. 'template' => $this->getTemplate(),
  214. 'panel' => $this->getPanelOptions(),
  215. 'navigation' => $this->getNavigationOptions()
  216. ];
  217. }
  218. }