Skip to content

Commit 77268c5

Browse files
committed
Init module
0 parents  commit 77268c5

28 files changed

+1288
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Controller\Adminhtml\Export;
9+
10+
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\Controller\ResultFactory;
13+
use Magento\Framework\Phrase;
14+
use Magento\Framework\View\Result\Page;
15+
16+
class Category extends Action implements HttpGetActionInterface
17+
{
18+
public const ADMIN_RESOURCE = 'Opengento_CategoryImportExport::export';
19+
20+
public function execute(): Page
21+
{
22+
$page = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
23+
$page->getConfig()->getTitle()->set(new Phrase('Export Categories'));
24+
25+
return $page;
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Controller\Adminhtml\Export;
9+
10+
use Opengento\CategoryImportExport\Model\Session\DownloadContext;
11+
use Exception;
12+
use Magento\Backend\App\Action;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\Response\Http\FileFactory;
16+
use Magento\Framework\Phrase;
17+
18+
class CategoryDownload extends Action implements HttpGetActionInterface
19+
{
20+
public const ADMIN_RESOURCE = 'Opengento_CategoryImportExport::export';
21+
22+
public function __construct(
23+
Action\Context $context,
24+
private DownloadContext $downloadContext,
25+
private FileFactory $fileFactory
26+
) {
27+
parent::__construct($context);
28+
}
29+
30+
public function execute()
31+
{
32+
$file = $this->downloadContext->getFile();
33+
if ($file) {
34+
$this->downloadContext->clearFile();
35+
try {
36+
return $this->fileFactory->create(
37+
basename($file),
38+
['type' => 'filename', 'value' => $file],
39+
DirectoryList::VAR_IMPORT_EXPORT
40+
);
41+
} catch (Exception $e) {
42+
$this->messageManager->addExceptionMessage($e, new Phrase('Something went wrong while downloading the file.'));
43+
}
44+
}
45+
46+
return $this->resultRedirectFactory->create()->setPath('*/*/category');
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Controller\Adminhtml\Export;
9+
10+
use Opengento\CategoryImportExport\Model\Csv\Options;
11+
use Opengento\CategoryImportExport\Model\Export\ToCsv;
12+
use Opengento\CategoryImportExport\Model\Session\DownloadContext;
13+
use Exception;
14+
use Magento\Backend\App\Action;
15+
use Magento\Framework\App\Action\HttpPostActionInterface;
16+
use Magento\Framework\Controller\Result\Redirect;
17+
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\Phrase;
19+
20+
class CategoryPost extends Action implements HttpPostActionInterface
21+
{
22+
public const ADMIN_RESOURCE = 'Opengento_CategoryImportExport::export';
23+
24+
public function __construct(
25+
Action\Context $context,
26+
private DownloadContext $downloadContext,
27+
private ToCsv $toCsv
28+
) {
29+
parent::__construct($context);
30+
}
31+
32+
public function execute(): Redirect
33+
{
34+
$request = $this->getRequest();
35+
try {
36+
$this->downloadContext->setFile(
37+
$this->toCsv->execute(
38+
(array)$request->getParam('store_ids'),
39+
(array)$request->getParam('attributes'),
40+
new Options(
41+
(string)$request->getParam('delimiter'),
42+
(string)$request->getParam('enclosure')
43+
)
44+
)
45+
);
46+
47+
$this->messageManager->addSuccessMessage(new Phrase('The export file is now ready for download.'));
48+
} catch (LocalizedException $e) {
49+
$this->messageManager->addErrorMessage($e, $e->getMessage());
50+
} catch (Exception $e) {
51+
$this->messageManager->addExceptionMessage($e, new Phrase('Something went wrong while exporting the data.'));
52+
}
53+
54+
return $this->resultRedirectFactory->create()->setPath('*/*/category');
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Controller\Adminhtml\Import;
9+
10+
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\Controller\ResultFactory;
13+
use Magento\Framework\Phrase;
14+
use Magento\Framework\View\Result\Page;
15+
16+
class Category extends Action implements HttpGetActionInterface
17+
{
18+
public const ADMIN_RESOURCE = 'Opengento_CategoryImportExport::import';
19+
20+
public function execute(): Page
21+
{
22+
$page = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
23+
$page->getConfig()->getTitle()->set(new Phrase('Import Categories'));
24+
25+
return $page;
26+
}
27+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Controller\Adminhtml\Import;
9+
10+
use Opengento\CategoryImportExport\Model\Csv\Options;
11+
use Opengento\CategoryImportExport\Model\Import\FromCsv;
12+
use Exception;
13+
use Magento\Backend\App\Action;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\App\Filesystem\DirectoryList;
16+
use Magento\Framework\Controller\Result\Redirect;
17+
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\File\UploaderFactory;
19+
use Magento\Framework\Filesystem;
20+
use Magento\Framework\Phrase;
21+
22+
class CategoryPost extends Action implements HttpPostActionInterface
23+
{
24+
public const ADMIN_RESOURCE = 'Opengento_CategoryImportExport::import';
25+
26+
public function __construct(
27+
Action\Context $context,
28+
private UploaderFactory $uploaderFactory,
29+
private Filesystem $filesystem,
30+
private FromCsv $fromCsv
31+
) {
32+
parent::__construct($context);
33+
}
34+
35+
public function execute(): Redirect
36+
{
37+
try {
38+
$this->fromCsv->execute(
39+
$this->uploadFile('import-' . time() . '-categories.csv'),
40+
new Options(
41+
(string)$this->getRequest()->getParam('delimiter'),
42+
(string)$this->getRequest()->getParam('enclosure')
43+
)
44+
);
45+
$this->messageManager->addSuccessMessage(new Phrase('Import successful!'));
46+
} catch (LocalizedException $e) {
47+
$this->messageManager->addErrorMessage($e->getMessage());
48+
} catch (Exception $e) {
49+
$this->messageManager->addExceptionMessage($e, new Phrase('Something went wrong while uploading the file.'));
50+
}
51+
52+
return $this->resultRedirectFactory->create()->setPath('*/*/category');
53+
}
54+
55+
/**
56+
* @throws Exception
57+
*/
58+
private function uploadFile(string $fileName): string
59+
{
60+
$directoryRead = $this->filesystem->getDirectoryRead(DirectoryList::VAR_IMPORT_EXPORT);
61+
$uploader = $this->uploaderFactory->create(['fileId' => 'file']);
62+
$uploader->setAllowCreateFolders(true);
63+
$uploader->setAllowRenameFiles(true);
64+
$uploader->setFilesDispersion(false);
65+
$uploader->setFilenamesCaseSensitivity(true);
66+
$uploader->setAllowedExtensions(['csv']);
67+
$uploader->setAllowRenameFiles(true);
68+
$result = $uploader->save($directoryRead->getAbsolutePath('import'), $fileName);
69+
if ($result === false) {
70+
throw new LocalizedException(new Phrase('The uploaded file could not be saved.'));
71+
}
72+
73+
return $directoryRead->getAbsolutePath($result['path'] . '/' . $result['file']);
74+
}
75+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) OpenGento
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Model\Config\Source\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Eav\Model\Attribute;
12+
use Magento\Eav\Model\Config;
13+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
14+
use Magento\Framework\Data\OptionSourceInterface;
15+
use Magento\Framework\Exception\LocalizedException;
16+
17+
class Attributes implements OptionSourceInterface
18+
{
19+
private const EXCLUDE_ATTRIBUTES = [
20+
'all_children',
21+
'category_code',
22+
'children',
23+
'children_count',
24+
'level',
25+
'path',
26+
'path_in_store',
27+
'url_path',
28+
];
29+
30+
private ?array $options = null;
31+
32+
public function __construct(
33+
private Config $config,
34+
private CollectionFactory $collectionFactory
35+
) {}
36+
37+
/**
38+
* @throws LocalizedException
39+
*/
40+
public function toOptionArray(): array
41+
{
42+
return $this->options ??= $this->resolveAttributes();
43+
}
44+
45+
/**
46+
* @throws LocalizedException
47+
*/
48+
private function resolveAttributes(): array
49+
{
50+
$collection = $this->collectionFactory->create();
51+
$collection->setEntityTypeFilter($this->config->getEntityType(Category::ENTITY));
52+
$collection->addFieldToSelect(['attribute_code', 'frontend_label']);
53+
$collection->addFieldToFilter('attribute_code', ['nin' => self::EXCLUDE_ATTRIBUTES]);
54+
$collection->setOrder('frontend_label', 'ASC');
55+
$collection->setOrder('attribute_code', 'ASC');
56+
57+
$options = [];
58+
/** @var Attribute $attribute */
59+
foreach ($collection->getItems() as $attribute) {
60+
$options[] = [
61+
'label' => sprintf('%s (%s)', $attribute->getDefaultFrontendLabel(), $attribute->getAttributeCode()),
62+
'value' => $attribute->getAttributeCode()
63+
];
64+
}
65+
66+
return $options;
67+
}
68+
}

Model/Csv/Options.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Model\Csv;
9+
10+
class Options
11+
{
12+
public function __construct(
13+
public readonly string $delimiter = ',',
14+
public readonly string $enclosure = '"'
15+
) {}
16+
}

Model/DataProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CategoryImportExport\Model;
9+
10+
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as BaseDataProvider;
11+
12+
class DataProvider extends BaseDataProvider
13+
{
14+
public function getData(): array
15+
{
16+
return [];
17+
}
18+
}

0 commit comments

Comments
 (0)