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\Api; |
||
13 | |||
14 | use Mautic\ApiBundle\Controller\CommonApiController; |
||
15 | use Mautic\ApiBundle\Serializer\Exclusion\FieldInclusionStrategy; |
||
16 | use Mautic\CampaignBundle\Entity\Campaign; |
||
17 | use Mautic\CampaignBundle\Entity\Event; |
||
18 | use Mautic\CampaignBundle\Model\EventLogModel; |
||
19 | use Mautic\CampaignBundle\Model\EventModel; |
||
20 | use Mautic\LeadBundle\Controller\LeadAccessTrait; |
||
21 | use Mautic\LeadBundle\Entity\Lead; |
||
22 | use Symfony\Component\HttpFoundation\Response; |
||
23 | use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
||
24 | |||
25 | /** |
||
26 | * Class EventLogApiController. |
||
27 | */ |
||
28 | class EventLogApiController extends CommonApiController |
||
29 | { |
||
30 | use LeadAccessTrait; |
||
31 | |||
32 | /** |
||
33 | * @var Campaign |
||
34 | */ |
||
35 | protected $campaign; |
||
36 | |||
37 | /** |
||
38 | * @var Lead |
||
39 | */ |
||
40 | protected $contact; |
||
41 | |||
42 | /** @var EventLogModel */ |
||
43 | protected $model; |
||
44 | |||
45 | public function initialize(FilterControllerEvent $event) |
||
46 | { |
||
47 | $this->model = $this->getModel('campaign.event_log'); |
||
48 | $this->entityClass = 'Mautic\CampaignBundle\Entity\LeadEventLog'; |
||
49 | $this->entityNameOne = 'event'; |
||
50 | $this->entityNameMulti = 'events'; |
||
51 | $this->parentChildrenLevelDepth = 1; |
||
52 | $this->serializerGroups = [ |
||
53 | 'campaignList', |
||
54 | 'ipAddressList', |
||
55 | 'log' => 'campaignEventLogDetails', |
||
56 | ]; |
||
57 | |||
58 | // Only include the id of the parent |
||
59 | $this->addExclusionStrategy(new FieldInclusionStrategy(['id'], 1, 'parent')); |
||
60 | |||
61 | parent::initialize($event); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return Response |
||
66 | */ |
||
67 | public function getEntitiesAction() |
||
68 | { |
||
69 | $this->serializerGroups['log'] = 'campaignEventStandaloneLogDetails'; |
||
70 | $this->serializerGroups[] = 'campaignEventStandaloneList'; |
||
71 | $this->serializerGroups[] = 'leadBasicList'; |
||
72 | |||
73 | return parent::getEntitiesAction(); // TODO: Change the autogenerated stub |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Get a list of events. |
||
78 | * |
||
79 | * @param $contactId |
||
80 | * @param null $campaignId |
||
81 | * |
||
82 | * @return \Symfony\Component\HttpFoundation\Response |
||
83 | */ |
||
84 | public function getContactEventsAction($contactId, $campaignId = null) |
||
85 | { |
||
86 | // Ensure contact exists and user has access |
||
87 | $contact = $this->checkLeadAccess($contactId, 'view'); |
||
88 | if ($contact instanceof Response) { |
||
89 | return $contact; |
||
90 | } |
||
91 | |||
92 | // Ensure campaign exists and user has access |
||
93 | if (!empty($campaignId)) { |
||
94 | $campaign = $this->getModel('campaign')->getEntity($campaignId); |
||
95 | if (null == $campaign || !$campaign->getId()) { |
||
96 | return $this->notFound(); |
||
97 | } |
||
98 | if (!$this->checkEntityAccess($campaign)) { |
||
99 | return $this->accessDenied(); |
||
100 | } |
||
101 | // Check that contact is part of the campaign |
||
102 | $membership = $campaign->getContactMembership($contact); |
||
103 | if (0 === count($membership)) { |
||
104 | return $this->returnError('mautic.campaign.error.contact_not_in_campaign', Response::HTTP_CONFLICT); |
||
105 | } |
||
106 | |||
107 | $this->campaign = $campaign; |
||
108 | $this->serializerGroups[] = 'campaignEventWithLogsList'; |
||
109 | $this->serializerGroups[] = 'campaignLeadList'; |
||
110 | } else { |
||
111 | unset($this->serializerGroups['log']); |
||
112 | $this->serializerGroups[] = 'campaignEventStandaloneList'; |
||
113 | $this->serializerGroups[] = 'campaignEventStandaloneLogDetails'; |
||
114 | } |
||
115 | |||
116 | $this->contact = $contact; |
||
117 | $this->extraGetEntitiesArguments = [ |
||
118 | 'contact_id' => $contactId, |
||
119 | 'campaign_id' => $campaignId, |
||
120 | ]; |
||
121 | |||
122 | return $this->getEntitiesAction(); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param $eventId |
||
127 | * @param $contactId |
||
128 | * |
||
129 | * @return \Symfony\Component\HttpFoundation\Response |
||
130 | */ |
||
131 | public function editContactEventAction($eventId, $contactId) |
||
132 | { |
||
133 | $parameters = $this->request->request->all(); |
||
134 | |||
135 | // Ensure contact exists and user has access |
||
136 | $contact = $this->checkLeadAccess($contactId, 'edit'); |
||
137 | if ($contact instanceof Response) { |
||
138 | return $contact; |
||
139 | } |
||
140 | |||
141 | /** @var EventModel $eventModel */ |
||
142 | $eventModel = $this->getModel('campaign.event'); |
||
143 | /** @var Event $event */ |
||
144 | $event = $eventModel->getEntity($eventId); |
||
145 | if (null === $event || !$event->getId()) { |
||
146 | return $this->notFound(); |
||
147 | } |
||
148 | |||
149 | // Ensure campaign edit access |
||
150 | $campaign = $event->getCampaign(); |
||
151 | if (!$this->checkEntityAccess($campaign, 'edit')) { |
||
152 | return $this->accessDenied(); |
||
153 | } |
||
154 | |||
155 | $result = $this->model->updateContactEvent($event, $contact, $parameters); |
||
156 | |||
157 | if (is_string($result)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
158 | return $this->returnError($result, Response::HTTP_CONFLICT); |
||
159 | } else { |
||
160 | list($log, $created) = $result; |
||
161 | } |
||
162 | |||
163 | $event->addContactLog($log); |
||
164 | $view = $this->view( |
||
165 | [ |
||
166 | $this->entityNameOne => $event, |
||
167 | ], |
||
168 | ($created) ? Response::HTTP_CREATED : Response::HTTP_OK |
||
169 | ); |
||
170 | $this->serializerGroups[] = 'campaignEventWithLogsDetails'; |
||
171 | $this->serializerGroups[] = 'campaignBasicList'; |
||
172 | $this->setSerializationContext($view); |
||
173 | |||
174 | return $this->handleView($view); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return array|Response |
||
179 | */ |
||
180 | public function editEventsAction() |
||
181 | { |
||
182 | $parameters = $this->request->request->all(); |
||
183 | |||
184 | $valid = $this->validateBatchPayload($parameters); |
||
185 | if ($valid instanceof Response) { |
||
186 | return $valid; |
||
187 | } |
||
188 | |||
189 | $events = $this->getBatchEntities($parameters, $errors, false, 'eventId', $this->getModel('campaign.event'), false); |
||
190 | $contacts = $this->getBatchEntities($parameters, $errors, false, 'contactId', $this->getModel('lead'), false); |
||
191 | |||
192 | $this->inBatchMode = true; |
||
193 | $errors = []; |
||
194 | foreach ($parameters as $key => $params) { |
||
195 | if (!isset($params['eventId']) || !isset($params['contactId']) || !isset($events[$params['eventId']]) |
||
196 | || !isset($contacts[$params['contactId']]) |
||
197 | ) { |
||
198 | $errors[$key] = $this->notFound('mautic.campaign.error.edit_events.request_invalid'); |
||
199 | |||
200 | continue; |
||
201 | } |
||
202 | |||
203 | $event = $events[$params['eventId']]; |
||
204 | |||
205 | // Ensure contact exists and user has access |
||
206 | $contact = $this->checkLeadAccess($contacts[$params['contactId']], 'edit'); |
||
207 | if ($contact instanceof Response) { |
||
208 | $errors[$key] = $contact->getContent(); |
||
209 | |||
210 | continue; |
||
211 | } |
||
212 | |||
213 | // Ensure campaign edit access |
||
214 | $campaign = $event->getCampaign(); |
||
215 | if (!$this->checkEntityAccess($campaign, 'edit')) { |
||
216 | $errors[$key] = $this->accessDenied(); |
||
217 | |||
218 | continue; |
||
219 | } |
||
220 | |||
221 | $result = $this->model->updateContactEvent($event, $contact, $params); |
||
222 | |||
223 | if (is_string($result)) { |
||
224 | $errors[$key] = $this->returnError($result, Response::HTTP_CONFLICT); |
||
225 | } else { |
||
226 | list($log, $created) = $result; |
||
227 | $event->addContactLog($log); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | $payload = [ |
||
232 | $this->entityNameMulti => $events, |
||
233 | ]; |
||
234 | |||
235 | if (!empty($errors)) { |
||
236 | $payload['errors'] = $errors; |
||
237 | } |
||
238 | |||
239 | $view = $this->view($payload, Response::HTTP_OK); |
||
240 | $this->serializerGroups[] = 'campaignEventWithLogsList'; |
||
241 | $this->setSerializationContext($view); |
||
242 | |||
243 | return $this->handleView($view); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param null $data |
||
248 | * @param null $statusCode |
||
249 | * |
||
250 | * @return \FOS\RestBundle\View\View |
||
251 | */ |
||
252 | protected function view($data = null, $statusCode = null, array $headers = []) |
||
253 | { |
||
254 | if ($this->campaign) { |
||
255 | $data['campaign'] = $this->campaign; |
||
256 | |||
257 | if ($this->contact) { |
||
258 | list($data['membership'], $ignore) = $this->prepareEntitiesForView($this->campaign->getContactMembership($this->contact)); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | return parent::view($data, $statusCode, $headers); |
||
263 | } |
||
264 | } |
||
265 |