* {members} * * * @package Opentalent\OtTemplating\ViewHelpers */ class GetAllViewHelper extends AbstractViewHelper { use TemplateVariableViewHelperTrait; /** * >> Required to prevent typo3 to escape the html output * @var boolean */ protected $escapeOutput = false; /** * @var \Opentalent\OtTemplating\Domain\Repository\MemberRepository * */ protected $memberRepository; public function initializeArguments() { $this->registerArgument( 'as', 'string', 'Name of the returned array', true ); $this->registerArgument( 'organizationId', 'integer', 'Id of the current structure', true ); } /** * @return string */ public function render() { // Get current settings $as = $this->arguments['as']; $organizationId = $this->arguments['organizationId']; // Get members of the structure $members = $this->memberRepository->findByOrganizationId($organizationId); // Sort alphabetically by name usort($members, function($a, $b) {return strcmp($a->getName(), $b->getName());}); // Instruments to display in first place (next will be sorted alphabetically) $stack1 = ['CONDUCTOR' => []]; $stack2 = []; foreach ($members as $member) { // If that Instrument exist in stack1: put it in this one if (array_key_exists($member->getInstrument(), $stack1)) { array_push($stack1[$member->getInstrument()], $member); } else { // Create the new array if needed in stack2, then put the member in it if (!array_key_exists($member->getInstrument(), $stack2)) { $stack2[$member->getInstrument()] = []; } array_push($stack2[$member->getInstrument()], $member); } } // remove empty instruments in stack 1 $stack1 = array_filter($stack1); // sort by instrument stack2 ksort($stack2); $membersByInstrument = array_merge($stack1, $stack2); $variables = [$as => $membersByInstrument]; return $this->renderChildrenWithVariables($variables); } /** * @param \Opentalent\OtTemplating\Domain\Repository\MemberRepository $memberRepository */ public function injectMemberRepository(MemberRepository $memberRepository) { $this->memberRepository = $memberRepository; } }