<?php declare(strict_types=1);
namespace CoeRechnungsMailSw6;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
/**
* Class CoeRechnungsMailSw6
* @package CoeRechnungsMailSw6
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
class CoeRechnungsMailSw6 extends Plugin
{
public static $field_set_name = "coe_rechnungsmail";
public static $field_name_mail = "coe_rechnungsmail_mail";
public static $field_name_active = "coe_rechnungsmail_active";
/**
* @param InstallContext $context
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function install(InstallContext $context): void{
parent::install($context);
if(!$this->hasCustomField($context->getContext())){
$this->createCustomField($context->getContext());
}
}
/**
* @param UninstallContext $context
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function uninstall(UninstallContext $context): void{
if($context->keepUserData()){
return;
}
parent::uninstall($context);
if($this->hasCustomField($context->getContext())){
$this->deleteCustomField($context->getContext());
}
}
/**
* @param Context $context
* @return bool
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
private function hasCustomField(Context $context){
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get("custom_field_set.repository");
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::$field_set_name));
/** @var IdSearchResult $result */
$result = $customFieldSetRepository->searchIds($criteria, $context);
return $result->getTotal() > 0;
}
/**
* @param Context $context
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
private function createCustomField(Context $context){
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get("custom_field_set.repository");
$customFieldSetRepository->create([
[
'name' => self::$field_set_name,
'global' => true,
'config' => [
'label' => [
'en-GB' => 'Alternative email address for documents',
'de-DE' => 'Alternative E-Mail Adresse für Dokumente'
]
],
'customFields' => [
[
'name' => self::$field_name_mail,
'type' => 'text',
'config' => [
'type' => 'text',
'label' => [
'en-GB' => 'Email address',
'de-DE' => 'E-Mail Addresse'
],
'customFieldPosition' => 1
]
],
[
'name' => self::$field_name_active,
'type' => 'bool',
'config' => [
'type' => 'checkbox',
'label' => [
'en-GB' => 'Is active',
'de-DE' => 'Ist aktiv'
],
'customFieldPosition' => 2
]
]
],
'relations' => [
[
"entityName" => "customer"
]
]
]
], $context);
}
/**
* @param Context $context
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
private function deleteCustomField(Context $context){
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get("custom_field_set.repository");
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::$field_set_name));
$result = $customFieldSetRepository->searchIds($criteria, $context);
$customFieldSetRepository->delete([
[
'id' => $result->firstId(),
],
], $context);
}
}