GetAllViewHelper.php 3.2 KB

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