mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
N°4621 Fix naming inconsistencies in sources/*
This commit is contained in:
253
sources/Application/UI/Base/Component/DataTable/DataTable.php
Normal file
253
sources/Application/UI/Base/Component/DataTable/DataTable.php
Normal file
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable;
|
||||
|
||||
|
||||
use ApplicationContext;
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
use Combodo\iTop\Application\UI\Base\tJSRefreshCallback;
|
||||
use DataTableConfig;
|
||||
|
||||
/**
|
||||
* Class DataTable
|
||||
*
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\DataTable
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class DataTable extends UIContentBlock
|
||||
{
|
||||
use tJSRefreshCallback;
|
||||
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatable';
|
||||
|
||||
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/datatable/layout';
|
||||
public const DEFAULT_JS_ON_READY_TEMPLATE_REL_PATH = 'base/components/datatable/layout';
|
||||
public const DEFAULT_JS_LIVE_TEMPLATE_REL_PATH = 'base/components/datatable/layout';
|
||||
public const DEFAULT_JS_FILES_REL_PATH = [
|
||||
'node_modules/datatables.net/js/jquery.dataTables.min.js',
|
||||
'node_modules/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js',
|
||||
'node_modules/datatables.net-responsive/js/dataTables.responsive.min.js',
|
||||
'node_modules/datatables.net-scroller/js/dataTables.scroller.min.js',
|
||||
'node_modules/datatables.net-select/js/dataTables.select.min.js',
|
||||
'js/field_sorter.js',
|
||||
'js/table-selectable-lines.js',
|
||||
'js/dataTables.main.js',
|
||||
'js/dataTables.settings.js',
|
||||
'js/dataTables.pipeline.js',
|
||||
];
|
||||
|
||||
protected $aOptions;//list of specific options for display datatable
|
||||
protected $sAjaxUrl;
|
||||
protected $aAjaxData;
|
||||
protected $aDisplayColumns;
|
||||
protected $aResultColumns;
|
||||
/*
|
||||
* array of data to display the first page
|
||||
*/
|
||||
protected $aInitDisplayData;
|
||||
|
||||
/**
|
||||
* Panel constructor.
|
||||
*
|
||||
*/
|
||||
public function __construct(?string $sId = null)
|
||||
{
|
||||
parent::__construct($sId);
|
||||
// This block contains a form, so it has to be added later in the flow
|
||||
$this->AddDeferredBlock(new DataTableConfig($this));
|
||||
$this->aDisplayColumns = [];
|
||||
$this->aOptions = [];
|
||||
$this->aResultColumns = [];
|
||||
$this->sJsonData = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetAjaxUrl(): string
|
||||
{
|
||||
return $this->sAjaxUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sAjaxUrl
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetAjaxUrl(string $sAjaxUrl)
|
||||
{
|
||||
if (strlen($sAjaxUrl) > 0)
|
||||
{
|
||||
$oAppContext = new ApplicationContext();
|
||||
if(strpos ($sAjaxUrl,'?')) {
|
||||
$this->sAjaxUrl = $sAjaxUrl."&".$oAppContext->GetForLink();
|
||||
} else {
|
||||
$this->sAjaxUrl = $sAjaxUrl."?".$oAppContext->GetForLink();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->sAjaxUrl = $sAjaxUrl;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetAjaxData(string $sName)
|
||||
{
|
||||
if (isset($this->aAjaxData[$sName])) {
|
||||
return $this->aAjaxData[$sName];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $aAjaxData as a JSON
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetJsonAjaxData(): string
|
||||
{
|
||||
return json_encode($this->aAjaxData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sAjaxData
|
||||
*/
|
||||
public function SetAjaxData(array $aAjaxData): void
|
||||
{
|
||||
$this->aAjaxData = $aAjaxData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sName
|
||||
* @param mixed $sValue
|
||||
*/
|
||||
public function AddOption($sName, $sValue): void
|
||||
{
|
||||
$this->aOptions[$sName] = $sValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $aInitDisplayData as a JSON This is data of first page
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function GetJsonInitDisplayData(): string
|
||||
{
|
||||
return json_encode($this->aInitDisplayData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $aInitDisplayData
|
||||
* @return array
|
||||
*/
|
||||
public function GetInitDisplayData(): array
|
||||
{
|
||||
return $this->aInitDisplayData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $aData
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetInitDisplayData(array $aData)
|
||||
{
|
||||
$this->aInitDisplayData = $aData;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function GetJSRefresh(): string
|
||||
{
|
||||
return "$('#".$this->sId."').DataTable().clearPipeline();
|
||||
$('#".$this->sId."').DataTable().ajax.reload(null, false);";
|
||||
}
|
||||
|
||||
public function GetDisabledSelect(): array
|
||||
{
|
||||
$aExtraParams = $this->aAjaxData['extra_params'];
|
||||
if (isset($aExtraParams['selection_enabled'])) {
|
||||
$aListDisabled = [];
|
||||
foreach ($aExtraParams['selection_enabled'] as $sKey => $bValue) {
|
||||
if ($bValue == false) {
|
||||
$aListDisabled[] = $sKey;
|
||||
}
|
||||
}
|
||||
|
||||
return $aListDisabled;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTable;
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
class DataTableConfig extends UIContentBlock
|
||||
{
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatableconfig';
|
||||
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/datatable/config/layout';
|
||||
|
||||
/** @var DataTable */
|
||||
private $oDataTable;
|
||||
|
||||
public function __construct(DataTable $oDataTable, ?string $sId = null)
|
||||
{
|
||||
parent::__construct($sId);
|
||||
$this->oDataTable = $oDataTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\DataTable\DataTable
|
||||
*/
|
||||
private function GetDataTable()
|
||||
{
|
||||
return $this->oDataTable;
|
||||
}
|
||||
|
||||
public function GetOption(string $sOption)
|
||||
{
|
||||
return $this->GetDataTable()->GetOption($sOption);
|
||||
}
|
||||
|
||||
public function GetTableId()
|
||||
{
|
||||
return $this->GetDataTable()->GetId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
<?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();
|
||||
if (isset($this->aColumns[$sAlias])) {
|
||||
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' => $bChecked,
|
||||
'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,993 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable;
|
||||
|
||||
use ApplicationException;
|
||||
use ApplicationContext;
|
||||
use appUserPreferences;
|
||||
use AttributeLinkedSet;
|
||||
use cmdbAbstractObject;
|
||||
use Combodo\iTop\Application\UI\Base\AbstractUIBlockFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\CollapsibleSection\CollapsibleSection;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTable\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\Component\Html\Html;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Panel\PanelUIBlockFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Title\TitleUIBlockFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarUIBlockFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
use Combodo\iTop\Controller\AjaxRenderController;
|
||||
use DBObjectSet;
|
||||
use Dict;
|
||||
use MenuBlock;
|
||||
use MetaModel;
|
||||
use UserRights;
|
||||
use utils;
|
||||
use WebPage;
|
||||
|
||||
/**
|
||||
* Class DataTableUIBlockFactory
|
||||
*
|
||||
* @author Anne-Catherine Cognet <anne-catherine.cognet@combodo.com>
|
||||
* @package Combodo\iTop\Application\UI\Base\Component\DataTable
|
||||
* @since 3.0.0
|
||||
* @api
|
||||
*/
|
||||
class DataTableUIBlockFactory extends AbstractUIBlockFactory
|
||||
{
|
||||
/** @inheritDoc */
|
||||
public const TWIG_TAG_NAME = 'UIDataTable';
|
||||
/** @inheritDoc */
|
||||
public const UI_BLOCK_CLASS_NAME = DataTable::class;
|
||||
|
||||
/**
|
||||
* @param \WebPage $oPage
|
||||
* @param string $sListId
|
||||
* @param \DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Layout\UIContentBlock
|
||||
* @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())
|
||||
{
|
||||
$oDataTable = DataTableUIBlockFactory::MakeForRendering($sListId, $oSet, $aExtraParams);
|
||||
return self::RenderDataTable($oDataTable, 'list', $oPage, $sListId, $oSet, $aExtraParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WebPage $oPage
|
||||
* @param string $sListId
|
||||
* @param DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Layout\UIContentBlock
|
||||
* @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())
|
||||
{
|
||||
$oDataTable = DataTableUIBlockFactory::MakeForRenderingObject($sListId, $oSet, $aExtraParams);
|
||||
if ($oPage->IsPrintableVersion()) {
|
||||
$oDataTable->AddOption('printVersion', true);
|
||||
}
|
||||
|
||||
return self::RenderDataTable($oDataTable, 'listInObject', $oPage, $sListId, $oSet, $aExtraParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Combodo\iTop\Application\UI\Base\Component\DataTable\DataTable $oDataTable
|
||||
* @param string $sStyle
|
||||
* @param \WebPage $oPage
|
||||
* @param string $sListId
|
||||
* @param \DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Layout\UIContentBlock
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MissingQueryArgument
|
||||
* @throws \MySQLException
|
||||
* @throws \MySQLHasGoneAwayException
|
||||
* @throws \OQLException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected static function RenderDataTable(DataTable $oDataTable, string $sStyle, WebPage $oPage, string $sListId, DBObjectSet $oSet, array $aExtraParams)
|
||||
{
|
||||
if (!isset($aExtraParams['menu']) || $aExtraParams['menu']) {
|
||||
$oMenuBlock = new MenuBlock($oSet->GetFilter(), $sStyle);
|
||||
$aExtraParams['refresh_action'] = $oDataTable->GetJSRefresh();
|
||||
$oBlockMenu = $oMenuBlock->GetRenderContent($oPage, $aExtraParams, $sListId);
|
||||
} else {
|
||||
$bToolkitMenu = true;
|
||||
if (isset($aExtraParams['toolkit_menu'])) {
|
||||
$bToolkitMenu = (bool)$aExtraParams['toolkit_menu'];
|
||||
}
|
||||
if (UserRights::IsPortalUser() || $oPage->IsPrintableVersion()) {
|
||||
// Portal users have a limited access to data, for now they can only see what's configured for them
|
||||
$bToolkitMenu = false;
|
||||
}
|
||||
if ($bToolkitMenu) {
|
||||
$aExtraParams['selection_mode'] = true;
|
||||
$oMenuBlock = new MenuBlock($oSet->GetFilter(), $sStyle);
|
||||
$oBlockMenu = $oMenuBlock->GetRenderContent($oPage, $aExtraParams, $sListId);
|
||||
} else {
|
||||
$oBlockMenu = new UIContentBlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($aExtraParams['surround_with_panel']) || $aExtraParams['surround_with_panel']) {
|
||||
if(!empty($oDataTable->GetInitDisplayData()) && isset($oDataTable->GetInitDisplayData()['recordsTotal'])){
|
||||
$iCount = $oDataTable->GetInitDisplayData()['recordsTotal'];
|
||||
} else {
|
||||
$iCount = $oSet->Count();
|
||||
}
|
||||
$oContainer = PanelUIBlockFactory::MakeForClass($oSet->GetClass(), '')->AddCSSClass('ibo-datatable-panel');
|
||||
if(isset($aExtraParams['panel_title'])){
|
||||
if(isset($aExtraParams['panel_title_is_html']) && $aExtraParams['panel_title_is_html'] === true) {
|
||||
$oContainer->AddTitleBlock(HtmlFactory::MakeRaw($aExtraParams['panel_title']));
|
||||
}
|
||||
else {
|
||||
$oContainer->SetTitle($aExtraParams['panel_title']);
|
||||
}
|
||||
}
|
||||
if ($oDataTable->GetOption("select_mode") == 'multiple')
|
||||
{
|
||||
$sSubTitle =Dict::Format('UI:Pagination:HeaderSelection', $iCount, '<span class="ibo-datatable--selected-count">0</span>');
|
||||
}
|
||||
else
|
||||
{
|
||||
$sSubTitle = Dict::Format('UI:Pagination:HeaderNoSelection', $iCount);
|
||||
}
|
||||
$oContainer->AddSubTitleBlock(new Html($sSubTitle));
|
||||
if(isset($aExtraParams["panel_icon"]) && strlen($aExtraParams["panel_icon"]) > 0){
|
||||
$oContainer->SetIcon($aExtraParams["panel_icon"]);
|
||||
}
|
||||
$oContainer->AddToolbarBlock($oBlockMenu);
|
||||
$oContainer->AddMainBlock($oDataTable);
|
||||
} else {
|
||||
$oContainer = new UIContentBlock();
|
||||
$oToolbar = ToolbarUIBlockFactory::MakeStandard();
|
||||
$oToolbar->AddSubBlock($oBlockMenu);
|
||||
$oContainer->AddSubBlock($oToolbar);
|
||||
$oContainer->AddSubBlock($oDataTable);
|
||||
}
|
||||
|
||||
return $oContainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a basis Panel component
|
||||
*
|
||||
* @param string $sListId
|
||||
* @param \DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return DataTable
|
||||
* @throws \ApplicationException
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
public static function MakeForRendering(string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oDataTable = new DataTable('datatable_'.$sListId);
|
||||
|
||||
$oAppRoot = utils::GetAbsoluteUrlAppRoot();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
if (count($oCustomSettings->aColumns) == 0) {
|
||||
$oCustomSettings->aColumns = $oDefaultSettings->aColumns;
|
||||
}
|
||||
if (count($oCustomSettings->GetSortOrder()) == 0) {
|
||||
$oCustomSettings->aSortOrder = $oDefaultSettings->aSortOrder;
|
||||
}
|
||||
|
||||
$sIdName = isset($aExtraParams["id_for_select"]) ? $aExtraParams["id_for_select"] : "";
|
||||
// Load only the requested columns
|
||||
$aColumnsToLoad = array();
|
||||
foreach ($oCustomSettings->aColumns as $sAlias => $aColumnsInfo) {
|
||||
foreach ($aColumnsInfo as $sAttCode => $aData) {
|
||||
$bForceLoad = false;
|
||||
if ($aData['sort'] != 'none' || isset($oCustomSettings->aSortOrder[$sAttCode])) {
|
||||
$bForceLoad = true;
|
||||
}
|
||||
if ($sAttCode != '_key_') {
|
||||
if ($aData['checked'] || $bForceLoad) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($sIdName == "") {
|
||||
$sIdName = $sAlias."/_key_";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->OptimizeColumnLoad($aColumnsToLoad);
|
||||
$aSortOrder=[];
|
||||
$aSortDatable=[];
|
||||
$aColumnDefinition = [];
|
||||
$iIndexColumn=0;
|
||||
if($sSelectMode!="") {
|
||||
$iIndexColumn++;
|
||||
}
|
||||
foreach ($aClassAliases as $sClassAlias => $sClassName) {
|
||||
foreach ($oCustomSettings->aColumns[$sClassAlias] as $sAttCode => $aData) {
|
||||
$sCode = ($aData['code'] == '_key_') ? 'friendlyname' : $aData['code'];
|
||||
if ($aData['sort'] != 'none') {
|
||||
$aSortOrder[$sAlias.'.'.$sCode] = ($aData['sort'] == 'asc'); // true for ascending, false for descending
|
||||
$aSortDatable=[$iIndexColumn,$aData['sort']];
|
||||
}
|
||||
elseif (isset($oCustomSettings->aSortOrder[$sAttCode])){
|
||||
$aSortOrder[$sAlias.'.'.$sCode] = $oCustomSettings->aSortOrder[$sAttCode]; // true for ascending, false for descending
|
||||
}
|
||||
|
||||
if ($aData['checked']) {
|
||||
if ($sAttCode == '_key_') {
|
||||
if ($bViewLink) {
|
||||
if (MetaModel::IsValidAttCode($sClassName, 'obsolescence_flag')) {
|
||||
$sDisplayFunction = "let displayField = '<span class=\"object-ref\" title=\"".$sClassAlias."::'+data+'\"><a class=\'object-ref-link\' href=\'".$oAppRoot."/pages/UI.php?operation=details&class=".$sClassName."&id='+data+'\'>'+row['".$sClassAlias."/friendlyname']+'</a></span>'; if (row['".$sClassAlias."/obsolescence_flag'].indexOf('no') == -1){displayField = '<span class=\"object-ref obsolete\" title=\"obsolete\"><a class=\'object-ref-link\' href=\'UI.php?operation=details&class=".$sClassName."&id='+data+'\'><span class=\"object-ref-icon text_decoration\"><span class=\"fas fa-eye-slash object-obsolete fa-1x fa-fw\"></span></span>'+row['".$sClassAlias."/friendlyname']+'</a></span>';} return displayField;";
|
||||
} else {
|
||||
$sDisplayFunction = "let displayField = '<span class=\"object-ref\" title=\"".$sClassAlias."::'+data+'\"><a class=\'object-ref-link\' href=\'".$oAppRoot."/pages/UI.php?operation=details&class=".$sClassName."&id='+data+'\'>'+row['".$sClassAlias."/friendlyname']+'</a></span>'; return displayField;";
|
||||
}
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $aData['label'],
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => MetaModel::GetName($sClassName),
|
||||
'render' => "return row['".$sClassAlias."/hyperlink'];",
|
||||
];
|
||||
|
||||
}
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof \AttributeCaseLog) {
|
||||
// Add JS files for display caselog
|
||||
// Dummy collapsible section created in order to get JS files
|
||||
$oCollapsibleSection = new CollapsibleSection('');
|
||||
$oDataTable->AddMultipleJsFilesRelPaths($oCollapsibleSection->GetJsFilesUrlRecursively());
|
||||
}
|
||||
$sAttDefClass = get_class($oAttDef);
|
||||
$sAttLabel = $oAttDef->GetLabel();
|
||||
$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['selectionMode'] = $aExtraParams['selectionMode']?? 'positive';
|
||||
|
||||
if (isset($aExtraParams['cssCount'])) {
|
||||
$aOptions['sCountSelector'] = $aExtraParams['cssCount'];
|
||||
}
|
||||
|
||||
$aOptions['iPageSize'] = 10;
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$aOptions['iPageSize'] = $oCustomSettings->iDefaultPageSize;
|
||||
}
|
||||
|
||||
// Max height is only set if necessary, otherwise we want the list to occupy all the height it can depending on its pagination
|
||||
if (isset($aExtraParams['max_height'])) {
|
||||
$aOptions['sMaxHeight'] = $aExtraParams['max_height'];
|
||||
}
|
||||
|
||||
$aOptions['processing'] = true;
|
||||
$aOptions['sTableId'] = $sTableId;
|
||||
$aOptions['bUseCustomSettings'] = $bUseCustomSettings;
|
||||
$aOptions['bViewLink'] = $bViewLink;
|
||||
$aOptions['sListId'] = $sListId;
|
||||
$aOptions['oClassAliases'] = json_encode($aClassAliases);
|
||||
if (isset($aExtraParams['selected_rows']) && !empty($aExtraParams['selected_rows'])) {
|
||||
$aOptions['sSelectedRows'] = json_encode($aExtraParams['selected_rows']);
|
||||
} else {
|
||||
$aOptions['sSelectedRows'] = '[]';
|
||||
}
|
||||
$aExtraParams['table_id']=$sTableId;
|
||||
$aExtraParams['list_id']=$sListId;
|
||||
|
||||
|
||||
$oDataTable->SetOptions($aOptions);
|
||||
$oDataTable->SetAjaxUrl(utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php");
|
||||
$oDataTable->SetAjaxData([
|
||||
"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);
|
||||
$oDataTable->SetInitDisplayData(AjaxRenderController::GetDataForTable($oSet, $aClassAliases, $aColumnsToLoad, $sIdName, $aExtraParams));
|
||||
|
||||
return $oDataTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sListId
|
||||
* @param DBObjectSet $oSet
|
||||
* @param array $aExtraParams
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\DataTable\DataTable
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
public static function MakeForRenderingObject(string $sListId, DBObjectSet $oSet, $aExtraParams = array())
|
||||
{
|
||||
$oDataTable = new DataTable('datatable_'.$sListId);
|
||||
$aList = array();
|
||||
$oAppRoot = utils::GetAbsoluteUrlAppRoot();
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
$sIdName = isset($extraParams["id_for_select"]) ? $extraParams["id_for_select"] : "";
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($sIdName == "") {
|
||||
$sIdName = $sAlias."/_key_";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$oSet->OptimizeColumnLoad($aColumnsToLoad);
|
||||
|
||||
$aColumnDefinition = [];
|
||||
$iIndexColumn = 0;
|
||||
|
||||
$bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
|
||||
$bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
|
||||
$sSelectMode = '';
|
||||
if ($bSelectMode) {
|
||||
$sSelectMode = $bSingleSelectMode ? 'single' : 'multiple';
|
||||
$iIndexColumn++;
|
||||
}
|
||||
|
||||
$aSortDatable = [];
|
||||
foreach ($aAuthorizedClasses as $sClassAlias => $sClassName) {
|
||||
if (isset($oCustomSettings->aColumns[$sClassAlias])) {
|
||||
foreach ($oCustomSettings->aColumns[$sClassAlias] as $sAttCode => $aData) {
|
||||
if ($aData['sort'] != 'none' && $aSortDatable == []) {
|
||||
$aSortDatable = [$index, $aData['sort']];
|
||||
}
|
||||
|
||||
if ($aData['checked']) {
|
||||
if ($sAttCode == '_key_') {
|
||||
if ($bViewLink) {
|
||||
$sAttLabel = MetaModel::GetName($sClassName);
|
||||
$aColumnDefinition[] = [
|
||||
'description' => $aData['label'],
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => $sAttLabel,
|
||||
"render" => "return row['".$sClassAlias."/hyperlink'];",
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof \AttributeCaseLog) {
|
||||
// Removed from the display list
|
||||
// Dummy collapsible section created in order to get JS files
|
||||
$sCollapsibleSection = new CollapsibleSection('');
|
||||
$oDataTable->AddMultipleJsFilesRelPaths($sCollapsibleSection->GetJsFilesUrlRecursively());
|
||||
}
|
||||
$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['selectionMode'] = $aExtraParams['selectionMode']?? 'positive';
|
||||
|
||||
$aOptions['sort'] = $aSortDatable;
|
||||
|
||||
$aOptions['iPageSize'] = 10;
|
||||
if ($oCustomSettings->iDefaultPageSize > 0) {
|
||||
$aOptions['iPageSize'] = $oCustomSettings->iDefaultPageSize;
|
||||
}
|
||||
|
||||
// Max height is only set if necessary, otherwise we want the list to occupy all the height it can depending on its pagination
|
||||
if (isset($aExtraParams['max_height'])) {
|
||||
$aOptions['sMaxHeight'] = $aExtraParams['max_height'];
|
||||
}
|
||||
|
||||
$aOptions['sTableId'] = $sTableId;
|
||||
$aOptions['bUseCustomSettings'] = $bUseCustomSettings;
|
||||
$aOptions['bViewLink'] = $bViewLink;
|
||||
$aOptions['oClassAliases'] = json_encode($aClassAliases);
|
||||
|
||||
$oDataTable->SetOptions($aOptions);
|
||||
$oDataTable->SetAjaxUrl("ajax.render.php");
|
||||
$oDataTable->SetAjaxData([
|
||||
"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);
|
||||
$oDataTable->SetInitDisplayData(AjaxRenderController::GetDataForTable($oSet, $aClassAliases, $aColumnsToLoad, $sIdName, $aExtraParams));
|
||||
|
||||
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, string $sTableId)
|
||||
{
|
||||
$oAppRoot = utils::GetAbsoluteUrlAppRoot();
|
||||
|
||||
$aOptions = [];
|
||||
$sListId = $aExtraParams["list_id"];
|
||||
$aColumnsDefinitions = [];
|
||||
$aColumnDefinition = [];
|
||||
|
||||
$sSortCol = utils::ReadParam('sort_col', '', false, 'raw_data');
|
||||
$sSortOrder = utils::ReadParam('sort_order', '', false, 'raw_data');
|
||||
$sOrder = [];
|
||||
$aJsFiles = [];
|
||||
if ($sSortCol != "") {
|
||||
$sOrder[] = [$sSortCol, $sSortOrder];
|
||||
}
|
||||
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,'".$sListId."');\" 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[]\" value='+row.id+' /></span>');";
|
||||
}
|
||||
else {
|
||||
$aColumnDefinition["render"]["display"] = $aColumnDefinition["render"]["display"] . " var oCheckboxElem = $('<span class=\"row_input\"><input type=\"radio\" class=\"selectList".$sTableId."\" name=\"selectObject[]\" value='+ row.id +' /></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_') {
|
||||
$sAttrLabel = $aData['alias'];
|
||||
$aColumnDefinition["title"] = $aData['alias'];
|
||||
$aColumnDefinition['metadata'] = [
|
||||
'object_class' => $sClassName,
|
||||
'class_alias' => $sClassAlias,
|
||||
'attribute_code' => $sAttCode,
|
||||
'attribute_type' => '_key_',
|
||||
'attribute_label' => $sAttrLabel,
|
||||
];
|
||||
$aColumnDefinition["data"] = $sClassAlias."/".$sAttCode;
|
||||
$aColumnDefinition["render"] = [
|
||||
"display" => "return row['".$sClassAlias."/hyperlink'];",
|
||||
"_" => $sClassAlias."/".$sAttCode,
|
||||
];
|
||||
$aColumnDefinition["createdCell"] = <<<JS
|
||||
$(td).attr('data-object-class', '$sClassName');
|
||||
$(td).attr('data-attribute-label', '$sAttrLabel');
|
||||
if (rowData["$sClassAlias/$sAttCode/raw"]) {
|
||||
$(td).attr('data-value-raw', rowData["$sClassAlias/$sAttCode/raw"]);
|
||||
}
|
||||
JS;
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof \AttributeCaseLog) {
|
||||
// Get JS files
|
||||
// Dummy collapsible section created in order to get JS files
|
||||
$oCollapsibleSection = new CollapsibleSection('');
|
||||
$aJsFiles = array_merge($aJsFiles, $oCollapsibleSection->GetJsFilesUrlRecursively());
|
||||
}
|
||||
$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,
|
||||
];
|
||||
$aColumnDefinition["createdCell"] = <<<JS
|
||||
$(td).attr('data-object-class', '$sClassName');
|
||||
$(td).attr('data-attribute-label', '$sAttrLabel');
|
||||
$(td).attr('data-attribute-code', '$sAttCode');
|
||||
$(td).attr('data-attribute-type', '$sAttDefClass');
|
||||
if (rowData["$sClassAlias/$sAttCode/raw"]) {
|
||||
$(td).attr('data-value-raw', rowData["$sClassAlias/$sAttCode/raw"]);
|
||||
}
|
||||
JS;
|
||||
}
|
||||
array_push($aColumnsDefinitions, $aColumnDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$aOptions['select'] = ["style" => $sSelectMode, "info" => false];
|
||||
|
||||
$aOptions['pageLength'] = $iLength;
|
||||
|
||||
$sAjaxData = json_encode([
|
||||
"operation" => 'search',
|
||||
"filter" => $sFilter,
|
||||
"columns" => $aColumns,
|
||||
"extra_params" => $aExtraParams,
|
||||
"class_aliases" => $aClassAliases,
|
||||
"select_mode" => $sSelectMode,
|
||||
]);
|
||||
|
||||
$oAppContext = new ApplicationContext();
|
||||
$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'<'ibo-datatable--toolbar-left' pl><'ibo-datatable--toolbar-right' i>>t<'ibo-datatable--toolbar'<'ibo-datatable--toolbar-left' pl><'ibo-datatable--toolbar-right' i>>",
|
||||
"scrollX" => true,
|
||||
"scrollCollapse" => true,
|
||||
"ordering" => true,
|
||||
"order" => $sOrder,
|
||||
"filter" => false,
|
||||
"processing" => true,
|
||||
"serverSide" => true,
|
||||
"columns" => $aColumnsDefinitions,
|
||||
"allColumns" => $aColumns,
|
||||
'ajax' => '$.fn.dataTable.pipeline( {
|
||||
"url": "ajax.render.php?'.$oAppContext->GetForLink().'",
|
||||
"data": '.$sAjaxData.',
|
||||
"method": "post",
|
||||
"pages": 5 // number of pages to cache
|
||||
} )'
|
||||
]);
|
||||
if (count($aJsFiles) > 0) {
|
||||
foreach ($aJsFiles as $sJsFile) {
|
||||
$aUrlFiles[] = utils::GetAbsoluteUrlAppRoot().$sJsFile;
|
||||
}
|
||||
$aOptions['js_files'] = $aUrlFiles;
|
||||
$aOptions['js_files_param'] = 'itopversion';
|
||||
$aOptions['js_files_value'] = ITOP_VERSION;
|
||||
}
|
||||
return $aOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sTitle
|
||||
* @param array $aColumns
|
||||
* @param array $aData
|
||||
* @param string|null $sId
|
||||
* @param array $aExtraParams
|
||||
* @param string $sFilter
|
||||
* @param array $aOptions
|
||||
* *
|
||||
* $aColumns =[
|
||||
* 'nameField1' => ['label' => labelFIeld1, 'description' => descriptionField1],
|
||||
* 'nameField2' => ['label' => labelFIeld2, 'description' => descriptionField2],
|
||||
* 'nameField3' => ['label' => labelFIeld3, 'description' => descriptionField3]];
|
||||
* $aData = [['nameField1' => valueField1, 'nameField2' => valueField2, 'nameField3' => valueField3],...]
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Layout\UIContentBlock
|
||||
*/
|
||||
public static function MakeForStaticData(string $sTitle, array $aColumns, array $aData, ?string $sId = null, array $aExtraParams = [], string $sFilter = "", array $aOptions = [])
|
||||
{
|
||||
$oBlock = new UIContentBlock();
|
||||
if ($sTitle != "") {
|
||||
$oTitle = TitleUIBlockFactory::MakeNeutral($sTitle, 3);
|
||||
$oBlock->AddSubBlock($oTitle);
|
||||
}
|
||||
$oTable = new StaticTable($sId, [], $aExtraParams);
|
||||
$oTable->SetColumns($aColumns);
|
||||
$oTable->SetData($aData);
|
||||
$oTable->SetFilter($sFilter);
|
||||
$oTable->SetOptions($aOptions);
|
||||
|
||||
$oBlock->AddSubBlock($oTable);
|
||||
|
||||
return $oBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sRef
|
||||
* @param array $aColumns
|
||||
* @param array $aData
|
||||
* @param string $sFilter
|
||||
*
|
||||
* $aColumns =[
|
||||
* 'nameField1' => ['label' => labelFIeld1, 'description' => descriptionField1],
|
||||
* 'nameField2' => ['label' => labelFIeld2, 'description' => descriptionField2],
|
||||
* 'nameField3' => ['label' => labelFIeld3, 'description' => descriptionField3]];
|
||||
* $aData = [['nameField1' => valueField1, 'nameField2' => valueField2, 'nameField3' => valueField3],...]
|
||||
*
|
||||
* @return \Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable\FormTable\FormTable
|
||||
*/
|
||||
public static function MakeForForm(string $sRef, array $aColumns, array $aData = [], string $sFilter = '')
|
||||
{
|
||||
$oTable = new FormTable("datatable_".$sRef);
|
||||
$oTable->SetRef($sRef);
|
||||
$oTable->SetColumns($aColumns);
|
||||
$oTable->SetFilter($sFilter);
|
||||
|
||||
foreach ($aData as $iRowId => $aRow) {
|
||||
$oRow = new FormTableRow($sRef, $aColumns, $aRow, $iRowId);
|
||||
$oTable->AddRow($oRow);
|
||||
}
|
||||
|
||||
return $oTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function GetAllowedParams(): array
|
||||
{
|
||||
return [
|
||||
'surround_with_panel', /** bool embed table into a Panel */
|
||||
'menu', /** bool display table menu */
|
||||
'view_link', /** bool display the friendlyname column with links to the objects details */
|
||||
'link_attr', /** string link att code */
|
||||
'object_id', /** int Id of the object linked */
|
||||
'target_attr', /** string target att code of the link */
|
||||
'selection_mode', /** bool activate selection */
|
||||
'selection_type', /** string 'multiple' or 'single' */
|
||||
'extra_fields', /** string comma separated list of link att code to display ('alias.attcode')*/
|
||||
'zlist', /** string name of the zlist to display when 'extra_fields' is not set */
|
||||
'display_limit', /** bool if true pagination is used (default = true) */
|
||||
'table_id', /** string datatable id */
|
||||
'cssCount', /** string external counter (input hidden) js selector */
|
||||
'selected_rows', /** array list of Ids already selected when displaying the datatable */
|
||||
'display_aliases', /** string comma separated list of class aliases to display */
|
||||
'list_id', /** string list outer id */
|
||||
'selection_enabled', /** list of id in witch select is allowed, if not exists all lines are selectable */
|
||||
'id_for_select', /**give definition of id for select checkbox*/
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 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 REQUIRES_ANCESTORS_DEFAULT_JS_FILES = true;
|
||||
public const REQUIRES_ANCESTORS_DEFAULT_CSS_FILES = true;
|
||||
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtable/layout';
|
||||
public const DEFAULT_JS_ON_READY_TEMPLATE_REL_PATH = 'base/components/datatable/static/formtable/layout';
|
||||
|
||||
/** @var string */
|
||||
private $sRef;
|
||||
|
||||
/** @var iUIBlock[] */
|
||||
private $aRows;
|
||||
|
||||
public function __construct(string $sRef, array $aContainerCSSClasses = [])
|
||||
{
|
||||
parent::__construct($sRef, $aContainerCSSClasses);
|
||||
$this->SetRef($sRef);
|
||||
$this->aRows = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetRef(): string
|
||||
{
|
||||
return $this->sRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sRef
|
||||
*/
|
||||
public function SetRef(string $sRef)
|
||||
{
|
||||
$this->sRef = $sRef;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function GetRows(): array
|
||||
{
|
||||
return $this->aRows;
|
||||
}
|
||||
|
||||
public function AddRow(FormTableRow $oRow)
|
||||
{
|
||||
$this->aRows[] = $oRow;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 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 DEFAULT_HTML_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
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetRef(string $sRef)
|
||||
{
|
||||
$this->sRef = $sRef;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetColumns(): array
|
||||
{
|
||||
return $this->aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aColumns
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetColumns(array $aColumns)
|
||||
{
|
||||
$this->aColumns = $aColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetData(): array
|
||||
{
|
||||
return $this->aData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aData
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetData(array $aData)
|
||||
{
|
||||
$this->aData = $aData;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function GetRowId(): int
|
||||
{
|
||||
return $this->iRowId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iRowId
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetRowId(int $iRowId)
|
||||
{
|
||||
$this->iRowId = $iRowId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Application\UI\Base\Component\DataTable\StaticTable;
|
||||
|
||||
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
|
||||
use Combodo\iTop\Application\UI\Base\tJSRefreshCallback;
|
||||
use utils;
|
||||
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tables with static data
|
||||
* Class StaticTable
|
||||
*/
|
||||
class StaticTable extends UIContentBlock
|
||||
{
|
||||
use tJSRefreshCallback;
|
||||
|
||||
// Overloaded constants
|
||||
public const BLOCK_CODE = 'ibo-datatable';
|
||||
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/datatable/static/layout';
|
||||
public const DEFAULT_JS_ON_READY_TEMPLATE_REL_PATH = 'base/components/datatable/static/layout';
|
||||
public const DEFAULT_JS_FILES_REL_PATH = [
|
||||
'node_modules/datatables.net/js/jquery.dataTables.min.js',
|
||||
'node_modules/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js',
|
||||
'node_modules/datatables.net-responsive/js/dataTables.responsive.min.js',
|
||||
'node_modules/datatables.net-scroller/js/dataTables.scroller.min.js',
|
||||
'node_modules/datatables.net-select/js/dataTables.select.min.js',
|
||||
'js/field_sorter.js',
|
||||
'js/table-selectable-lines.js',
|
||||
'js/dataTables.main.js',
|
||||
'js/dataTables.settings.js',
|
||||
'js/dataTables.pipeline.js',
|
||||
];
|
||||
|
||||
/**
|
||||
* @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;
|
||||
private $aExtraParams;
|
||||
/*@var string $sUrlForRefresh*/
|
||||
private $sFilter;
|
||||
/** @var array $aOptions
|
||||
* List of specific options for display datatable
|
||||
*/
|
||||
private $aOptions;
|
||||
|
||||
public function __construct(string $sId = null, array $aContainerCSSClasses = [], array $aExtraParams = [])
|
||||
{
|
||||
parent::__construct($sId, $aContainerCSSClasses);
|
||||
$this->aColumns = [];
|
||||
$this->aData = [];
|
||||
$this->aExtraParams = $aExtraParams;
|
||||
$this->aOptions = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetColumns(): array
|
||||
{
|
||||
return $this->aColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aColumns
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetColumns(array $aColumns)
|
||||
{
|
||||
$this->aColumns = $aColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetData(): array
|
||||
{
|
||||
return $this->aData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aData
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetData(array $aData)
|
||||
{
|
||||
$this->aData = $aData;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sFilter
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetFilter($sFilter)
|
||||
{
|
||||
$this->sFilter = $sFilter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function GetJSRefresh(): string
|
||||
{
|
||||
//$('#".$this->sId."').DataTable().clear().rows.add(data).draw()
|
||||
$aParams = [
|
||||
'style' => 'list',
|
||||
'filter' => $this->sFilter,
|
||||
'extra_params' => $this->aExtraParams,
|
||||
];
|
||||
|
||||
return "$.post('".utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=refreshDashletList', ".json_encode($aParams).",
|
||||
function (data) {
|
||||
$('#".$this->sId."').DataTable().clear();
|
||||
$('#".$this->sId."').dataTable().fnAddData(data);
|
||||
});";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function GetOption(string $sOption)
|
||||
{
|
||||
if (isset($this->aOptions[$sOption])) {
|
||||
return $this->aOptions[$sOption];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetOptions(): array
|
||||
{
|
||||
return $this->aOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aOptions
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetOptions($aOptions)
|
||||
{
|
||||
$this->aOptions = $aOptions;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aOptions
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function AddOption($sName, $sValue)
|
||||
{
|
||||
$this->aOptions[$sName] = $sValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user