GetAllCaViewHelper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Members;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\MemberRepository;
  5. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  6. /**
  7. * This view helper give access to an array named according to the 'as' variable
  8. * and which contains the CA members of the structure classed by role
  9. *
  10. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  11. *
  12. * <ot:members.getAllCa as="members"
  13. * organizationId="{settings.organizationId}">
  14. * <f:debug>{members}</f:debug>
  15. * </ot:members.getAllCa>
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetAllCaViewHelper extends AbstractViewHelper {
  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 MemberRepository
  28. *
  29. */
  30. protected $memberRepository;
  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. 'organizationId',
  45. 'integer',
  46. 'Id of the current structure',
  47. true
  48. );
  49. $this->registerArgument(
  50. 'groupByMissions',
  51. 'bool',
  52. 'If true, returns an array of the form [mission=>[members]],
  53. else the returned array is simply [members]',
  54. false,
  55. false
  56. );
  57. }
  58. /**
  59. * -- This method is expected by Fluid --
  60. * Renders the content as html
  61. *
  62. * @return string
  63. * @throws \GuzzleHttp\Exception\GuzzleException
  64. * @throws \Opentalent\OtTemplating\Exception\ApiRequestException
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $organizationId = $this->arguments['organizationId'];
  71. $groupByMissions = $this->arguments['groupByMissions'];
  72. // Missions to display (sorted)
  73. $missions = [
  74. 'PRESIDENT',
  75. 'HONORARY_PRESIDENT',
  76. 'VICE_PRESIDENT',
  77. 'VICE_PRESIDENT_OF_HONOR',
  78. 'PRESIDENT_ASSISTANT',
  79. 'HOUR_PRESIDENT',
  80. 'SECRETARY',
  81. 'ASSISTANT_SECRETARY',
  82. 'TREASURER',
  83. 'TREASURER_ASSISTANT',
  84. 'MEMBER_OF_THE_BOARD',
  85. 'MEMBER_OF_BOARD_OF_HONOR',
  86. 'ACTIVE_COOPTED_BOARD_MEMBER',
  87. 'ACTIVE_MEMBER_OF_THE_CA',
  88. 'HONORARY_MEMBER',
  89. 'YOUTH_REPRESENTATIVE'
  90. ];
  91. // Get members of the structure (only CA members)
  92. $members = $this->memberRepository->findByOrganizationId($organizationId, true);
  93. $members = array_filter($members, function($m) use ($missions) {
  94. return array_search($m->getMission(), $missions) !== false;
  95. });
  96. // Sort by roles, then alphabetically by name
  97. usort($members,
  98. function($a, $b) use ($missions) {
  99. if ($a->getMission() != $b->getMission()) {
  100. $ia = array_search($a->getMission(), $missions);
  101. $ib = array_search($b->getMission(), $missions);
  102. return $ia - $ib;
  103. } else {
  104. return strcmp($a->getName(), $b->getName());
  105. }
  106. }
  107. );
  108. if ($groupByMissions) {
  109. // Missions to display (sorted)
  110. $membersByMission = [];
  111. foreach ($missions as $mission) {
  112. $membersByMission[$mission] = [];
  113. }
  114. // Put members into their categories
  115. foreach ($members as $member) {
  116. if (array_key_exists($member->getMission(), $membersByMission)) {
  117. array_push($membersByMission[$member->getMission()], $member);
  118. }
  119. }
  120. // Remove empty sections
  121. $members = array_filter($membersByMission);
  122. }
  123. $variables = [$as => $members];
  124. return $this->renderChildrenWithVariables($variables);
  125. }
  126. /**
  127. * @param MemberRepository $memberRepository
  128. */
  129. public function injectMemberRepository(MemberRepository $memberRepository)
  130. {
  131. $this->memberRepository = $memberRepository;
  132. }
  133. }