Issues (3627)

Controller/BatchContactController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CategoryBundle\Controller;
13
14
use Mautic\CategoryBundle\Model\CategoryModel;
15
use Mautic\CategoryBundle\Model\ContactActionModel;
16
use Mautic\CoreBundle\Controller\AbstractFormController;
17
use Mautic\LeadBundle\Form\Type\BatchType;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
20
21
class BatchContactController extends AbstractFormController
22
{
23
    /**
24
     * @var ContactActionModel
25
     */
26
    private $actionModel;
27
28
    /**
29
     * @var CategoryModel
30
     */
31
    private $categoryModel;
32
33
    /**
34
     * Initialize object props here to simulate constructor
35
     * and make the future controller refactoring easier.
36
     */
37
    public function initialize(FilterControllerEvent $event)
38
    {
39
        $this->actionModel   = $this->container->get('mautic.category.model.contact.action');
40
        $this->categoryModel = $this->container->get('mautic.category.model.category');
41
    }
42
43
    /**
44
     * Adds or removes categories to multiple contacts defined by contact ID.
45
     *
46
     * @return JsonResponse
47
     */
48
    public function execAction()
49
    {
50
        $params = $this->request->get('lead_batch');
51
        $ids    = empty($params['ids']) ? [] : json_decode($params['ids']);
52
53
        if ($ids && is_array($ids)) {
54
            $categoriesToAdd    = isset($params['add']) ? $params['add'] : [];
55
            $categoriesToRemove = isset($params['remove']) ? $params['remove'] : [];
56
            $contactIds         = json_decode($params['ids']);
57
58
            $this->actionModel->addContactsToCategories($contactIds, $categoriesToAdd);
59
            $this->actionModel->removeContactsFromCategories($contactIds, $categoriesToRemove);
60
61
            $this->addFlash('mautic.lead.batch_leads_affected', [
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            /** @scrutinizer ignore-deprecated */ $this->addFlash('mautic.lead.batch_leads_affected', [

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
                'pluralCount' => count($ids),
63
                '%count%'     => count($ids),
64
            ]);
65
        } else {
66
            $this->addFlash('mautic.core.error.ids.missing');
67
        }
68
69
        return new JsonResponse([
70
            'closeModal' => true,
71
            'flashes'    => $this->getFlashContent(),
72
        ]);
73
    }
74
75
    /**
76
     * View the modal form for adding contacts into categories in batches.
77
     *
78
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
79
     */
80
    public function indexAction()
81
    {
82
        $route = $this->generateUrl('mautic_category_batch_contact_set');
83
        $rows  = $this->categoryModel->getLookupResults('global', '', 300);
84
        $items = [];
85
86
        foreach ($rows as $category) {
87
            $items[$category['title']] = $category['id'];
88
        }
89
90
        return $this->delegateView(
91
            [
92
                'viewParameters' => [
93
                    'form' => $this->createForm(
94
                        BatchType::class,
95
                        [],
96
                        [
97
                            'items'  => $items,
98
                            'action' => $route,
99
                        ]
100
                    )->createView(),
101
                ],
102
                'contentTemplate' => 'MauticLeadBundle:Batch:form.html.php',
103
                'passthroughVars' => [
104
                    'activeLink'    => '#mautic_contact_index',
105
                    'mauticContent' => 'leadBatch',
106
                    'route'         => $route,
107
                ],
108
            ]
109
        );
110
    }
111
}
112