ASettings.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Settings;
  8. use OCA\WorkflowEngine\AppInfo\Application;
  9. use OCA\WorkflowEngine\Manager;
  10. use OCP\AppFramework\Http\TemplateResponse;
  11. use OCP\AppFramework\Services\IInitialState;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IConfig;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\Settings\ISettings;
  17. use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
  18. use OCP\WorkflowEngine\ICheck;
  19. use OCP\WorkflowEngine\IComplexOperation;
  20. use OCP\WorkflowEngine\IEntity;
  21. use OCP\WorkflowEngine\IEntityEvent;
  22. use OCP\WorkflowEngine\IOperation;
  23. use OCP\WorkflowEngine\ISpecificOperation;
  24. abstract class ASettings implements ISettings {
  25. private IL10N $l10n;
  26. private string $appName;
  27. private IEventDispatcher $eventDispatcher;
  28. protected Manager $manager;
  29. private IInitialState $initialStateService;
  30. private IConfig $config;
  31. private IURLGenerator $urlGenerator;
  32. public function __construct(
  33. string $appName,
  34. IL10N $l,
  35. IEventDispatcher $eventDispatcher,
  36. Manager $manager,
  37. IInitialState $initialStateService,
  38. IConfig $config,
  39. IURLGenerator $urlGenerator
  40. ) {
  41. $this->appName = $appName;
  42. $this->l10n = $l;
  43. $this->eventDispatcher = $eventDispatcher;
  44. $this->manager = $manager;
  45. $this->initialStateService = $initialStateService;
  46. $this->config = $config;
  47. $this->urlGenerator = $urlGenerator;
  48. }
  49. abstract public function getScope(): int;
  50. /**
  51. * @return TemplateResponse
  52. */
  53. public function getForm(): TemplateResponse {
  54. // @deprecated in 20.0.0: retire this one in favor of the typed event
  55. $this->eventDispatcher->dispatch(
  56. 'OCP\WorkflowEngine::loadAdditionalSettingScripts',
  57. new LoadSettingsScriptsEvent()
  58. );
  59. $this->eventDispatcher->dispatchTyped(new LoadSettingsScriptsEvent());
  60. $entities = $this->manager->getEntitiesList();
  61. $this->initialStateService->provideInitialState(
  62. 'entities',
  63. $this->entitiesToArray($entities)
  64. );
  65. $operators = $this->manager->getOperatorList();
  66. $this->initialStateService->provideInitialState(
  67. 'operators',
  68. $this->operatorsToArray($operators)
  69. );
  70. $checks = $this->manager->getCheckList();
  71. $this->initialStateService->provideInitialState(
  72. 'checks',
  73. $this->checksToArray($checks)
  74. );
  75. $this->initialStateService->provideInitialState(
  76. 'scope',
  77. $this->getScope()
  78. );
  79. $this->initialStateService->provideInitialState(
  80. 'appstoreenabled',
  81. $this->config->getSystemValueBool('appstoreenabled', true)
  82. );
  83. $this->initialStateService->provideInitialState(
  84. 'doc-url',
  85. $this->urlGenerator->linkToDocs('admin-workflowengine')
  86. );
  87. return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank');
  88. }
  89. /**
  90. * @return string the section ID, e.g. 'sharing'
  91. */
  92. public function getSection(): ?string {
  93. return 'workflow';
  94. }
  95. /**
  96. * @return int whether the form should be rather on the top or bottom of
  97. * the admin section. The forms are arranged in ascending order of the
  98. * priority values. It is required to return a value between 0 and 100.
  99. *
  100. * E.g.: 70
  101. */
  102. public function getPriority(): int {
  103. return 0;
  104. }
  105. private function entitiesToArray(array $entities) {
  106. return array_map(function (IEntity $entity) {
  107. $events = array_map(function (IEntityEvent $entityEvent) {
  108. return [
  109. 'eventName' => $entityEvent->getEventName(),
  110. 'displayName' => $entityEvent->getDisplayName()
  111. ];
  112. }, $entity->getEvents());
  113. return [
  114. 'id' => get_class($entity),
  115. 'icon' => $entity->getIcon(),
  116. 'name' => $entity->getName(),
  117. 'events' => $events,
  118. ];
  119. }, $entities);
  120. }
  121. private function operatorsToArray(array $operators) {
  122. $operators = array_filter($operators, function (IOperation $operator) {
  123. return $operator->isAvailableForScope($this->getScope());
  124. });
  125. return array_map(function (IOperation $operator) {
  126. return [
  127. 'id' => get_class($operator),
  128. 'icon' => $operator->getIcon(),
  129. 'name' => $operator->getDisplayName(),
  130. 'description' => $operator->getDescription(),
  131. 'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '',
  132. 'isComplex' => $operator instanceof IComplexOperation,
  133. 'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '',
  134. ];
  135. }, $operators);
  136. }
  137. private function checksToArray(array $checks) {
  138. $checks = array_filter($checks, function (ICheck $check) {
  139. return $check->isAvailableForScope($this->getScope());
  140. });
  141. return array_map(function (ICheck $check) {
  142. return [
  143. 'id' => get_class($check),
  144. 'supportedEntities' => $check->supportedEntities(),
  145. ];
  146. }, $checks);
  147. }
  148. }