Issues (3627)

bundles/LeadBundle/Controller/NoteController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 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\LeadBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\CoreBundle\Helper\InputHelper;
16
use Mautic\LeadBundle\Entity\LeadNote;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Response;
19
20
class NoteController extends FormController
0 ignored issues
show
Deprecated Code introduced by
The class Mautic\CoreBundle\Controller\FormController has been deprecated: 2.3 - to be removed in 3.0; use AbstractFormController instead ( Ignorable by Annotation )

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

20
class NoteController extends /** @scrutinizer ignore-deprecated */ FormController
Loading history...
21
{
22
    use LeadAccessTrait;
23
24
    /**
25
     * Generate's default list view.
26
     *
27
     * @param $leadId
28
     *
29
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
30
     */
31
    public function indexAction($leadId = 0, $page = 1)
32
    {
33
        if (empty($leadId)) {
34
            return $this->accessDenied();
35
        }
36
37
        $lead = $this->checkLeadAccess($leadId, 'view');
38
        if ($lead instanceof Response) {
39
            return $lead;
40
        }
41
42
        $this->setListFilters();
43
44
        $session = $this->get('session');
45
46
        //set limits
47
        $limit = $session->get(
48
            'mautic.lead.'.$lead->getId().'.note.limit',
49
            $this->get('mautic.helper.core_parameters')->get('default_pagelimit')
50
        );
51
        $start = (1 === $page) ? 0 : (($page - 1) * $limit);
52
        if ($start < 0) {
53
            $start = 0;
54
        }
55
56
        $search = $this->request->get('search', $session->get('mautic.lead.'.$lead->getId().'.note.filter', ''));
57
        $session->set('mautic.lead.'.$lead->getId().'.note.filter', $search);
58
59
        //do some default filtering
60
        $orderBy    = $session->get('mautic.lead.'.$lead->getId().'.note.orderby', 'n.dateTime');
61
        $orderByDir = $session->get('mautic.lead.'.$lead->getId().'.note.orderbydir', 'DESC');
62
63
        $model = $this->getModel('lead.note');
64
        $force = [
65
            [
66
                'column' => 'n.lead',
67
                'expr'   => 'eq',
68
                'value'  => $lead,
69
            ],
70
        ];
71
72
        $tmpl     = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';
73
        $noteType = InputHelper::clean($this->request->request->get('noteTypes', [], true));
74
        if (empty($noteType) && 'index' == $tmpl) {
75
            $noteType = $session->get('mautic.lead.'.$lead->getId().'.notetype.filter', []);
76
        }
77
        $session->set('mautic.lead.'.$lead->getId().'.notetype.filter', $noteType);
78
79
        $noteTypes = [
80
            'general' => 'mautic.lead.note.type.general',
81
            'email'   => 'mautic.lead.note.type.email',
82
            'call'    => 'mautic.lead.note.type.call',
83
            'meeting' => 'mautic.lead.note.type.meeting',
84
        ];
85
86
        if (!empty($noteType)) {
87
            $force[] = [
88
                'column' => 'n.type',
89
                'expr'   => 'in',
90
                'value'  => $noteType,
91
            ];
92
        }
93
94
        $items = $model->getEntities(
95
            [
96
                'filter' => [
97
                    'force'  => $force,
98
                    'string' => $search,
99
                ],
100
                'start'          => $start,
101
                'limit'          => $limit,
102
                'orderBy'        => $orderBy,
103
                'orderByDir'     => $orderByDir,
104
                'hydration_mode' => 'HYDRATE_ARRAY',
105
            ]
106
        );
107
108
        $security = $this->get('mautic.security');
109
110
        return $this->delegateView(
111
            [
112
                'viewParameters' => [
113
                    'notes'       => $items,
114
                    'lead'        => $lead,
115
                    'page'        => $page,
116
                    'limit'       => $limit,
117
                    'search'      => $search,
118
                    'noteType'    => $noteType,
119
                    'noteTypes'   => $noteTypes,
120
                    'tmpl'        => $tmpl,
121
                    'permissions' => [
122
                        'edit'   => $security->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $lead->getPermissionUser()),
123
                        'delete' => $security->hasEntityAccess('lead:leads:deleteown', 'lead:leads:deleteown', $lead->getPermissionUser()),
124
                    ],
125
                ],
126
                'passthroughVars' => [
127
                    'route'         => false,
128
                    'mauticContent' => 'leadNote',
129
                    'noteCount'     => count($items),
130
                ],
131
                'contentTemplate' => 'MauticLeadBundle:Note:list.html.php',
132
            ]
133
        );
134
    }
