GetAllViewHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Donors;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Domain\Repository\DonorRepository;
  5. use Opentalent\OtCore\Exception\ApiRequestException;
  6. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  7. /**
  8. * This view helper give access to an ApiPagedCollection named according to the 'as' variable
  9. * and which contains all the donors of the given structure
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:donors.getAll as="donors"
  14. * organizationId="{settings.organizationId}">
  15. * <f:debug>{donors}</f:debug>
  16. * </ot:donors.getAll>
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetAllViewHelper extends OtAbstractViewHelper {
  21. use TemplateVariableViewHelperTrait;
  22. /**
  23. * >> Required to prevent typo3 to escape the html output
  24. * @var boolean
  25. */
  26. protected $escapeOutput = false;
  27. /**
  28. * @var DonorRepository
  29. *
  30. */
  31. protected $donorRepository;
  32. /**
  33. * -- This method is expected by Fluid --
  34. * Declares the viewhelper's parameters
  35. */
  36. public function initializeArguments()
  37. {
  38. $this->registerArgument(
  39. 'as',
  40. 'string',
  41. 'Name of the returned array',
  42. true
  43. );
  44. $this->registerArgument(
  45. 'organizationId',
  46. 'integer',
  47. 'Id of the current structure',
  48. true
  49. );
  50. $this->registerArgument(
  51. 'fromParents',
  52. 'bool',
  53. 'Get donors from parents instead',
  54. false,
  55. 0
  56. );
  57. }
  58. /**
  59. * -- This method is expected by Fluid --
  60. * Renders the content as html
  61. *
  62. * @return string
  63. * @throws ApiRequestException
  64. */
  65. public function render()
  66. {
  67. // Get current settings
  68. $as = $this->arguments['as'];
  69. $organizationId = $this->arguments['organizationId'];
  70. $fromParents = $this->arguments['fromParents'];
  71. if ($fromParents) {
  72. // Get the donors of the parent structures
  73. try {
  74. $donors = $this->donorRepository->findParentsByOrganizationId($organizationId);
  75. } catch (ApiRequestException $e) {
  76. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  77. $donors = [];
  78. }
  79. } else {
  80. // Get donors of the structure
  81. try {
  82. $donors = $this->donorRepository->findByOrganizationId($organizationId);
  83. } catch (ApiRequestException $e) {
  84. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  85. $donors = [];
  86. }
  87. }
  88. $variables = [$as => $donors];
  89. return $this->renderChildrenWithVariables($variables);
  90. }
  91. /**
  92. * @param DonorRepository $donorRepository
  93. */
  94. public function injectDonorRepository(DonorRepository $donorRepository)
  95. {
  96. $this->donorRepository = $donorRepository;
  97. }
  98. }