GetByIdViewHelper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Logging\OtLogger;
  5. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  6. use Opentalent\OtCore\Domain\Model\Organization;
  7. use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
  8. use Opentalent\OtCore\Exception\ApiRequestException;
  9. /**
  10. * This view helper return the Organization object matching the given id
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * <ot:organizations.getById as="organization"
  15. * organizationId="1">
  16. * <f:debug>{organization}</f:debug>
  17. * </ot:organizations.getById>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class GetByIdViewHelper extends OtAbstractViewHelper {
  22. use TemplateVariableViewHelperTrait;
  23. /**
  24. * >> Required to prevent typo3 to escape the html output
  25. * @var boolean
  26. */
  27. protected $escapeOutput = false;
  28. /**
  29. * @var OrganizationRepository
  30. *
  31. */
  32. protected $organizationRepository;
  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. 'organizationId',
  47. 'integer',
  48. 'Id of the organization',
  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. $organizationId = $this->arguments['organizationId'];
  63. try {
  64. $organization = $this->organizationRepository->findById($organizationId);
  65. } catch (ApiRequestException $e) {
  66. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  67. $organization = new Organization();
  68. $organization->setName("<Erreur: impossible d'afficher le résultat>");
  69. }
  70. $variables = [$as => $organization];
  71. return $this->renderChildrenWithVariables($variables);
  72. }
  73. /**
  74. * @param OrganizationRepository $organizationRepository
  75. */
  76. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  77. {
  78. $this->organizationRepository = $organizationRepository;
  79. }
  80. }