135
136
    /**
137
     * Generate's new note and processes post data.
138
     *
139
     * @param $leadId
140
     *
141
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
142
     */
143
    public function newAction($leadId)
144
    {
145
        $lead = $this->checkLeadAccess($leadId, 'view');
146
        if ($lead instanceof Response) {
147
            return $lead;
148
        }
149
150
        //retrieve the entity
151
        $note = new LeadNote();
152
        $note->setLead($lead);
153
154
        $model  = $this->getModel('lead.note');
155
        $action = $this->generateUrl(
156
            'mautic_contactnote_action',
157
            [
158
                'objectAction' => 'new',
159
                'leadId'       => $leadId,
160
            ]
161
        );
162
        //get the user form factory
163
        $form       = $model->createForm($note, $this->get('form.factory'), $action);
164
        $closeModal = false;
165
        $valid      = false;
166
        ///Check for a submitted form and process it
167
        if ('POST' == $this->request->getMethod()) {
168
            if (!$cancelled = $this->isFormCancelled($form)) {
169
                if ($valid = $this->isFormValid($form)) {
170
                    $closeModal = true;
171
172
                    //form is valid so process the data
173
                    $model->saveEntity($note);
174
                }
175
            } else {
176
                $closeModal = true;
177
            }
178
        }
179
180
        $security    = $this->get('mautic.security');
181
        $permissions = [
182
            'edit'   => $security->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $lead->getPermissionUser()),
183
            'delete' => $security->hasEntityAccess('lead:leads:deleteown', 'lead:leads:deleteown', $lead->getPermissionUser()),
184
        ];
185
186
        if ($closeModal) {
187
            //just close the modal
188
            $passthroughVars = [
189
                'closeModal'    => 1,
190
                'mauticContent' => 'leadNote',
191
            ];
192
193
            if ($valid && !$cancelled) {
194
                $passthroughVars['upNoteCount'] = 1;
195
                $passthroughVars['noteHtml']    = $this->renderView(
196
                    'MauticLeadBundle:Note:note.html.php',
197
                    [
198
                        'note'        => $note,
199
                        'lead'        => $lead,
200
                        'permissions' => $permissions,
201
                    ]
202
                );
203
                $passthroughVars['noteId'] = $note->getId();
204
            }
205
206
            return new JsonResponse($passthroughVars);
207
        } else {
208
            return $this->delegateView(
209
                [
210
                    'viewParameters' => [
211
                        'form'        => $form->createView(),
212
                        'lead'        => $lead,
213
                        'permissions' => $permissions,
214
                    ],
215
                    'contentTemplate' => 'MauticLeadBundle:Note:form.html.php',
216
                ]
217
            );
218
        }
219
    }
220
221
    /**
222
     * Generate's edit form and processes post data.
223
     *
224
     * @param $leadId
225
     * @param $objectId
226
     *
227
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
228
     */
229
    public function editAction($leadId, $objectId)
