<?php declare(strict_types=1);
namespace Virgin\LeadManager;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class LeadManager extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext); // TODO: Change the autogenerated stub
$newFields =
[
[
'name' => 'lead_implicit_registration',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-single-select',
'validation' => 'required',
'options' => [[
'label' => [
'en-GB' => 'implicit registration',
],
'value' => true, ],
[
'label' => [
'en-GB' => 'explicit registration',
],
'value' => false,
],
],
'customFieldType' => 'select',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Registration type',
],
],
],
[
'name' => 'phone_number',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'Telephone number',
],
],
],
[
'name' => 'virgin_guid',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'label' => [
'en-GB' => 'virgin guid',
],
],
],
[
'name' => 'privacy_consent',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-single-select',
'validation' => 'required',
'options' => [[
'label' => [
'en-GB' => 'privacy si',
],
'value' => true, ],
[
'label' => [
'en-GB' => 'privacy no',
],
'value' => false,
], [
'label' => [
'en-GB' => 'privacy null',
],
'value' => null,
],
],
'customFieldType' => 'select',
'customFieldPosition' => 4,
'label' => [
'en-GB' => 'privacy consent',
],
],
],
[
'name' => 'zipcode',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 5,
'label' => [
'en-GB' => 'zipcode training',
],
],
],
[
'name' => 'home_training',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 6,
'label' => [
'en-GB' => 'home training',
],
],
],
[
'name' => 'balance',
'type' => CustomFieldTypes::FLOAT,
'config' => [
'componentName' => 'sw-field',
'type' => 'number',
'numberType'=> 'float',
'customFieldType' => 'text',
'customFieldPosition' => 7,
'label' => [
'en-GB' => 'balance',
],
],
],
];
$this->addCustomerCustomFields($newFields);
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext); // TODO: Change the autogenerated stub
$newFields =
[
/*
[
'name' => 'token_mc_cart',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 9,
'label' => [
'en-GB' => 'Token MC cart',
],
],
],
[
'name' => 'test_test',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 11,
'label' => [
'en-GB' => 'Test test',
],
],
], */
/*[
'name' => 'balance',
'type' => CustomFieldTypes::FLOAT,
'config' => [
'componentName' => 'sw-field',
'type' => 'number',
'numberType'=> 'float',
'customFieldType' => 'text',
'customFieldPosition' => 7,
'label' => [
'en-GB' => 'balance',
],
],
],
[
'name' => 'mc_code_customer',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 8,
'label' => [
'en-GB' => 'mc code',
],
],
],
[
'name' => 'id_richiesta',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 9,
'label' => [
'en-GB' => 'id richiesta',
],
],
],
[
'name' => 'data_richiesta_mc',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'componentName' => 'sw-field',
'time_24hr' => true,
'dateType' => 'datetime',
'customFieldType' => 'date',
'customFieldPosition' => 10,
'label' => [
'en-GB' => 'data richiesta mc',
],
],
],
[
'name' => 'unsolved_under_evaluation',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 11,
'label' => [
'en-GB' => 'Insoluto check in corso',
],
],
],*/
/*
[
'name' => 'email',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 12,
'label' => [
'en-GB' => 'Partner User Email',
],
],
],
[
'name' => 'userName',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 13,
'label' => [
'en-GB' => 'Partner User Firstname',
],
],
],
[
'name' => 'surnameName',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 14,
'label' => [
'en-GB' => 'Partner User Lastname',
],
],
],
[
'name' => 'fiscalCode',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 15,
'label' => [
'en-GB' => 'Partner User Fiscal Code',
],
],
],
[
'name' => 'birthdate',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'componentName' => 'sw-field',
'time_24hr' => false,
'dateType' => 'datetime',
'customFieldType' => 'date',
'customFieldPosition' => 16,
'label' => [
'en-GB' => 'Partner User Birth Date',
],
],
],
[
'name' => 'residentAddress',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'dateType' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 17,
'label' => [
'en-GB' => 'Partner User Resident Address',
],
],
],
*/
[
'name' => 'activationCode',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'dateType' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 18,
'label' => [
'en-GB' => 'Shopware Account Activation Code',
],
],
]
];
$this->addCustomerCustomFields($newFields, 'lead', 'Lead Generation', 'customer');
}
private function addCustomerCustomFields($newProductCustomFields, $setName, $label, $entity)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newProductCustomFields as $newProductCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newProductCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newProductCustomField;
}
}
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $setName)
);
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
if (!empty($createCustomFields)) {
if (null == $customFieldSetId) {
$customFieldSetRepository->create([
[
'name' => $setName,
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => $label,
],
],
'relations' => [
['entityName' => $entity],
],
],
], $context);
} else {
$customFieldSetRepository->update(
[
[
'id' => $customFieldSetId,
'customFields' => $createCustomFields
]
], $context);
}
}
}
/*private function addCustomerCustomFields($newCustomerCustomFields)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newCustomerCustomFields as $newCustomerCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newCustomerCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newCustomerCustomField;
}
}
if (!empty($createCustomFields)) {
$customFieldSetRepository->create([
[
'name' => 'lead',
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => 'Lead Generation',
],
],
'relations' => [
['entityName' => 'customer'],
],
],
], $context);
}
}*/
}