GetFormUrlByEventIdViewHelper.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\HelloAsso;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Exception\ApiRequestException;
  5. use Opentalent\OtCore\Logging\OtLogger;
  6. use Opentalent\OtCore\Service\OpentalentApiService;
  7. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  8. /**
  9. * This view helper returns the HelloAsso form's URL for the given event
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:helloAsso.getFormUrlByEventId as="form" eventId="1">
  14. * <f:debug>{form}</f:debug>
  15. * </ot:helloAsso.getFormUrlByEventId>
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetFormUrlByEventIdViewHelper extends OtAbstractViewHelper {
  20. use TemplateVariableViewHelperTrait;
  21. /**
  22. * >> Required to prevent typo3 to escape the html output
  23. * @var boolean
  24. */
  25. protected $escapeOutput = false;
  26. /**
  27. * @var OpentalentApiService
  28. */
  29. protected OpentalentApiService $opentalentApiService;
  30. public function injectOpentalentApiService(OpentalentApiService $opentalentApiService) {
  31. $this->opentalentApiService = $opentalentApiService;
  32. }
  33. /**
  34. * -- This method is expected by Fluid --
  35. * Declares the viewhelper's parameters
  36. */
  37. public function initializeArguments()
  38. {
  39. $this->registerArgument(
  40. 'as',
  41. 'string',
  42. 'Name of the returned array',
  43. true
  44. );
  45. $this->registerArgument(
  46. 'eventId',
  47. 'integer',
  48. 'Id of the event',
  49. true
  50. );
  51. }
  52. /**
  53. * -- This method is expected by Fluid --
  54. * Renders the content as html
  55. *
  56. * @return string
  57. * @throws ApiRequestException
  58. */
  59. public function render()
  60. {
  61. $as = $this->arguments['as'];
  62. $eventId = $this->arguments['eventId'];
  63. $uri = $this->opentalentApiService->getApiUri(
  64. 'api/public/helloasso/form/by-event/'.$eventId,
  65. false,
  66. true
  67. );
  68. try {
  69. $form = $this->opentalentApiService->getJsonDecoded($uri);
  70. } catch (ApiRequestException $e) {
  71. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  72. return "<error/><!--Error while getting the form data-->";
  73. }
  74. $variables = [$as => $form];
  75. return $this->renderChildrenWithVariables($variables);
  76. }
  77. }