Skip to content

Commit

Permalink
Merge pull request #8065 from Webmecanik/dashboard-bundle-deprecation…
Browse files Browse the repository at this point in the history
…-refactoring

Dashboard bundle deprecation refactoring
  • Loading branch information
escopecz committed Nov 21, 2019
2 parents 021f5da + f0a92b3 commit d41a8b0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 100 deletions.
16 changes: 4 additions & 12 deletions app/bundles/DashboardBundle/Config/config.php
Expand Up @@ -53,18 +53,10 @@
'forms' => [
'mautic.dashboard.form.type.widget' => [
'class' => 'Mautic\DashboardBundle\Form\Type\WidgetType',
'arguments' => 'mautic.factory',
'alias' => 'widget',
],
'mautic.dashboard.form.uplload' => [
'class' => 'Mautic\DashboardBundle\Form\Type\UploadType',
'arguments' => 'mautic.factory',
'alias' => 'dashboard_upload',
],
'mautic.dashboard.form.filter' => [
'class' => 'Mautic\DashboardBundle\Form\Type\FilterType',
'arguments' => 'mautic.factory',
'alias' => 'dashboard_filter',
'arguments' => [
'event_dispatcher',
'mautic.security',
],
],
],
'models' => [
Expand Down
5 changes: 3 additions & 2 deletions app/bundles/DashboardBundle/Controller/AjaxController.php
Expand Up @@ -13,6 +13,7 @@

use Mautic\CoreBundle\Controller\AjaxController as CommonAjaxController;
use Mautic\DashboardBundle\Entity\Widget;
use Mautic\DashboardBundle\Form\Type\WidgetType;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -58,9 +59,9 @@ protected function updateWidgetFormAction(Request $request)
}

$widget = new Widget();
$form = $this->get('form.factory')->create('widget', $widget);
$form = $this->get('form.factory')->create(WidgetType::class, $widget);
$formHtml = $this->render('MauticDashboardBundle::Widget\\form.html.php',
['form' => $form->bind($data)->createView()]
['form' => $form->submit($data)->createView()]
)->getContent();

$dataArray['formHtml'] = $formHtml;
Expand Down
Expand Up @@ -11,17 +11,18 @@

namespace Mautic\DashboardBundle\Controller;

