Skip to content

Commit

Permalink
N°8031 - Make all portal bricks use custom templates for all templates
Browse files Browse the repository at this point in the history
  • Loading branch information
steffunky committed Dec 17, 2024
1 parent afd96a0 commit 2418921
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 85 deletions.
36 changes: 22 additions & 14 deletions datamodels/2.x/itop-portal-base/portal/src/Brick/AbstractBrick.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ abstract class AbstractBrick
const DEFAULT_ALLOWED_PROFILES_OQL = '';
/** @var string DEFAULT_DENIED_PROFILES_OQL */
const DEFAULT_DENIED_PROFILES_OQL = '';
/** @var array $DEFAULT_TEMPLATES_PATH */
protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
];

/** @var string $sId */
protected $sId;
Expand Down Expand Up @@ -112,7 +116,7 @@ public function __construct()
$this->bActive = static::DEFAULT_ACTIVE;
$this->bVisible = static::DEFAULT_VISIBLE;
$this->fRank = static::DEFAULT_RANK;
$this->sPageTemplatePath = static::DEFAULT_PAGE_TEMPLATE_PATH;
$this->sPageTemplatePath = static::$DEFAULT_TEMPLATES_PATH['page'];
$this->sTitle = static::DEFAULT_TITLE;
$this->sDescription = static::DEFAULT_DESCRIPTION;
$this->sDataLoading = static::DEFAULT_DATA_LOADING;
Expand Down Expand Up @@ -572,6 +576,20 @@ public function HasDescription()
return ($this->sDescription !== null && $this->sDescription !== '');
}

/**
* @param $sTemplateName
* @param $sTemplatePath
*
* @return void
* @since 3.2.1
*/
public static function SetDefaultTemplatePath($sTemplateName, $sTemplatePath)
{
if(array_key_exists($sTemplateName, static::$DEFAULT_TEMPLATES_PATH)) {
static::$DEFAULT_TEMPLATES_PATH[$sTemplateName] = $sTemplatePath;
}
}

