<?php declare(strict_types=1);
namespace ProcUploadDocument;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
class ProcUploadDocument extends Plugin {
const DOWNLOAD_URL = 'custom-uploaded-document-url';
const MEDIA_ID = 'custom-uploaded-document-mediaId';
const FILE_NAME = 'custom-uploaded-document-fileName';
const SALES_ID = 'sales-representative-identity';
public function install(InstallContext $installContext): void
{
$this->insertCustomFields($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$this->deleteCustomFields($uninstallContext->getContext());
}
private function insertCustomFields(Context $context): void
{
$repo = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'order_additional_fields'));
if (!$repo->search($criteria, $context)->first()) {
$attributeProduct = $this->getProductCustomFields();
$result = $repo->upsert([$attributeProduct], $context);
}
}
private function deleteCustomFields(Context $context): void
{
$repo = $this->container->get('custom_field_set.repository');
$fields = ['order_additional_fields'];
foreach ($fields as $field) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $field));
$result = $repo->search($criteria, $context);
if ($result != null) {
$attributeSet = $result->first();
if ($attributeSet != null) {
$id = $attributeSet->getId();
if ($id != null) {
$repo->delete([['id' => $id]], $context);
}
}
}
}
}
private function getProductCustomFields(): array
{
return [
'name' => 'order_additional_fields',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Order additional fields',
'de-DE' => 'Zusätzliche Bestellfelder'
]
],
'customFields' => [
[
'name' => self::DOWNLOAD_URL,
'type' => 'text',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Download-URL',
'de-DE' => 'Download-URL'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'helpText' => [
'en-GB' => 'Url from which the file uploaded during checkout is available for viewing or downloading',
'de-DE' => 'Url, unter der die beim Checkout hochgeladene Datei zur Ansicht oder zum Herunterladen verfügbar ist'
]
]
],
[
'name' => self::SALES_ID,
'type' => 'text',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Agent name',
'de-DE' => 'Agentenname'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'helpText' => [
'en-GB' => 'Agent name of Sales representative-identity',
'de-DE' => 'Agentenname der Handelsvertreter-Identität'
]
]
],
],
'relations' => [
[
'entityName' => 'order'
]
]
];
}
}