GetByIdViewHelper.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Model\Event;
  5. use Opentalent\OtTemplating\Domain\Repository\EventRepository;
  6. use Opentalent\OtTemplating\Exception\ApiRequestException;
  7. use Psr\Log\LoggerAwareInterface;
  8. use Psr\Log\LoggerAwareTrait;
  9. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  10. /**
  11. * This view helper returns the Event object matching the given id
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * <ot:events.getById as="event"
  16. * eventId="1">
  17. * <f:debug>{event}</f:debug>
  18. * </ot:events.getById>
  19. *
  20. * @package Opentalent\OtTemplating\ViewHelpers
  21. */
  22. class GetByIdViewHelper extends AbstractViewHelper implements LoggerAwareInterface {
  23. use LoggerAwareTrait;
  24. use TemplateVariableViewHelperTrait;
  25. /**
  26. * >> Required to prevent typo3 to escape the html output
  27. * @var boolean
  28. */
  29. protected $escapeOutput = false;
  30. /**
  31. * @var EventRepository
  32. */
  33. protected $eventRepository;
  34. /**
  35. * -- This method is expected by Fluid --
  36. * Declares the viewhelper's parameters
  37. */
  38. public function initializeArguments()
  39. {
  40. $this->registerArgument(
  41. 'as',
  42. 'string',
  43. 'Name of the returned array',
  44. true
  45. );
  46. $this->registerArgument(
  47. 'eventId',
  48. 'integer',
  49. 'Id of the event',
  50. true
  51. );
  52. }
  53. /**
  54. * -- This method is expected by Fluid --
  55. * Renders the content as html
  56. *
  57. * @return string
  58. * @throws ApiRequestException
  59. */
  60. public function render()
  61. {
  62. $as = $this->arguments['as'];
  63. $eventId = $this->arguments['eventId'];
  64. try {
  65. $event = $this->eventRepository->findById($eventId);
  66. } catch (ApiRequestException $e) {
  67. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  68. $event = new Event();
  69. $event->setName("<Erreur: impossible d'afficher l'évènement>");
  70. $event->setDescription("Une erreur s'est produite et ne permet pas l'affichage de cet évènement. Veuillez nous excusez pour la gêne occasionnée.");
  71. }
  72. $variables = [$as => $event];
  73. return $this->renderChildrenWithVariables($variables);
  74. }
  75. /**
  76. * @param EventRepository $eventRepository
  77. */
  78. public function injectEventRepository(EventRepository $eventRepository)
  79. {
  80. $this->eventRepository = $eventRepository;
  81. }
  82. }