/**
* Load the brick's data from the xml passed as a ModuleDesignElement.
* This is used to set all the brick attributes at once.
Expand Down Expand Up @@ -665,14 +683,13 @@ public function LoadFromXml(DesignElement $oMDElement)
* @param $aPortalProperties
*
* @return void
* @throws \DOMFormatException
* @since 3.2.1
*/
public function LoadFromPortalProperties($aPortalProperties)
public static function LoadFromPortalProperties($aPortalProperties)
{
// Get the bricks templates
$aBricksTemplates = $aPortalProperties['templates']['bricks'];
$sClassFQCN = get_class($this);
$sClassFQCN = static::class;

// Get the current brick templates
$aCurrentBricksTemplates = array_key_exists($sClassFQCN, $aBricksTemplates) ? $aBricksTemplates[$sClassFQCN] : [];
Expand All @@ -681,16 +698,7 @@ public function LoadFromPortalProperties($aPortalProperties)
$sTemplateId = str_ireplace($sClassFQCN.':', '', $sTemplateKey);

// Call the set method for the template
$sSetTemplateMethodName = 'Set'.$sTemplateId.'TemplatePath';

if(method_exists($this, $sSetTemplateMethodName)) {
$this->{$sSetTemplateMethodName}($sTemplate);
}
else {
throw new DOMFormatException(
'Template "'.$sTemplateId.'" is not a valid template for brick ' . $sClassFQCN,
null, null);
}
static::SetDefaultTemplatePath($sTemplateId, $sTemplate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class AggregatePageBrick extends PortalBrick
const DEFAULT_DECORATION_CLASS_HOME = 'fas fa-tachometer-alt';
const DEFAULT_DECORATION_CLASS_NAVIGATION_MENU = 'fas fa-tachometer-alt fa-2x';
const DEFAULT_PAGE_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/aggregate-page/layout.html.twig';

protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
'tile' => self::DEFAULT_TILE_TEMPLATE_PATH,
];
// Overloaded variables
public static $sRouteName = 'p_aggregatepage_brick';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BrickCollection
* @throws \Exception
* @since 3.2.1 Added $aCombodoPortalInstanceConf parameter
*/
public function __construct(ModuleDesign $oModuleDesign, $aCombodoPortalInstanceConf)
public function __construct(ModuleDesign $oModuleDesign, array $aCombodoPortalInstanceConf)
{
$this->oModuleDesign = $oModuleDesign;
$this->aAllowedBricks = null;
Expand Down Expand Up @@ -202,11 +202,12 @@ private function GetRawBrickList()
{
if (class_exists($sBrickClass))
{
/** @var \Combodo\iTop\Portal\Brick\PortalBrick $oBrick */
$oBrick = new $sBrickClass();

// Load the portal properties that are common to all bricks of this type
$oBrick->LoadFromPortalProperties($this->aCombodoPortalInstanceConf['properties']);
$sBrickClass::LoadFromPortalProperties($this->aCombodoPortalInstanceConf['properties']);

/** @var \Combodo\iTop\Portal\Brick\PortalBrick $oBrick */
$oBrick = new $sBrickClass();

// Load the brick specific properties from its XML definition
$oBrick->LoadFromXml($oBrickNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class BrowseBrick extends PortalBrick
const DEFAULT_ACTION_OPENING_TARGET = self::ENUM_OPENING_TARGET_MODAL;
/** @var int DEFAULT_LIST_LENGTH */
const DEFAULT_LIST_LENGTH = 20;
protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
'tile' => self::DEFAULT_TILE_TEMPLATE_PATH,
'mode-list'=> 'itop-portal-base/portal/templates/bricks/browse/mode_list.html.twig',
'mode-mosaic'=> 'itop-portal-base/portal/templates/bricks/browse/mode_mosaic.html.twig',
'mode-tree'=> 'itop-portal-base/portal/templates/bricks/browse/mode_tree.html.twig',
];

// Overloaded variables
public static $sRouteName = 'p_browse_brick';
Expand Down Expand Up @@ -359,7 +366,7 @@ public function LoadFromXml(DesignElement $oMDElement)
}
else
{
$sTemplatePath = 'itop-portal-base/portal/templates/bricks/browse/mode_'.$sModeId.'.html.twig';
$sTemplatePath = static::$DEFAULT_TEMPLATES_PATH['mode-'.$sModeId];
}
$aModeData['template'] = $sTemplatePath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class CreateBrick extends PortalBrick
const DEFAULT_DECORATION_CLASS_HOME = 'fas fa-plus';
const DEFAULT_DECORATION_CLASS_NAVIGATION_MENU = 'fas fa-plus fa-2x';
const DEFAULT_PAGE_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/create/modal.html.twig';

protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
'tile' => self::DEFAULT_TILE_TEMPLATE_PATH,
];
/** @var string DEFAULT_CLASS */
const DEFAULT_CLASS = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class FilterBrick extends PortalBrick
const DEFAULT_TILE_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/filter/tile.html.twig';
const DEFAULT_DECORATION_CLASS_HOME = 'fas fa-search';
const DEFAULT_DECORATION_CLASS_NAVIGATION_MENU = 'fas fa-search fa-2x';

protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
'tile' => self::DEFAULT_TILE_TEMPLATE_PATH,
];
/** @var string DEFAULT_TARGET_BRICK_CLASS */
const DEFAULT_TARGET_BRICK_CLASS = 'Combodo\\iTop\\Portal\\Brick\\BrowseBrick';
/** @var string DEFAULT_SEARCH_PLACEHOLDER_VALUE */
Expand Down
140 changes: 94 additions & 46 deletions datamodels/2.x/itop-portal-base/portal/src/Brick/ManageBrick.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class ManageBrick extends PortalBrick
const DEFAULT_DATA_LOADING = self::ENUM_DATA_LOADING_LAZY;
const DEFAULT_TILE_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/tile-default.html.twig';
const DEFAULT_TILE_CONTROLLER_ACTION = 'Combodo\\iTop\\Portal\\Controller\\ManageBrickController::TileAction';
const DEFAULT_LAYOUT_CHART_TEMPLATE_PATH = self::ENUM_PAGE_TEMPLATE_PATH_CHART;
const DEFAULT_LAYOUT_TABLE_TEMPLATE_PATH = self::ENUM_PAGE_TEMPLATE_PATH_TABLE;
const DEFAULT_LAYOUT_BADGE_TEMPLATE_PATH = self::ENUM_PAGE_TEMPLATE_PATH_TABLE;
const DEFAULT_TILE_CHART_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/tile-chart.html.twig';
const DEFAULT_TILE_TOP_LIST_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/tile-top-list.html.twig';
const DEFAULT_TILE_BADGE_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/tile-badge.html.twig';
const DEFAULT_TILE_DEFAULT_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/tile-default.html.twig';
const DEFAULT_POPUP_EXPORT_EXCEL_TEMPLATE_PATH = 'itop-portal-base/portal/templates/bricks/manage/popup-export-excel.html.twig';

/** @var string DEFAULT_OQL */
const DEFAULT_OQL = '';
Expand All @@ -83,6 +91,19 @@ class ManageBrick extends PortalBrick
/** @var bool DEFAULT_GROUP_SHOW_OTHERS */
const DEFAULT_GROUP_SHOW_OTHERS = true;

protected static $DEFAULT_TEMPLATES_PATH = [
'page' => self::DEFAULT_PAGE_TEMPLATE_PATH,
'tile' => self::DEFAULT_TILE_TEMPLATE_PATH,
'layout-chart' => self::DEFAULT_LAYOUT_CHART_TEMPLATE_PATH,
'layout-table' => self::DEFAULT_LAYOUT_TABLE_TEMPLATE_PATH,
'layout-badge' => self::DEFAULT_LAYOUT_BADGE_TEMPLATE_PATH,
'tile-chart' => self::DEFAULT_TILE_CHART_TEMPLATE_PATH,
'tile-top-list' => self::DEFAULT_TILE_TOP_LIST_TEMPLATE_PATH,
'tile-badge' => self::DEFAULT_TILE_BADGE_TEMPLATE_PATH,
'tile-default' => self::DEFAULT_TILE_DEFAULT_TEMPLATE_PATH,
'popup-export-excel' => self::DEFAULT_POPUP_EXPORT_EXCEL_TEMPLATE_PATH,
];

/** @var array $aDisplayModes */
static $aDisplayModes = array(
self::ENUM_DISPLAY_MODE_LIST,
Expand All @@ -97,44 +118,13 @@ class ManageBrick extends PortalBrick
self::ENUM_TILE_MODE_BAR,
self::ENUM_TILE_MODE_TOP,
);
/** Initialized in its getter as we need DEFAULT_TEMPLATE static values to be accessible */
/** @var array $aDefaultPresentationData */
private static $aDefaultPresentationData = [];

/** Specific data for the current brick, including brick definition overloads */
/** @var array $aPresentationData */
public static $aPresentationData = array(
self::ENUM_TILE_MODE_BADGE => array(
'decorationCssClass' => 'fas fa-id-card fa-2x',
'tileTemplate' => 'itop-portal-base/portal/templates/bricks/manage/tile-badge.html.twig',
'layoutTemplate' => self::ENUM_PAGE_TEMPLATE_PATH_TABLE,
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
self::ENUM_TILE_MODE_TOP => array(
'decorationCssClass' => 'fas fa-signal fa-rotate-270 fa-2x',
'tileTemplate' => 'itop-portal-base/portal/templates/bricks/manage/tile-top-list.html.twig',
'layoutTemplate' => self::ENUM_PAGE_TEMPLATE_PATH_TABLE,
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
self::ENUM_TILE_MODE_PIE => array(
'decorationCssClass' => 'fas fa-chart-pie fa-2x',
'tileTemplate' => 'itop-portal-base/portal/templates/bricks/manage/tile-chart.html.twig',
'layoutTemplate' => self::ENUM_PAGE_TEMPLATE_PATH_CHART,
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_PIE,
'need_details' => false,
),
self::ENUM_TILE_MODE_BAR => array(
'decorationCssClass' => 'fas fa-chart-bar fa-2x',
'tileTemplate' => 'itop-portal-base/portal/templates/bricks/manage/tile-chart.html.twig',
'layoutTemplate' => self::ENUM_PAGE_TEMPLATE_PATH_CHART,
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_BAR,
'need_details' => false,
),
self::ENUM_TILE_MODE_TEXT => array(
'decorationCssClass' => 'fas fa-pen-square fa-2x',
'tileTemplate' => self::DEFAULT_TILE_TEMPLATE_PATH,
'layoutTemplate' => self::ENUM_PAGE_TEMPLATE_PATH_TABLE,
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
);
public $aPresentationData = [];

// Overloaded variables
public static $sRouteName = 'p_manage_brick';
Expand Down Expand Up @@ -163,6 +153,8 @@ class ManageBrick extends PortalBrick
protected $bGroupShowOthers;
/** @var int $iDefaultListLength */
protected $iDefaultListLength;
/** @var string $sPopupExportExcelTemplatePath */
protected $sPopupExportExcelTemplatePath;

/**
* Returns true if the $sDisplayMode need objects details for rendering.
Expand All @@ -174,7 +166,7 @@ class ManageBrick extends PortalBrick
static public function AreDetailsNeededForDisplayMode($sDisplayMode)
{
$bNeedDetails = false;
foreach (static::$aPresentationData as $aData)
foreach (static::GetDefaultPresentationData() as $aData)
{
if ($aData['layoutDisplayMode'] === $sDisplayMode)
{
Expand All @@ -195,8 +187,8 @@ static public function AreDetailsNeededForDisplayMode($sDisplayMode)
*/
static public function GetPageTemplateFromDisplayMode($sDisplayMode)
{
$sTemplate = static::DEFAULT_PAGE_TEMPLATE_PATH;
foreach (static::$aPresentationData as $aData)
$sTemplate = static::$DEFAULT_TEMPLATES_PATH['page'];
foreach (static::GetDefaultPresentationData() as $aData)
{
if ($aData['layoutDisplayMode'] === $sDisplayMode)
{
Expand Down Expand Up @@ -227,6 +219,7 @@ public function __construct()
$this->iGroupLimit = static::DEFAULT_GROUP_LIMIT;
$this->bGroupShowOthers = static::DEFAULT_GROUP_SHOW_OTHERS;
$this->iDefaultListLength = static::DEFAULT_LIST_LENGTH;
$this->sPopupExportExcelTemplatePath = static::$DEFAULT_TEMPLATES_PATH['popup-export-excel'];

// This is hardcoded for now, we might allow area grouping on another attribute in the future
$this->AddGrouping('areas', array('attribute' => 'finalclass'));
Expand Down Expand Up @@ -328,7 +321,7 @@ public function GetTileMode()

public function GetDecorationCssClass()
{
return static::$aPresentationData[$this->sTileMode]['decorationCssClass'];
return static::GetDefaultPresentationData()[$this->sTileMode]['decorationCssClass'];
}
/**
* Sets the tile mode (display)
Expand All @@ -344,19 +337,65 @@ public function SetTileMode($sTileMode)
return $this;
}

public static function GetDefaultPresentationData()
{
/** If the table isn't initialized yet, do it now */
if (count(static::$aDefaultPresentationData) === 0) {
static::$aDefaultPresentationData = array(
self::ENUM_TILE_MODE_BADGE => array(
'decorationCssClass' => 'fas fa-id-card fa-2x',
'tileTemplate' => static::$DEFAULT_TEMPLATES_PATH['tile-badge'],
'layoutTemplate' => static::$DEFAULT_TEMPLATES_PATH['layout-table'],
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
self::ENUM_TILE_MODE_TOP => array(
'decorationCssClass' => 'fas fa-signal fa-rotate-270 fa-2x',
'tileTemplate' => static::$DEFAULT_TEMPLATES_PATH['tile-top-list'],
'layoutTemplate' => static::$DEFAULT_TEMPLATES_PATH['layout-table'],
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
self::ENUM_TILE_MODE_PIE => array(
'decorationCssClass' => 'fas fa-chart-pie fa-2x',
'tileTemplate' => static::$DEFAULT_TEMPLATES_PATH['tile-chart'],
'layoutTemplate' => static::$DEFAULT_TEMPLATES_PATH['layout-chart'],
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_PIE,
'need_details' => false,
),
self::ENUM_TILE_MODE_BAR => array(
'decorationCssClass' => 'fas fa-chart-bar fa-2x',
'tileTemplate' => static::$DEFAULT_TEMPLATES_PATH['tile-chart'],
'layoutTemplate' => static::$DEFAULT_TEMPLATES_PATH['layout-chart'],
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_BAR,
'need_details' => false,
),
self::ENUM_TILE_MODE_TEXT => array(
'decorationCssClass' => 'fas fa-pen-square fa-2x',
'tileTemplate' => static::$DEFAULT_TEMPLATES_PATH['tile-default'],
'layoutTemplate' => static::$DEFAULT_TEMPLATES_PATH['layout-table'],
'layoutDisplayMode' => self::ENUM_DISPLAY_MODE_LIST,
'need_details' => true,
),
);
}

return static::$aDefaultPresentationData;
}

/**
* @param string $sTileMode
*
* @return string[] parameters for specified type, default parameters if type is invalid
*/
public function GetPresentationDataForTileMode($sTileMode)
{
if (isset(static::$aPresentationData[$sTileMode]))
if (isset(static::GetDefaultPresentationData()[$sTileMode]))
{
return static::$aPresentationData[$sTileMode];
return static::GetDefaultPresentationData()[$sTileMode];
}

return static::$aPresentationData[static::DEFAULT_TILE_MODE];
return static::GetDefaultPresentationData()[static::DEFAULT_TILE_MODE];
}

/**
Expand Down Expand Up @@ -467,6 +506,15 @@ public function SetDefaultListLength($iDefaultListLength) {
return $this;
}

public function GetPopupExportExcelTemplatePath() {
return $this->sPopupExportExcelTemplatePath;
}

public function SetPopupExportExcelTemplatePath($sPopupExportExcelTemplatePath) {
$this->sPopupExportExcelTemplatePath = $sPopupExportExcelTemplatePath;
return $this;
}

/**
* Adds a grouping.
*
Expand Down Expand Up @@ -965,10 +1013,10 @@ public function LoadFromXml(DesignElement $oMDElement)

// Checking the navigation icon
$sDecorationClassNavigationMenu = $this->GetDecorationClassNavigationMenu();
if (empty($sDecorationClassNavigationMenu) && isset(static::$aPresentationData[$this->sTileMode]))
if (empty($sDecorationClassNavigationMenu) && isset(static::GetDefaultPresentationData()[$this->sTileMode]))
{
/** @var string $sDecorationClassNavigationMenu */
$sDecorationClassNavigationMenu = static::$aPresentationData[$this->sTileMode]['decorationCssClass'];
$sDecorationClassNavigationMenu = static::GetDefaultPresentationData()[$this->sTileMode]['decorationCssClass'];
if (!empty($sDecorationClassNavigationMenu))
{
$this->SetDecorationClassNavigationMenu($sDecorationClassNavigationMenu);
Expand Down
Loading

0 comments on commit 2418921

Please sign in to comment.