ASettings.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. public function __construct(
  26. private string $appName,
  27. private IL10N $l10n,
  28. private IEventDispatcher $eventDispatcher,
  29. protected Manager $manager,
  30. private IInitialState $initialStateService,
  31. private IConfig $config,
  32. private IURLGenerator $urlGenerator,
  33. ) {
  34. }
  35. abstract public function getScope(): int;
  36. /**
  37. * @return TemplateResponse
  38. */
  39. public function getForm(): TemplateResponse {
  40. // @deprecated in 20.0.0: retire this one in favor of the typed event
  41. $this->eventDispatcher->dispatch(
  42. 'OCP\WorkflowEngine::loadAdditionalSettingScripts',
  43. new LoadSettingsScriptsEvent()
  44. );
  45. $this->eventDispatcher->dispatchTyped(new LoadSettingsScriptsEvent());
  46. $entities = $this->manager->getEntitiesList();
  47. $this->initialStateService->provideInitialState(
  48. 'entities',
  49. $this->entitiesToArray($entities)
  50. );
  51. $operators = $this->manager->getOperatorList();
  52. $this->initialStateService->provideInitialState(
  53. 'operators',
  54. $this->operatorsToArray($operators)
  55. );
  56. $checks = $this->manager->getCheckList();
  57. $this->initialStateService->provideInitialState(
  58. 'checks',
  59. $this->checksToArray($checks)
  60. );
  61. $this->initialStateService->provideInitialState(
  62. 'scope',
  63. $this->getScope()
  64. );
  65. $this->initialStateService->provideInitialState(
  66. 'appstoreenabled',
  67. $this->config->getSystemValueBool('appstoreenabled', true)
  68. );
  69. $this->initialStateService->provideInitialState(
  70. 'doc-url',
  71. $this->urlGenerator->linkToDocs('admin-workflowengine')
  72. );
  73. return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank');
  74. }
  75. /**
  76. * @return string the section ID, e.g. 'sharing'
  77. */
  78. public function getSection(): ?string {
  79. return 'workflow';
  80. }
  81. /**
  82. * @return int whether the form should be rather on the top or bottom of
  83. * the admin section. The forms are arranged in ascending order of the
  84. * priority values. It is required to return a value between 0 and 100.
  85. *
  86. * E.g.: 70
  87. */
  88. public function getPriority(): int {
  89. return 0;
  90. }
  91. private function entitiesToArray(array $entities) {
  92. return array_map(function (IEntity $entity) {
  93. $events = array_map(function (IEntityEvent $entityEvent) {
  94. return [
  95. 'eventName' => $entityEvent->getEventName(),
  96. 'displayName' => $entityEvent->getDisplayName()
  97. ];
  98. }, $entity->getEvents());
  99. return [
  100. 'id' => get_class($entity),
  101. 'icon' => $entity->getIcon(),
  102. 'name' => $entity->getName(),
  103. 'events' => $events,
  104. ];
  105. }, $entities);
  106. }
  107. private function operatorsToArray(array $operators) {
  108. $operators = array_filter($operators, function (IOperation $operator) {
  109. return $operator->isAvailableForScope($this->getScope());
  110. });
  111. return array_map(function (IOperation $operator) {
  112. return [
  113. 'id' => get_class($operator),
  114. 'icon' => $operator->getIcon(),
  115. 'name' => $operator->getDisplayName(),
  116. 'description' => $operator->getDescription(),
  117. 'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '',
  118. 'isComplex' => $operator instanceof IComplexOperation,
  119. 'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '',
  120. ];
  121. }, $operators);
  122. }
  123. private function checksToArray(array $checks) {
  124. $checks = array_filter($checks, function (ICheck $check) {
  125. return $check->isAvailableForScope($this->getScope());
  126. });
  127. return array_map(function (ICheck $check) {
  128. return [
  129. 'id' => get_class($check),
  130. 'supportedEntities' => $check->supportedEntities(),
  131. ];
  132. }, $checks);
  133. }
  134. }