diff --git a/app/bundles/DashboardBundle/Config/config.php b/app/bundles/DashboardBundle/Config/config.php index 91bb4096028..bd770e069fa 100644 --- a/app/bundles/DashboardBundle/Config/config.php +++ b/app/bundles/DashboardBundle/Config/config.php @@ -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' => [ diff --git a/app/bundles/DashboardBundle/Controller/AjaxController.php b/app/bundles/DashboardBundle/Controller/AjaxController.php index c2974ab5d86..73cff878ff9 100644 --- a/app/bundles/DashboardBundle/Controller/AjaxController.php +++ b/app/bundles/DashboardBundle/Controller/AjaxController.php @@ -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; /** @@ -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; diff --git a/app/bundles/DashboardBundle/Controller/DashboardController.php b/app/bundles/DashboardBundle/Controller/DashboardController.php index 524db31cc13..c8df52d5e78 100644 --- a/app/bundles/DashboardBundle/Controller/DashboardController.php +++ b/app/bundles/DashboardBundle/Controller/DashboardController.php @@ -11,9 +11,10 @@ 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; @@ -21,7 +22,7 @@ /** * Class DashboardController. */ -class DashboardController extends FormController +class DashboardController extends AbstractFormController { /** * Generates the default view. @@ -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)) { diff --git a/app/bundles/DashboardBundle/Form/Type/UploadType.php b/app/bundles/DashboardBundle/Form/Type/UploadType.php index 56a3275ed9a..41ead467c30 100644 --- a/app/bundles/DashboardBundle/Form/Type/UploadType.php +++ b/app/bundles/DashboardBundle/Form/Type/UploadType.php @@ -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; /** @@ -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', @@ -53,7 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) /** * @return string */ - public function getName() + public function getBlockPrefix() { return 'dashboard_upload'; } diff --git a/app/bundles/DashboardBundle/Form/Type/WidgetType.php b/app/bundles/DashboardBundle/Form/Type/WidgetType.php index c94c33f1c4f..a2997f9ac98 100644 --- a/app/bundles/DashboardBundle/Form/Type/WidgetType.php +++ b/app/bundles/DashboardBundle/Form/Type/WidgetType.php @@ -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; @@ -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; } /** @@ -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(); @@ -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); @@ -126,14 +157,22 @@ 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']); @@ -141,13 +180,13 @@ public function buildForm(FormBuilderInterface $builder, array $options) // 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'; } diff --git a/app/bundles/DashboardBundle/Model/DashboardModel.php b/app/bundles/DashboardBundle/Model/DashboardModel.php index 32676a3bfb4..2f0f0025978 100644 --- a/app/bundles/DashboardBundle/Model/DashboardModel.php +++ b/app/bundles/DashboardBundle/Model/DashboardModel.php @@ -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; @@ -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); } /**