GetAllCaViewHelper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Members;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Domain\Repository\MemberCaRepository;
  5. use Opentalent\OtCore\Logging\OtLogger;
  6. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  7. use Opentalent\OtCore\Exception\ApiRequestException;
  8. /**
  9. * This view helper give access to an array named according to the 'as' variable
  10. * and which contains the CA members of the structure classed by role
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * <ot:members.getAllCa as="members"
  15. * organizationId="{settings.organizationId}">
  16. * <f:debug>{members}</f:debug>
  17. * </ot:members.getAllCa>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class GetAllCaViewHelper extends OtAbstractViewHelper {
  22. use TemplateVariableViewHelperTrait;
  23. /**
  24. * >> Required to prevent typo3 to escape the html output
  25. * @var boolean
  26. */
  27. protected $escapeOutput = false;
  28. /**
  29. * @var MemberCaRepository
  30. *
  31. */
  32. protected MemberCaRepository $memberCaRepository;
  33. /**
  34. * -- This method is expected by Fluid --
  35. * Declares the viewhelper's parameters
  36. */
  37. public function initializeArguments(): void
  38. {
  39. $this->registerArgument(
  40. 'as',
  41. 'string',
  42. 'Name of the returned array',
  43. true
  44. );
  45. $this->registerArgument(
  46. 'organizationId',
  47. 'integer',
  48. 'Id of the current structure',
  49. true
  50. );
  51. $this->registerArgument(
  52. 'groupByMissions',
  53. 'bool',
  54. 'If true, returns an array of the form [mission=>[members]],
  55. else the returned array is simply [members]',
  56. false,
  57. false
  58. );
  59. }
  60. /**
  61. * -- This method is expected by Fluid --
  62. * Renders the content as html
  63. *
  64. * @return string
  65. */
  66. public function render(): string
  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. try {
  93. $collection = $this->memberCaRepository->findByOrganizationId($organizationId);
  94. $members = $collection->getMembers();
  95. } catch (ApiRequestException $e) {
  96. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  97. $members = [];
  98. }
  99. $members = array_filter($members, static function($m) use ($missions) {
  100. return in_array($m->getMission(), $missions, true);
  101. });
  102. // Sort by roles, then alphabetically by name
  103. usort($members,
  104. static function($a, $b) use ($missions) {
  105. if ($a->getMission() !== $b->getMission()) {
  106. $ia = array_search($a->getMission(), $missions, true);
  107. $ib = array_search($b->getMission(), $missions, true);
  108. return $ia - $ib;
  109. }
  110. return strcmp($a->getName(), $b->getName());
  111. }
  112. );
  113. if ($groupByMissions && !empty($members)) {
  114. // Missions to display (sorted)
  115. $membersByMission = [];
  116. foreach ($missions as $mission) {
  117. $membersByMission[$mission] = [];
  118. }
  119. // Put members into their categories
  120. foreach ($members as $member) {
  121. if (array_key_exists($member->getMission(), $membersByMission)) {
  122. $membersByMission[$member->getMission()][] = $member;
  123. }
  124. }
  125. // Remove empty sections and translate
  126. $members = [];
  127. foreach ($membersByMission as $mission => $groupMembers) {
  128. if (!$groupMembers) {
  129. continue;
  130. }
  131. $members[ucfirst($this->translate($mission))] = $groupMembers;
  132. }
  133. }
  134. $variables = [$as => $members];
  135. return $this->renderChildrenWithVariables($variables);
  136. }
  137. /**
  138. * @param MemberCaRepository $memberCaRepository
  139. */
  140. public function injectMemberCaRepository(MemberCaRepository $memberCaRepository): void
  141. {
  142. $this->memberCaRepository = $memberCaRepository;
  143. }
  144. }