Skip to content

Commit

Permalink
run ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 5, 2024
1 parent f7b2f7f commit 66f0027
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
5 changes: 1 addition & 4 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
*/


namespace HeimrichHannot\ListWidgetBundle\ContaoManager;


use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
Expand All @@ -20,12 +18,11 @@

class Plugin implements BundlePluginInterface
{

public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(HeimrichHannotListWidgetBundle::class)->setLoadAfter([
ContaoCoreBundle::class
ContaoCoreBundle::class,
]),
];
}
Expand Down
11 changes: 7 additions & 4 deletions src/Controller/ListWidgetContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function __construct(
public readonly string $id,
public readonly array $fieldConfig,
public readonly Request $request,
)
{
) {
}

public static function createFromRequest(Request $request): ListWidgetContext
Expand Down Expand Up @@ -64,15 +63,19 @@ public function applyListConfigToCriteria(Criteria $criteria, array $fields): vo

if (!empty($this->request->query->get('order'))) {
foreach ($this->request->query->get('order') as $order) {
$criteria->orderBy([$fields[$order['column']] => $order['dir']]);
$criteria->orderBy([
$fields[$order['column']] => $order['dir'],
]);
}
}
}

public function adjustDataResultStructure(array &$data): void
{
array_walk($data, function (&$date) {
$date = array_map(fn($value) => ['value' => $value], array_values($date));
$date = array_map(fn($value) => [
'value' => $value,
], array_values($date));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ListWidgetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(int $draw, int $recordsTotal, int $recordsFiltered,
'recordsFiltered' => $recordsFiltered,
'data' => $data,
],
]
],
];

if ($error) {
Expand Down
3 changes: 0 additions & 3 deletions src/HeimrichHannotListWidgetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
*/


namespace HeimrichHannot\ListWidgetBundle;


use Symfony\Component\HttpKernel\Bundle\Bundle;

class HeimrichHannotListWidgetBundle extends Bundle
Expand All @@ -20,5 +18,4 @@ public function getPath()
{
return \dirname(__DIR__);
}

}
96 changes: 49 additions & 47 deletions src/Widget/ListWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace HeimrichHannot\ListWidgetBundle\Widget;


use Contao\BackendTemplate;
use Contao\Controller;
use Contao\Database;
use Contao\Input;
use Contao\Model;
use Contao\RequestToken;
use Contao\System;
use Contao\Template;
use Contao\Widget;
Expand All @@ -23,9 +20,13 @@ class ListWidget extends Widget
public const LOAD_ACTION = 'list-load';

protected $blnForAttribute = true;

protected $strTemplate = 'be_widget';

protected $strListTemplate = 'list_widget';

protected $arrDca;

protected $arrWidgetErrors = [];

protected static $arrSkipFields = ['id', 'tstamp', 'pid', 'dateAdded'];
Expand All @@ -38,7 +39,6 @@ public function __construct($arrData)
parent::__construct($arrData);
}


