Skip to content

Commit

Permalink
OP-466 - Add autocomplete to customer field
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPalen committed Aug 5, 2024
1 parent 8cf6e96 commit 7c7b020
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 16 deletions.
46 changes: 46 additions & 0 deletions src/Form/Type/CustomerAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusBlacklistPlugin\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class CustomerAutocompleteChoiceType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'resource' => 'sylius.customer',
'choice_name' => 'email',
'choice_value' => 'id',
]);
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['remote_criteria_type'] = 'contains';
$view->vars['remote_criteria_name'] = 'email';
}

public function getBlockPrefix(): string
{
return 'bitbag_sylius_customer_autocomplete_choice';
}

public function getParent(): string
{
return ResourceAutocompleteChoiceType::class;
}
}
27 changes: 13 additions & 14 deletions src/Form/Type/FraudSuspicionType.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusBlacklistPlugin\Form\Type;

use BitBag\SyliusBlacklistPlugin\Entity\FraudPrevention\FraudSuspicionInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotNull;

final class FraudSuspicionType extends AbstractResourceType
{
/** @var string */
private $customerClass;

public function __construct(
string $dataClass,
string $customerClass,
array $validationGroups = []
array $validationGroups = [],
) {
parent::__construct($dataClass, $validationGroups);

$this->customerClass = $customerClass;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('customer', EntityType::class, [
'class' => $this->customerClass,
->add('customer', CustomerAutocompleteChoiceType::class, [
'label' => 'sylius.ui.customer',
'priority' => 1,
'constraints' => [
new NotNull(),
],
])
->add('company', TextType::class, [
'required' => false,
Expand Down
26 changes: 26 additions & 0 deletions src/Repository/CustomerRepositoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusBlacklistPlugin\Repository;

trait CustomerRepositoryTrait
{

public function findByEmailPart(string $email, ?int $limit = null): array
{
return $this->createQueryBuilder('c')
->andWhere('c.email LIKE :email')
->setParameter('email', '%' . $email . '%')
->setMaxResults($limit)
->getQuery()
->getResult();
}
}
26 changes: 26 additions & 0 deletions src/Resources/config/routing/admin/fraud_suspicion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ bitbag_sylius_blacklist_plugin_admin_order_mark_suspicious:
method: createForOrder
arguments:
- "expr:notFoundOnNull(service('sylius.repository.order').find($orderId))"

bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_email:
path: /customer-search
methods: [GET]
defaults:
_controller: sylius.controller.customer::indexAction
_format: json
_sylius:
permission: true
repository:
method: findByEmailPart
arguments:
email: $email
limit: "!!int %bitbag.ajax.customer.autocomplete_limit%"

bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_id:
path: /customer-search-by-id
methods: [GET]
defaults:
_controller: sylius.controller.customer::indexAction
_format: json
_sylius:
permission: true
repository:
method: findBy
arguments: [id: $id]
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<import resource="services/**/*.xml" />
</imports>

<parameters>
<parameter key="bitbag.ajax.customer.autocomplete_limit">25</parameter>
</parameters>

<services>
<service id="bitbag_sylius_blacklist_plugin.registry_blacklisting_rule_checker" class="Sylius\Component\Registry\ServiceRegistry">
<argument>BitBag\SyliusBlacklistPlugin\Checker\BlacklistingRule\BlacklistingRuleCheckerInterface</argument>
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
</service>
<service id="bitbag.sylius_blacklist_plugin.form.type.fraud_suspicion" class="BitBag\SyliusBlacklistPlugin\Form\Type\FraudSuspicionType">
<argument>%bitbag_sylius_blacklist_plugin.model.fraud_suspicion.class%</argument>
<argument>%sylius.model.customer.class%</argument>
<argument>%bitbag.sylius_blacklist_plugin.form.type.validation_groups%</argument>
<tag name="form.type"/>
</service>
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/views/Form/theme.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends '@SyliusUi/Form/theme.html.twig' %}

{% block bitbag_sylius_customer_autocomplete_choice_row %}
{{ form_row(form, {
'remote_url': path('bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_email'),
'load_edit_url': path('bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_id')
}) }}
{% endblock %}
3 changes: 3 additions & 0 deletions tests/Application/config/packages/twig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ twig:
paths: ['%kernel.project_dir%/templates']
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
form_themes:
- '@BitBagSyliusBlacklistPlugin/Form/theme.html.twig'
- '@SyliusUi/Form/theme.html.twig'

services:
_defaults:
Expand Down
20 changes: 20 additions & 0 deletions tests/Application/src/Repository/CustomerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusBlacklistPlugin\Repository;

use BitBag\SyliusBlacklistPlugin\Repository\CustomerRepositoryTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository as BaseCustomerRepository;

class CustomerRepository extends BaseCustomerRepository
{
use CustomerRepositoryTrait;
}
3 changes: 2 additions & 1 deletion tests/Application/src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ sylius_customer:
resources:
customer:
classes:
model: Tests\BitBag\SyliusBlacklistPlugin\Entity\Customer
model: Tests\BitBag\SyliusBlacklistPlugin\Entity\Customer
repository: Tests\BitBag\SyliusBlacklistPlugin\Repository\CustomerRepository

0 comments on commit 7c7b020

Please sign in to comment.