Skip to content

Commit

Permalink
compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ericges committed Mar 4, 2024
1 parent 0adfc1a commit 7e56170
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
51 changes: 27 additions & 24 deletions src/Widget/ListWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Contao\System;
use Contao\Template;
use Contao\Widget;
use HeimrichHannot\AjaxBundle\Exception\AjaxExitException;
use HeimrichHannot\AjaxBundle\Response\ResponseData;
use HeimrichHannot\AjaxBundle\Response\ResponseSuccess;
use HeimrichHannot\UtilsBundle\Util\ArrayUtil;
use HeimrichHannot\UtilsBundle\Util\Utils;

class ListWidget extends Widget
Expand All @@ -37,6 +39,7 @@ public function __construct($arrData)
* Generate the widget and return it as string
*
* @return string
* @throws AjaxExitException
*/
public function generate(): string
{
Expand All @@ -55,7 +58,6 @@ public function generate(): string
$this,
$this->objDca
);

}

static::addToTemplate($objTemplate, $arrConfig);
Expand All @@ -74,7 +76,7 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
$arrConfig = $arrConfig ?: [];

// header
$arrConfig['headerFields'] = Polyfill::utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction($arrConfig, 'header_fields', [$arrConfig, $objContext, $objDca]);
$arrConfig['headerFields'] = Polyfill::getConfigByArrayOrCallbackOrFunction($arrConfig, 'header_fields', [$arrConfig, $objContext, $objDca]);

if ($arrConfig['useDbAsHeader'] && $arrConfig['table']) {
$strTable = $arrConfig['table'];
Expand All @@ -86,19 +88,19 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
continue;
}

$arrHeaderFields[$strField] = Polyfill::utilsV2_dcaUtil_getLocalizedFieldName($strField, $strTable);
$arrHeaderFields[$strField] = Polyfill::getLocalizedFieldName($strField, $strTable);
}

$arrConfig['headerFields'] = $arrHeaderFields;
}

if (!$arrConfig['ajax']) {
$arrConfig['items'] = Polyfill::utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction($arrConfig, 'items', [$arrConfig, $objContext, $objDca]);
$arrConfig['items'] = Polyfill::getConfigByArrayOrCallbackOrFunction($arrConfig, 'items', [$arrConfig, $objContext, $objDca]);
}

$arrConfig['language'] = Polyfill::utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction($arrConfig, 'language', [$arrConfig, $objContext, $objDca]);
$arrConfig['language'] = Polyfill::getConfigByArrayOrCallbackOrFunction($arrConfig, 'language', [$arrConfig, $objContext, $objDca]);

$arrConfig['columns'] = Polyfill::utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction($arrConfig, 'columns', [$arrConfig, $objContext, $objDca]);
$arrConfig['columns'] = Polyfill::getConfigByArrayOrCallbackOrFunction($arrConfig, 'columns', [$arrConfig, $objContext, $objDca]);

// prepare columns -> if not specified, get it from header fields
if (!$arrConfig['columns']) {
Expand All @@ -123,10 +125,12 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
return $arrConfig;
}

/**
* @throws AjaxExitException
*/
public static function initAjaxLoading(array $arrConfig, $objContext = null, $objDc = null): void
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
$dcaUtil = System::getContainer()->get('huh.utils.dca');

if ($request->headers->get('X-Requested-With') !== 'XMLHttpRequest') {
return;
Expand All @@ -149,7 +153,7 @@ public static function initAjaxLoading(array $arrConfig, $objContext = null, $ob
};
}

