GetChildFederationViewHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Domain\Repository\FederationStructureRepository;
  5. use Opentalent\OtCore\Exception\ApiRequestException;
  6. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  7. /**
  8. * This view helper returns all of the children federations as an ApiPagedCollection, without pagination
  9. *
  10. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  11. *
  12. * <ot:organizations.getChildFederation as="organization"
  13. * organizationId="1">
  14. * <f:debug>{organization}</f:debug>
  15. * </ot:organizations.getChildFederation>
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetChildFederationViewHelper 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 FederationStructureRepository
  28. *
  29. */
  30. protected $federationRepository;
  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. 'parentId',
  45. 'integer',
  46. 'Id of the parent organization',
  47. true
  48. );
  49. $this->registerArgument(
  50. 'itemsPerPage',
  51. 'string',
  52. 'Number of items per page',
  53. false,
  54. 10
  55. );
  56. }
  57. /**
  58. * -- This method is expected by Fluid --
  59. * Renders the content as html
  60. *
  61. * @return string
  62. * @throws ApiRequestException
  63. */
  64. public function render()
  65. {
  66. $as = $this->arguments['as'];
  67. $parentId = $this->arguments['parentId'];
  68. $itemsPerPage = $this->arguments['itemsPerPage'];
  69. $page = $_REQUEST['page'] ?? 1;
  70. if ($itemsPerPage == 'all') {
  71. $itemsPerPage = 99999; // the 'all' keyword raise an error during the api call
  72. $page = 1;
  73. }
  74. $searchParams = ['itemsPerPage' => $itemsPerPage];
  75. $searchParams['filter[where][n1Id]'] = $parentId;
  76. $searchParams['filter[end][principalType]'] = 'FEDERATION';
  77. $organizations = $this->federationRepository->findChildrenById($parentId, $searchParams, $page, 1);
  78. $variables = [
  79. $as => $organizations
  80. ];
  81. return $this->renderChildrenWithVariables($variables);
  82. }
  83. /**
  84. * @param FederationStructureRepository $federationRepository
  85. */
  86. public function injectFederationRepository(FederationStructureRepository $federationRepository)
  87. {
  88. $this->federationRepository = $federationRepository;
  89. }
  90. }