GetChildrenViewHelper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  5. use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
  6. use Opentalent\OtCore\Exception\ApiRequestException;
  7. /**
  8. * This view helper returns the organization children structures as an ApiPagedCollection
  9. *
  10. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  11. *
  12. * <ot:organizations.getChildren as="organization"
  13. * organizationId="1">
  14. * <f:debug>{organization}</f:debug>
  15. * </ot:organizations.getChildren>
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetChildrenViewHelper extends OtAbstractViewHelper {
  20. use TemplateVariableViewHelperTrait;
  21. /**
  22. * >> Required to prevent typo3 to escape the html output
  23. * @var boolean
  24. */
  25. protected $escapeOutput = false;
  26. /**
  27. * @var OrganizationRepository
  28. *
  29. */
  30. protected $organizationRepository;
  31. /**
  32. * -- This method is expected by Fluid --
  33. * Declares the viewhelper's parameters
  34. */
  35. public function initializeArguments()
  36. {
  37. $this->registerArgument(
  38. 'as',
  39. 'string',
  40. 'Name of the returned array',
  41. true
  42. );
  43. $this->registerArgument(
  44. 'organizationId',
  45. 'integer',
  46. 'Id of the organization',
  47. true
  48. );
  49. $this->registerArgument(
  50. 'page',
  51. 'integer',
  52. 'Page number',
  53. false,
  54. 1
  55. );
  56. }
  57. /**
  58. * -- This method is expected by Fluid --
  59. * Renders the content as html
  60. *
  61. * @return string
  62. */
  63. public function render()
  64. {
  65. $as = $this->arguments['as'];
  66. $organizationId = $this->arguments['organizationId'];
  67. $page = $_REQUEST['page'] ?? 1;
  68. $searchParams = [];
  69. if($_REQUEST['search-loc']) {
  70. $searchParams['where'] = $_REQUEST['search-loc'];
  71. }
  72. try {
  73. $organizations = $this->organizationRepository->findChildrenById(
  74. $organizationId,
  75. $searchParams,
  76. $page
  77. );
  78. } catch (ApiRequestException $e) {
  79. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  80. $organizations = [];
  81. }
  82. $variables = [
  83. $as => $organizations
  84. ];
  85. return $this->renderChildrenWithVariables($variables);
  86. }
  87. /**
  88. * @param OrganizationRepository $organizationRepository
  89. */
  90. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  91. {
  92. $this->organizationRepository = $organizationRepository;
  93. }
  94. }