WidgetSetting.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 WidgetSetting
  31. *
  32. * Each setting that can be edited by a user should be defined in a
  33. * WidgetSetting.
  34. *
  35. * When using this framework, the settings interface is generated by the
  36. * Dashboard app.
  37. *
  38. * Each WidgetSetting must be generated and declared in the WidgetTemplate
  39. * during the setup of the widget in the IDashboardWidget using addSetting().
  40. *
  41. * @see IDashboardWidget::getWidgetTemplate
  42. * @see WidgetTemplate::addSetting
  43. *
  44. * @since 15.0.0
  45. *
  46. * @package OCP\Dashboard\Model
  47. */
  48. final class WidgetSetting implements JsonSerializable {
  49. const TYPE_INPUT = 'input';
  50. const TYPE_CHECKBOX = 'checkbox';
  51. /** @var string */
  52. private $name = '';
  53. /** @var string */
  54. private $title = '';
  55. /** @var string */
  56. private $type = '';
  57. /** @var string */
  58. private $placeholder = '';
  59. /** @var string */
  60. private $default = '';
  61. /**
  62. * WidgetSetting constructor.
  63. *
  64. * @since 15.0.0
  65. *
  66. * @param string $type
  67. */
  68. public function __construct(string $type = '') {
  69. $this->type = $type;
  70. }
  71. /**
  72. * Set the name of the setting (full string, no space)
  73. *
  74. * @since 15.0.0
  75. *
  76. * @param string $name
  77. *
  78. * @return WidgetSetting
  79. */
  80. public function setName(string $name): WidgetSetting {
  81. $this->name = $name;
  82. return $this;
  83. }
  84. /**
  85. * Get the name of the setting
  86. *
  87. * @since 15.0.0
  88. *
  89. * @return string
  90. */
  91. public function getName(): string {
  92. return $this->name;
  93. }
  94. /**
  95. * Set the title/display name of the setting.
  96. *
  97. * @since 15.0.0
  98. *
  99. * @param string $title
  100. *
  101. * @return WidgetSetting
  102. */
  103. public function setTitle(string $title): WidgetSetting {
  104. $this->title = $title;
  105. return $this;
  106. }
  107. /**
  108. * Get the title of the setting
  109. *
  110. * @since 15.0.0
  111. *
  112. * @return string
  113. */
  114. public function getTitle(): string {
  115. return $this->title;
  116. }
  117. /**
  118. * Set the type of the setting (input, checkbox, ...)
  119. *
  120. * @since 15.0.0
  121. *
  122. * @param string $type
  123. *
  124. * @return WidgetSetting
  125. */
  126. public function setType(string $type): WidgetSetting {
  127. $this->type = $type;
  128. return $this;
  129. }
  130. /**
  131. * Get the type of the setting.
  132. *
  133. * @since 15.0.0
  134. *
  135. * @return string
  136. */
  137. public function getType(): string {
  138. return $this->type;
  139. }
  140. /**
  141. * Set the placeholder (in case of type=input)
  142. *
  143. * @since 15.0.0
  144. *
  145. * @param string $text
  146. *
  147. * @return WidgetSetting
  148. */
  149. public function setPlaceholder(string $text): WidgetSetting {
  150. $this->placeholder = $text;
  151. return $this;
  152. }
  153. /**
  154. * Get the placeholder.
  155. *
  156. * @since 15.0.0
  157. *
  158. * @return string
  159. */
  160. public function getPlaceholder(): string {
  161. return $this->placeholder;
  162. }
  163. /**
  164. * Set the default value of the setting.
  165. *
  166. * @since 15.0.0
  167. *
  168. * @param string $value
  169. *
  170. * @return WidgetSetting
  171. */
  172. public function setDefault(string $value): WidgetSetting {
  173. $this->default = $value;
  174. return $this;
  175. }
  176. /**
  177. * Get the default value.
  178. *
  179. * @since 15.0.0
  180. *
  181. * @return string
  182. */
  183. public function getDefault(): string {
  184. return $this->default;
  185. }
  186. /**
  187. * @since 15.0.0
  188. *
  189. * @return array
  190. */
  191. public function jsonSerialize() {
  192. return [
  193. 'name' => $this->getName(),
  194. 'title' => $this->getTitle(),
  195. 'type' => $this->getTitle(),
  196. 'default' => $this->getDefault(),
  197. 'placeholder' => $this->getPlaceholder()
  198. ];
  199. }
  200. }