GetFederationStructuresViewHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 organization children structures as an ApiPagedCollection, without pagination
  8. *
  9. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  10. *
  11. * <ot:organizations.getFederationStructures as="organization"
  12. * organizationId="1">
  13. * <f:debug>{organization}</f:debug>
  14. * </ot:organizations.getFederationStructures>
  15. *
  16. * @package Opentalent\OtTemplating\ViewHelpers
  17. */
  18. class GetFederationStructuresViewHelper 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. */
  62. public function render()
  63. {
  64. $as = $this->arguments['as'];
  65. $parentId = $this->arguments['parentId'];
  66. $itemsPerPage = $this->arguments['itemsPerPage'];
  67. $page = $_REQUEST['page'] ?? 1;
  68. if ($itemsPerPage == 'all') {
  69. $itemsPerPage = 99999; // the 'all' keyword raise an error during the api call
  70. $page = 1;
  71. }
  72. $searchParams = ['itemsPerPage' => $itemsPerPage];
  73. // If a federation was selected in the filters, it is used as parent
  74. if($_REQUEST['search-federation']) {
  75. $parentId = $_REQUEST['search-federation'];
  76. }
  77. // Search structures by name
  78. if($_REQUEST['search-query']) {
  79. $searchParams['what'] = $_REQUEST['search-query'];
  80. }
  81. // Filters
  82. if($_REQUEST['search-category']) {
  83. $searchParams['category'] = $_REQUEST['search-category'];
  84. }
  85. if($_REQUEST['search-province']) {
  86. $province = ltrim($_REQUEST['search-province'], '_'); // a trailing dot may be used to avoid fluid to strip the zero from the postal code
  87. $searchParams['province'] = $province;
  88. }
  89. if($_REQUEST['search-distance-max']) {
  90. $searchParams['category'] = $_REQUEST['search-distance-max'];
  91. }
  92. $organizations = $this->federationRepository->findChildrenById($parentId, $searchParams, $page);
  93. $variables = [
  94. $as => $organizations
  95. ];
  96. return $this->renderChildrenWithVariables($variables);
  97. }
  98. /**
  99. * @param FederationStructureRepository $federationRepository
  100. */
  101. public function injectFederationRepository(FederationStructureRepository $federationRepository)
  102. {
  103. $this->federationRepository = $federationRepository;
  104. }
  105. }