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.

329 lines
5.9 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Maxence Lange <maxence@artificial-owl.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\Dashboard\Model;
  26. use JsonSerializable;
  27. /**
  28. * Interface WidgetTemplate
  29. *
  30. * A widget must create an WidgetTemplate object and returns it in the
  31. * IDashboardWidget::getWidgetTemplate method.
  32. *
  33. * @see IDashboardWidget::getWidgetTemplate
  34. *
  35. * @since 15.0.0
  36. * @deprecated 20.0.0
  37. *
  38. */
  39. final class WidgetTemplate implements JsonSerializable {
  40. /** @var string */
  41. private $icon = '';
  42. /** @var array */
  43. private $css = [];
  44. /** @var array */
  45. private $js = [];
  46. /** @var string */
  47. private $content = '';
  48. /** @var string */
  49. private $function = '';
  50. /** @var WidgetSetting[] */
  51. private $settings = [];
  52. /**
  53. * Get the icon class of the widget.
  54. *
  55. * @since 15.0.0
  56. * @deprecated 20.0.0
  57. *
  58. * @return string
  59. */
  60. public function getIcon(): string {
  61. return $this->icon;
  62. }
  63. /**
  64. * Set the icon class of the widget.
  65. * This class must be defined in one of the CSS file used by the widget.
  66. *
  67. * @see addCss
  68. *
  69. * @since 15.0.0
  70. * @deprecated 20.0.0
  71. *
  72. * @param string $icon
  73. *
  74. * @return WidgetTemplate
  75. */
  76. public function setIcon(string $icon): WidgetTemplate {
  77. $this->icon = $icon;
  78. return $this;
  79. }
  80. /**
  81. * Get CSS files to be included when displaying a widget
  82. *
  83. * @since 15.0.0
  84. * @deprecated 20.0.0
  85. *
  86. * @return array
  87. */
  88. public function getCss(): array {
  89. return $this->css;
  90. }
  91. /**
  92. * path and name of CSS files
  93. *
  94. * @since 15.0.0
  95. * @deprecated 20.0.0
  96. *
  97. * @param array $css
  98. *
  99. * @return WidgetTemplate
  100. */
  101. public function setCss(array $css): WidgetTemplate {
  102. $this->css = $css;
  103. return $this;
  104. }
  105. /**
  106. * Add a CSS file to be included when displaying a widget.
  107. *
  108. * @since 15.0.0
  109. * @deprecated 20.0.0
  110. *
  111. * @param string $css
  112. *
  113. * @return WidgetTemplate
  114. */
  115. public function addCss(string $css): WidgetTemplate {
  116. $this->css[] = $css;
  117. return $this;
  118. }
  119. /**
  120. * Get JS files to be included when loading a widget
  121. *
  122. * @since 15.0.0
  123. * @deprecated 20.0.0
  124. *
  125. * @return array
  126. */
  127. public function getJs(): array {
  128. return $this->js;
  129. }
  130. /**
  131. * Set an array of JS files to be included when loading a widget.
  132. *
  133. * @since 15.0.0
  134. * @deprecated 20.0.0
  135. *
  136. * @param array $js
  137. *
  138. * @return WidgetTemplate
  139. */
  140. public function setJs(array $js): WidgetTemplate {
  141. $this->js = $js;
  142. return $this;
  143. }
  144. /**
  145. * Add a JS file to be included when loading a widget.
  146. *
  147. * @since 15.0.0
  148. * @deprecated 20.0.0
  149. *
  150. * @param string $js
  151. *
  152. * @return WidgetTemplate
  153. */
  154. public function addJs(string $js): WidgetTemplate {
  155. $this->js[] = $js;
  156. return $this;
  157. }
  158. /**
  159. * Get the HTML file that contains the content of the widget.
  160. *
  161. * @since 15.0.0
  162. * @deprecated 20.0.0
  163. *
  164. * @return string
  165. */
  166. public function getContent(): string {
  167. return $this->content;
  168. }
  169. /**
  170. * Set the HTML file that contains the content of the widget.
  171. *
  172. * @since 15.0.0
  173. * @deprecated 20.0.0
  174. *
  175. * @param string $content
  176. *
  177. * @return WidgetTemplate
  178. */
  179. public function setContent(string $content): WidgetTemplate {
  180. $this->content = $content;
  181. return $this;
  182. }
  183. /**
  184. * Get the JS function to be called when loading the widget.
  185. *
  186. * @since 15.0.0
  187. * @deprecated 20.0.0
  188. *
  189. * @return string
  190. */
  191. public function getInitFunction(): string {
  192. return $this->function;
  193. }
  194. /**
  195. * JavaScript function to be called when loading the widget on the
  196. * dashboard
  197. *
  198. * @since 15.0.0
  199. * @deprecated 20.0.0
  200. *
  201. * @param string $function
  202. *
  203. * @return WidgetTemplate
  204. */
  205. public function setInitFunction(string $function): WidgetTemplate {
  206. $this->function = $function;
  207. return $this;
  208. }
  209. /**
  210. * Get all WidgetSetting defined for the widget.
  211. *
  212. * @see WidgetSetting
  213. *
  214. * @since 15.0.0
  215. * @deprecated 20.0.0
  216. *
  217. * @return WidgetSetting[]
  218. */
  219. public function getSettings(): array {
  220. return $this->settings;
  221. }
  222. /**
  223. * Define all WidgetSetting for the widget.
  224. *
  225. * @since 15.0.0
  226. * @deprecated 20.0.0
  227. *
  228. * @see WidgetSetting
  229. *
  230. * @param WidgetSetting[] $settings
  231. *
  232. * @return WidgetTemplate
  233. */
  234. public function setSettings(array $settings): WidgetTemplate {
  235. $this->settings = $settings;
  236. return $this;
  237. }
  238. /**
  239. * Add a WidgetSetting.
  240. *
  241. * @see WidgetSetting
  242. *
  243. * @since 15.0.0
  244. * @deprecated 20.0.0
  245. *
  246. * @param WidgetSetting $setting
  247. *
  248. * @return WidgetTemplate
  249. */
  250. public function addSetting(WidgetSetting $setting): WidgetTemplate {
  251. $this->settings[] = $setting;
  252. return $this;
  253. }
  254. /**
  255. * Get a WidgetSetting by its name
  256. *
  257. * @see WidgetSetting::setName
  258. *
  259. * @since 15.0.0
  260. * @deprecated 20.0.0
  261. *
  262. * @param string $key
  263. *
  264. * @return WidgetSetting
  265. */
  266. public function getSetting(string $key): WidgetSetting {
  267. if (!array_key_exists($key, $this->settings)) {
  268. return null;
  269. }
  270. return $this->settings[$key];
  271. }
  272. /**
  273. * @since 15.0.0
  274. * @deprecated 20.0.0
  275. *
  276. * @return array
  277. */
  278. public function jsonSerialize() {
  279. return [
  280. 'icon' => $this->getIcon(),
  281. 'css' => $this->getCss(),
  282. 'js' => $this->getJs(),
  283. 'content' => $this->getContent(),
  284. 'function' => $this->getInitFunction(),
  285. 'settings' => $this->getSettings()
  286. ];
  287. }
  288. }