/**
* Generate the widget and return it as string
*
Expand All @@ -65,7 +65,7 @@ public function generate()
}

$arrConfig['ptable'] = $this->strTable;
$arrConfig['pid'] = $this->objDca->id;
$arrConfig['pid'] = $this->objDca->id;
static::addToTemplate($objTemplate, $arrConfig);

return $objTemplate->parse();
Expand All @@ -79,16 +79,16 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
'ajax' => false,
], $arrConfig);

$dcaUtil = System::getContainer()->get('huh.utils.dca');
$dcaUtil = System::getContainer()->get('huh.utils.dca');

$arrConfig = $arrConfig ?: [];

// header
$arrConfig['headerFields'] = $dcaUtil->getConfigByArrayOrCallbackOrFunction($arrConfig, 'header_fields', [$arrConfig, $objContext, $objDca]);

if ($arrConfig['useDbAsHeader'] && $arrConfig['table']) {
$strTable = $arrConfig['table'];
$arrFields = Database::getInstance()->getFieldNames($strTable, true);
$strTable = $arrConfig['table'];
$arrFields = Database::getInstance()->getFieldNames($strTable, true);
$arrHeaderFields = [];

foreach ($arrFields as $strField) {
Expand All @@ -114,15 +114,15 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
if (!$arrConfig['columns']) {
if (is_array($arrConfig['headerFields'])) {
$arrColumns = [];
$i = 0;
$i = 0;

foreach ($arrConfig['headerFields'] as $strField => $strLabel) {
$arrColumns[] = [
'name' => $strLabel,
'db' => $strField,
'dt' => $i++,
'name' => $strLabel,
'db' => $strField,
'dt' => $i++,
'searchable' => true,
'className' => is_numeric($strField) ? 'col_' . $strField : $strField,
'className' => is_numeric($strField) ? 'col_' . $strField : $strField,
];
}

Expand All @@ -133,7 +133,6 @@ public static function prepareConfig($arrConfig = [], $objContext = null, $objDc
return $arrConfig;
}


public static function initAjaxLoading(array $arrConfig, $objContext = null, $objDc = null): void
{
$request = System::getContainer()->get('huh.request');
Expand Down Expand Up @@ -179,11 +178,11 @@ public static function addToTemplate(Template $objTemplate, array $configuration
'language' => '',
], $configuration);

$objTemplate->class = $configuration['class'];
$objTemplate->ajax = $configuration['ajax'];
$objTemplate->class = $configuration['class'];
$objTemplate->ajax = $configuration['ajax'];
$objTemplate->headerFields = $configuration['headerFields'];
$objTemplate->columnDefs = htmlentities(json_encode(static::getColumnDefsData($configuration['columns'])));
$objTemplate->language = htmlentities(json_encode($configuration['language']));
$objTemplate->columnDefs = htmlentities(json_encode(static::getColumnDefsData($configuration['columns'])));
$objTemplate->language = htmlentities(json_encode($configuration['language']));

if ($configuration['ajax'] ?? false) {

Expand All @@ -198,20 +197,21 @@ public static function addToTemplate(Template $objTemplate, array $configuration
);
} else {
$objTemplate->processingAction = System::getContainer()->get(Utils::class)->url()->addQueryStringParameterToUrl(
'key=' . static::LOAD_ACTION . '&scope=' . $configuration['identifier'] . '&rt=' . \Contao\System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue());
'key=' . static::LOAD_ACTION . '&scope=' . $configuration['identifier'] . '&rt=' . \Contao\System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue()
);
}
} else {
$objTemplate->items = $configuration['items'];
}

if ($configuration['loadAssets']) {
$GLOBALS['TL_JAVASCRIPT']['datatables-i18n'] = 'assets/datatables-additional/datatables-i18n/datatables-i18n.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-core'] = 'assets/datatables/datatables/media/js/jquery.dataTables.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-i18n'] = 'assets/datatables-additional/datatables-i18n/datatables-i18n.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-core'] = 'assets/datatables/datatables/media/js/jquery.dataTables.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-rowReorder'] = 'assets/datatables-additional/datatables-RowReorder/js/dataTables.rowReorder.min.js';

$GLOBALS['TL_JAVASCRIPT']['jquery.list_widget.js'] = 'bundles/heimrichhannotlistwidget/assets/js/jquery.list_widget.js';

$GLOBALS['TL_CSS']['datatables-core'] = 'assets/datatables-additional/datatables.net-dt/css/jquery.dataTables.min.css';
$GLOBALS['TL_CSS']['datatables-core'] = 'assets/datatables-additional/datatables.net-dt/css/jquery.dataTables.min.css';
$GLOBALS['TL_CSS']['datatables-rowReorder'] = 'assets/datatables-additional/datatables-RowReorder/css/rowReorder.dataTables.min.css';
}
}
Expand All @@ -223,14 +223,14 @@ public static function loadItems($arrConfig, $arrOptions = [], $objContext = nul
$arrOptions = !empty($arrOptions)
? $arrOptions
: [
'table' => $arrConfig['table'],
'table' => $arrConfig['table'],
'columns' => $arrConfig['columns'],
];

$objItems = static::fetchItems($arrOptions);
$arrResponse = [];
$arrResponse['draw'] = $request->hasGet('draw') ? intval($request->getGet('draw')) : 0;
$arrResponse['recordsTotal'] = intval(static::countTotal($arrOptions));
$objItems = static::fetchItems($arrOptions);
$arrResponse = [];
$arrResponse['draw'] = $request->hasGet('draw') ? intval($request->getGet('draw')) : 0;
$arrResponse['recordsTotal'] = intval(static::countTotal($arrOptions));
$arrResponse['recordsFiltered'] = intval(static::countFiltered($arrOptions));

// prepare
Expand Down Expand Up @@ -285,8 +285,14 @@ protected static function getColumnDefsData($arrColumns)
foreach ($arrColumns as $i => $arrColumn) {
$arrConfig[] = array_merge(
$arrayUtil->filterByPrefixes($arrColumn, ['searchable', 'className', 'orderable', 'type']),
['targets' => $arrColumn['dt']],
['render' => ['_' => 'value']]
[
'targets' => $arrColumn['dt'],
],
[
'render' => [
'_' => 'value',
],
]
);
}

Expand All @@ -295,9 +301,6 @@ protected static function getColumnDefsData($arrColumns)

/**
* Count the total matching items
*
* @param array $options
* @return int
*/
protected static function countTotal(array $options): int
{
Expand Down Expand Up @@ -372,7 +375,7 @@ protected static function limitSQL($arrOptions)
{
$request = System::getContainer()->get('huh.request');
if ($request->hasGet('start') && $request->getGet('length') != -1) {
$arrOptions['limit'] = $request->getGet('length');
$arrOptions['limit'] = $request->getGet('length');
$arrOptions['offset'] = $request->getGet('start');
}

Expand All @@ -398,18 +401,18 @@ protected static function filterSQL($arrOptions)

$t = $arrOptions['table'];

$columns = $arrOptions['columns'];
$columns = $arrOptions['columns'];
$globalSearch = [];
$columnSearch = [];
$dtColumns = self::pluck($columns, 'dt');
$request = $request->query->all();
$dtColumns = self::pluck($columns, 'dt');
$request = $request->query->all();

if (isset($request['search']) && $request['search']['value'] != '') {
$str = $request['search']['value'];
for ($i = 0, $ien = count($request['columns']); $i < $ien; $i++) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];

if (!$column['db']) {
continue;
Expand All @@ -424,9 +427,9 @@ protected static function filterSQL($arrOptions)
if (isset($request['columns'])) {
for ($i = 0, $ien = count($request['columns']); $i < $ien; $i++) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];
$str = $requestColumn['search']['value'];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];
$str = $requestColumn['search']['value'];

if (!($column['db'] ?? null)) {
continue;
Expand Down Expand Up @@ -477,19 +480,19 @@ protected static function orderSQL($arrOptions)
{
$request = System::getContainer()->get('huh.request');

$t = $arrOptions['table'];
$t = $arrOptions['table'];
$request = $request->query->all();
$columns = $arrOptions['columns'];

if (isset($request['order']) && count($request['order'])) {
$orderBy = [];
$orderBy = [];
$dtColumns = static::pluck($columns, 'dt');
for ($i = 0, $ien = count($request['order']); $i < $ien; $i++) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[$columnIdx];

if (!$column['db']) {
continue;
Expand All @@ -515,7 +518,6 @@ protected static function orderSQL($arrOptions)
return $arrOptions;
}


/**
* Pull a particular property from each assoc. array in a numeric array,
* returning and array of the property values from each item.
Expand Down

0 comments on commit 66f0027

Please sign in to comment.