init
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerVat\Setup\Patch\Data;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Framework\Setup\Patch\DataPatchInterface;
|
||||
|
||||
class MoveVatSelectionColumnFirst implements DataPatchInterface
|
||||
{
|
||||
private const GRID_NAMESPACE = 'esun_customer_vat_listing';
|
||||
|
||||
public function __construct(
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly Json $json
|
||||
) {
|
||||
}
|
||||
|
||||
public function apply(): self
|
||||
{
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$table = $this->resourceConnection->getTableName('ui_bookmark');
|
||||
$bookmarks = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($table, ['bookmark_id', 'config'])
|
||||
->where('namespace = ?', self::GRID_NAMESPACE)
|
||||
);
|
||||
|
||||
foreach ($bookmarks as $bookmark) {
|
||||
$config = $this->json->unserialize((string)$bookmark['config']);
|
||||
if (!is_array($config) || !$this->moveSelectionFirst($config)) {
|
||||
continue;
|
||||
}
|
||||
$connection->update(
|
||||
$table,
|
||||
['config' => $this->json->serialize($config)],
|
||||
['bookmark_id = ?' => (int)$bookmark['bookmark_id']]
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function moveSelectionFirst(array &$config): bool
|
||||
{
|
||||
$changed = false;
|
||||
if (isset($config['positions']) && is_array($config['positions'])
|
||||
&& array_key_exists('ids', $config['positions'])) {
|
||||
asort($config['positions'], SORT_NUMERIC);
|
||||
$columns = array_keys($config['positions']);
|
||||
$columns = array_values(array_diff($columns, ['ids']));
|
||||
array_unshift($columns, 'ids');
|
||||
$config['positions'] = array_flip($columns);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
foreach ($config as $key => &$value) {
|
||||
if ($key !== 'positions' && is_array($value)) {
|
||||
$changed = $this->moveSelectionFirst($value) || $changed;
|
||||
}
|
||||
}
|
||||
unset($value);
|
||||
|
||||
return $changed;
|
||||
}
|
||||
|
||||
public static function getDependencies(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAliases(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user