<?php declare(strict_types=1);
namespace ExpertXmlOrderExport;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class ExpertXmlOrderExport extends Plugin
{
/**
* set create mode name
*/
private const FIELD_SET_HELPER_MODE_CREATE = 'create';
/**
* set delete mode name
*/
private const FIELD_SET_HELPER_MODE_DELETE = 'delete';
/**
* @param ActivateContext $activateContext
*/
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->customFieldSetHelper($activateContext->getContext(), null, self::FIELD_SET_HELPER_MODE_CREATE);
}
/**
* @param DeactivateContext $deactivateContext
*/
public function deactivate(DeactivateContext $deactivateContext): void
{
parent::deactivate($deactivateContext);
$this->customFieldSetHelper(
$deactivateContext->getContext(),
['expert_order_export_states'],
self::FIELD_SET_HELPER_MODE_DELETE
);
}
/**
* @param Context $context
* @param array $fieldNames
* @param string $mode
*/
private function customFieldSetHelper(
Context $context,
$fieldNames = [],
$mode = self::FIELD_SET_HELPER_MODE_CREATE
): void {
// get custom fieldset repository
$customFieldsetRepository = $this->container->get('custom_field_set.repository');
if ($mode == self::FIELD_SET_HELPER_MODE_CREATE) {
$customFieldsetRepository->create($this->getCustomFieldsContainer(), $context);
} elseif ($mode == self::FIELD_SET_HELPER_MODE_DELETE && !empty($fieldNames)) {
// get Criteria
$criteria = new Criteria();
foreach ($fieldNames as $fieldName) {
// filter criteria by name
$criteria->addFilter(new EqualsFilter('name', $fieldName));
$ids = $customFieldsetRepository->searchIds($criteria, $context);
if ($ids) {
$customFieldsetRepository->delete([['id' => $ids->getIds()[0]]], $context);
}
}
} else {
new \Exception();
}
}
/**
* @return array|array[]
*/
private function getCustomFieldsContainer(): array
{
return [
[
'name' => 'expert_order_export_states',
'config' => [
'label' => [
'en-GB' => 'XML orderexport status',
'de-DE' => 'XML Exportstatus',
],
'translated' => false,
],
'customFields' => [
[
'name' => 'expert_order_export_state',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Status',
'de-DE' => 'Status',
],
'helpText' => [
'en-GB' => 'Checked: Already exported | Unchecked: Not exported yet',
'de-DE' => 'Wenn Checked: Bestellung bereits exportiert | Wenn Unchecked: Bestellung noch nicht exportiert',
],
]
]
],
'relations' => [
[
'entityName' => 'order'
]
]
], // end
];
}
}