GetAllViewHelper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Members;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\MemberRepository;
  5. use Opentalent\OtTemplating\Exception\ApiRequestException;
  6. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  7. /**
  8. * This view helper give access to an array named according to the 'as' variable
  9. * and which contains the members of the structure, eventually grouped by instruments
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:members.getAll as="members"
  14. * organizationId="{settings.organizationId}">
  15. * <f:debug>{members}</f:debug>
  16. * </ot:members.getAll>
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetAllViewHelper extends AbstractViewHelper {
  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 MemberRepository
  29. *
  30. */
  31. protected $memberRepository;
  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 current structure',
  48. true
  49. );
  50. $this->registerArgument(
  51. 'groupByInstruments',
  52. 'bool',
  53. 'If true, returns an array of the form [instrument=>[members]],
  54. else the returned array is simply [members]',
  55. false,
  56. false
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Fluid --
  61. * Renders the content as html
  62. *
  63. * @return string
  64. * @throws ApiRequestException
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $organizationId = $this->arguments['organizationId'];
  71. $groupByInstruments = $this->arguments['groupByInstruments'];
  72. // Get members of the structure
  73. $members = $this->memberRepository->findByOrganizationId($organizationId);
  74. // Sort by instrument (conductor first), then alphabetically by name
  75. usort($members,
  76. function($a, $b) {
  77. if ($a->getInstrument() == 'CONDUCTOR') {
  78. return -1;
  79. } else if ($b->getInstrument() == 'CONDUCTOR') {
  80. return 1;
  81. } else if ($a->getInstrument() != $b->getInstrument()) {
  82. return strcmp($a->getInstrument(), $b->getInstrument());
  83. } else {
  84. return strcmp($a->getName(), $b->getName());
  85. }
  86. }
  87. );
  88. if ($groupByInstruments) {
  89. // Instruments to display in first place (next will be sorted alphabetically)
  90. $stack1 = ['CONDUCTOR' => []];
  91. $stack2 = [];
  92. foreach ($members as $member) {
  93. // If that Instrument exist in stack1: put it in this one
  94. if (array_key_exists($member->getInstrument(), $stack1)) {
  95. array_push($stack1[$member->getInstrument()], $member);
  96. } else {
  97. // Create the new array if needed in stack2, then put the member in it
  98. if (!array_key_exists($member->getInstrument(), $stack2)) {
  99. $stack2[$member->getInstrument()] = [];
  100. }
  101. array_push($stack2[$member->getInstrument()], $member);
  102. }
  103. }
  104. // remove empty instruments in stack 1
  105. $stack1 = array_filter($stack1);
  106. // sort by instrument stack2
  107. ksort($stack2);
  108. $members = array_merge($stack1, $stack2);
  109. }
  110. $variables = [$as => $members];
  111. return $this->renderChildrenWithVariables($variables);
  112. }
  113. /**
  114. * @param MemberRepository $memberRepository
  115. */
  116. public function injectMemberRepository(MemberRepository $memberRepository)
  117. {
  118. $this->memberRepository = $memberRepository;
  119. }
  120. }