1
0

ASettings.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\WorkflowEngine\Settings;
  30. use OCA\WorkflowEngine\AppInfo\Application;
  31. use OCA\WorkflowEngine\Manager;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Services\IInitialState;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\Settings\ISettings;
  38. use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
  39. use OCP\WorkflowEngine\ICheck;
  40. use OCP\WorkflowEngine\IComplexOperation;
  41. use OCP\WorkflowEngine\IEntity;
  42. use OCP\WorkflowEngine\IEntityEvent;
  43. use OCP\WorkflowEngine\IOperation;
  44. use OCP\WorkflowEngine\ISpecificOperation;
  45. abstract class ASettings implements ISettings {
  46. /** @var IL10N */
  47. private $l10n;
  48. /** @var string */
  49. private $appName;
  50. /** @var IEventDispatcher */
  51. private $eventDispatcher;
  52. /** @var Manager */
  53. protected $manager;
  54. /** @var IInitialState */
  55. private $initialStateService;
  56. /** @var IConfig */
  57. private $config;
  58. public function __construct(
  59. string $appName,
  60. IL10N $l,
  61. IEventDispatcher $eventDispatcher,
  62. Manager $manager,
  63. IInitialState $initialStateService,
  64. IConfig $config
  65. ) {
  66. $this->appName = $appName;
  67. $this->l10n = $l;
  68. $this->eventDispatcher = $eventDispatcher;
  69. $this->manager = $manager;
  70. $this->initialStateService = $initialStateService;
  71. $this->config = $config;
  72. }
  73. abstract public function getScope(): int;
  74. /**
  75. * @return TemplateResponse
  76. */
  77. public function getForm(): TemplateResponse {
  78. // @deprecated in 20.0.0: retire this one in favor of the typed event
  79. $this->eventDispatcher->dispatch(
  80. 'OCP\WorkflowEngine::loadAdditionalSettingScripts',
  81. new LoadSettingsScriptsEvent()
  82. );
  83. $this->eventDispatcher->dispatchTyped(new LoadSettingsScriptsEvent());
  84. $entities = $this->manager->getEntitiesList();
  85. $this->initialStateService->provideInitialState(
  86. 'entities',
  87. $this->entitiesToArray($entities)
  88. );
  89. $operators = $this->manager->getOperatorList();
  90. $this->initialStateService->provideInitialState(
  91. 'operators',
  92. $this->operatorsToArray($operators)
  93. );
  94. $checks = $this->manager->getCheckList();
  95. $this->initialStateService->provideInitialState(
  96. 'checks',
  97. $this->checksToArray($checks)
  98. );
  99. $this->initialStateService->provideInitialState(
  100. 'scope',
  101. $this->getScope()
  102. );
  103. $this->initialStateService->provideInitialState(
  104. 'appstoreenabled',
  105. $this->config->getSystemValueBool('appstoreenabled', true)
  106. );
  107. return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank');
  108. }
  109. /**
  110. * @return string the section ID, e.g. 'sharing'
  111. */
  112. public function getSection(): ?string {
  113. return 'workflow';
  114. }
  115. /**
  116. * @return int whether the form should be rather on the top or bottom of
  117. * the admin section. The forms are arranged in ascending order of the
  118. * priority values. It is required to return a value between 0 and 100.
  119. *
  120. * E.g.: 70
  121. */
  122. public function getPriority(): int {
  123. return 0;
  124. }
  125. private function entitiesToArray(array $entities) {
  126. return array_map(function (IEntity $entity) {
  127. $events = array_map(function (IEntityEvent $entityEvent) {
  128. return [
  129. 'eventName' => $entityEvent->getEventName(),
  130. 'displayName' => $entityEvent->getDisplayName()
  131. ];
  132. }, $entity->getEvents());
  133. return [
  134. 'id' => get_class($entity),
  135. 'icon' => $entity->getIcon(),
  136. 'name' => $entity->getName(),
  137. 'events' => $events,
  138. ];
  139. }, $entities);
  140. }
  141. private function operatorsToArray(array $operators) {
  142. $operators = array_filter($operators, function (IOperation $operator) {
  143. return $operator->isAvailableForScope($this->getScope());
  144. });
  145. return array_map(function (IOperation $operator) {
  146. return [
  147. 'id' => get_class($operator),
  148. 'icon' => $operator->getIcon(),
  149. 'name' => $operator->getDisplayName(),
  150. 'description' => $operator->getDescription(),
  151. 'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '',
  152. 'isComplex' => $operator instanceof IComplexOperation,
  153. 'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '',
  154. ];
  155. }, $operators);
  156. }
  157. private function checksToArray(array $checks) {
  158. $checks = array_filter($checks, function (ICheck $check) {
  159. return $check->isAvailableForScope($this->getScope());
  160. });
  161. return array_map(function (ICheck $check) {
  162. return [
  163. 'id' => get_class($check),
  164. 'supportedEntities' => $check->supportedEntities(),
  165. ];
  166. }, $checks);
  167. }
  168. }