* @author Stephen Abello * @author Guillaume Lajarige * @since 2.5.0 */ class AggregatePageBrickController extends BrickController { /** * Constructor. * * @param \Combodo\iTop\Portal\Brick\BrickCollection $oBrickCollection * * @since 3.2.0 N°6933 */ public function __construct( protected BrickCollection $oBrickCollection ) { } /** * @param \Symfony\Component\HttpFoundation\Request $oRequest * @param string $sBrickId * * @return \Symfony\Component\HttpFoundation\Response * * @throws BrickNotFoundException * @throws \Exception */ public function DisplayAction(Request $oRequest, $sBrickId) { /** @var \Combodo\iTop\Portal\Brick\AggregatePageBrick $oBrick */ $oBrick = $this->oBrickCollection->GetBrickById($sBrickId); $aAggregatePageBricksConf = $oBrick->GetAggregatePageBricks(); $aAggregatePageBricks = $this->GetOrderedAggregatePageBricksObjectsById($aAggregatePageBricksConf); $aTilesRendering = $this->GetBricksTileRendering($oRequest, $aAggregatePageBricks); $sLayoutTemplate = $oBrick->GetTemplatePath('page'); $aData = array( 'oBrick' => $oBrick, 'aggregatepage_bricks' => $aAggregatePageBricks, 'aTilesRendering' => $aTilesRendering, ); $oResponse = $this->render($sLayoutTemplate, $aData); return $oResponse; } /** * @param array $aAggregatePageBricksConf * * @return array * @throws \Exception */ private function GetOrderedAggregatePageBricksObjectsById($aAggregatePageBricksConf) { $aAggregatePageBricks = array(); foreach ($aAggregatePageBricksConf as $sBrickId => $iBrickRank) { try { $oPortalBrick = $this->oBrickCollection->GetBrickById($sBrickId); } catch (BrickNotFoundException $oException) { IssueLog::Debug('AggregatePageBrick: Could not display brick, either wrong id or user profile not allowed', LogChannels::PORTAL, [ 'brick_id' => $sBrickId, 'user_profiles' => UserRights::ListProfiles(), ]); continue; } $aAggregatePageBricks[] = $oPortalBrick; } return $aAggregatePageBricks; } /** * @param \Symfony\Component\HttpFoundation\Request $oRequest * @param \Combodo\iTop\Portal\Brick\PortalBrick[] $aBricks * * @return array rendering for each included tile (key = brick id, value = rendering) */ private function GetBricksTileRendering(Request $oRequest, $aBricks) { $aTilesRendering = array(); foreach ($aBricks as $oBrick) { if ($oBrick->GetTileControllerAction() !== null) { $aControllerActionParts = explode('::', $oBrick->GetTileControllerAction()); if (count($aControllerActionParts) !== 2) { throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Tile controller action must be of form "\Namespace\ControllerClass::FunctionName" for brick "'.$oBrick->GetId().'"'); } $aRouteParams = array(); // Add sBrickId in the route params as it is necessary for each brick actions if (is_a($aControllerActionParts[0], BrickController::class, true)) { $aRouteParams['sBrickId'] = $oBrick->GetId(); } /** @var \Symfony\Component\HttpFoundation\Response $oResponse */ $oResponse = $this->forward($oBrick->GetTileControllerAction(), $aRouteParams, $oRequest->query->all()); $aTilesRendering[$oBrick->GetId()] = $oResponse->getContent(); } } return $aTilesRendering; } }