<?php declare(strict_types=1);
namespace Neon\Unitarticles;
use Doctrine\DBAL\Connection;
use Neon\Unitarticles\Services\CustomFieldFactory;
use Neon\Unitarticles\UnitarticleSetting\UnitarticleSettingDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class NeonPlatformUnitarticles extends Plugin
{
const PAYLOAD_KEY = 'neon_unitarticles';
const PAYLOAD_KEY_DIMENSION = 'dimensions';
const PAYLOAD_KEY_ISQUANTITYHIDDEN = 'isQuantityHidden';
const PAYLOAD_KEY_UNITLABEL = 'unitlabel';
const CUSTOMFIELD_ISUNITARTICLE = 'neon_unitarticles_isunitarticle';
const CUSTOMFIELD_BASEPRICEVALUE = 'neon_unitarticles_basepricevalue';
const CUSTOMFIELD_HIDEQUANTITYSELECTION = 'neon_unitarticles_hidequantityselection';
const CUSTOMFIELD_ISPRICECALCULATIONDISABLED = 'neon_unitarticles_ispricecalculationdisabled';
const CUSTOMFIELD_DESCRIPTION = 'neon_unitarticles_description1';
const CUSTOMFIELD_UNITLABEL = 'neon_unitarticles_unitlabel1';
const CUSTOMFIELD_STEPZISE = 'neon_unitarticles_stepsize1';
const CUSTOMFIELD_MINVALUE = 'neon_unitarticles_minvalue1';
const CUSTOMFIELD_MAXVALUE = 'neon_unitarticles_maxvalue1';
const CUSTOMFIELD_DEFAULTVALUE = 'neon_unitarticles_defaultvalue1';
const CUSTOMFIELD_EXCLUDEDVALUES = 'neon_unitarticles_excludedvalues1';
const CUSTOMFIELD_HASDIMENSION2 = 'neon_unitarticles_hasdimension2';
const CUSTOMFIELD_DESCRIPTION2 = 'neon_unitarticles_description2';
const CUSTOMFIELD_STEPZISE2 = 'neon_unitarticles_stepsize2';
const CUSTOMFIELD_MINVALUE2 = 'neon_unitarticles_minvalue2';
const CUSTOMFIELD_MAXVALUE2 = 'neon_unitarticles_maxvalue2';
const CUSTOMFIELD_DEFAULTVALUE2 = 'neon_unitarticles_defaultvalue2';
const CUSTOMFIELD_EXCLUDEDVALUES2 = 'neon_unitarticles_excludedvalues2';
const CUSTOMFIELD_PRICECALCULATION = 'neon_unitarticles_pricecalculation';
const CUSTOMFIELD_PRICECALCULATION_CUSTOM = 'neon_unitarticles_pricecalculation_custom';
const CUSTOMFIELD_BASEPRICEVALUELABEL = 'neon_unitarticles_basepricevaluelabel';
const CUSTOMFIELD_SURCHARGE1VALUE = 'neon_unitarticles_surcharge1value';
const CUSTOMFIELD_SURCHARGE1LABEL = 'neon_unitarticles_surcharge1label';
const CUSTOMFIELD_SURCHARGE2VALUE = 'neon_unitarticles_surcharge2value';
const CUSTOMFIELD_SURCHARGE2LABEL = 'neon_unitarticles_surcharge2label';
const CUSTOMFIELD_HIDEREFERENCEPRICE = 'neon_unitarticles_hidereferenceprice';
const CUSTOMFIELD_CALCULATEDPRICEFACTOR = 'neon_unitarticles_calculated_price_factor';
const CUSTOMFIELD_PRICECALCULATION_DEFAULTVALUE = '{unitvalue1}';
const PLUGIN_NAME = 'NeonPlatformUnitarticles';
const PAYLOAD_KEY_SURCHARGES = 'surcharges';
const PAYLOAD_KEY_ISMERGEUSERVALUES = 'isMergeUserValues';
const PARAM_UNITVALUE = 'neonUnitarticleUnitValue';
const UNITARTICLE_SETTING_UPDATE_PRIVILEGE = 'neon_unitarticles_setting:update';
const UNITARTICLE_SETTING_READ_PRIVILEGE = 'neon_unitarticles_setting:read';
const UNITARTICLE_SETTING_DELETE_PRIVILEGE = 'neon_unitarticles_setting:delete';
const UNITARTICLE_SETTING_CREATE_PRIVILEGE = 'neon_unitarticles_setting:create';
const CUSTOMFIELD_MERGEUSERVALUES = 'neon_unitarticles_mergeuservalues';
public function activate(Plugin\Context\ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->addCustomPrivileges();
}
public function deactivate(Plugin\Context\DeactivateContext $deactivateContext): void
{
parent::deactivate($deactivateContext);
$this->removeCustomPrivileges();
}
public function install(Plugin\Context\InstallContext $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$this->installCustomFieldsForProduct($context, $customFieldSetRepository);
}
/**
* @param Plugin\Context\UninstallContext $context
* @throws \Doctrine\DBAL\DBALException
*/
public function uninstall(Plugin\Context\UninstallContext $context): void
{
if (!$context->keepUserData()) {
/** @var Connection $connection */
$connection = $this->container->get('Doctrine\DBAL\Connection');
$this->uninstallCustomFieldset($connection);
$this->uninstallSnippets($connection);
$this->removeTables($connection);
}
}
public function update(Plugin\Context\UpdateContext $context): void
{
parent::update($context);
$this->addCustomPrivileges();
}
/**
* @param Plugin\Context\InstallContext $context
* @param object|null $customFieldSetRepository
*/
private function installCustomFieldsForProduct(Plugin\Context\InstallContext $context, ?object $customFieldSetRepository): void
{
/** @var Connection $connection */
$connection = $this->container->get('Doctrine\DBAL\Connection');
$this->uninstallCustomFieldset($connection);
$this->uninstallSnippets($connection);
$customFieldSetRepository->create([[
'name' => 'neon_unitarticles',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Settings unit article',
'de-DE' => 'Konfiguration Einheitenartikel'
],
],
'customFields' => $this->getCustomFields(),
'relations' => [
[
'entityName' => 'product'
]
]
]], $context->getContext());
}
/**
* @param Connection $connection
* @throws \Doctrine\DBAL\DBALException
*/
private function uninstallCustomFieldset($connection): void
{
$query = new RetryableQuery(
$connection->prepare("DELETE FROM custom_field_set WHERE `name` = :name;")
);
$query->execute([
'name' => 'neon_unitarticles'
]);
$query->execute([
'name' => 'neon_unitarticles_additional_dimensions'
]);
//todo: delete product_translation entries
}
/**
* @param Connection $connection
* @throws \Doctrine\DBAL\DBALException
*/
private function uninstallSnippets($connection)
{
$query = new RetryableQuery(
$connection->prepare("DELETE FROM snippet WHERE `translation_key` LIKE :translationKey;")
);
$query->execute([
'translationKey' => 'customFields.neon_unitarticles_%'
]);
}
/**
* @return array[]
*/
private function getCustomFields(): array
{
/** @var CustomFieldFactory $fieldFactory */
$fieldFactory = new CustomFieldFactory();
return [
[
'name' => self::CUSTOMFIELD_ISUNITARTICLE,
'type' => CustomFieldTypes::BOOL,
'config' => [
'bordered' => false,
'label' => [
'de-DE' => 'Ist Einheitenartikel',
'en-GB' => 'Is unit article'
],
'customFieldPosition' => 0,
'helpText' => [
'de-DE' => 'Soll Artikel nach Einheiten verkauft werden? Aktiviert die Plugin-Funktion für diesen Artikel in dieser Sprache',
'en-GB' => 'Sell this article by unit.'
],
],
],
[
'name' => self::CUSTOMFIELD_BASEPRICEVALUE,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'label' => [
'de-DE' => 'Grundeinheit',
'en-GB' => 'Base unit'
],
'placeholder' => [
'de-DE' => 'Einheitenwert des Artikel-Grundpreises (z.B. 1)',
'en-GB' => 'Unit value of base price (i.e. 1)'
],
'helpText' => [
'de-DE' => 'Beispiel: Artikel soll von 1 - 100 m verkauft werden. Den in den Stammdaten angegebenen Preis entspricht dem Preis pro 1 Meter. Dann muss hier 1 eingetrangen werden.',
'en-GB' => ''
],
'customFieldPosition' => 2
],
],
$fieldFactory->getQuantityHiddenField(self::CUSTOMFIELD_HIDEQUANTITYSELECTION, 4),
$fieldFactory->getDescriptionField(self::CUSTOMFIELD_DESCRIPTION, 99, $this->getLabelPrefix(1)),
$fieldFactory->getUnitField(self::CUSTOMFIELD_UNITLABEL, 100, $this->getLabelPrefix(1)),
$fieldFactory->getCustomFieldMinValue(self::CUSTOMFIELD_MINVALUE, 101, $this->getLabelPrefix(1)),
$fieldFactory->getCustomFieldMaxValue(self::CUSTOMFIELD_MAXVALUE, 102, $this->getLabelPrefix(1)),
$fieldFactory->getCustomFieldStepSize(self::CUSTOMFIELD_STEPZISE, 103, $this->getLabelPrefix(1)),
$fieldFactory->getCustomFieldDefaultValue(self::CUSTOMFIELD_DEFAULTVALUE, 104, $this->getLabelPrefix(1)),
$fieldFactory->getCustomFieldExcludedValues(self::CUSTOMFIELD_EXCLUDEDVALUES, 105, $this->getLabelPrefix(1)),
];
}
/**
* @return string[]
*/
private function getLabelPrefix($number): array
{
return ['de' => $number . '. Dimension: ', 'en' => ($number == 1 ? '1st' : '2nd') . ' dimension: '];
}
private function removeTables(Connection $connection)
{
$classNames = [
UnitarticleSettingDefinition::ENTITY_NAME
];
foreach ($classNames as $className) {
$connection->executeUpdate(\sprintf('DROP TABLE IF EXISTS `%s`', $className));
}
}
private function addCustomPrivileges(): void
{
if (!\method_exists($this, 'addPrivileges')) {
return;
}
$this->addPrivileges(
'product.editor',
[
self::UNITARTICLE_SETTING_UPDATE_PRIVILEGE,
]
);
$this->addPrivileges(
'product.creator',
[
self::UNITARTICLE_SETTING_CREATE_PRIVILEGE,
]
);
}
private function removeCustomPrivileges(): void
{
if (!\method_exists($this, 'removePrivileges')) {
return;
}
$this->removePrivileges([
self::UNITARTICLE_SETTING_UPDATE_PRIVILEGE,
self::UNITARTICLE_SETTING_CREATE_PRIVILEGE
]);
}
}