$strResult = $dcaUtil->getConfigByArrayOrCallbackOrFunction(
$strResult = Polyfill::getConfigByArrayOrCallbackOrFunction(
$arrConfig['ajaxConfig'],
'load_items',
[$arrConfig, [], $objContext, $objDc]
Expand Down Expand Up @@ -219,8 +223,8 @@ public static function loadItems($arrConfig, $arrOptions = [], $objContext = nul
$arrResponse = [];
$getDraw = Input::get('draw');
$arrResponse['draw'] = ($getDraw && is_string($getDraw)) ? intval($getDraw) : 0;
$arrResponse['recordsTotal'] = intval(static::countTotal($arrOptions));
$arrResponse['recordsFiltered'] = intval(static::countFiltered($arrOptions));
$arrResponse['recordsTotal'] = static::countTotal($arrOptions);
$arrResponse['recordsFiltered'] = static::countFiltered($arrOptions);

// prepare
if (!isset($arrConfig['ajaxConfig']['prepare_items_callback'])) {
Expand All @@ -229,7 +233,7 @@ public static function loadItems($arrConfig, $arrOptions = [], $objContext = nul
};
}

$arrResponse['data'] = System::getContainer()->get('huh.utils.dca')->getConfigByArrayOrCallbackOrFunction(
$arrResponse['data'] = Polyfill::getConfigByArrayOrCallbackOrFunction(
$arrConfig['ajaxConfig'],
'prepare_items',
[
Expand Down Expand Up @@ -268,14 +272,13 @@ protected static function prepareItems($objItems, $arrConfig, $arrOptions = [],
return $arrItems;
}

protected static function getColumnDefsData($arrColumns)
protected static function getColumnDefsData($arrColumns): array
{
$arrayUtil = System::getContainer()->get('huh.utils.array');
$prefixes = ['searchable', 'className', 'orderable', 'type'];
$arrConfig = [];

foreach ($arrColumns as $i => $arrColumn) {
foreach ($arrColumns as $arrColumn) {
$arrConfig[] = array_merge(
$arrayUtil->filterByPrefixes($arrColumn, ['searchable', 'className', 'orderable', 'type']),
Polyfill::filterByPrefixes($arrColumn, $prefixes),
['targets' => $arrColumn['dt']],
['render' => ['_' => 'value']]
);
Expand Down Expand Up @@ -337,32 +340,32 @@ protected static function countFiltered(array $options): int
/**
* Fetch the matching items
*
* @param array $arrOptions SQL options
* @param array $arrOptions SQL options
*
* @return array Server-side processing response array
*/
protected static function fetchItems(&$arrOptions = [])
protected static function fetchItems(array &$arrOptions = []): array
{
$arrOptions = static::limitSQL($arrOptions);
$arrOptions = static::filterSQL($arrOptions);
$arrOptions = static::orderSQL($arrOptions);

/** @var class-string<Model> $modelClass */
$strModel = Model::getClassFromTable($arrOptions['table']);
$modelClass = Model::getClassFromTable($arrOptions['table']);

return $strModel::findAll($arrOptions);
return $modelClass::findAll($arrOptions);
}

/**
* Paging
*
* Construct the LIMIT clause for server-side processing SQL query
*
* @param array $arrOptions SQL options
* @param array $arrOptions SQL options
*
* @return array The $arrOptions filled with limit clause
*/
protected static function limitSQL($arrOptions)
protected static function limitSQL(array $arrOptions): array
{
$start = Input::get('start');
$length = Input::get('length');
Expand All @@ -383,11 +386,11 @@ protected static function limitSQL($arrOptions)
* word by word on any field. It's possible to do here performance on large
* databases would be very poor
*
* @param array $arrOptions SQL options
* @param array $arrOptions SQL options
*
* @return array The $arrOptions filled with where conditions (values and columns)
*/
protected static function filterSQL($arrOptions)
protected static function filterSQL(array $arrOptions): array
{
$t = $arrOptions['table'];

Expand Down
46 changes: 37 additions & 9 deletions src/Widget/Polyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Contao\Controller;
use Contao\System;
use Error;
use Exception;

class Polyfill
{
Expand All @@ -13,8 +15,10 @@ class Polyfill
* 1. The value associated to $array[$property]
* 2. The value retrieved by $array[$property . '_callback'] which is a callback array like ['Class', 'method'] or ['service.id', 'method']
* 3. The value retrieved by $array[$property . '_callback'] which is a function closure array like ['Class', 'method']
*
* @internal This is a polyfill for DcaUtil::getConfigByArrayOrCallbackOrFunction of Utils v2
*/
public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(array $array, $property, array $arguments = []): mixed
public static function getConfigByArrayOrCallbackOrFunction(array $array, $property, array $arguments = []): mixed
{
if (isset($array[$property])) {
return $array[$property];
Expand All @@ -24,7 +28,7 @@ public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(arra
return null;
}

if (\is_array($array[$property.'_callback'])) {
if (is_array($array[$property.'_callback'])) {
$callback = $array[$property.'_callback'];

if (!isset($callback[0]) || !isset($callback[1])) {
Expand All @@ -33,7 +37,7 @@ public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(arra

try {
$instance = Controller::importStatic($callback[0]);
} catch (\Exception $e) {
} catch (Exception) {
return null;
}

Expand All @@ -42,26 +46,50 @@ public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(arra
}

try {
return \call_user_func_array([$instance, $callback[1]], $arguments);
} catch (\Error $e) {
return call_user_func_array([$instance, $callback[1]], $arguments);
} catch (Error) {
return null;
}
} elseif (\is_callable($array[$property.'_callback'])) {
} elseif (is_callable($array[$property.'_callback'])) {
try {
return \call_user_func_array($array[$property.'_callback'], $arguments);
} catch (\Error $e) {
return call_user_func_array($array[$property.'_callback'], $arguments);
} catch (Error) {
return null;
}
}

return null;
}

public static function utilsV2_dcaUtil_getLocalizedFieldName(string $strField, string $strTable): mixed
public static function getLocalizedFieldName(string $strField, string $strTable): mixed
{
Controller::loadDataContainer($strTable);
System::loadLanguageFile($strTable);

return ($GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['label'][0] ?? $strField) ?: $strField;
}

/**
* Filter an Array by given prefixes.
*
* @internal Polyfill for ArrayUtil::filterByPrefixes of Utils v2
*/
public static function filterByPrefixes(array $data = [], array $prefixes = []): array
{
$extract = [];

if (!is_array($prefixes) || empty($prefixes)) {
return $data;
}

foreach ($data as $key => $value) {
foreach ($prefixes as $prefix) {
if (str_starts_with($key, $prefix)) {
$extract[$key] = $value;
}
}
}

return $extract;
}
}

0 comments on commit 7e56170

Please sign in to comment.