230
    {
231
        $lead = $this->checkLeadAccess($leadId, 'view');
232
        if ($lead instanceof Response) {
233
            return $lead;
234
        }
235
236
        $model      = $this->getModel('lead.note');
237
        $note       = $model->getEntity($objectId);
238
        $closeModal = false;
239
        $valid      = false;
240
241
        if (null === $note || !$this->get('mautic.security')->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $lead->getPermissionUser())) {
242
            return $this->accessDenied();
243
        }
244
245
        $action = $this->generateUrl(
246
            'mautic_contactnote_action',
247
            [
248
                'objectAction' => 'edit',
249
                'objectId'     => $objectId,
250
                'leadId'       => $leadId,
251
            ]
252
        );
253
        $form = $model->createForm($note, $this->get('form.factory'), $action);
254
255
        ///Check for a submitted form and process it
256
        if ('POST' == $this->request->getMethod()) {
257
            if (!$cancelled = $this->isFormCancelled($form)) {
258
                if ($valid = $this->isFormValid($form)) {
259
                    //form is valid so process the data
260
                    $model->saveEntity($note);
261
                    $closeModal = true;
262
                }
263
            } else {
264
                $closeModal = true;
265
            }
266
        }
267
268
        $security    = $this->get('mautic.security');
269
        $permissions = [
270
            'edit'   => $security->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $lead->getPermissionUser()),
271
            'delete' => $security->hasEntityAccess('lead:leads:deleteown', 'lead:leads:deleteown', $lead->getPermissionUser()),
272
        ];
273
274
        if ($closeModal) {
275
            //just close the modal
276
            $passthroughVars['closeModal'] = 1;
277
278
            if ($valid && !$cancelled) {
279
                $passthroughVars['noteHtml'] = $this->renderView(
280
                    'MauticLeadBundle:Note:note.html.php',
281
                    [
282
                        'note'        => $note,
283
                        'lead'        => $lead,
284
                        'permissions' => $permissions,
285
                    ]
286
                );
287
                $passthroughVars['noteId'] = $note->getId();
288
            }
289
290
            $passthroughVars['mauticContent'] = 'leadNote';
291
292
            return new JsonResponse($passthroughVars);
293
        } else {
294
            return $this->delegateView(
295
                [
296
                    'viewParameters' => [
297
                        'form'        => $form->createView(),
298
                        'lead'        => $lead,
299
                        'permissions' => $permissions,
300
                    ],
301
                    'contentTemplate' => 'MauticLeadBundle:Note:form.html.php',
302
                ]
303
            );
304
        }
305
    }
306
307
    /**
308
     * Deletes the entity.
309
     *
310
     * @param $objectId
311
     *
312
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
313
     */
314
    public function deleteAction($leadId, $objectId)
315
    {
316
        $lead = $this->checkLeadAccess($leadId, 'view');
317
        if ($lead instanceof Response) {
318
            return $lead;
319
        }
320
321
        $model = $this->getModel('lead.note');
322
        $note  = $model->getEntity($objectId);
323
324
        if (null === $note) {
325
            return $this->notFound();
326
        }
327
328
        if (
329
            !$this->get('mautic.security')->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $lead->getPermissionUser())
330
            || $model->isLocked($note)
331
        ) {
332
            return $this->accessDenied();
333
        }
334
335
        $model->deleteEntity($note);
336
337
        return new JsonResponse(
338
            [
339
                'deleteId'      => $objectId,
340
                'mauticContent' => 'leadNote',
341
                'downNoteCount' => 1,
342
            ]
343
        );
344
    }
345
346
    /**
347
     * Executes an action defined in route.
348
     *
349
     * @param     $objectAction
350
     * @param int $objectId
351
     * @param int $leadId
352
     *
353
     * @return Response
354
     */
355
    public function executeNoteAction($objectAction, $objectId = 0, $leadId = 0)
356
    {
357
        if (method_exists($this, "{$objectAction}Action")) {
358
            return $this->{"{$objectAction}Action"}($leadId, $objectId);
359
        } else {
360
            return $this->accessDenied();
361
        }
362
    }
363
}
364