GetChildFederationViewHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\ViewHelpers\OtAbstractViewHelper;
  6. /**
  7. * This view helper returns all of the children federations as an ApiPagedCollection, without pagination
  8. *
  9. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  10. *
  11. * <ot:organizations.getChildFederation as="organization"
  12. * organizationId="1">
  13. * <f:debug>{organization}</f:debug>
  14. * </ot:organizations.getChildFederation>
  15. *
  16. * @package Opentalent\OtTemplating\ViewHelpers
  17. */
  18. class GetChildFederationViewHelper extends OtAbstractViewHelper {
  19. use TemplateVariableViewHelperTrait;
  20. /**
  21. * >> Required to prevent typo3 to escape the html output
  22. * @var boolean
  23. */
  24. protected $escapeOutput = false;
  25. /**
  26. * @var FederationStructureRepository
  27. *
  28. */
  29. protected $federationRepository;
  30. /**
  31. * -- This method is expected by Fluid --
  32. * Declares the viewhelper's parameters
  33. */
  34. public function initializeArguments()
  35. {
  36. $this->registerArgument(
  37. 'as',
  38. 'string',
  39. 'Name of the returned array',
  40. true
  41. );
  42. $this->registerArgument(
  43. 'parentId',
  44. 'integer',
  45. 'Id of the parent organization',
  46. true
  47. );
  48. $this->registerArgument(
  49. 'itemsPerPage',
  50. 'string',
  51. 'Number of items per page',
  52. false,
  53. 10
  54. );
  55. }
  56. /**
  57. * -- This method is expected by Fluid --
  58. * Renders the content as html
  59. *
  60. * @return string
  61. * @throws \Opentalent\OtCore\Exception\ApiRequestException
  62. */
  63. public function render()
  64. {
  65. $as = $this->arguments['as'];
  66. $parentId = $this->arguments['parentId'];
  67. $itemsPerPage = $this->arguments['itemsPerPage'];
  68. $page = $_REQUEST['page'] ?? 1;
  69. if ($itemsPerPage == 'all') {
  70. $itemsPerPage = 99999; // the 'all' keyword raise an error during the api call
  71. $page = 1;
  72. }
  73. $searchParams = ['itemsPerPage' => $itemsPerPage];
  74. $searchParams['filter[where][n1Id]'] = $parentId;
  75. $searchParams['filter[end][principalType]'] = 'FEDERATION';
  76. $organizations = $this->federationRepository->findChildrenById($parentId, $searchParams, $page, 1);
  77. $variables = [
  78. $as => $organizations
  79. ];
  80. return $this->renderChildrenWithVariables($variables);
  81. }
  82. /**
  83. * @param FederationStructureRepository $federationRepository
  84. */
  85. public function injectFederationRepository(FederationStructureRepository $federationRepository)
  86. {
  87. $this->federationRepository = $federationRepository;
  88. }
  89. }