FlowOperations.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Controller;
  22. use OCA\WorkflowEngine\Manager;
  23. use OCP\AppFramework\Controller;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use OCP\IRequest;
  27. class FlowOperations extends Controller {
  28. /** @var Manager */
  29. protected $manager;
  30. /**
  31. * @param IRequest $request
  32. * @param Manager $manager
  33. */
  34. public function __construct(IRequest $request, Manager $manager) {
  35. parent::__construct('workflowengine', $request);
  36. $this->manager = $manager;
  37. }
  38. /**
  39. * @NoCSRFRequired
  40. *
  41. * @param string $class
  42. * @return JSONResponse
  43. */
  44. public function getOperations($class) {
  45. $operations = $this->manager->getOperations($class);
  46. foreach ($operations as &$operation) {
  47. $operation = $this->prepareOperation($operation);
  48. }
  49. return new JSONResponse($operations);
  50. }
  51. /**
  52. * @PasswordConfirmationRequired
  53. *
  54. * @param string $class
  55. * @param string $name
  56. * @param array[] $checks
  57. * @param string $operation
  58. * @return JSONResponse The added element
  59. */
  60. public function addOperation($class, $name, $checks, $operation) {
  61. try {
  62. $operation = $this->manager->addOperation($class, $name, $checks, $operation);
  63. $operation = $this->prepareOperation($operation);
  64. return new JSONResponse($operation);
  65. } catch (\UnexpectedValueException $e) {
  66. return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  67. }
  68. }
  69. /**
  70. * @PasswordConfirmationRequired
  71. *
  72. * @param int $id
  73. * @param string $name
  74. * @param array[] $checks
  75. * @param string $operation
  76. * @return JSONResponse The updated element
  77. */
  78. public function updateOperation($id, $name, $checks, $operation) {
  79. try {
  80. $operation = $this->manager->updateOperation($id, $name, $checks, $operation);
  81. $operation = $this->prepareOperation($operation);
  82. return new JSONResponse($operation);
  83. } catch (\UnexpectedValueException $e) {
  84. return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  85. }
  86. }
  87. /**
  88. * @PasswordConfirmationRequired
  89. *
  90. * @param int $id
  91. * @return JSONResponse
  92. */
  93. public function deleteOperation($id) {
  94. $deleted = $this->manager->deleteOperation((int) $id);
  95. return new JSONResponse($deleted);
  96. }
  97. /**
  98. * @param array $operation
  99. * @return array
  100. */
  101. protected function prepareOperation(array $operation) {
  102. $checkIds = json_decode($operation['checks']);
  103. $checks = $this->manager->getChecks($checkIds);
  104. $operation['checks'] = [];
  105. foreach ($checks as $check) {
  106. // Remove internal values
  107. unset($check['id']);
  108. unset($check['hash']);
  109. $operation['checks'][] = $check;
  110. }
  111. return $operation;
  112. }
  113. }