From 7e561707cd59098c9d6c0562c786b9cda54809e9 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Mon, 4 Mar 2024 16:49:05 +0100 Subject: [PATCH] compatibility --- src/Widget/ListWidget.php | 51 +++++++++++++++++++++------------------ src/Widget/Polyfill.php | 46 ++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/Widget/ListWidget.php b/src/Widget/ListWidget.php index 5bbcdfb..50db5c7 100644 --- a/src/Widget/ListWidget.php +++ b/src/Widget/ListWidget.php @@ -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 @@ -37,6 +39,7 @@ public function __construct($arrData) * Generate the widget and return it as string * * @return string + * @throws AjaxExitException */ public function generate(): string { @@ -55,7 +58,6 @@ public function generate(): string $this, $this->objDca ); - } static::addToTemplate($objTemplate, $arrConfig); @@ -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']; @@ -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']) { @@ -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; @@ -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] @@ -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'])) { @@ -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', [ @@ -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']] ); @@ -337,20 +340,20 @@ 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 $modelClass */ - $strModel = Model::getClassFromTable($arrOptions['table']); + $modelClass = Model::getClassFromTable($arrOptions['table']); - return $strModel::findAll($arrOptions); + return $modelClass::findAll($arrOptions); } /** @@ -358,11 +361,11 @@ protected static function fetchItems(&$arrOptions = []) * * 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'); @@ -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']; diff --git a/src/Widget/Polyfill.php b/src/Widget/Polyfill.php index f6e51e8..bebb2f8 100644 --- a/src/Widget/Polyfill.php +++ b/src/Widget/Polyfill.php @@ -4,6 +4,8 @@ use Contao\Controller; use Contao\System; +use Error; +use Exception; class Polyfill { @@ -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]; @@ -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])) { @@ -33,7 +37,7 @@ public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(arra try { $instance = Controller::importStatic($callback[0]); - } catch (\Exception $e) { + } catch (Exception) { return null; } @@ -42,14 +46,14 @@ 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; } } @@ -57,11 +61,35 @@ public static function utilsV2_dcaUtil_getConfigByArrayOrCallbackOrFunction(arra 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; + } } \ No newline at end of file