Skip to content

Commit

Permalink
add attribute group export action in grid and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Rolland committed Aug 21, 2023
1 parent 7f0facc commit 75529bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ protected function getGridActions()
],
])
)
->add(
(new LinkGridAction('export'))
->setName($this->trans('Export', [], 'Admin.Actions'))
->setIcon('cloud_download')
->setOptions([
'route' => 'admin_attribute_groups_export',
])
)
->add((new SimpleGridAction('common_refresh_list'))
->setName($this->trans('Refresh list', [], 'Admin.Advparameters.Feature'))
->setIcon('refresh')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdaterInterface;
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactoryInterface;
use PrestaShop\PrestaShop\Core\Search\Filters\AttributeGroupFilters;
use PrestaShopBundle\Component\CsvResponse;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -169,14 +170,36 @@ public function editAction(Request $request, int $attributeGroupId): Response
* )
*
* @param int $attributeGroupId
* @param AttributeGroupFilters $filters
*
* @return RedirectResponse
* @return CsvResponse
*/
public function exportAction(int $attributeGroupId)
public function exportAction(AttributeGroupFilters $filters): CsvResponse
{
//@todo: implement in antoher pr
return $this->redirectToRoute('admin_attribute_groups_index');
$filters = new AttributeGroupFilters(['limit' => null] + $filters->all());
$attributeGroupGridFactory = $this->get('prestashop.core.grid.factory.attribute_group');
$attributeGroupGrid = $attributeGroupGridFactory->getGrid($filters);

$headers = [
'id_attribute_group' => $this->trans('ID', 'Admin.Global'),
'name' => $this->trans('Name', 'Admin.Global'),
'position' => $this->trans('Position', 'Admin.Global'),
];

$data = [];

foreach ($attributeGroupGrid->getData()->getRecords()->all() as $record) {
$data[] = [
'id_attribute_group' => $record['id_attribute_group'],
'name' => $record['name'],
'position' => $record['position'],
];
}

return (new CsvResponse())
->setData($data)
->setHeadersData($headers)
->setFileName('attribute_group_' . date('Y-m-d_His') . '.csv');
}

/**
Expand Down

0 comments on commit 75529bb

Please sign in to comment.