GetChildrenViewHelper.php 2.8 KB

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