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.

277 lines
5.3 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Maxence Lange <maxence@artificial-owl.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCP\Dashboard\Model;
  27. use JsonSerializable;
  28. /**
  29. * Interface WidgetSetup
  30. *
  31. * A widget must create an WidgetSetup object and returns it in the
  32. * IDashboardWidget::getWidgetSetup method.
  33. *
  34. * @see IDashboardWidget::getWidgetSetup
  35. *
  36. * @since 15.0.0
  37. * @deprecated 20.0.0
  38. *
  39. */
  40. final class WidgetSetup implements JsonSerializable {
  41. public const SIZE_TYPE_MIN = 'min';
  42. public const SIZE_TYPE_MAX = 'max';
  43. public const SIZE_TYPE_DEFAULT = 'default';
  44. /** @var array */
  45. private $sizes = [];
  46. /** @var array */
  47. private $menus = [];
  48. /** @var array */
  49. private $jobs = [];
  50. /** @var string */
  51. private $push = '';
  52. /** @var array */
  53. private $settings = [];
  54. /**
  55. * Get the defined size for a specific type (min, max, default)
  56. * Returns an array:
  57. * [
  58. * 'width' => width,
  59. * 'height' => height
  60. * ]
  61. *
  62. *
  63. * @since 15.0.0
  64. * @deprecated 20.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. * @deprecated 20.0.0
  81. *
  82. * @return array
  83. */
  84. public function getSizes(): array {
  85. return $this->sizes;
  86. }
  87. /**
  88. * Add a new size to the setup.
  89. *
  90. * @since 15.0.0
  91. * @deprecated 20.0.0
  92. *
  93. * @param string $type
  94. * @param int $width
  95. * @param int $height
  96. *
  97. * @return WidgetSetup
  98. */
  99. public function addSize(string $type, int $width, int $height): WidgetSetup {
  100. $this->sizes[$type] = [
  101. 'width' => $width,
  102. 'height' => $height
  103. ];
  104. return $this;
  105. }
  106. /**
  107. * Returns menu entries.
  108. *
  109. * @since 15.0.0
  110. * @deprecated 20.0.0
  111. *
  112. * @return array
  113. */
  114. public function getMenuEntries(): array {
  115. return $this->menus;
  116. }
  117. /**
  118. * Add a menu entry to the widget.
  119. * $function is the Javascript function to be called when clicking the
  120. * menu entry.
  121. * $icon is the css class of the icon.
  122. * $text is the display name of the menu entry.
  123. *
  124. * @since 15.0.0
  125. * @deprecated 20.0.0
  126. *
  127. * @param string $function
  128. * @param string $icon
  129. * @param string $text
  130. *
  131. * @return WidgetSetup
  132. */
  133. public function addMenuEntry(string $function, string $icon, string $text): WidgetSetup {
  134. $this->menus[] = [
  135. 'function' => $function,
  136. 'icon' => $icon,
  137. 'text' => $text
  138. ];
  139. return $this;
  140. }
  141. /**
  142. * Add a delayed job to the widget.
  143. *
  144. * $function is the Javascript function to be called.
  145. * $delay is the time in seconds between each call.
  146. *
  147. * @since 15.0.0
  148. * @deprecated 20.0.0
  149. *
  150. * @param string $function
  151. * @param int $delay
  152. *
  153. * @return WidgetSetup
  154. */
  155. public function addDelayedJob(string $function, int $delay): WidgetSetup {
  156. $this->jobs[] = [
  157. 'function' => $function,
  158. 'delay' => $delay
  159. ];
  160. return $this;
  161. }
  162. /**
  163. * Get delayed jobs.
  164. *
  165. * @since 15.0.0
  166. * @deprecated 20.0.0
  167. *
  168. * @return array
  169. */
  170. public function getDelayedJobs(): array {
  171. return $this->jobs;
  172. }
  173. /**
  174. * Get the push function, called when an event is send to the front-end
  175. *
  176. * @since 15.0.0
  177. * @deprecated 20.0.0
  178. *
  179. * @return string
  180. */
  181. public function getPush(): string {
  182. return $this->push;
  183. }
  184. /**
  185. * Set the Javascript function to be called when an event is pushed to the
  186. * frontend.
  187. *
  188. * @since 15.0.0
  189. * @deprecated 20.0.0
  190. *
  191. * @param string $function
  192. *
  193. * @return WidgetSetup
  194. */
  195. public function setPush(string $function): WidgetSetup {
  196. $this->push = $function;
  197. return $this;
  198. }
  199. /**
  200. * Returns the default settings for a widget.
  201. *
  202. * @since 15.0.0
  203. * @deprecated 20.0.0
  204. *
  205. * @return array
  206. */
  207. public function getDefaultSettings(): array {
  208. return $this->settings;
  209. }
  210. /**
  211. * Set the default settings for a widget.
  212. * This method is used by the Dashboard app, using the settings created
  213. * using WidgetSetting
  214. *
  215. * @see WidgetSetting
  216. *
  217. * @since 15.0.0
  218. * @deprecated 20.0.0
  219. *
  220. * @param array $settings
  221. *
  222. * @return WidgetSetup
  223. */
  224. public function setDefaultSettings(array $settings): WidgetSetup {
  225. $this->settings = $settings;
  226. return $this;
  227. }
  228. /**
  229. * @since 15.0.0
  230. * @deprecated 20.0.0
  231. *
  232. * @return array
  233. */
  234. public function jsonSerialize() {
  235. return [
  236. 'size' => $this->getSizes(),
  237. 'menu' => $this->getMenuEntries(),
  238. 'jobs' => $this->getDelayedJobs(),
  239. 'push' => $this->getPush(),
  240. 'settings' => $this->getDefaultSettings()
  241. ];
  242. }
  243. }