1
0

ASettings.php 5.6 KB

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