WidgetSetup.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Nextcloud - Dashboard App
  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, Maxence Lange <maxence@artificial-owl.com>
  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 OCP\Dashboard\Model;
  28. use JsonSerializable;
  29. /**
  30. * Interface WidgetSetup
  31. *
  32. * A widget must create an WidgetSetup object and returns it in the
  33. * IDashboardWidget::getWidgetSetup method.
  34. *
  35. * @see IDashboardWidget::getWidgetSetup
  36. *
  37. * @since 15.0.0
  38. *
  39. * @package OCP\Dashboard\Model
  40. */
  41. final class WidgetSetup implements JsonSerializable {
  42. const SIZE_TYPE_MIN = 'min';
  43. const SIZE_TYPE_MAX = 'max';
  44. const SIZE_TYPE_DEFAULT = 'default';
  45. /** @var array */
  46. private $sizes = [];
  47. /** @var array */
  48. private $menus = [];
  49. /** @var array */
  50. private $jobs = [];
  51. /** @var string */
  52. private $push = '';
  53. /** @var array */
  54. private $settings = [];
  55. /**
  56. * Get the defined size for a specific type (min, max, default)
  57. * Returns an array:
  58. * [
  59. * 'width' => width,
  60. * 'height' => height
  61. * ]
  62. *
  63. *
  64. * @since 15.0.0
  65. *
  66. * @param string $type
  67. *
  68. * @return array
  69. */
  70. public function getSize(string $type): array {
  71. if (array_key_exists($type, $this->sizes)) {
  72. return $this->sizes[$type];
  73. }
  74. return [];
  75. }
  76. /**
  77. * Returns all sizes defined for the widget.
  78. *
  79. * @since 15.0.0
  80. *
  81. * @return array
  82. */
  83. public function getSizes(): array {
  84. return $this->sizes;
  85. }
  86. /**
  87. * Add a new size to the setup.
  88. *
  89. * @since 15.0.0
  90. *
  91. * @param string $type
  92. * @param int $width
  93. * @param int $height
  94. *
  95. * @return WidgetSetup
  96. */
  97. public function addSize(string $type, int $width, int $height): WidgetSetup {
  98. $this->sizes[$type] = [
  99. 'width' => $width,
  100. 'height' => $height
  101. ];
  102. return $this;
  103. }
  104. /**
  105. * Returns menu entries.
  106. *
  107. * @since 15.0.0
  108. *
  109. * @return array
  110. */
  111. public function getMenuEntries(): array {
  112. return $this->menus;
  113. }
  114. /**
  115. * Add a menu entry to the widget.
  116. * $function is the Javascript function to be called when clicking the
  117. * menu entry.
  118. * $icon is the css class of the icon.
  119. * $text is the display name of the menu entry.
  120. *
  121. * @since 15.0.0
  122. *
  123. * @param string $function
  124. * @param string $icon
  125. * @param string $text
  126. *
  127. * @return WidgetSetup
  128. */
  129. public function addMenuEntry(string $function, string $icon, string $text): WidgetSetup {
  130. $this->menus[] = [
  131. 'function' => $function,
  132. 'icon' => $icon,
  133. 'text' => $text
  134. ];
  135. return $this;
  136. }
  137. /**
  138. * Add a delayed job to the widget.
  139. *
  140. * $function is the Javascript function to be called.
  141. * $delay is the time in seconds between each call.
  142. *
  143. * @since 15.0.0
  144. *
  145. * @param string $function
  146. * @param int $delay
  147. *
  148. * @return WidgetSetup
  149. */
  150. public function addDelayedJob(string $function, int $delay): WidgetSetup {
  151. $this->jobs[] = [
  152. 'function' => $function,
  153. 'delay' => $delay
  154. ];
  155. return $this;
  156. }
  157. /**
  158. * Get delayed jobs.
  159. *
  160. * @since 15.0.0
  161. *
  162. * @return array
  163. */
  164. public function getDelayedJobs(): array {
  165. return $this->jobs;
  166. }
  167. /**
  168. * Get the push function, called when an event is send to the front-end
  169. *
  170. * @since 15.0.0
  171. *
  172. * @return string
  173. */
  174. public function getPush(): string {
  175. return $this->push;
  176. }
  177. /**
  178. * Set the Javascript function to be called when an event is pushed to the
  179. * frontend.
  180. *
  181. * @since 15.0.0
  182. *
  183. * @param string $function
  184. *
  185. * @return WidgetSetup
  186. */
  187. public function setPush(string $function): WidgetSetup {
  188. $this->push = $function;
  189. return $this;
  190. }
  191. /**
  192. * Returns the default settings for a widget.
  193. *
  194. * @since 15.0.0
  195. *
  196. * @return array
  197. */
  198. public function getDefaultSettings(): array {
  199. return $this->settings;
  200. }
  201. /**
  202. * Set the default settings for a widget.
  203. * This method is used by the Dashboard app, using the settings created
  204. * using WidgetSetting
  205. *
  206. * @see WidgetSetting
  207. *
  208. * @since 15.0.0
  209. *
  210. * @param array $settings
  211. *
  212. * @return WidgetSetup
  213. */
  214. public function setDefaultSettings(array $settings): WidgetSetup {
  215. $this->settings = $settings;
  216. return $this;
  217. }
  218. /**
  219. * @since 15.0.0
  220. *
  221. * @return array
  222. */
  223. public function jsonSerialize() {
  224. return [
  225. 'size' => $this->getSizes(),
  226. 'menu' => $this->getMenuEntries(),
  227. 'jobs' => $this->getDelayedJobs(),
  228. 'push' => $this->getPush(),
  229. 'settings' => $this->getDefaultSettings()
  230. ];
  231. }
  232. }