1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\CampaignBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\Form\FormInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
use Oro\Bundle\SecurityBundle\Annotation\Acl; |
12
|
|
|
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor; |
13
|
|
|
use OroCRM\Bundle\CampaignBundle\Entity\EmailCampaign; |
14
|
|
|
use OroCRM\Bundle\CampaignBundle\Form\Handler\EmailCampaignHandler; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @Route("/campaign/email") |
18
|
|
|
*/ |
19
|
|
|
class EmailCampaignController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/", name="orocrm_email_campaign_index") |
23
|
|
|
* @AclAncestor("orocrm_email_campaign_view") |
24
|
|
|
* @Template |
25
|
|
|
*/ |
26
|
|
|
public function indexAction() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'entity_class' => $this->container->getParameter('orocrm_campaign.email_campaign.entity.class') |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create email campaign |
35
|
|
|
* |
36
|
|
|
* @Route("/create", name="orocrm_email_campaign_create") |
37
|
|
|
* @Template("OroCRMCampaignBundle:EmailCampaign:update.html.twig") |
38
|
|
|
* @Acl( |
39
|
|
|
* id="orocrm_email_campaign_create", |
40
|
|
|
* type="entity", |
41
|
|
|
* permission="CREATE", |
42
|
|
|
* class="OroCRMCampaignBundle:EmailCampaign" |
43
|
|
|
* ) |
44
|
|
|
*/ |
45
|
|
|
public function createAction() |
46
|
|
|
{ |
47
|
|
|
return $this->update(new EmailCampaign()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Edit email campaign |
52
|
|
|
* |
53
|
|
|
* @Route("/update/{id}", name="orocrm_email_campaign_update", requirements={"id"="\d+"}, defaults={"id"=0}) |
54
|
|
|
* @Template |
55
|
|
|
* @Acl( |
56
|
|
|
* id="orocrm_email_campaign_update", |
57
|
|
|
* type="entity", |
58
|
|
|
* permission="EDIT", |
59
|
|
|
* class="OroCRMCampaignBundle:EmailCampaign" |
60
|
|
|
* ) |
61
|
|
|
* @param EmailCampaign $entity |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function updateAction(EmailCampaign $entity) |
65
|
|
|
{ |
66
|
|
|
return $this->update($entity); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* View email campaign |
71
|
|
|
* |
72
|
|
|
* @Route("/view/{id}", name="orocrm_email_campaign_view", requirements={"id"="\d+"}) |
73
|
|
|
* @Acl( |
74
|
|
|
* id="orocrm_email_campaign_view", |
75
|
|
|
* type="entity", |
76
|
|
|
* permission="VIEW", |
77
|
|
|
* class="OroCRMCampaignBundle:EmailCampaign" |
78
|
|
|
* ) |
79
|
|
|
* @Template |
80
|
|
|
* @param EmailCampaign $entity |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function viewAction(EmailCampaign $entity) |
84
|
|
|
{ |
85
|
|
|
$stats = $this->getDoctrine() |
86
|
|
|
->getRepository("OroCRMCampaignBundle:EmailCampaignStatistics") |
87
|
|
|
->getEmailCampaignStats($entity); |
88
|
|
|
|
89
|
|
|
return [ |
90
|
|
|
'entity' => $entity, |
91
|
|
|
'stats' => $stats, |
92
|
|
|
'show_stats' => (bool) array_sum($stats), |
93
|
|
|
'send_allowed' => $this->isManualSendAllowed($entity) |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Process save email campaign entity |
99
|
|
|
* |
100
|
|
|
* @param EmailCampaign $entity |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
protected function update(EmailCampaign $entity) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if ($this->get('orocrm_campaign.form.handler.email_campaign')->process($entity)) { |
106
|
|
|
$this->get('session')->getFlashBag()->add( |
107
|
|
|
'success', |
108
|
|
|
$this->get('translator')->trans('orocrm.campaign.emailcampaign.controller.saved.message') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $this->get('oro_ui.router')->redirect($entity); |
112
|
|
|
} |
113
|
|
|
$form = $this->getForm(); |
114
|
|
|
|
115
|
|
|
return [ |
116
|
|
|
'entity' => $entity, |
117
|
|
|
'form' => $form->createView() |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns form instance |
123
|
|
|
* |
124
|
|
|
* @return FormInterface |
125
|
|
|
*/ |
126
|
|
|
protected function getForm() |
127
|
|
|
{ |
128
|
|
|
$isUpdateOnly = $this->get('request')->get(EmailCampaignHandler::UPDATE_MARKER, false); |
129
|
|
|
|
130
|
|
|
$form = $this->get('orocrm_campaign.email_campaign.form'); |
131
|
|
|
if ($isUpdateOnly) { |
132
|
|
|
// substitute submitted form with new not submitted instance to ignore validation errors |
133
|
|
|
// on form after transport field was changed |
134
|
|
|
$form = $this->get('form.factory') |
135
|
|
|
->createNamed('orocrm_email_campaign', 'orocrm_email_campaign', $form->getData()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $form; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @Route("/send/{id}", name="orocrm_email_campaign_send", requirements={"id"="\d+"}) |
143
|
|
|
* @Acl( |
144
|
|
|
* id="orocrm_email_campaign_send", |
145
|
|
|
* type="action", |
146
|
|
|
* label="orocrm.campaign.acl.send_emails.label", |
147
|
|
|
* description="orocrm.campaign.acl.send_emails.description", |
148
|
|
|
* group_name="" |
149
|
|
|
* ) |
150
|
|
|
* |
151
|
|
|
* @param EmailCampaign $entity |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function sendAction(EmailCampaign $entity) |
155
|
|
|
{ |
156
|
|
|
if ($this->isManualSendAllowed($entity)) { |
157
|
|
|
$senderFactory = $this->get('orocrm_campaign.email_campaign.sender.builder'); |
158
|
|
|
$sender = $senderFactory->getSender($entity); |
159
|
|
|
$sender->send($entity); |
160
|
|
|
|
161
|
|
|
$this->get('session')->getFlashBag()->add( |
162
|
|
|
'success', |
163
|
|
|
$this->get('translator')->trans('orocrm.campaign.emailcampaign.controller.sent') |
164
|
|
|
); |
165
|
|
|
} else { |
166
|
|
|
$this->get('session')->getFlashBag()->add( |
167
|
|
|
'error', |
168
|
|
|
$this->get('translator')->trans('orocrm.campaign.emailcampaign.controller.send_disallowed') |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->redirect( |
173
|
|
|
$this->generateUrl( |
174
|
|
|
'orocrm_email_campaign_view', |
175
|
|
|
['id' => $entity->getId()] |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param EmailCampaign $entity |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
protected function isManualSendAllowed(EmailCampaign $entity) |
185
|
|
|
{ |
186
|
|
|
$sendAllowed = $entity->getSchedule() === EmailCampaign::SCHEDULE_MANUAL |
187
|
|
|
&& !$entity->isSent() |
188
|
|
|
&& $this->get('oro_security.security_facade')->isGranted('orocrm_email_campaign_send'); |
189
|
|
|
|
190
|
|
|
if ($sendAllowed) { |
191
|
|
|
$transportSettings = $entity->getTransportSettings(); |
192
|
|
|
if ($transportSettings) { |
193
|
|
|
$validator = $this->get('validator'); |
194
|
|
|
$errors = $validator->validate($transportSettings); |
195
|
|
|
$sendAllowed = count($errors) === 0; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $sendAllowed; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.