mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-20 09:08:42 +02:00
N°3123 : refactoring ajax call for datatables
This commit is contained in:
@@ -31,7 +31,6 @@ require_once(APPROOT.'application/wizardhelper.class.inc.php');
|
||||
require_once(APPROOT.'application/ui.linkswidget.class.inc.php');
|
||||
require_once(APPROOT.'application/ui.searchformforeignkeys.class.inc.php');
|
||||
require_once(APPROOT.'application/ui.extkeywidget.class.inc.php');
|
||||
require_once(APPROOT.'application/datatable.class.inc.php');
|
||||
require_once(APPROOT.'application/excelexporter.class.inc.php');
|
||||
|
||||
|
||||
@@ -42,7 +41,6 @@ function LogErrorMessage($sMsgPrefix, $aContextInfo) {
|
||||
IssueLog::Error($sErrorMessage);
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
@@ -72,7 +70,6 @@ try
|
||||
$oPage = new AjaxPage("");
|
||||
$oPage->no_cache();
|
||||
|
||||
|
||||
$sFilter = utils::ReadParam('filter', '', false, 'raw_data');
|
||||
$sEncoding = utils::ReadParam('encoding', 'serialize');
|
||||
$sClass = utils::ReadParam('class', 'MissingAjaxParam', false, 'class');
|
||||
@@ -93,387 +90,27 @@ try
|
||||
$oAjaxRenderController = new AjaxRenderController();
|
||||
|
||||
switch ($operation) {
|
||||
case 'datatable':
|
||||
case 'pagination':
|
||||
$oPage->SetContentType('text/html');
|
||||
$extraParams = utils::ReadParam('extra_param', '', false, 'raw_data');
|
||||
$aExtraParams = array();
|
||||
if (is_array($extraParams)) {
|
||||
$aExtraParams = $extraParams;
|
||||
} else {
|
||||
$sExtraParams = stripslashes($extraParams);
|
||||
if (!empty($sExtraParams)) {
|
||||
$val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
|
||||
if ($val !== null) {
|
||||
$aExtraParams = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($sEncoding == 'oql')
|
||||
{
|
||||
$oFilter = DBSearch::FromOQL($sFilter);
|
||||
}
|
||||
else
|
||||
{
|
||||
$oFilter = DBSearch::unserialize($sFilter);
|
||||
}
|
||||
$iStart = utils::ReadParam('start', 0);
|
||||
$iEnd = utils::ReadParam('end', 1);
|
||||
$iSortCol = utils::ReadParam('sort_col', 'null');
|
||||
$sSelectMode = utils::ReadParam('select_mode', '');
|
||||
if (!empty($sSelectMode) && ($sSelectMode != 'none'))
|
||||
{
|
||||
// The first column is used for the selection (radio / checkbox) and is not sortable
|
||||
$iSortCol--;
|
||||
}
|
||||
$bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array());
|
||||
$iListId = utils::ReadParam('list_id', 0);
|
||||
|
||||
// Filter the list to removed linked set since we are not able to display them here
|
||||
$aOrderBy = array();
|
||||
$iSortIndex = 0;
|
||||
|
||||
$aColumnsLoad = array();
|
||||
foreach($aClassAliases as $sAlias => $sClassName)
|
||||
{
|
||||
$aColumnsLoad[$sAlias] = array();
|
||||
foreach($aColumns[$sAlias] as $sAttCode => $aData)
|
||||
{
|
||||
if ($aData['checked'] == 'true')
|
||||
{
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = true;
|
||||
if ($sAttCode == '_key_')
|
||||
{
|
||||
if ($iSortCol == $iSortIndex)
|
||||
{
|
||||
if (!MetaModel::HasChildrenClasses($oFilter->GetClass()))
|
||||
{
|
||||
$aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
|
||||
if ($aNameSpec[0] == '%1$s')
|
||||
{
|
||||
// The name is made of a single column, let's sort according to the sort algorithm for this column
|
||||
$aOrderBy[$sAlias.'.'.$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
|
||||
}
|
||||
else
|
||||
{
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof AttributeLinkedSet)
|
||||
{
|
||||
// Removed from the display list
|
||||
unset($aColumns[$sAlias][$sAttCode]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$aColumnsLoad[$sAlias][] = $sAttCode;
|
||||
}
|
||||
if ($iSortCol == $iSortIndex)
|
||||
{
|
||||
if ($oAttDef->IsExternalKey())
|
||||
{
|
||||
$sSortCol = $sAttCode.'_friendlyname';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sSortCol = $sAttCode;
|
||||
}
|
||||
$aOrderBy[$sAlias.'.'.$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
|
||||
}
|
||||
}
|
||||
$iSortIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Load only the requested columns
|
||||
$oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd - $iStart, $iStart);
|
||||
$oSet->OptimizeColumnLoad($aColumnsLoad);
|
||||
|
||||
if (isset($aExtraParams['show_obsolete_data']))
|
||||
{
|
||||
$bShowObsoleteData = $aExtraParams['show_obsolete_data'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$bShowObsoleteData = utils::ShowObsoleteData();
|
||||
}
|
||||
$oSet->SetShowObsoleteData($bShowObsoleteData);
|
||||
$oKPI = new ExecutionKPI();
|
||||
$oDataTable = new DataTable($iListId, $oSet, $oSet->GetSelectedClasses());
|
||||
if ($operation == 'datatable')
|
||||
{
|
||||
// Redraw the whole table
|
||||
$oDataTable->UpdatePager($oPage, $iEnd - $iStart, $iStart); // Set the default page size
|
||||
$sHtml = $oDataTable->GetHTMLTable($oPage, $aColumns, $sSelectMode, $iEnd - $iStart, $bDisplayKey, $aExtraParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
// redraw just the needed rows
|
||||
$sHtml = $oDataTable->GetAsHTMLTableRows($oPage, $iEnd - $iStart, $aColumns, $sSelectMode, $bDisplayKey, $aExtraParams);
|
||||
}
|
||||
$oPage->add($sHtml);
|
||||
$oKPI->ComputeAndReport('Data fetch and format');
|
||||
break;
|
||||
|
||||
case 'search_and_refresh':
|
||||
$oPage->SetContentType('application/json');
|
||||
$extraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
|
||||
$aExtraParams = array();
|
||||
if (is_array($extraParams))
|
||||
{
|
||||
$aExtraParams = $extraParams;
|
||||
}
|
||||
else
|
||||
{
|
||||
$sExtraParams = stripslashes($extraParams);
|
||||
if (!empty($sExtraParams))
|
||||
{
|
||||
$val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
|
||||
if ($val !== null)
|
||||
{
|
||||
$aExtraParams = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
$iLength = utils::ReadParam('end', 10);
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
$sSelectMode = utils::ReadParam('select_mode', '');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array());
|
||||
$aResult = DataTableFactory::GetOptionsForRendering( $aColumns, $sSelectMode, $sFilter, $iLength, $aClassAliases, $aExtraParams);
|
||||
$aResult = AjaxRenderController::SearchAndRefresh($sFilter);
|
||||
$oPage->add(json_encode($aResult));
|
||||
break;
|
||||
|
||||
case 'search':
|
||||
$oPage->SetContentType('application/json');
|
||||
$extraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
|
||||
$aExtraParams = array();
|
||||
if (is_array($extraParams))
|
||||
{
|
||||
$aExtraParams = $extraParams;
|
||||
}
|
||||
else
|
||||
{
|
||||
$sExtraParams = stripslashes($extraParams);
|
||||
if (!empty($sExtraParams))
|
||||
{
|
||||
$val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
|
||||
if ($val !== null)
|
||||
{
|
||||
$aExtraParams = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($sEncoding == 'oql')
|
||||
{
|
||||
$oFilter = DBSearch::FromOQL($sFilter);
|
||||
}
|
||||
else
|
||||
{
|
||||
$oFilter = DBSearch::unserialize($sFilter);
|
||||
}
|
||||
$iStart = utils::ReadParam('start', 0);
|
||||
$iEnd = utils::ReadParam('end', 1);
|
||||
$iDrawNumber= utils::ReadParam('draw', 1);
|
||||
|
||||
$aSort = utils::ReadParam('order', [], false, 'array');
|
||||
if(count($aSort)>0){
|
||||
$iSortCol = $aSort[0]["column"];
|
||||
$sSortOrder = $aSort[0]["dir"];
|
||||
}
|
||||
else{
|
||||
$iSortCol = 0;
|
||||
$sSortOrder = "asc";
|
||||
}
|
||||
$sSelectMode = utils::ReadParam('select_mode', '');
|
||||
if (!empty($sSelectMode) && ($sSelectMode != 'none'))
|
||||
{
|
||||
// The first column is used for the selection (radio / checkbox) and is not sortable
|
||||
$iSortCol--;
|
||||
}
|
||||
$bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array());
|
||||
$iListId = utils::ReadParam('list_id', 0);
|
||||
|
||||
// Filter the list to removed linked set since we are not able to display them here
|
||||
$sIdName ="";
|
||||
$aOrderBy = array();
|
||||
$iSortIndex = 0;
|
||||
|
||||
$aColumnsLoad = array();
|
||||
foreach($aClassAliases as $sAlias => $sClassName)
|
||||
{
|
||||
$aColumnsLoad[$sAlias] = array();
|
||||
if (!isset($aColumns[$sAlias])) {
|
||||
continue;
|
||||
}
|
||||
foreach ($aColumns[$sAlias] as $sAttCode => $aData) {
|
||||
if ($aData['checked'] == 'true') {
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = true;
|
||||
if ($sAttCode == '_key_') {
|
||||
if ($sIdName == "") {
|
||||
$sIdName = $sAlias."/_key_";
|
||||
}
|
||||
if ($iSortCol == $iSortIndex)
|
||||
{
|
||||
if (!MetaModel::HasChildrenClasses($oFilter->GetClass()))
|
||||
{
|
||||
$aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
|
||||
if ($aNameSpec[0] == '%1$s')
|
||||
{
|
||||
// The name is made of a single column, let's sort according to the sort algorithm for this column
|
||||
$aOrderBy[$sAlias.'.'.$aNameSpec[1][0]] = ($sSortOrder == 'asc');
|
||||
}
|
||||
else
|
||||
{
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = ($sSortOrder == 'asc');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = ($sSortOrder == 'asc');
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof AttributeLinkedSet)
|
||||
{
|
||||
// Removed from the display list
|
||||
unset($aColumns[$sAlias][$sAttCode]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$aColumnsLoad[$sAlias][] = $sAttCode;
|
||||
}
|
||||
if ($iSortCol == $iSortIndex)
|
||||
{
|
||||
if ($oAttDef->IsExternalKey())
|
||||
{
|
||||
$sSortCol = $sAttCode.'_friendlyname';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sSortCol = $sAttCode;
|
||||
}
|
||||
$aOrderBy[$sAlias.'.'.$sSortCol] = ($sSortOrder == 'asc');
|
||||
}
|
||||
}
|
||||
$iSortIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
$aQueryParams = isset($aExtraParams['query_params']) ? $aExtraParams['query_params'] : [];
|
||||
|
||||
// Load only the requested columns
|
||||
$oSet = new DBObjectSet($oFilter, $aOrderBy, $aQueryParams, null, $iEnd - $iStart, $iStart);
|
||||
$oSet->OptimizeColumnLoad($aColumnsLoad);
|
||||
|
||||
if (isset($aExtraParams['show_obsolete_data'])) {
|
||||
$bShowObsoleteData = $aExtraParams['show_obsolete_data'];
|
||||
}
|
||||
else {
|
||||
$bShowObsoleteData = utils::ShowObsoleteData();
|
||||
}
|
||||
$oSet->SetShowObsoleteData($bShowObsoleteData);
|
||||
$oKPI = new ExecutionKPI();
|
||||
$aResult["draw"] = $iDrawNumber;
|
||||
$aResult["recordsTotal"] = $oSet->Count() ;
|
||||
$aResult["recordsFiltered"] = $oSet->Count() ;
|
||||
$aResult["data"] = [];
|
||||
while ($aObject = $oSet->FetchAssoc()) {
|
||||
foreach($aClassAliases as $sAlias=>$sClass) {
|
||||
if (isset($aColumns[$sAlias])) {
|
||||
foreach ($aColumns[$sAlias] as $sAttCode => $oAttDef) {
|
||||
if ($sAttCode == "_key_") {
|
||||
$aObj[$sAlias."/".$sAttCode] = $aObject[$sAlias]->GetKey();
|
||||
} else {
|
||||
$aObj[$sAlias."/".$sAttCode] = $aObject[$sAlias]->GetAsHTML($sAttCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if($sIdName!="")
|
||||
{
|
||||
$aObj["id" ] = $aObj[$sIdName];
|
||||
}
|
||||
array_push($aResult["data"], $aObj);
|
||||
}
|
||||
$aResult = AjaxRenderController::Search($sEncoding, $sFilter);
|
||||
$oPage->add(json_encode($aResult));
|
||||
$oKPI->ComputeAndReport('Data fetch and format');
|
||||
break;
|
||||
|
||||
case 'datatable_save_settings':
|
||||
$oPage->SetContentType('text/plain');
|
||||
$iPageSize = utils::ReadParam('page_size', 10);
|
||||
$sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
|
||||
$bSaveAsDefaults = (utils::ReadParam('defaults', 'true') == 'true');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
|
||||
foreach($aColumns as $sAlias => $aList)
|
||||
{
|
||||
foreach($aList as $sAttCode => $aData)
|
||||
{
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true');
|
||||
$aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true');
|
||||
$aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
|
||||
}
|
||||
}
|
||||
|
||||
$oSettings = new DataTableSettings($aClassAliases, $sTableId);
|
||||
$oSettings->iDefaultPageSize = $iPageSize;
|
||||
$oSettings->aColumns = $aColumns;
|
||||
|
||||
if ($bSaveAsDefaults)
|
||||
{
|
||||
if ($sTableId != null)
|
||||
{
|
||||
$oCurrSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId, true /* bOnlyTable */);
|
||||
if ($oCurrSettings)
|
||||
{
|
||||
$oCurrSettings->ResetToDefault(false); // Reset this table to the defaults
|
||||
}
|
||||
}
|
||||
$bRet = $oSettings->SaveAsDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
$bRet = $oSettings->Save();
|
||||
}
|
||||
$bRet = AjaxRenderController::DatatableSaveSettings();
|
||||
$oPage->add($bRet ? 'Ok' : 'KO');
|
||||
break;
|
||||
|
||||
case 'datatable_reset_settings':
|
||||
$oPage->SetContentType('text/plain');
|
||||
$sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
|
||||
$bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
|
||||
|
||||
$oSettings = new DataTableSettings($aClassAliases, $sTableId);
|
||||
$bRet = $oSettings->ResetToDefault($bResetAll);
|
||||
$bRet = AjaxRenderController::DatatableResetSettings();
|
||||
$oPage->add($bRet ? 'Ok' : 'KO');
|
||||
break;
|
||||
|
||||
@@ -1305,7 +942,8 @@ try
|
||||
}
|
||||
$oDashboard->Render($oPage, false, $aExtraParams);
|
||||
}
|
||||
$oPage->add_ready_script("$('.ibo-dashboard table.listResults').tableHover(); $('.ibo-dashboard table.listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
//$oPage->add_ready_script("$('.ibo-dashboard table.listResults').tableHover(); $('.ibo-dashboard table.listResults')
|
||||
//.tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
break;
|
||||
|
||||
case 'reload_dashboard':
|
||||
@@ -1324,7 +962,8 @@ try
|
||||
}
|
||||
$oDashboard->Render($oPage, false, $aExtraParams);
|
||||
}
|
||||
$oPage->add_ready_script("$('.ibo-dashboard table.listResults').tableHover(); $('.ibo-dashboard table.listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
//$oPage->add_ready_script("$('.ibo-dashboard table.listResults').tableHover(); $('.ibo-dashboard table.listResults')
|
||||
//.tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
break;
|
||||
|
||||
case 'save_dashboard':
|
||||
@@ -1832,7 +1471,8 @@ JS
|
||||
$oObj = MetaModel::GetObject($sClass, $id);
|
||||
$oObj->DisplayBareHistory($oPage, false, $iCount, $iStart);
|
||||
$oKPI->ComputeAndReport('Data fetch and format');
|
||||
$oPage->add_ready_script("$('#history table.listResults').tableHover(); $('#history table.listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
//$oPage->add_ready_script("$('#history table.listResults').tableHover(); $('#history table.listResults').tablesorter( {
|
||||
// widgets: ['myZebra', 'truncatedList']} );");
|
||||
break;
|
||||
|
||||
// TODO 3.0.0: What to do with this?
|
||||
@@ -1846,7 +1486,8 @@ JS
|
||||
$oBlock->SetLimit($iCount, $iStart);
|
||||
$oBlock->Display($oPage, 'history');
|
||||
$oKPI->ComputeAndReport('Data fetch and format');
|
||||
$oPage->add_ready_script("$('#history table.listResults').tableHover(); $('#history table.listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} );");
|
||||
//$oPage->add_ready_script("$('#history table.listResults').tableHover(); $('#history table.listResults').tablesorter( {
|
||||
// widgets: ['myZebra', 'truncatedList']} );");
|
||||
break;
|
||||
|
||||
case 'full_text_search':
|
||||
|
||||
@@ -8,12 +8,18 @@ namespace Combodo\iTop\Controller;
|
||||
|
||||
use AjaxPage;
|
||||
use ApplicationMenu;
|
||||
use AttributeLinkedSet;
|
||||
use BulkExport;
|
||||
use BulkExportException;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableFactory;
|
||||
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableSettings;
|
||||
use DBObjectSearch;
|
||||
use DBObjectSet;
|
||||
use DBSearch;
|
||||
use Dict;
|
||||
use Exception;
|
||||
use ExecutionKPI;
|
||||
use MetaModel;
|
||||
use utils;
|
||||
|
||||
class AjaxRenderController
|
||||
@@ -129,4 +135,236 @@ class AjaxRenderController
|
||||
$aResult = ['code' => 'done', 'counts' => $aCounts];
|
||||
$oPage->add(json_encode($aResult));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sFilter
|
||||
*
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function SearchAndRefresh($sFilter): array
|
||||
{
|
||||
$extraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
|
||||
$aExtraParams = array();
|
||||
if (is_array($extraParams)) {
|
||||
$aExtraParams = $extraParams;
|
||||
} else {
|
||||
$sExtraParams = stripslashes($extraParams);
|
||||
if (!empty($sExtraParams)) {
|
||||
$val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
|
||||
if ($val !== null) {
|
||||
$aExtraParams = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
$iLength = utils::ReadParam('end', 10);
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
$sSelectMode = utils::ReadParam('select_mode', '');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array());
|
||||
$aResult = DataTableFactory::GetOptionsForRendering($aColumns, $sSelectMode, $sFilter, $iLength, $aClassAliases, $aExtraParams);
|
||||
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sEncoding
|
||||
* @param $sFilter
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \DictExceptionMissingString
|
||||
* @throws \MissingQueryArgument
|
||||
* @throws \MySQLException
|
||||
* @throws \MySQLHasGoneAwayException
|
||||
* @throws \OQLException
|
||||
*/
|
||||
public static function Search($sEncoding, $sFilter)
|
||||
{
|
||||
$extraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
|
||||
$aExtraParams = array();
|
||||
if (is_array($extraParams)) {
|
||||
$aExtraParams = $extraParams;
|
||||
} else {
|
||||
$sExtraParams = stripslashes($extraParams);
|
||||
if (!empty($sExtraParams)) {
|
||||
$val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
|
||||
if ($val !== null) {
|
||||
$aExtraParams = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($sEncoding == 'oql') {
|
||||
$oFilter = DBSearch::FromOQL($sFilter);
|
||||
} else {
|
||||
$oFilter = DBSearch::unserialize($sFilter);
|
||||
}
|
||||
$iStart = utils::ReadParam('start', 0);
|
||||
$iEnd = utils::ReadParam('end', 1);
|
||||
$iDrawNumber = utils::ReadParam('draw', 1);
|
||||
|
||||
$aSort = utils::ReadParam('order', [], false, 'array');
|
||||
if (count($aSort) > 0) {
|
||||
$iSortCol = $aSort[0]["column"];
|
||||
$sSortOrder = $aSort[0]["dir"];
|
||||
} else {
|
||||
$iSortCol = 0;
|
||||
$sSortOrder = "asc";
|
||||
}
|
||||
$sSelectMode = utils::ReadParam('select_mode', '');
|
||||
if (!empty($sSelectMode) && ($sSelectMode != 'none')) {
|
||||
// The first column is used for the selection (radio / checkbox) and is not sortable
|
||||
$iSortCol--;
|
||||
}
|
||||
$bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array());
|
||||
$iListId = utils::ReadParam('list_id', 0);
|
||||
|
||||
// Filter the list to removed linked set since we are not able to display them here
|
||||
$sIdName = "";
|
||||
$aOrderBy = array();
|
||||
$iSortIndex = 0;
|
||||
|
||||
$aColumnsLoad = array();
|
||||
foreach ($aClassAliases as $sAlias => $sClassName) {
|
||||
$aColumnsLoad[$sAlias] = array();
|
||||
if (!isset($aColumns[$sAlias])) {
|
||||
continue;
|
||||
}
|
||||
foreach ($aColumns[$sAlias] as $sAttCode => $aData) {
|
||||
if ($aData['checked'] == 'true') {
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = true;
|
||||
if ($sAttCode == '_key_') {
|
||||
if ($sIdName == "") {
|
||||
$sIdName = $sAlias."/_key_";
|
||||
}
|
||||
if ($iSortCol == $iSortIndex) {
|
||||
if (!MetaModel::HasChildrenClasses($oFilter->GetClass())) {
|
||||
$aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
|
||||
if ($aNameSpec[0] == '%1$s') {
|
||||
// The name is made of a single column, let's sort according to the sort algorithm for this column
|
||||
$aOrderBy[$sAlias.'.'.$aNameSpec[1][0]] = ($sSortOrder == 'asc');
|
||||
} else {
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = ($sSortOrder == 'asc');
|
||||
}
|
||||
} else {
|
||||
$aOrderBy[$sAlias.'.'.'friendlyname'] = ($sSortOrder == 'asc');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
|
||||
if ($oAttDef instanceof AttributeLinkedSet) {
|
||||
// Removed from the display list
|
||||
unset($aColumns[$sAlias][$sAttCode]);
|
||||
} else {
|
||||
$aColumnsLoad[$sAlias][] = $sAttCode;
|
||||
}
|
||||
if ($iSortCol == $iSortIndex) {
|
||||
if ($oAttDef->IsExternalKey()) {
|
||||
$sSortCol = $sAttCode.'_friendlyname';
|
||||
} else {
|
||||
$sSortCol = $sAttCode;
|
||||
}
|
||||
$aOrderBy[$sAlias.'.'.$sSortCol] = ($sSortOrder == 'asc');
|
||||
}
|
||||
}
|
||||
$iSortIndex++;
|
||||
} else {
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
$aQueryParams = isset($aExtraParams['query_params']) ? $aExtraParams['query_params'] : [];
|
||||
|
||||
// Load only the requested columns
|
||||
$oSet = new DBObjectSet($oFilter, $aOrderBy, $aQueryParams, null, $iEnd - $iStart, $iStart);
|
||||
$oSet->OptimizeColumnLoad($aColumnsLoad);
|
||||
|
||||
if (isset($aExtraParams['show_obsolete_data'])) {
|
||||
$bShowObsoleteData = $aExtraParams['show_obsolete_data'];
|
||||
} else {
|
||||
$bShowObsoleteData = utils::ShowObsoleteData();
|
||||
}
|
||||
$oSet->SetShowObsoleteData($bShowObsoleteData);
|
||||
$oKPI = new ExecutionKPI();
|
||||
$aResult["draw"] = $iDrawNumber;
|
||||
$aResult["recordsTotal"] = $oSet->Count();
|
||||
$aResult["recordsFiltered"] = $oSet->Count();
|
||||
$aResult["data"] = [];
|
||||
while ($aObject = $oSet->FetchAssoc()) {
|
||||
foreach ($aClassAliases as $sAlias => $sClass) {
|
||||
if (isset($aColumns[$sAlias])) {
|
||||
foreach ($aColumns[$sAlias] as $sAttCode => $oAttDef) {
|
||||
if ($sAttCode == "_key_") {
|
||||
$aObj[$sAlias."/".$sAttCode] = $aObject[$sAlias]->GetKey();
|
||||
} else {
|
||||
$aObj[$sAlias."/".$sAttCode] = $aObject[$sAlias]->GetAsHTML($sAttCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($sIdName != "") {
|
||||
$aObj["id"] = $aObj[$sIdName];
|
||||
}
|
||||
array_push($aResult["data"], $aObj);
|
||||
}
|
||||
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function DatatableSaveSettings(): bool
|
||||
{
|
||||
$iPageSize = utils::ReadParam('page_size', 10);
|
||||
$sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
|
||||
$bSaveAsDefaults = (utils::ReadParam('defaults', 'true') == 'true');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
|
||||
$aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
|
||||
|
||||
foreach ($aColumns as $sAlias => $aList) {
|
||||
foreach ($aList as $sAttCode => $aData) {
|
||||
$aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true');
|
||||
$aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true');
|
||||
$aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
|
||||
}
|
||||
}
|
||||
|
||||
$oSettings = new DataTableSettings($aClassAliases, $sTableId);
|
||||
$oSettings->iDefaultPageSize = $iPageSize;
|
||||
$oSettings->aColumns = $aColumns;
|
||||
|
||||
if ($bSaveAsDefaults) {
|
||||
if ($sTableId != null) {
|
||||
$oCurrSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId, true /* bOnlyTable */);
|
||||
if ($oCurrSettings) {
|
||||
$oCurrSettings->ResetToDefault(false); // Reset this table to the defaults
|
||||
}
|
||||
}
|
||||
$bRet = $oSettings->SaveAsDefault();
|
||||
} else {
|
||||
$bRet = $oSettings->Save();
|
||||
}
|
||||
|
||||
return $bRet;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function DatatableResetSettings(): bool
|
||||
{
|
||||
$sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
|
||||
$aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
|
||||
$bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
|
||||
|
||||
$oSettings = new DataTableSettings($aClassAliases, $sTableId);
|
||||
$bRet = $oSettings->ResetToDefault($bResetAll);
|
||||
|
||||
return $bRet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user