<?php declare(strict_types=1);
namespace SelfdelveBillbee;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Context;
// Manually load package classes as it is not possible in the Plugin's composer file yet
if (file_exists(\dirname(__DIR__) . '/packages/billbee-php-sdk/vendor/autoload.php')) {
$loader = require_once \dirname(__DIR__) . '/packages/billbee-php-sdk/vendor/autoload.php';
if ($loader !== true) {
spl_autoload_unregister([$loader, 'loadClass']);
$loader->register(false);
}
}
class SelfdelveBillbee extends Plugin
{
const CUSTOM_FIELDS_SET_SHIPPING_METHODS = 'selfdelve_billbee_shipping_methods';
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
$this->addCustomFieldSet($installContext->getContext());
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeCustomFieldSet($uninstallContext->getContext());
}
/**
* @param Context $context
*/
private function addCustomFieldSet(Context $context): void
{
$customFieldSetRepo = $this->container->get('custom_field_set.repository');
if ($customFieldSetRepo->search((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDS_SET_SHIPPING_METHODS)), $context)->count() === 0) {
$customFieldSetRepo->create([
[
'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHODS,
'position' => 100,
'config' => [
'label' => [
'en-GB' => 'SelfDelve - Shipping Methods',
'de-DE' => 'SelfDelve - Versandarten'
]
],
'customFields' => [
[
'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHODS . '_name_billbee',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'Shipping product name in Billbee',
'de-DE' => 'Name des Versandprodukts in Billbee'
],
'customFieldType' => 'text',
'customFieldPosition' => 0
]
],
],
'relations' => [
[
'id' => Uuid::randomHex(),
'entityName' => 'shipping_method'
]
]
]
], $context);
}
}
/**
* @param Context $context
*/
private function removeCustomFieldSet(Context $context): void
{
$customFieldSetRepo = $this->container->get('custom_field_set.repository');
$id = $customFieldSetRepo->searchIds((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDS_SET_SHIPPING_METHODS)), $context)->firstId();
if ($id) {
$customFieldSetRepo->delete([
['id' => $id]
], $context);
}
}
}