GetByIdViewHelper.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Model\Organization;
  5. use Opentalent\OtTemplating\Domain\Repository\OrganizationRepository;
  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 return the Organization object matching the given id
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * <ot:organizations.getById as="organization"
  16. * organizationId="1">
  17. * <f:debug>{organization}</f:debug>
  18. * </ot:organizations.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 OrganizationRepository
  32. *
  33. */
  34. protected $organizationRepository;
  35. /**
  36. * -- This method is expected by Fluid --
  37. * Declares the viewhelper's parameters
  38. */
  39. public function initializeArguments()
  40. {
  41. $this->registerArgument(
  42. 'as',
  43. 'string',
  44. 'Name of the returned array',
  45. true
  46. );
  47. $this->registerArgument(
  48. 'organizationId',
  49. 'integer',
  50. 'Id of the organization',
  51. true
  52. );
  53. }
  54. /**
  55. * -- This method is expected by Fluid --
  56. * Renders the content as html
  57. *
  58. * @return string
  59. * @throws ApiRequestException
  60. */
  61. public function render()
  62. {
  63. $as = $this->arguments['as'];
  64. $organizationId = $this->arguments['organizationId'];
  65. try {
  66. $organization = $this->organizationRepository->findById($organizationId);
  67. } catch (ApiRequestException $e) {
  68. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  69. $organization = new Organization();
  70. $organization->setName("<Erreur: impossible d'afficher le résultat>");
  71. }
  72. $variables = [$as => $organization];
  73. return $this->renderChildrenWithVariables($variables);
  74. }
  75. /**
  76. * @param OrganizationRepository $organizationRepository
  77. */
  78. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  79. {
  80. $this->organizationRepository = $organizationRepository;
  81. }
  82. }