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