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\CampaignBundle\Controller; |
||
13 | |||
14 | use Mautic\CampaignBundle\Form\Type\CampaignLeadSourceType; |
||
15 | use Mautic\CoreBundle\Controller\FormController as CommonFormController; |
||
16 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
17 | |||
18 | class SourceController extends CommonFormController |
||
19 | { |
||
20 | private $supportedSourceTypes = ['lists', 'forms']; |
||
21 | |||
22 | /** |
||
23 | * @param int $objectId |
||
24 | * |
||
25 | * @return JsonResponse |
||
26 | */ |
||
27 | public function newAction($objectId = 0) |
||
28 | { |
||
29 | $success = 0; |
||
30 | $valid = $cancelled = false; |
||
31 | $method = $this->request->getMethod(); |
||
32 | $session = $this->get('session'); |
||
33 | if ('POST' == $method) { |
||
34 | $source = $this->request->request->get('campaign_leadsource'); |
||
35 | $sourceType = $source['sourceType']; |
||
36 | } else { |
||
37 | $sourceType = $this->request->query->get('sourceType'); |
||
38 | $source = [ |
||
39 | 'sourceType' => $sourceType, |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | //set the sourceType key for sources |
||
44 | if (!in_array($sourceType, $this->supportedSourceTypes)) { |
||
45 | return $this->modalAccessDenied(); |
||
46 | } |
||
47 | |||
48 | //ajax only for form fields |
||
49 | if (!$this->request->isXmlHttpRequest() |
||
50 | || !$this->get('mautic.security')->isGranted( |
||
51 | [ |
||
52 | 'campaign:campaigns:edit', |
||
53 | 'campaign:campaigns:create', |
||
54 | ], |
||
55 | 'MATCH_ONE' |
||
56 | ) |
||
57 | ) { |
||
58 | return $this->modalAccessDenied(); |
||
59 | } |
||
60 | |||
61 | $sourceList = $this->getModel('campaign')->getSourceLists($sourceType); |
||
62 | $form = $this->get('form.factory')->create( |
||
63 | CampaignLeadSourceType::class, |
||
64 | $source, |
||
65 | [ |
||
66 | 'action' => $this->generateUrl('mautic_campaignsource_action', ['objectAction' => 'new', 'objectId' => $objectId]), |
||
67 | 'source_choices' => $sourceList, |
||
68 | ] |
||
69 | ); |
||
70 | |||
71 | //Check for a submitted form and process it |
||
72 | if ('POST' == $method) { |
||
73 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
74 | if ($valid = $this->isFormValid($form)) { |
||
75 | $success = 1; |
||
76 | |||
77 | $modifiedSources = $session->get('mautic.campaign.'.$objectId.'.leadsources.modified'); |
||
78 | $modifiedSources[$sourceType] = array_flip($form[$sourceType]->getData()); |
||
79 | $session->set('mautic.campaign.'.$objectId.'.leadsources.modified', $modifiedSources); |
||
80 | } else { |
||
81 | $success = 0; |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | $passthroughVars = [ |
||
87 | 'mauticContent' => 'campaignSource', |
||
88 | 'success' => $success, |
||
89 | 'route' => false, |
||
90 | ]; |
||
91 | |||
92 | if ($cancelled || $valid) { |
||
93 | if ($valid) { |
||
94 | $passthroughVars['sourceHtml'] = $this->renderView( |
||
95 | 'MauticCampaignBundle:Source:index.html.php', |
||
96 | [ |
||
97 | 'sourceType' => $sourceType, |
||
98 | 'campaignId' => $objectId, |
||
99 | 'names' => implode(', ', array_intersect_key($sourceList, $modifiedSources[$sourceType])), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
100 | ] |
||
101 | ); |
||
102 | $passthroughVars['sourceType'] = $sourceType; |
||
103 | } |
||
104 | |||
105 | //just close the modal |
||
106 | $passthroughVars['closeModal'] = 1; |
||
107 | |||
108 | return new JsonResponse($passthroughVars); |
||
109 | } else { |
||
110 | $viewParams = [ |
||
111 | 'sourceType' => $sourceType, |
||
112 | 'form' => $form->createView(), |
||
113 | ]; |
||
114 | |||
115 | return $this->ajaxAction( |
||
116 | [ |
||
117 | 'contentTemplate' => 'MauticCampaignBundle:Source:form.html.php', |
||
118 | 'viewParameters' => $viewParams, |
||
119 | 'passthroughVars' => $passthroughVars, |
||
120 | ] |
||
121 | ); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param $objectId |
||
127 | * |
||
128 | * @return JsonResponse |
||
129 | */ |
||
130 | public function editAction($objectId) |
||
131 | { |
||
132 | $session = $this->get('session'); |
||
133 | $method = $this->request->getMethod(); |
||
134 | $modifiedSources = $selectedSources = $session->get('mautic.campaign.'.$objectId.'.leadsources.modified', []); |
||
135 | if ('POST' == $method) { |
||
136 | $source = $this->request->request->get('campaign_leadsource'); |
||
137 | $sourceType = $source['sourceType']; |
||
138 | } else { |
||
139 | $sourceType = $this->request->query->get('sourceType'); |
||
140 | $source = [ |
||
141 | 'sourceType' => $sourceType, |
||
142 | $sourceType => array_flip($selectedSources[$sourceType]), |
||
143 | ]; |
||
144 | } |
||
145 | |||
146 | $success = 0; |
||
147 | $valid = $cancelled = false; |
||
148 | |||
149 | if (!in_array($sourceType, $this->supportedSourceTypes)) { |
||
150 | return $this->modalAccessDenied(); |
||
151 | } |
||
152 | |||
153 | //ajax only for form fields |
||
154 | if (!$this->request->isXmlHttpRequest() |
||
155 | || !$this->get('mautic.security')->isGranted( |
||
156 | [ |
||
157 | 'campaign:campaigns:edit', |
||
158 | 'campaign:campaigns:create', |
||
159 | ], |
||
160 | 'MATCH_ONE' |
||
161 | ) |
||
162 | ) { |
||
163 | return $this->modalAccessDenied(); |
||
164 | } |
||
165 | |||
166 | $sourceList = $this->getModel('campaign')->getSourceLists($sourceType); |
||
167 | $form = $this->get('form.factory')->create( |
||
168 | CampaignLeadSourceType::class, |
||
169 | $source, |
||
170 | [ |
||
171 | 'action' => $this->generateUrl('mautic_campaignsource_action', ['objectAction' => 'edit', 'objectId' => $objectId]), |
||
172 | 'source_choices' => $sourceList, |
||
173 | ] |
||
174 | ); |
||
175 | |||
176 | //Check for a submitted form and process it |
||
177 | if ('POST' == $method) { |
||
178 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
179 | if ($valid = $this->isFormValid($form)) { |
||
180 | $success = 1; |
||
181 | |||
182 | //save the properties to session |
||
183 | $modifiedSources[$sourceType] = array_flip($form[$sourceType]->getData()); |
||
184 | $session->set('mautic.campaign.'.$objectId.'.leadsources.modified', $modifiedSources); |
||
185 | } else { |
||
186 | $success = 0; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | $passthroughVars = [ |
||
192 | 'mauticContent' => 'campaignSource', |
||
193 | 'success' => $success, |
||
194 | 'route' => false, |
||
195 | ]; |
||
196 | |||
197 | if ($cancelled || $valid) { |
||
198 | if ($valid) { |
||
199 | $passthroughVars['updateHtml'] = $this->renderView( |
||
200 | 'MauticCampaignBundle:Source:index.html.php', |
||
201 | [ |
||
202 | 'sourceType' => $sourceType, |
||
203 | 'campaignId' => $objectId, |
||
204 | 'names' => implode(', ', array_intersect_key($sourceList, $modifiedSources[$sourceType])), |
||
205 | 'update' => true, |
||
206 | ] |
||
207 | ); |
||
208 | $passthroughVars['sourceType'] = $sourceType; |
||
209 | } |
||
210 | |||
211 | //just close the modal |
||
212 | $passthroughVars['closeModal'] = 1; |
||
213 | |||
214 | return new JsonResponse($passthroughVars); |
||
215 | } else { |
||
216 | $viewParams = [ |
||
217 | 'sourceType' => $sourceType, |
||
218 | 'form' => $form->createView(), |
||
219 | ]; |
||
220 | |||
221 | return $this->ajaxAction( |
||
222 | [ |
||
223 | 'contentTemplate' => 'MauticCampaignBundle:Source:form.html.php', |
||
224 | 'viewParameters' => $viewParams, |
||
225 | 'passthroughVars' => $passthroughVars, |
||
226 | ] |
||
227 | ); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Deletes the entity. |
||
233 | * |
||
234 | * @param $objectId |
||
235 | * |
||
236 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
237 | */ |
||
238 | public function deleteAction($objectId) |
||
239 | { |
||
240 | $session = $this->get('session'); |
||
241 | $modifiedSources = $session->get('mautic.campaign.'.$objectId.'.leadsources.modified', []); |
||
242 | $sourceType = $this->request->get('sourceType'); |
||
243 | |||
244 | //ajax only for form fields |
||
245 | if (!$this->request->isXmlHttpRequest() |
||
246 | || !$this->get('mautic.security')->isGranted( |
||
247 | [ |
||
248 | 'campaign:campaigns:edit', |
||
249 | 'campaign:campaigns:create', |
||
250 | ], |
||
251 | 'MATCH_ONE' |
||
252 | ) |
||
253 | ) { |
||
254 | return $this->accessDenied(); |
||
255 | } |
||
256 | |||
257 | if ('POST' == $this->request->getMethod()) { |
||
258 | // Add the field to the delete list |
||
259 | if (isset($modifiedSources[$sourceType])) { |
||
260 | unset($modifiedSources[$sourceType]); |
||
261 | $session->set('mautic.campaign.'.$objectId.'.leadsources.modified', $modifiedSources); |
||
262 | } |
||
263 | |||
264 | $dataArray = [ |
||
265 | 'mauticContent' => 'campaignSource', |
||
266 | 'success' => 1, |
||
267 | 'route' => false, |
||
268 | 'sourceType' => $sourceType, |
||
269 | 'deleted' => 1, |
||
270 | ]; |
||
271 | } else { |
||
272 | $dataArray = ['success' => 0]; |
||
273 | } |
||
274 | |||
275 | return new JsonResponse($dataArray); |
||
276 | } |
||
277 | } |
||
278 |