use Mautic\CoreBundle\Controller\FormController;
use Mautic\CoreBundle\Controller\AbstractFormController;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\DashboardBundle\Entity\Widget;
use Mautic\DashboardBundle\Form\Type\UploadType;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* Class DashboardController.
*/
class DashboardController extends FormController
class DashboardController extends AbstractFormController
{
/**
* Generates the default view.
Expand Down Expand Up @@ -427,7 +428,7 @@ public function importAction()
];

$action = $this->generateUrl('mautic_dashboard_action', ['objectAction' => 'import']);
$form = $this->get('form.factory')->create('dashboard_upload', [], ['action' => $action]);
$form = $this->get('form.factory')->create(UploadType::class, [], ['action' => $action]);

if ($this->request->getMethod() == 'POST') {
if (isset($form) && !$cancelled = $this->isFormCancelled($form)) {
Expand Down
33 changes: 19 additions & 14 deletions app/bundles/DashboardBundle/Form/Type/UploadType.php
Expand Up @@ -12,6 +12,8 @@
namespace Mautic\DashboardBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;

/**
Expand All @@ -25,19 +27,22 @@ class UploadType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', 'file', [
'label' => 'mautic.lead.import.file',
'attr' => [
'accept' => '.json',
'class' => 'form-control',
],
]);
$constraints = [
new \Symfony\Component\Validator\Constraints\NotBlank(
['message' => 'mautic.core.value.required']
),
];
$builder->add('start', 'submit', [
$builder->add(
'file',
FileType::class,
[
'label' => 'mautic.lead.import.file',
'attr' => [
'accept' => '.json',
'class' => 'form-control',
],
]
);

$builder->add(
'start',
SubmitType::class,
[
'attr' => [
'class' => 'btn btn-primary',
'icon' => 'fa fa-upload',
Expand All @@ -53,7 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* @return string
*/
public function getName()
public function getBlockPrefix()
{
return 'dashboard_upload';
}
Expand Down
175 changes: 107 additions & 68 deletions app/bundles/DashboardBundle/Form/Type/WidgetType.php
Expand Up @@ -11,11 +11,17 @@

namespace Mautic\DashboardBundle\Form\Type;

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Form\Type\FormButtonsType;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\DashboardBundle\DashboardEvents;
use Mautic\DashboardBundle\Event\WidgetFormEvent;
use Mautic\DashboardBundle\Event\WidgetTypeListEvent;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand All @@ -25,14 +31,24 @@
*/
class WidgetType extends AbstractType
{
private $factory;
/**
* @var ContainerAwareEventDispatcher
*/
protected $dispatcher;

/**
* @var CorePermissions
*/
protected $security;

/**
* @param MauticFactory $factory
* @param EventDispatcherInterface $dispatcher
* @param CorePermissions $security
*/
public function __construct(MauticFactory $factory)
public function __construct(EventDispatcherInterface $dispatcher, CorePermissions $security)
{
$this->factory = $factory;
$this->dispatcher = $dispatcher;
$this->security = $security;
}

/**
Expand All @@ -41,60 +57,75 @@ public function __construct(MauticFactory $factory)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', [
'label' => 'mautic.dashboard.widget.form.name',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]);

$dispatcher = $this->factory->getDispatcher();
$event = new WidgetTypeListEvent();
$event->setSecurity($this->factory->getSecurity());
$dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_LIST_GENERATE, $event);

$builder->add('type', 'choice', [
'label' => 'mautic.dashboard.widget.form.type',
'choices' => $event->getTypes(),
'label_attr' => ['class' => 'control-label'],
'empty_value' => 'mautic.core.select',
'attr' => [
'class' => 'form-control',
'onchange' => 'Mautic.updateWidgetForm(this)',
],
]);

$builder->add('width', 'choice', [
'label' => 'mautic.dashboard.widget.form.width',
'choices' => [
'25' => '25%',
'50' => '50%',
'75' => '75%',
'100' => '100%',
],
'empty_data' => '100',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]);

$builder->add('height', 'choice', [
'label' => 'mautic.dashboard.widget.form.height',
'choices' => [
'215' => '215px',
'330' => '330px',
'445' => '445px',
'560' => '560px',
'675' => '675px',
],
'empty_data' => '330',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]);
$builder->add(
'name',
TextType::class,
[
'label' => 'mautic.dashboard.widget.form.name',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]
);

$event = new WidgetTypeListEvent();
$event->setSecurity($this->security);
$this->dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_LIST_GENERATE, $event);

$builder->add(
'type',
ChoiceType::class,
[
'label' => 'mautic.dashboard.widget.form.type',
'choices' => $event->getTypes(),
'label_attr' => ['class' => 'control-label'],
'empty_value' => 'mautic.core.select',
'attr' => [
'class' => 'form-control',
'onchange' => 'Mautic.updateWidgetForm(this)',
],
]
);

$builder->add(
'width',
ChoiceType::class,
[
'label' => 'mautic.dashboard.widget.form.width',
'choices' => [
'25' => '25%',
'50' => '50%',
'75' => '75%',
'100' => '100%',
],
'empty_data' => '100',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]
);

$builder->add(
'height',
ChoiceType::class,
[
'label' => 'mautic.dashboard.widget.form.height',
'choices' => [
'215' => '215px',
'330' => '330px',
'445' => '445px',
'560' => '560px',
'675' => '675px',
],
'empty_data' => '330',
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
'required' => false,
]
);

// function to add a form for specific widget type dynamically
$func = function (FormEvent $e) use ($dispatcher) {
$func = function (FormEvent $e) {
$data = $e->getData();
$form = $e->getForm();
$event = new WidgetFormEvent();
Expand All @@ -115,7 +146,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

$event->setType($type);
$dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_FORM_GENERATE, $event);
$this->dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_FORM_GENERATE, $event);
$widgetForm = $event->getForm();
$form->setData($params);

Expand All @@ -126,28 +157,36 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
};

$builder->add('id', 'hidden', [
'mapped' => false,
]);

$builder->add('buttons', 'form_buttons', [
'apply_text' => false,
'save_text' => 'mautic.core.form.save',
]);
$builder->add(
'id',
HiddenType::class,
[
'mapped' => false,
]
);

$builder->add(
'buttons',
FormButtonsType::class,
[
'apply_text' => false,
'save_text' => 'mautic.core.form.save',
]
);

if (!empty($options['action'])) {
$builder->setAction($options['action']);
}

// Register the function above as EventListener on PreSet and PreBind
$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
$builder->addEventListener(FormEvents::PRE_BIND, $func);
$builder->addEventListener(FormEvents::PRE_SUBMIT, $func);
}

/**
* @return string
*/
public function getName()
public function getBlockPrefix()
{
return 'widget';
}
Expand Down
3 changes: 2 additions & 1 deletion app/bundles/DashboardBundle/Model/DashboardModel.php
Expand Up @@ -19,6 +19,7 @@
use Mautic\DashboardBundle\DashboardEvents;
use Mautic\DashboardBundle\Entity\Widget;
use Mautic\DashboardBundle\Event\WidgetDetailEvent;
use Mautic\DashboardBundle\Form\Type\WidgetType;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Session\Session;
Expand Down Expand Up @@ -299,7 +300,7 @@ public function createForm($entity, $formFactory, $action = null, $options = [])
$options['action'] = $action;
}

return $formFactory->create('widget', $entity, $options);
return $formFactory->create(WidgetType::class, $entity, $options);
}

/**
Expand Down

0 comments on commit d41a8b0

Please sign in to comment.