FlowOperations.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * @param string $class
  53. * @param string $name
  54. * @param array[] $checks
  55. * @param string $operation
  56. * @return JSONResponse The added element
  57. */
  58. public function addOperation($class, $name, $checks, $operation) {
  59. try {
  60. $operation = $this->manager->addOperation($class, $name, $checks, $operation);
  61. $operation = $this->prepareOperation($operation);
  62. return new JSONResponse($operation);
  63. } catch (\UnexpectedValueException $e) {
  64. return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  65. }
  66. }
  67. /**
  68. * @param int $id
  69. * @param string $name
  70. * @param array[] $checks
  71. * @param string $operation
  72. * @return JSONResponse The updated element
  73. */
  74. public function updateOperation($id, $name, $checks, $operation) {
  75. try {
  76. $operation = $this->manager->updateOperation($id, $name, $checks, $operation);
  77. $operation = $this->prepareOperation($operation);
  78. return new JSONResponse($operation);
  79. } catch (\UnexpectedValueException $e) {
  80. return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  81. }
  82. }
  83. /**
  84. * @param int $id
  85. * @return JSONResponse
  86. */
  87. public function deleteOperation($id) {
  88. $deleted = $this->manager->deleteOperation((int) $id);
  89. return new JSONResponse($deleted);
  90. }
  91. /**
  92. * @param array $operation
  93. * @return array
  94. */
  95. protected function prepareOperation(array $operation) {
  96. $checkIds = json_decode($operation['checks']);
  97. $checks = $this->manager->getChecks($checkIds);
  98. $operation['checks'] = [];
  99. foreach ($checks as $check) {
  100. // Remove internal values
  101. unset($check['id']);
  102. unset($check['hash']);
  103. $operation['checks'][] = $check;
  104. }
  105. return $operation;
  106. }
  107. }