Skip to content

Commit

Permalink
Datatables v2.1 (#801)
Browse files Browse the repository at this point in the history
* reorganize code

* +searchBuilder

* +SearchBuilder

* fix searchBuilder

* fix searchBuilder

* add object_hash

* Add files via upload

* fix use DIWikiPage

* fix position sticky

* fix searchBuilderType

* add datatables.mark

* add datatables.mark

* add mark, spinner, fix keyword

* add mark, spinner

* fix context

* fix test

* fix spinner hide

* prevents double spinner

* fix regex

* fix delimiter

---------

Co-authored-by: Bernhard Krabina <[email protected]>
  • Loading branch information
thomas-topway-it and krabina authored Mar 30, 2024
1 parent 6b4c2f2 commit 6dd7e46
Show file tree
Hide file tree
Showing 11 changed files with 1,223 additions and 965 deletions.
4 changes: 4 additions & 0 deletions Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,10 +1017,14 @@

'ext.srf.datatables.v2.module' => $moduleTemplate + [
'scripts' => [
'resources/jquery/datatables/object_hash.js',
'resources/jquery/datatables/jquery.mark.min.js',
'resources/jquery/datatables/datatables.mark.min.js',
'resources/jquery/datatables/datatables.min.js',
'resources/jquery/datatables/jquery.dataTables.extras.js',
],
'styles' => [
'resources/jquery/datatables/datatables.mark.min.css',
'resources/jquery/datatables/datatables.min.css',
]
],
Expand Down
127 changes: 77 additions & 50 deletions formats/datatables/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public function execute() {
// get request parameters
$requestParams = $this->extractRequestParams();

$data = json_decode( $requestParams['data'], true );

// @see https://datatables.net/reference/option/ajax
$datatableData = json_decode( $requestParams['datatable'], true );
$settings = json_decode( $requestParams['settings'], true );
$datatableData = $data['datatableData'];
$settings = $data['settings'];

if ( empty( $datatableData['length'] ) ) {
$datatableData['length'] = $settings['defer-each'];
Expand All @@ -59,15 +61,14 @@ public function execute() {
$parameters[$def->getName()] = $def->getDefault();
}

$printoutsRaw = json_decode( $requestParams['printouts'], true );
$printoutsRaw = $data['printouts'];

// add/set specific parameters for this call
$parameters = array_merge(
$parameters,
[
// *** important !!
'format' => 'datatables',

"apicall" => "apicall",
// @see https://datatables.net/manual/server-side
// array length will be sliced client side if greater
Expand Down Expand Up @@ -104,10 +105,10 @@ public function execute() {
foreach ( $printoutsRaw as $printoutData ) {

// create property from property key
if ( $printoutData[0] === SMWPrintRequest::PRINT_PROP ) {
$data = $dataValueFactory->newPropertyValueByLabel( $printoutData[1] );
if ( $printoutData[0] === SMWPrintRequest::PRINT_PROP ) {
$data_ = $dataValueFactory->newPropertyValueByLabel( $printoutData[1] );
} else {
$data = null;
$data_ = null;
if ( $hasMainlabel && trim( $parameters['mainlabel'] ) === '-' ) {
continue;
}
Expand All @@ -118,7 +119,7 @@ public function execute() {
$printouts[] = new SMWPrintRequest(
$printoutData[0], // mode
$printoutData[1], // (canonical) label
$data, // property name
$data_, // property name
$printoutData[3], // output format
$printoutData[4] // parameters
);
Expand All @@ -127,8 +128,8 @@ public function execute() {

// SMWQueryProcessor::addThisPrintout( $printouts, $parameters );

$printrequests = json_decode( $requestParams['printrequests'], true );
$columnDefs = json_decode( $requestParams['columndefs'], true );
$printrequests = $data['printrequests'];
$columnDefs = $data['columnDefs'];

$getColumnAttribute = function( $label, $attr ) use( $columnDefs ) {
foreach ( $columnDefs as $value ) {
Expand Down Expand Up @@ -171,13 +172,74 @@ public function execute() {
}
}

// @see https://datatables.net/extensions/searchbuilder/customConditions.html
// @see https://datatables.net/reference/option/searchBuilder.depthLimit
if ( !empty( $datatableData['searchBuilder'] ) ) {
$searchBuilder = [];
foreach ( $datatableData['searchBuilder']['criteria'] as $criteria ) {
foreach ( $printoutsRaw as $key => $value ) {
// @FIXME $label isn't simply $value[1] ?
$printrequest = $printrequests[$key];
$label = ( $printrequest['key'] !== '' ? $value[1] : '' );
if ( $label === $criteria['data'] ) {

// nested condition, skip for now
if ( !array_key_exists( 'condition', $criteria ) ) {
continue;
}
$v = implode( $criteria['value'] );
$str = ( $label !== '' ? "$label::" : '' );
switch( $criteria['condition'] ) {
case '=':
$searchBuilder[] = "[[{$str}{$v}]]";
break;
case '!=':
$searchBuilder[] = "[[{$str}!~$v]]";
break;
case 'starts':
$searchBuilder[] = "[[{$str}~$v*]]";
break;
case '!starts':
$searchBuilder[] = "[[{$str}!~$v*]]";
break;
case 'contains':
$searchBuilder[] = "[[{$str}~*$v*]]";
break;
case '!contains':
$searchBuilder[] = "[[{$str}!~*$v*]]";
break;
case 'ends':
$searchBuilder[] = "[[{$str}~*$v]]";
break;
case '!ends':
$searchBuilder[] = "[[$str}!~*$v]]";
break;
// case 'null':
// break;
case '!null':
if ( $label ) {
$searchBuilder[] = "[[$label::+]]";
}
break;

}
}
}
}
if ( $datatableData['searchBuilder']['logic'] === 'AND' ) {
$queryConjunction = array_merge( $queryConjunction, $searchBuilder );
} else if ( $datatableData['searchBuilder']['logic'] === 'OR' ) {
$queryDisjunction = array_merge( $queryDisjunction, $searchBuilder );
}
}

global $smwgQMaxSize;

if ( !count( $queryDisjunction ) ) {
$queryDisjunction = [''];
}

$query = $requestParams['query'] . implode( '', $queryConjunction );
$query = $data['queryString'] . implode( '', $queryConjunction );

$conditions = array_map( static function( $value ) use ( $query ) {
return $query . $value;
Expand All @@ -188,8 +250,6 @@ public function execute() {

$queryStr = implode( 'OR', $conditions );

// trigger_error('queryStr ' . $queryStr);

$log['queryStr '] = $queryStr;

$query = SMWQueryProcessor::createQuery(
Expand All @@ -205,7 +265,6 @@ public function execute() {
// $smwgQMaxSize = max( $smwgQMaxSize, $size );
// trigger_error('smwgQMaxSize ' . $smwgQMaxSize);


$applicationFactory = ServicesFactory::getInstance();
$queryEngine = $applicationFactory->getStore();
$results = $queryEngine->getQueryResult( $query );
Expand Down Expand Up @@ -234,6 +293,8 @@ public function execute() {
'data' => $res,
'recordsTotal' => $settings['count'],
'recordsFiltered' => $count,
'cacheKey' => $data['cacheKey'],
'datalength' => $datatableData['length']
];

if ( $settings['displayLog'] ) {
Expand Down Expand Up @@ -275,44 +336,10 @@ public function getHelpUrls() {
*/
protected function getAllowedParams() {
return [
'query' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'columndefs' => [
'data' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'printouts' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'printrequests' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'settings' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'datatable' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
];
}

/**
* Returns an array of parameter descriptions.
* Don't call this function directly: use getFinalParamDescription() to
* allow hooks to modify descriptions as needed.
*
* @return array|bool False on no parameter descriptions
*/
protected function getParamDescription() {
return [
'query' => 'Original query',
'printouts' => 'Printouts used in the original query',
]
];
}

Expand Down
Loading

0 comments on commit 6dd7e46

Please sign in to comment.