GetAllViewHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Logging\OtLogger;
  7. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  8. /**
  9. * This view helper give access to an ApiPagedCollection named according to the 'as' variable
  10. * and which contains all the donors of the given structure
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * <ot:donors.getAll as="donors"
  15. * organizationId="{settings.organizationId}">
  16. * <f:debug>{donors}</f:debug>
  17. * </ot:donors.getAll>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class GetAllViewHelper 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 DonorRepository
  30. *
  31. */
  32. protected $donorRepository;
  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 current structure',
  49. true
  50. );
  51. $this->registerArgument(
  52. 'fromParents',
  53. 'bool',
  54. 'Get donors from parents instead',
  55. false,
  56. 0
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Fluid --
  61. * Renders the content as html
  62. *
  63. * @return string
  64. * @throws ApiRequestException
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $organizationId = $this->arguments['organizationId'];
  71. $fromParents = $this->arguments['fromParents'];
  72. if ($fromParents) {
  73. // Get the donors of the parent structures
  74. try {
  75. $donors = $this->donorRepository->findParentsByOrganizationId($organizationId);
  76. } catch (ApiRequestException $e) {
  77. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  78. $donors = [];
  79. }
  80. } else {
  81. // Get donors of the structure
  82. try {
  83. $donors = $this->donorRepository->findByOrganizationId($organizationId);
  84. } catch (ApiRequestException $e) {
  85. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  86. $donors = [];
  87. }
  88. }
  89. $variables = [$as => $donors];
  90. return $this->renderChildrenWithVariables($variables);
  91. }
  92. /**
  93. * @param DonorRepository $donorRepository
  94. */
  95. public function injectDonorRepository(DonorRepository $donorRepository)
  96. {
  97. $this->donorRepository = $donorRepository;
  98. }
  99. }