mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-25 11:38:44 +02:00
N°3123 - Refactor Directories
This commit is contained in:
138
sources/application/UI/Base/Component/DataTable/DataTable.php
Normal file
138
sources/application/UI/Base/Component/DataTable/DataTable.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable;
|
||||
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
use DataTableConfig;
|
||||
|
||||
/**
|
||||
* Class DataTable
|
||||
*
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\DataTable
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class DataTableBlock extends UIContentBlock
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatable';
|
||||
public const HTML_TEMPLATE_REL_PATH = 'base/components/datatable/layout';
|
||||
public const JS_TEMPLATE_REL_PATH = 'base/components/datatable/layout';
|
||||
|
||||
protected $aOptions;//list of specific options for display datatable
|
||||
protected $sAjaxUrl;
|
||||
protected $sAjaxData;
|
||||
protected $aDisplayColumns;
|
||||
protected $aResultColumns;
|
||||
|
||||
/**
|
||||
* Panel constructor.
|
||||
*
|
||||
*/
|
||||
public function __construct(?string $sId = null)
|
||||
{
|
||||
parent::__construct($sId);
|
||||
$this->AddDeferredBlock(new DataTableConfig($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetAjaxUrl(): string
|
||||
{
|
||||
return $this->sAjaxUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sAjaxUrl
|
||||
*/
|
||||
public function SetAjaxUrl(string $sAjaxUrl): void
|
||||
{
|
||||
$this->sAjaxUrl = $sAjaxUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetAjaxData(): string
|
||||
{
|
||||
return $this->sAjaxData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sAjaxData
|
||||
*/
|
||||
public function SetAjaxData(string $sAjaxData): void
|
||||
{
|
||||
$this->sAjaxData = $sAjaxData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetDisplayColumns(): array
|
||||
{
|
||||
return $this->aDisplayColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $aColumns
|
||||
*/
|
||||
public function SetDisplayColumns($aColumns): void
|
||||
{
|
||||
$this->aDisplayColumns = $aColumns;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetResultColumns(): array
|
||||
{
|
||||
return $this->aResultColumns;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetResultColumnsAsJson(): string
|
||||
{
|
||||
return json_encode($this->aResultColumns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $aColumns
|
||||
*/
|
||||
public function SetResultColumns($aColumns): void
|
||||
{
|
||||
$this->aResultColumns = $aColumns;
|
||||
}
|
||||
|
||||
public function GetOption(string $sOption)
|
||||
{
|
||||
if (isset($this->aOptions[$sOption])) {
|
||||
return $this->aOptions[$sOption];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetOptions(): array
|
||||
{
|
||||
return $this->aOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $aOptions
|
||||
*/
|
||||
public function SetOptions($aOptions): void
|
||||
{
|
||||
$this->aOptions = $aOptions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableBlock;
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
class DataTableConfig extends UIContentBlock
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatableconfig';
|
||||
public const HTML_TEMPLATE_REL_PATH = 'base/components/datatable/config/layout';
|
||||
|
||||
/** @var DataTableBlock */
|
||||
private $oDataTable;
|
||||
|
||||
public function __construct(DataTableBlock $oDataTable, ?string $sId = null)
|
||||
{
|
||||
parent::__construct($sId);
|
||||
$this->oDataTable = $oDataTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableBlock
|
||||
*/
|
||||
private function GetDataTable(): DataTableBlock
|
||||
{
|
||||
return $this->oDataTable;
|
||||
}
|
||||
|
||||
public function GetOption(string $sOption)
|
||||
{
|
||||
return $this->GetDataTable()->GetOption($sOption);
|
||||
}
|
||||
|
||||
public function GetTableId()
|
||||
{
|
||||
return $this->GetDataTable()->GetId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,725 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable;
|
||||
|
||||
use ApplicationException;
|
||||
use appUserPreferences;
|
||||
use AttributeLinkedSet;
|
||||
use cmdbAbstractObject;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTable\FormTable;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\StaticTable;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Panel\PanelFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Title\TitleFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
use DBObjectSet;
|
||||
use Dict;
|
||||
use MenuBlock;
|
||||
use MetaModel;
|
||||
use UserRights;
|
||||
use utils;
|
||||
use WebPage;
|
||||
|
||||
/**
|
||||
* Class DataTableFactory
|
||||
*
|
||||
* @internal
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\DataTable
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class DataTableFactory
|
||||
{
|
||||
/**
|
||||
* @param \WebPage $oPage
|
||||
* @param string $sListId
|
||||
* @param \DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\Panel\Panel
|
||||
* @throws \ApplicationException
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MissingQueryArgument
|
||||
* @throws \MySQLException
|
||||
* @throws \MySQLHasGoneAwayException
|
||||
* @throws \OQLException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public static function MakeForResult(WebPage $oPage, string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oPanel = PanelFactory::MakeForClass($oSet->GetClass(), "Result")->AddCSSClasses('ibo-datatable-panel');
|
||||
$oDataTable = DataTableFactory::MakeForRendering($sListId, $oSet, $aExtraParams);
|
||||
$oPanel->AddMainBlock($oDataTable);
|
||||
|
||||
$oMenuBlock = new MenuBlock($oSet->GetFilter(), 'list');
|
||||
$oBlock = $oMenuBlock->GetRenderContent($oPage, $aExtraParams, $sListId);
|
||||
$oBlockMenu = new UIContentBlock();
|
||||
$oBlockMenu->AddSubBlock($oBlock);
|
||||
$oPanel->AddToolbarBlock($oBlockMenu);
|
||||
|
||||
return $oPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WebPage $oPage
|
||||
* @param string $sListId
|
||||
* @param DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\Panel\Panel
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MissingQueryArgument
|
||||
* @throws \MySQLException
|
||||
* @throws \MySQLHasGoneAwayException
|
||||
* @throws \OQLException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public static function MakeForObject(WebPage $oPage, string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oPanel = PanelFactory::MakeForClass($oSet->GetClass(), "Result");
|
||||
$oDataTable = DataTableFactory::MakeForRenderingObject($sListId, $oSet, $aExtraParams);
|
||||
$oPanel->AddMainBlock($oDataTable);
|
||||
|
||||
$oMenuBlock = new MenuBlock($oSet->GetFilter(), 'listInObject');
|
||||
$oBlock = $oMenuBlock->GetRenderContent($oPage, $aExtraParams, $sListId);
|
||||
$oBlockMenu = new UIContentBlock();
|
||||
$oBlockMenu->AddSubBlock($oBlock);
|
||||
$oPanel->AddToolbarBlock($oBlockMenu);
|
||||
|
||||
return $oPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a basis Panel component
|
||||
*
|
||||
* @param string $sListId
|
||||
* @param \DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return DataTableBlock
|
||||
* @throws \ApplicationException
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
public static function MakeForRendering(string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oDataTable = new DataTableBlock('datatable_'.$sListId);
|
||||
///////////////////////////////////////////////////
|
||||
/*TODO 3.0.0 PrintableVersion
|
||||
if ($oPage->IsPrintableVersion() || $oPage->is_pdf())
|
||||
{
|
||||
return self::GetDisplaySetForPrinting($oPage, $oSet, $aExtraParams);
|
||||
}
|
||||
*/
|
||||
|
||||
// Initialize and check the parameters
|
||||
$bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
|
||||
$sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
|
||||
$iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
|
||||
$sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
|
||||
if (!empty($sLinkageAttribute)) {
|
||||
if ($iLinkedObjectId == 0) {
|
||||
// if 'links' mode is requested the id of the object to link to must be specified
|
||||
throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
|
||||
}
|
||||
if ($sTargetAttr == '') {
|
||||
// if 'links' mode is requested the d of the object to link to must be specified
|
||||
throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
|
||||
}
|
||||
}
|
||||
$bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
|
||||
$bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
|
||||
|
||||
$aExtraFieldsRaw = isset($aExtraParams['extra_fields']) ? explode(',',
|
||||
trim($aExtraParams['extra_fields'])) : array();
|
||||
$aExtraFields = array();
|
||||
foreach ($aExtraFieldsRaw as $sFieldName) {
|
||||
// Ignore attributes not of the main queried class
|
||||
if (preg_match('/^(.*)\.(.*)$/', $sFieldName, $aMatches)) {
|
||||
$sClassAlias = $aMatches[1];
|
||||
$sAttCode = $aMatches[2];
|
||||
if ($sClassAlias == $oSet->GetFilter()->GetClassAlias()) {//$oSet->GetFilter()->GetSelectedClasses()
|
||||
$aExtraFields[] = $sAttCode;
|
||||
}
|
||||
} else {
|
||||
$aExtraFields[] = $sFieldName;
|
||||
}
|
||||
}
|
||||
$sClassName = $oSet->GetFilter()->GetClass();
|
||||
$sZListName = isset($aExtraParams['zlist']) ? ($aExtraParams['zlist']) : 'list';
|
||||
if ($sZListName !== false) {
|
||||
$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, $sZListName));
|
||||
$aList = array_merge($aList, $aExtraFields);
|
||||
} else {
|
||||
$aList = $aExtraFields;
|
||||
}
|
||||
|
||||
// Filter the list to removed linked set since we are not able to display them here
|
||||
foreach ($aList as $index => $sAttCode) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof AttributeLinkedSet) {
|
||||
// Removed from the display list
|
||||
unset($aList[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($sLinkageAttribute)) {
|
||||
// The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
|
||||
// and other objects...
|
||||
// The display will then group all the attributes related to the link itself:
|
||||
// | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
|
||||
$aDisplayList = array();
|
||||
$aAttDefs = MetaModel::ListAttributeDefs($sClassName);
|
||||
assert(isset($aAttDefs[$sLinkageAttribute]));
|
||||
$oAttDef = $aAttDefs[$sLinkageAttribute];
|
||||
assert($oAttDef->IsExternalKey());
|
||||
// First display all the attributes specific to the link record
|
||||
foreach ($aList as $sLinkAttCode) {
|
||||
$oLinkAttDef = $aAttDefs[$sLinkAttCode];
|
||||
if ((!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField())) {
|
||||
$aDisplayList[] = $sLinkAttCode;
|
||||
}
|
||||
}
|
||||
// Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
|
||||
foreach ($aList as $sLinkAttCode) {
|
||||
$oLinkAttDef = $aAttDefs[$sLinkAttCode];
|
||||
if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
|
||||
|| ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode() != $sLinkageAttribute))) {
|
||||
$aDisplayList[] = $sLinkAttCode;
|
||||
}
|
||||
}
|
||||
// First display all the attributes specific to the link
|
||||
// Then display all the attributes linked to the other end of the relationship
|
||||
$aList = $aDisplayList;
|
||||
}
|
||||
|
||||
$sSelectMode = '';
|
||||
if ($bSelectMode) {
|
||||
$sSelectMode = $bSingleSelectMode ? 'single' : 'multiple';
|
||||
}
|
||||
|
||||
$sClassAlias = $oSet->GetClassAlias();
|
||||
$bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
|
||||
|
||||
$sTableId = isset($aExtraParams['table_id']) ? $aExtraParams['table_id'] : null;
|
||||
$aClassAliases = array($sClassAlias => $sClassName);
|
||||
$oDefaultSettings = DataTableSettings::GetDataModelSettings($aClassAliases, $bViewLink, array($sClassAlias => $aList));
|
||||
|
||||
if ($bDisplayLimit) {
|
||||
$iDefaultPageSize = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit());
|
||||
$oDefaultSettings->iDefaultPageSize = $iDefaultPageSize;
|
||||
} else {
|
||||
$oDefaultSettings->iDefaultPageSize = 0;
|
||||
}
|
||||
$oDefaultSettings->aSortOrder = MetaModel::GetOrderByDefault($sClassName);
|
||||
|
||||
$bUseCustomSettings = false;
|
||||
// Identified tables can have their own specific settings
|
||||
$oCustomSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId);
|
||||
|
||||
if ($oCustomSettings != null) {
|
||||
// Custom settings overload the default ones
|
||||
$bUseCustomSettings = true;
|
||||
if ($oDefaultSettings->iDefaultPageSize == 0) {
|
||||
$oCustomSettings->iDefaultPageSize = 0;
|
||||
}
|
||||
} else {
|
||||
$oCustomSettings = $oDefaultSettings;
|
||||
}
|
||||
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$oSet->SetLimit($oCustomSettings->iDefaultPageSize);
|
||||
}
|
||||
|
||||
// Load only the requested columns
|
||||
$aColumnsToLoad = array();
|
||||
foreach ($oCustomSettings->aColumns as $sAlias => $aColumnsInfo) {
|
||||
foreach ($aColumnsInfo as $sAttCode => $aData) {
|
||||
if ($sAttCode != '_key_') {
|
||||
if ($aData['checked']) {
|
||||
$aColumnsToLoad[$sAlias][] = $sAttCode;
|
||||
} else {
|
||||
// See if this column is a must to load
|
||||
$sClass = $aClassAliases[$sAlias];
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
|
||||
if ($oAttDef->alwaysLoadInTables()) {
|
||||
$aColumnsToLoad[$sAlias][] = $sAttCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->OptimizeColumnLoad($aColumnsToLoad);
|
||||
$aSortOrder=[];
|
||||
$aSortDatable=[];
|
||||
$aColumnDefinition = [];
|
||||
$iIndexColumn=0;
|
||||
if($sSelectMode!="") {
|
||||
$iIndexColumn++;
|
||||
}
|
||||
foreach ($aClassAliases as $sClassAlias => $sClassName) {
|
||||
foreach ($oCustomSettings->aColumns[$sClassAlias] as $sAttCode => $aData) {
|
||||
if ($aData['sort'] != 'none') {
|
||||
$sCode = ($aData['code'] == '_key_') ? 'friendlyname' : $aData['code'];
|
||||
$aSortOrder[$sAlias.$sCode] = ($aData['sort'] == 'asc'); // true for ascending, false for descending
|
||||
$aSortDatable=[$iIndexColumn,$aData['sort']];
|
||||
}
|
||||
if ($aData['checked']) {
|
||||
if ($sAttCode == '_key_') {
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $aData['label'],
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => $aData['alias'],
|
||||
"render" => "return '<a class=\'object-ref-link\' href= \'UI.php?operation=details&class=".$sClassName."&id='+data+'\'>'+row['".$sClassAlias."/friendlyname']+'</a>' ;",
|
||||
];
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
$sAttDefClass = get_class($oAttDef);
|
||||
$sAttLabel = MetaModel::GetLabel($sClassName, $sAttCode);
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $oAttDef->GetOrderByHint(),
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => $sAttDefClass,
|
||||
'attribute_label' => $sAttLabel,
|
||||
"render" => $oAttDef->GetRenderForDataTable($sClassAlias),
|
||||
];
|
||||
}
|
||||
$iIndexColumn++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->SetOrderBy($aSortOrder);
|
||||
|
||||
$aOptions = [];
|
||||
if ($oDefaultSettings != null) {
|
||||
$aOptions['oDefaultSettings'] = json_encode(array('iDefaultPageSize' => $oDefaultSettings->iDefaultPageSize, 'oColumns' => $oDefaultSettings->aColumns));
|
||||
}
|
||||
$aOptions['sort'] = $aSortDatable;
|
||||
if ($sSelectMode == 'multiple') {
|
||||
$aOptions['select_mode'] = "multiple";
|
||||
} else {
|
||||
if ($sSelectMode == 'single') {
|
||||
$aOptions['select_mode'] = "single";
|
||||
}
|
||||
}
|
||||
|
||||
$aOptions['iPageSize'] = 10;
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$aOptions['iPageSize'] = $oCustomSettings->iDefaultPageSize;
|
||||
}
|
||||
|
||||
$aOptions['sTableId'] = $sTableId;
|
||||
$aOptions['bUseCustomSettings'] = $bUseCustomSettings;
|
||||
$aOptions['bViewLink'] = $bViewLink;
|
||||
$aOptions['sListId'] = $sListId;
|
||||
$aOptions['oClassAliases'] = json_encode($aClassAliases);
|
||||
|
||||
$oDataTable->SetOptions($aOptions);
|
||||
$oDataTable->SetAjaxUrl(utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php");
|
||||
$oDataTable->SetAjaxData(json_encode([
|
||||
"operation" => 'search',
|
||||
"filter" => $oSet->GetFilter()->serialize(),
|
||||
"columns" => $oCustomSettings->aColumns,
|
||||
"extra_params" => $aExtraParams,
|
||||
"class_aliases" => $aClassAliases,
|
||||
"select_mode" => $sSelectMode,
|
||||
]));
|
||||
$oDataTable->SetDisplayColumns($aColumnDefinition);
|
||||
$oDataTable->SetResultColumns($oCustomSettings->aColumns);
|
||||
|
||||
return $oDataTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sListId
|
||||
* @param DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableBlock
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
public static function MakeForRenderingObject(string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oDataTable = new DataTableBlock('datatable_'.$sListId);
|
||||
$aList = array();
|
||||
|
||||
// Initialize and check the parameters
|
||||
$bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
|
||||
// Check if there is a list of aliases to limit the display to...
|
||||
$aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',',
|
||||
$aExtraParams['display_aliases']) : array();
|
||||
$sZListName = isset($aExtraParams['zlist']) ? ($aExtraParams['zlist']) : 'list';
|
||||
|
||||
$aExtraFieldsRaw = isset($aExtraParams['extra_fields']) ? explode(',',
|
||||
trim($aExtraParams['extra_fields'])) : array();
|
||||
$aExtraFields = array();
|
||||
$sAttCode = '';
|
||||
foreach ($aExtraFieldsRaw as $sFieldName) {
|
||||
// Ignore attributes not of the main queried class
|
||||
if (preg_match('/^(.*)\.(.*)$/', $sFieldName, $aMatches)) {
|
||||
$sClassAlias = $aMatches[1];
|
||||
$sAttCode = $aMatches[2];
|
||||
if (array_key_exists($sClassAlias, $oSet->GetSelectedClasses())) {
|
||||
$aExtraFields[$sClassAlias][] = $sAttCode;
|
||||
}
|
||||
} else {
|
||||
$aExtraFields['*'] = $sAttCode;
|
||||
}
|
||||
}
|
||||
|
||||
$aClassAliases = $oSet->GetFilter()->GetSelectedClasses();
|
||||
$aAuthorizedClasses = array();
|
||||
foreach ($aClassAliases as $sAlias => $sClassName) {
|
||||
if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) != UR_ALLOWED_NO) &&
|
||||
((count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases)))) {
|
||||
$aAuthorizedClasses[$sAlias] = $sClassName;
|
||||
}
|
||||
}
|
||||
foreach ($aAuthorizedClasses as $sAlias => $sClassName) {
|
||||
if (array_key_exists($sAlias, $aExtraFields)) {
|
||||
$aList[$sAlias] = $aExtraFields[$sAlias];
|
||||
} else {
|
||||
$aList[$sAlias] = array();
|
||||
}
|
||||
if ($sZListName !== false) {
|
||||
$aDefaultList = MetaModel::FlattenZList(MetaModel::GetZListItems($sClassName, $sZListName));
|
||||
$aList[$sAlias] = array_merge($aDefaultList, $aList[$sAlias]);
|
||||
}
|
||||
|
||||
// Filter the list to removed linked set since we are not able to display them here
|
||||
foreach ($aList[$sAlias] as $index => $sAttCode) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof AttributeLinkedSet) {
|
||||
// Removed from the display list
|
||||
unset($aList[$sAlias][$index]);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($aList[$sAlias])) {
|
||||
unset($aList[$sAlias], $aAuthorizedClasses[$sAlias]);
|
||||
}
|
||||
}
|
||||
|
||||
$sSelectMode = 'none';
|
||||
|
||||
$oDefaultSettings = DataTableSettings::GetDataModelSettings($aAuthorizedClasses, $bViewLink, $aList);
|
||||
|
||||
$bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
|
||||
if ($bDisplayLimit) {
|
||||
$iDefaultPageSize = appUserPreferences::GetPref('default_page_size',
|
||||
MetaModel::GetConfig()->GetMinDisplayLimit());
|
||||
$oDefaultSettings->iDefaultPageSize = $iDefaultPageSize;
|
||||
}
|
||||
|
||||
$sTableId = isset($aExtraParams['table_id']) ? $aExtraParams['table_id'] : null;
|
||||
$oDefaultSettings->aSortOrder = MetaModel::GetOrderByDefault($sClassName);
|
||||
|
||||
$bUseCustomSettings = false;
|
||||
// Identified tables can have their own specific settings
|
||||
$oCustomSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId);
|
||||
|
||||
if ($oCustomSettings != null) {
|
||||
// Custom settings overload the default ones
|
||||
$bUseCustomSettings = true;
|
||||
if ($oDefaultSettings->iDefaultPageSize == 0) {
|
||||
$oCustomSettings->iDefaultPageSize = 0;
|
||||
}
|
||||
} else {
|
||||
$oCustomSettings = $oDefaultSettings;
|
||||
}
|
||||
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$oSet->SetLimit($oCustomSettings->iDefaultPageSize);
|
||||
}
|
||||
|
||||
// Load only the requested columns
|
||||
$aColumnsToLoad = array();
|
||||
foreach ($oCustomSettings->aColumns as $sAlias => $aColumnsInfo) {
|
||||
foreach ($aColumnsInfo as $sAttCode => $aData) {
|
||||
if ($sAttCode != '_key_') {
|
||||
if ($aData['checked']) {
|
||||
$aColumnsToLoad[$sAlias][] = $sAttCode;
|
||||
} else {
|
||||
// See if this column is a must to load
|
||||
$sClass = $aClassAliases[$sAlias];
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
|
||||
if ($oAttDef->alwaysLoadInTables()) {
|
||||
$aColumnsToLoad[$sAlias][] = $sAttCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->OptimizeColumnLoad($aColumnsToLoad);
|
||||
|
||||
$aColumnDefinition = [];
|
||||
$aSortOrder = [];
|
||||
$iIndexColumn = 0;
|
||||
if ($sSelectMode != "") {
|
||||
$iIndexColumn++;
|
||||
}
|
||||
$aSortDatable = [];
|
||||
foreach ($aAuthorizedClasses as $sClassAlias => $sClassName) {
|
||||
foreach ($oCustomSettings->aColumns[$sClassAlias] as $sAttCode => $aData) {
|
||||
if ($aData['sort'] != 'none') {
|
||||
$sCode = ($aData['code'] == '_key_') ? 'friendlyname' : $aData['code'];
|
||||
$aSortOrder[$sAlias.$sCode] = ($aData['sort'] == 'asc'); // true for ascending, false for descending
|
||||
$aSortDatable = [$iIndexColumn, $aData['sort']];
|
||||
}
|
||||
if ($aData['checked']) {
|
||||
if ($sAttCode == '_key_') {
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $aData['label'],
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => $aData['alias'],
|
||||
"render" => "return '<a class=\'object-ref-link\' href= \'UI.php?operation=details&class=".$sClassName."&id='+data+'\'>'+row['".$sClassAlias."/friendlyname']+'</a>' ;",
|
||||
];
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
$sAttDefClass = get_class($oAttDef);
|
||||
$sAttLabel = MetaModel::GetLabel($sClassName, $sAttCode);
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $oAttDef->GetOrderByHint(),
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => $sAttDefClass,
|
||||
'attribute_label' => $sAttLabel,
|
||||
"render" => $oAttDef->GetRenderForDataTable($sClassAlias),
|
||||
];
|
||||
}
|
||||
$iIndexColumn++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->SetOrderBy($oCustomSettings->GetSortOrder());
|
||||
|
||||
$aOptions = [];
|
||||
if ($oDefaultSettings != null) {
|
||||
$aOptions['oDefaultSettings'] = json_encode(array('iDefaultPageSize' => $oDefaultSettings->iDefaultPageSize, 'oColumns' => $oDefaultSettings->aColumns));
|
||||
}
|
||||
|
||||
if ($sSelectMode == 'multiple') {
|
||||
$aOptions['select_mode'] = "multiple";
|
||||
} else {
|
||||
if ($sSelectMode == 'single') {
|
||||
$aOptions['select_mode'] = "single";
|
||||
}
|
||||
}
|
||||
|
||||
$aOptions['sort'] = $aSortDatable;
|
||||
|
||||
$aOptions['iPageSize'] = 10;
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$aOptions['iPageSize'] = $oCustomSettings->iDefaultPageSize;
|
||||
}
|
||||
|
||||
$aOptions['sTableId'] = $sTableId;
|
||||
$aOptions['bUseCustomSettings'] = $bUseCustomSettings;
|
||||
$aOptions['bViewLink'] = $bViewLink;
|
||||
$aOptions['oClassAliases'] = json_encode($aClassAliases);
|
||||
|
||||
$oDataTable->SetOptions($aOptions);
|
||||
$oDataTable->SetAjaxUrl("ajax.render.php");
|
||||
$oDataTable->SetAjaxData(json_encode([
|
||||
"operation" => 'search',
|
||||
"filter" => $oSet->GetFilter()->serialize(),
|
||||
"columns" => $oCustomSettings->aColumns,
|
||||
"extra_params" => $aExtraParams,
|
||||
"class_aliases" => $aClassAliases,
|
||||
"select_mode" => $sSelectMode,
|
||||
]));
|
||||
$oDataTable->SetDisplayColumns($aColumnDefinition);
|
||||
$oDataTable->SetResultColumns($oCustomSettings->aColumns);
|
||||
|
||||
return $oDataTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aColumns
|
||||
* @param string $sSelectMode
|
||||
* @param string $sFilter
|
||||
* @param int $iLength
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function GetOptionsForRendering(array $aColumns, string $sSelectMode, string $sFilter, int $iLength, array $aClassAliases, array $aExtraParams)
|
||||
{
|
||||
$aOptions = [];
|
||||
$sTableId=$aExtraParams["table_id"];
|
||||
$aColumnsDefinitions = [];
|
||||
$aColumnDefinition = [];
|
||||
|
||||
if ($sSelectMode!=""){
|
||||
$aColumnDefinition["width"] = "auto";
|
||||
$aColumnDefinition["searchable"] = false;
|
||||
$aColumnDefinition["sortable"] = false;
|
||||
if ($sSelectMode != "single") {
|
||||
$aColumnDefinition["title"] = "<span class=\"row_input\"><input type=\"checkbox\" onclick=\"checkAllDataTable('#".$sTableId."',this.checked);\" class=\"checkAll\" id=\"field_".$sTableId."_check_all\" name=\"field_".$sTableId."_check_all\" title=\"".Dict::S('UI:SearchValue:CheckAll' )." / ".Dict::S('UI:SearchValue:UncheckAll')."\" /></span>";
|
||||
} else{
|
||||
$aColumnDefinition["title"] = "";
|
||||
}
|
||||
$aColumnDefinition["type"] = "html";
|
||||
$aColumnDefinition["data"] = "";
|
||||
$aColumnDefinition["render"]["display"] = "";
|
||||
if ($sSelectMode != "single") {
|
||||
$aColumnDefinition["render"]["display"] = $aColumnDefinition["render"]["display"] . " var oCheckboxElem = $('<span class=\"row_input\"><input type=\"checkbox\" class=\"selectList".$sTableId."\" name=\"selectObject\" /></span>');";
|
||||
}
|
||||
else {
|
||||
$aColumnDefinition["render"]["display"] = $aColumnDefinition["render"]["display"] . " var oCheckboxElem = $('<span class=\"row_input\"><input type=\"radio\" class=\"selectList".$sTableId."\" name=\"selectObject\" /></span>');";
|
||||
}
|
||||
$aColumnDefinition["render"]["display"] = $aColumnDefinition["render"]["display"] . " if (row.limited_access) { oCheckboxElem.html('-'); } else { oCheckboxElem.find(':input').attr('data-object-id', row.id).attr('data-target-object-id', row.target_id); }";
|
||||
$aColumnDefinition["render"]["display"] = $aColumnDefinition["render"]["display"]. " return oCheckboxElem.prop('outerHTML'); ";
|
||||
array_push($aColumnsDefinitions, $aColumnDefinition);
|
||||
}
|
||||
|
||||
foreach ($aColumns as $sClassAlias => $aClassColumns) {
|
||||
$sClassName=$aClassAliases[$sClassAlias];
|
||||
foreach ($aClassColumns as $sAttCode => $aData) {
|
||||
if ($aData['checked'] == "true") {
|
||||
$aColumnDefinition["width"] = "auto";
|
||||
$aColumnDefinition["searchable"] = false;
|
||||
$aColumnDefinition["sortable"] = true;
|
||||
$aColumnDefinition["defaultContent"] = "";
|
||||
$aColumnDefinition["type"] = "html";
|
||||
|
||||
if ($sAttCode == '_key_') {
|
||||
$aColumnDefinition["title"] = $aData['alias'];
|
||||
$aColumnDefinition['metadata'] = [
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => $aData['alias'],
|
||||
];
|
||||
$aColumnDefinition["data"] = $sClassAlias."/".$sAttCode;
|
||||
$aColumnDefinition["render"] = [
|
||||
"display" => "return '<a class=\'object-ref-link\' href=\'UI.php?operation=details&class=".$sClassName."&id='+data+'\'>'+row['".$sClassAlias."/friendlyname']+'</a>' ;",
|
||||
"_" => $sClassAlias."/".$sAttCode,
|
||||
];
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
$sAttDefClass = get_class($oAttDef);
|
||||
$sAttLabel = MetaModel::GetLabel($sClassName, $sAttCode);
|
||||
|
||||
$aColumnDefinition["title"] = $sAttLabel;
|
||||
$aColumnDefinition['metadata'] = [
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => $sAttDefClass,
|
||||
'attribute_label' => $sAttLabel,
|
||||
];
|
||||
$aColumnDefinition["data"] = $sClassAlias."/".$sAttCode;
|
||||
$aColumnDefinition["render"] = [
|
||||
"display" => $oAttDef->GetRenderForDataTable($sClassAlias),
|
||||
"_" => $sClassAlias."/".$sAttCode,
|
||||
];
|
||||
}
|
||||
array_push($aColumnsDefinitions, $aColumnDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$aOptions['select'] = ["style"=>$sSelectMode];
|
||||
|
||||
$aOptions['pageLength'] = $iLength;
|
||||
|
||||
$sAjaxData = json_encode([
|
||||
"operation" => 'search',
|
||||
"filter" => $sFilter,
|
||||
"columns" => $aColumns,
|
||||
"extra_params" => $aExtraParams,
|
||||
"class_aliases" => $aClassAliases,
|
||||
"select_mode" => $sSelectMode,
|
||||
]);
|
||||
|
||||
|
||||
$aOptions =array_merge ($aOptions, [
|
||||
"language" =>
|
||||
[
|
||||
"processing" => Dict::Format('UI:Datatables:Language:Processing'),
|
||||
"search" => Dict::Format('UI:Datatables:Language:Search'),
|
||||
"lengthMenu" => Dict::Format('UI:Datatables:Language:LengthMenu'),
|
||||
"zeroRecords" => Dict::Format('UI:Datatables:Language:ZeroRecords'),
|
||||
"info" => Dict::Format('UI:Datatables:Language:Info'),
|
||||
"infoEmpty" => Dict::Format('UI:Datatables:Language:InfoEmpty'),
|
||||
"infoFiltered" => Dict::Format('UI:Datatables:Language:InfoFiltered'),
|
||||
"emptyTable" => Dict::Format('UI:Datatables:Language:EmptyTable'),
|
||||
"paginate" => [
|
||||
"first" => "<<",
|
||||
"previous" => "<",
|
||||
"next" => ">",
|
||||
"last" => ">>"
|
||||
],
|
||||
"aria" => [
|
||||
"sortAscending" => Dict::Format('UI:Datatables:Language:Sort:Ascending'),
|
||||
"sortDescending" => Dict::Format('UI:Datatables:Language:Sort:Descending')
|
||||
],
|
||||
],
|
||||
"lengthMenu" => Dict::Format('Portal:Datatables:Language:DisplayLength:All'),
|
||||
"dom" => "<'ibo-datatable-toolbar'pil>t<'ibo-datatable-toolbar'pil>",
|
||||
"ordering"=>true,
|
||||
"order" => [],
|
||||
"filter" => false,
|
||||
"processing" => true,
|
||||
"serverSide" => true,
|
||||
"columns" => $aColumnsDefinitions,
|
||||
"allColumns" => $aColumns,
|
||||
'ajax' => '$.fn.dataTable.pipeline( {
|
||||
"url": "ajax.render.php",
|
||||
"data": '.$sAjaxData.',
|
||||
"method": "post",
|
||||
"pages": 5 // number of pages to cache
|
||||
} )'
|
||||
]);
|
||||
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
public static function MakeForStaticData(string $sTitle, array $aColumns, array $aData, ?string $sId = null)
|
||||
{
|
||||
$oBlock = new UIContentBlock();
|
||||
$oTitle = TitleFactory::MakeNeutral($sTitle, 3);
|
||||
$oBlock->AddSubBlock($oTitle);
|
||||
$oTable = new StaticTable($sId);
|
||||
$oTable->SetColumns($aColumns);
|
||||
$oTable->SetData($aData);
|
||||
$oBlock->AddSubBlock($oTable);
|
||||
|
||||
return $oBlock;
|
||||
}
|
||||
|
||||
public Static function MakeForForm(string $sRef, array $aColumns): FormTable
|
||||
{
|
||||
$oTable = new FormTable($sRef);
|
||||
$oTable->SetColumns($aColumns);
|
||||
|
||||
return $oTable;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable;
|
||||
|
||||
use appUserPreferences;
|
||||
use AttributeDashboard;
|
||||
use AttributeFriendlyName;
|
||||
use AttributeLinkedSet;
|
||||
use cmdbAbstract;
|
||||
use cmdbAbstractObject;
|
||||
use Dict;
|
||||
use Metamodel;
|
||||
use Serializable;
|
||||
|
||||
class DataTableSettings implements Serializable
|
||||
{
|
||||
public $aClassAliases;
|
||||
public $sTableId;
|
||||
public $iDefaultPageSize;
|
||||
public $aColumns;
|
||||
|
||||
|
||||
/**
|
||||
* DataTableSettings constructor.
|
||||
*
|
||||
* @param $aClassAliases
|
||||
* @param null $sTableId
|
||||
*/
|
||||
public function __construct($aClassAliases, $sTableId = null)
|
||||
{
|
||||
$this->aClassAliases = $aClassAliases;
|
||||
$this->sTableId = $sTableId;
|
||||
$this->iDefaultPageSize = 10;
|
||||
$this->aColumns = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $iDefaultPageSize
|
||||
* @param $aSortOrder
|
||||
* @param $aColumns
|
||||
*/
|
||||
protected function Init($iDefaultPageSize, $aSortOrder, $aColumns)
|
||||
{
|
||||
$this->iDefaultPageSize = $iDefaultPageSize;
|
||||
$this->aColumns = $aColumns;
|
||||
$this->FixVisibleColumns();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
// Save only the 'visible' columns
|
||||
$aColumns = array();
|
||||
foreach ($this->aClassAliases as $sAlias => $sClass) {
|
||||
$aColumns[$sAlias] = array();
|
||||
foreach ($this->aColumns[$sAlias] as $sAttCode => $aData) {
|
||||
unset($aData['label']); // Don't save the display name
|
||||
unset($aData['alias']); // Don't save the alias (redundant)
|
||||
unset($aData['code']); // Don't save the code (redundant)
|
||||
if ($aData['checked']) {
|
||||
$aColumns[$sAlias][$sAttCode] = $aData;
|
||||
}
|
||||
}
|
||||
}
|
||||
return serialize(
|
||||
array(
|
||||
'iDefaultPageSize' => $this->iDefaultPageSize,
|
||||
'aColumns' => $aColumns,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sData
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function unserialize($sData)
|
||||
{
|
||||
$aData = unserialize($sData);
|
||||
$this->iDefaultPageSize = $aData['iDefaultPageSize'];
|
||||
$this->aColumns = $aData['aColumns'];
|
||||
foreach ($this->aClassAliases as $sAlias => $sClass) {
|
||||
foreach ($this->aColumns[$sAlias] as $sAttCode => $aData) {
|
||||
$aFieldData = false;
|
||||
if ($sAttCode == '_key_') {
|
||||
$aFieldData = $this->GetFieldData($sAlias, $sAttCode, null, true /* bChecked */, $aData['sort']);
|
||||
} else if (MetaModel::isValidAttCode($sClass, $sAttCode)) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
|
||||
$aFieldData = $this->GetFieldData($sAlias, $sAttCode, $oAttDef, true /* bChecked */, $aData['sort']);
|
||||
}
|
||||
|
||||
if ($aFieldData) {
|
||||
$this->aColumns[$sAlias][$sAttCode] = $aFieldData;
|
||||
} else {
|
||||
unset($this->aColumns[$sAlias][$sAttCode]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->FixVisibleColumns();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $aClassAliases
|
||||
* @param $bViewLink
|
||||
* @param $aDefaultLists
|
||||
*
|
||||
* @return DataTableSettings
|
||||
* @throws \CoreException
|
||||
* @throws \DictExceptionMissingString
|
||||
*/
|
||||
public static function GetDataModelSettings($aClassAliases, $bViewLink, $aDefaultLists)
|
||||
{
|
||||
$oSettings = new DataTableSettings($aClassAliases);
|
||||
// Retrieve the class specific settings for each class/alias based on the 'list' ZList
|
||||
//TODO let the caller pass some other default settings (another Zlist, extre fields...)
|
||||
$aColumns = array();
|
||||
foreach ($aClassAliases as $sAlias => $sClass) {
|
||||
if ($aDefaultLists == null) {
|
||||
$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClass, 'list'));
|
||||
} else {
|
||||
$aList = $aDefaultLists[$sAlias];
|
||||
}
|
||||
|
||||
$aSortOrder = MetaModel::GetOrderByDefault($sClass);
|
||||
if ($bViewLink) {
|
||||
$sSort = 'none';
|
||||
if (array_key_exists('friendlyname', $aSortOrder)) {
|
||||
$sSort = $aSortOrder['friendlyname'] ? 'asc' : 'desc';
|
||||
}
|
||||
$sNormalizedFName = MetaModel::NormalizeFieldSpec($sClass, 'friendlyname');
|
||||
if (array_key_exists($sNormalizedFName, $aSortOrder)) {
|
||||
$sSort = $aSortOrder[$sNormalizedFName] ? 'asc' : 'desc';
|
||||
}
|
||||
|
||||
$aColumns[$sAlias]['_key_'] = $oSettings->GetFieldData($sAlias, '_key_', null, true /* bChecked */, $sSort);
|
||||
}
|
||||
foreach ($aList as $sAttCode) {
|
||||
$sSort = 'none';
|
||||
if (array_key_exists($sAttCode, $aSortOrder)) {
|
||||
$sSort = $aSortOrder[$sAttCode] ? 'asc' : 'desc';
|
||||
}
|
||||
$oAttDef = Metamodel::GetAttributeDef($sClass, $sAttCode);
|
||||
$aFieldData = $oSettings->GetFieldData($sAlias, $sAttCode, $oAttDef, true /* bChecked */, $sSort);
|
||||
if ($aFieldData) $aColumns[$sAlias][$sAttCode] = $aFieldData;
|
||||
}
|
||||
}
|
||||
$iDefaultPageSize = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit());
|
||||
$oSettings->Init($iDefaultPageSize, $aSortOrder, $aColumns);
|
||||
return $oSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \CoreException
|
||||
*/
|
||||
protected function FixVisibleColumns()
|
||||
{
|
||||
foreach ($this->aClassAliases as $sAlias => $sClass) {
|
||||
if (!isset($this->aColumns[$sAlias])) {
|
||||
continue;
|
||||
}
|
||||
foreach ($this->aColumns[$sAlias] as $sAttCode => $aData) {
|
||||
// Remove non-existent columns
|
||||
// TODO: check if the existing ones are still valid (in case their type changed)
|
||||
if (($sAttCode != '_key_') && (!MetaModel::IsValidAttCode($sClass, $sAttCode))) {
|
||||
unset($this->aColumns[$sAlias][$sAttCode]);
|
||||
}
|
||||
}
|
||||
$aList = MetaModel::ListAttributeDefs($sClass);
|
||||
|
||||
// Add the other (non visible ones), sorted in alphabetical order
|
||||
$aTempData = array();
|
||||
foreach ($aList as $sAttCode => $oAttDef) {
|
||||
if ((!array_key_exists($sAttCode, $this->aColumns[$sAlias])) && (!($oAttDef instanceof AttributeLinkedSet || $oAttDef instanceof AttributeDashboard))) {
|
||||
$aFieldData = $this->GetFieldData($sAlias, $sAttCode, $oAttDef, false /* bChecked */, 'none');
|
||||
if ($aFieldData) $aTempData[$aFieldData['label']] = $aFieldData;
|
||||
}
|
||||
}
|
||||
ksort($aTempData);
|
||||
foreach ($aTempData as $sLabel => $aFieldData) {
|
||||
$this->aColumns[$sAlias][$aFieldData['code']] = $aFieldData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $aClassAliases
|
||||
* @param null $sTableId
|
||||
* @param bool $bOnlyOnTable
|
||||
*
|
||||
* @return DataTableSettings|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
static public function GetTableSettings($aClassAliases, $sTableId = null, $bOnlyOnTable = false)
|
||||
{
|
||||
$pref = null;
|
||||
$oSettings = new DataTableSettings($aClassAliases, $sTableId);
|
||||
|
||||
if ($sTableId != null) {
|
||||
// An identified table, let's fetch its own settings (if any)
|
||||
$pref = appUserPreferences::GetPref($oSettings->GetPrefsKey($sTableId), null);
|
||||
}
|
||||
|
||||
if ($pref == null) {
|
||||
if (!$bOnlyOnTable) {
|
||||
// Try the global preferred values for this class / set of classes
|
||||
$pref = appUserPreferences::GetPref($oSettings->GetPrefsKey(null), null);
|
||||
}
|
||||
if ($pref == null) {
|
||||
// no such settings, use the default values provided by the data model
|
||||
return null;
|
||||
}
|
||||
}
|
||||
$oSettings->unserialize($pref);
|
||||
|
||||
return $oSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetSortOrder()
|
||||
{
|
||||
$aSortOrder = array();
|
||||
foreach ($this->aColumns as $sAlias => $aColumns) {
|
||||
foreach ($aColumns as $aColumn) {
|
||||
if ($aColumn['sort'] != 'none') {
|
||||
$sCode = ($aColumn['code'] == '_key_') ? 'friendlyname' : $aColumn['code'];
|
||||
$aSortOrder[$sCode] = ($aColumn['sort'] == 'asc'); // true for ascending, false for descending
|
||||
}
|
||||
}
|
||||
break; // TODO: For now the Set object supports only sorting on the first class of the set
|
||||
}
|
||||
return $aSortOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $sTargetTableId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Save($sTargetTableId = null)
|
||||
{
|
||||
$sSaveId = is_null($sTargetTableId) ? $this->sTableId : $sTargetTableId;
|
||||
if ($sSaveId == null) return false; // Cannot save, the table is not identified, use SaveAsDefault instead
|
||||
|
||||
$sSettings = $this->serialize();
|
||||
appUserPreferences::SetPref($this->GetPrefsKey($sSaveId), $sSettings);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function SaveAsDefault()
|
||||
{
|
||||
$sSettings = $this->serialize();
|
||||
appUserPreferences::SetPref($this->GetPrefsKey(null), $sSettings);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clear the preferences for this particular table
|
||||
* @param $bResetAll boolean If true,the settings for all tables of the same class(es)/alias(es) are reset
|
||||
*/
|
||||
public function ResetToDefault($bResetAll)
|
||||
{
|
||||
if (($this->sTableId == null) && (!$bResetAll)) return false; // Cannot reset, the table is not identified, use force $bResetAll instead
|
||||
if ($bResetAll) {
|
||||
// Turn the key into a suitable PCRE pattern
|
||||
$sKey = $this->GetPrefsKey(null);
|
||||
$sPattern = str_replace(array('|'), array('\\|'), $sKey); // escape the | character
|
||||
$sPattern = '#^' . str_replace(array('*'), array('.*'), $sPattern) . '$#'; // Don't use slash as the delimiter since it's used in our key to delimit aliases
|
||||
appUserPreferences::UnsetPref($sPattern, true);
|
||||
} else {
|
||||
appUserPreferences::UnsetPref($this->GetPrefsKey($this->sTableId), false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $sTableId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function GetPrefsKey($sTableId = null)
|
||||
{
|
||||
return static::GetAppUserPreferenceKey($this->aClassAliases, $sTableId);
|
||||
}
|
||||
|
||||
public static function GetAppUserPreferenceKey($aClassAliases, $sTableId)
|
||||
{
|
||||
if ($sTableId === null) {
|
||||
$sTableId = '*';
|
||||
}
|
||||
|
||||
$aKeys = array();
|
||||
foreach ($aClassAliases as $sAlias => $sClass) {
|
||||
$aKeys[] = $sAlias . '-' . $sClass;
|
||||
}
|
||||
return implode('/', $aKeys) . '|' . $sTableId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sAlias
|
||||
* @param $sAttCode
|
||||
* @param $oAttDef
|
||||
* @param $bChecked
|
||||
* @param $sSort
|
||||
*
|
||||
* @return array|bool
|
||||
* @throws \CoreException
|
||||
* @throws \DictExceptionMissingString
|
||||
*/
|
||||
protected function GetFieldData($sAlias, $sAttCode, $oAttDef, $bChecked, $sSort)
|
||||
{
|
||||
$ret = false;
|
||||
if ($sAttCode == '_key_') {
|
||||
$sLabel = Dict::Format('UI:ExtKey_AsLink', MetaModel::GetName($this->aClassAliases[$sAlias]));
|
||||
$ret = array(
|
||||
'label' => $sLabel,
|
||||
'checked' => true,
|
||||
'disabled' => true,
|
||||
'alias' => $sAlias,
|
||||
'code' => $sAttCode,
|
||||
'sort' => $sSort,
|
||||
);
|
||||
} else if (!$oAttDef->IsLinkSet()) {
|
||||
$sLabel = $oAttDef->GetLabel();
|
||||
if ($oAttDef->IsExternalKey()) {
|
||||
$sLabel = Dict::Format('UI:ExtKey_AsLink', $oAttDef->GetLabel());
|
||||
} else if ($oAttDef->IsExternalField()) {
|
||||
if ($oAttDef->IsFriendlyName()) {
|
||||
$sLabel = Dict::Format('UI:ExtKey_AsFriendlyName', $oAttDef->GetLabel());
|
||||
} else {
|
||||
$oExtAttDef = $oAttDef->GetExtAttDef();
|
||||
$sLabel = Dict::Format('UI:ExtField_AsRemoteField', $oAttDef->GetLabel(), $oExtAttDef->GetLabel());
|
||||
}
|
||||
} elseif ($oAttDef instanceof AttributeFriendlyName) {
|
||||
$sLabel = Dict::Format('UI:ExtKey_AsFriendlyName', $oAttDef->GetLabel());
|
||||
}
|
||||
$ret = array(
|
||||
'label' => $sLabel,
|
||||
'checked' => $bChecked,
|
||||
'disabled' => false,
|
||||
'alias' => $sAlias,
|
||||
'code' => $sAttCode,
|
||||
'sort' => $sSort,
|
||||
);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTable;
|
||||
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTableRow\FormTableRow;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\StaticTable;
|
||||
use Combodo\iTop\Application\UI\Base\iUIBlock;
|
||||
|
||||
/**
|
||||
* Class FormTable
|
||||
*
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\FormTable
|
||||
*/
|
||||
class FormTable extends StaticTable
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-formtable';
|
||||
public const HTML_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtable/layout';
|
||||
public const JS_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtable/layout';
|
||||
|
||||
/** @var string */
|
||||
private $sRef;
|
||||
|
||||
/** @var iUIBlock[] */
|
||||
private $aRows;
|
||||
|
||||
public function __construct(string $sRef, string $sContainerCSSClass = '')
|
||||
{
|
||||
parent::__construct("dt_{$sRef}", $sContainerCSSClass);
|
||||
$this->SetRef($sRef);
|
||||
$this->aRows = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetRef(): string
|
||||
{
|
||||
return $this->sRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sRef
|
||||
*/
|
||||
public function SetRef(string $sRef): void
|
||||
{
|
||||
$this->sRef = $sRef;
|
||||
}
|
||||
|
||||
public function GetRows(): array
|
||||
{
|
||||
return $this->aRows;
|
||||
}
|
||||
|
||||
public function AddRow(FormTableRow $oRow): self
|
||||
{
|
||||
$this->aRows[] = $oRow;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTableRow;
|
||||
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\UIBlock;
|
||||
|
||||
/**
|
||||
* Class FormTableRow
|
||||
*
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\FormTableRow
|
||||
*/
|
||||
class FormTableRow extends UIBlock
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-formtablerow';
|
||||
public const HTML_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtablerow/layout';
|
||||
public const JS_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtablerow/layout';
|
||||
|
||||
/** @var string */
|
||||
private $sRef;
|
||||
/** @var int */
|
||||
private $iRowId;
|
||||
/**
|
||||
* @var array
|
||||
* [
|
||||
* 'entry name' => [
|
||||
* 'description' => tooltip,
|
||||
* 'label' => label to display,
|
||||
* 'class' => cell CSS class,
|
||||
* 'metadata' => [key => value] transformed into data-key="value"
|
||||
* ], ...
|
||||
* ]
|
||||
*/
|
||||
private $aColumns;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* [
|
||||
* 'entry name' => [
|
||||
* 'value_html' => value to display in the cell,
|
||||
* 'value_raw' => real value put into data-value-raw
|
||||
* ], ...
|
||||
* ]
|
||||
*/
|
||||
private $aData;
|
||||
|
||||
public function __construct(string $sRef, array $aColumns, array $aData, int $iRowId)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->SetRef($sRef);
|
||||
$this->SetColumns($aColumns);
|
||||
$this->SetData($aData);
|
||||
$this->SetRowId($iRowId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetRef(): string
|
||||
{
|
||||
return $this->sRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sRef
|
||||
*/
|
||||
public function SetRef(string $sRef): void
|
||||
{
|
||||
$this->sRef = $sRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetColumns(): array
|
||||
{
|
||||
return $this->aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aColumns
|
||||
*/
|
||||
public function SetColumns(array $aColumns): void
|
||||
{
|
||||
$this->aColumns = $aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetData(): array
|
||||
{
|
||||
return $this->aData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aData
|
||||
*/
|
||||
public function SetData(array $aData): void
|
||||
{
|
||||
$this->aData = $aData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function GetRowId(): int
|
||||
{
|
||||
return $this->iRowId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iRowId
|
||||
*/
|
||||
public function SetRowId(int $iRowId): void
|
||||
{
|
||||
$this->iRowId = $iRowId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable;
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2020 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tables with static data
|
||||
* Class StaticTable
|
||||
*/
|
||||
class StaticTable extends UIContentBlock
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatable';
|
||||
public const HTML_TEMPLATE_REL_PATH = 'base/components/datatable/static/layout';
|
||||
public const JS_TEMPLATE_REL_PATH = 'base/components/datatable/static/layout';
|
||||
|
||||
/**
|
||||
* @var array of 'entry name' => [
|
||||
* 'description' => tooltip,
|
||||
* 'label' => label to display,
|
||||
* 'class' => cell CSS class,
|
||||
* 'metadata' => [key => value] transformed into data-key="value"
|
||||
* ]
|
||||
*/
|
||||
private $aColumns;
|
||||
|
||||
/**
|
||||
* @var array of [
|
||||
* '@class' => css class of the row,
|
||||
* 'entry name' => [
|
||||
* 'value_html' => value to display in the cell,
|
||||
* 'value_raw' => real value put into data-value-raw
|
||||
* ], ...
|
||||
* ]
|
||||
*/
|
||||
private $aData;
|
||||
|
||||
public function __construct(string $sId = null, string $sContainerCSSClass = '')
|
||||
{
|
||||
parent::__construct($sId, $sContainerCSSClass);
|
||||
$this->aColumns = [];
|
||||
$this->aData = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetColumns(): array
|
||||
{
|
||||
return $this->aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aColumns
|
||||
*/
|
||||
public function SetColumns(array $aColumns): void
|
||||
{
|
||||
$this->aColumns = $aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetData(): array
|
||||
{
|
||||
return $this->aData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aData
|
||||
*/
|
||||
public function SetData(array $aData): void
|
||||
{
|
||||
$this->aData = $aData;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user