Spaces:
No application file
No application file
File size: 7,499 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
<?php
namespace Mautic\LeadBundle\Controller;
use Mautic\EmailBundle\Model\EmailModel;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Form\Type\ContactFrequencyType;
use Mautic\LeadBundle\Model\LeadModel;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\RequestStack;
trait FrequencyRuleTrait
{
protected $leadLists;
protected $dncChannels;
/**
* @var bool
*/
protected $isPublicView = false;
private \Mautic\LeadBundle\Model\DoNotContact $doNotContactModel;
private ?RequestStack $requestStack = null;
/**
* @param array $viewParameters
* @param bool $isPublic
* @param bool $isPreferenceCenter
*
* @return true|Form
*/
protected function getFrequencyRuleForm($lead, &$viewParameters = [], &$data = null, $isPublic = false, $action = null, $isPreferenceCenter = false)
{
/** @var LeadModel $model */
$model = $this->getModel('lead');
$leadChannels = $model->getContactChannels($lead);
$allChannels = $model->getPreferenceChannels();
$leadLists = $model->getLists($lead, true, true, $isPublic, $isPreferenceCenter);
$viewParameters = array_merge(
$viewParameters,
[
'leadsLists' => $leadLists,
'channels' => $allChannels,
'leadChannels' => $leadChannels,
]
);
// find the email
$currentChannelId = null;
if (!empty($viewParameters['idHash'])) {
$emailModel = $this->getModel('email');
\assert($emailModel instanceof EmailModel);
if ($stat = $emailModel->getEmailStatus($viewParameters['idHash'])) {
if ($email = $stat->getEmail()) {
$currentChannelId = $email->getId();
}
}
}
if (null == $data) {
$data = $this->getFrequencyRuleFormData($lead, $allChannels, $leadChannels, $isPublic, null, $isPreferenceCenter);
}
/** @var Form $form */
$form = $this->formFactory->create(
ContactFrequencyType::class,
$data,
[
'action' => $action,
'channels' => $allChannels,
'public_view' => $isPublic,
'preference_center_only' => $isPreferenceCenter,
'allow_extra_fields' => true,
]
);
$request = $this->requestStack->getCurrentRequest();
\assert(null !== $request);
$method = $request->getMethod();
if ('GET' !== $method) {
if (!$this->isFormCancelled($form)) {
if ($this->isFormValid($form)) {
$this->persistFrequencyRuleFormData($lead, $form->getData(), $allChannels, $leadChannels, $currentChannelId);
return true;
}
}
}
return $form;
}
/**
* @param bool $isPublic
*/
protected function getFrequencyRuleFormData(Lead $lead, array $allChannels = null, $leadChannels = null, $isPublic = false, $frequencyRules = null, $isPreferenceCenter = false): array
{
$data = [];
/** @var LeadModel $model */
$model = $this->getModel('lead');
if (null === $allChannels) {
$allChannels = $model->getPreferenceChannels();
}
if (null === $leadChannels) {
$leadChannels = $model->getContactChannels($lead);
}
if (null === $frequencyRules) {
$frequencyRules = $model->getFrequencyRules($lead);
}
foreach ($allChannels as $channel) {
if (isset($frequencyRules[$channel])) {
$frequencyRule = $frequencyRules[$channel];
$data['lead_channels']['frequency_number_'.$channel] = $frequencyRule['frequency_number'];
$data['lead_channels']['frequency_time_'.$channel] = $frequencyRule['frequency_time'];
if ($frequencyRule['pause_from_date']) {
$data['lead_channels']['contact_pause_start_date_'.$channel] = new \DateTime($frequencyRule['pause_from_date']);
}
if ($frequencyRule['pause_to_date']) {
$data['lead_channels']['contact_pause_end_date_'.$channel] = new \DateTime($frequencyRule['pause_to_date']);
}
if (!empty($frequencyRule['preferred_channel'])) {
$data['lead_channels']['preferred_channel'] = $channel;
}
}
}
$data['global_categories'] = $frequencyRules['global_categories'] ?? $model->getLeadCategories(
$lead
);
$this->leadLists = $model->getLists($lead, false, false, $isPublic, $isPreferenceCenter);
$data['lead_lists'] = [];
foreach ($this->leadLists as $leadList) {
$data['lead_lists'][] = $leadList->getId();
}
$data['lead_channels']['subscribed_channels'] = $leadChannels;
$this->isPublicView = $isPublic;
return $data;
}
/**
* @param int $currentChannelId
*/
protected function persistFrequencyRuleFormData(Lead $lead, array $formData, array $allChannels, $leadChannels, $currentChannelId = null)
{
/** @var LeadModel $leadModel */
$leadModel = $this->getModel('lead.lead');
$dncModel = $this->doNotContactModel;
\assert($dncModel instanceof \Mautic\LeadBundle\Model\DoNotContact);
$request = $this->requestStack->getCurrentRequest();
\assert(null !== $request);
// iF subscribed_channels are enabled in form, then touch DNC
if (isset($request->request->get('lead_contact_frequency_rules')['lead_channels'])) {
foreach ($formData['lead_channels']['subscribed_channels'] as $contactChannel) {
if (!isset($leadChannels[$contactChannel])) {
$contactable = $dncModel->isContactable($lead, $contactChannel);
if (DoNotContact::UNSUBSCRIBED == $contactable || DoNotContact::MANUAL == $contactable) {
$dncModel->removeDncForContact($lead->getId(), $contactChannel);
}
}
}
$dncChannels = array_diff($allChannels, $formData['lead_channels']['subscribed_channels']);
foreach ($dncChannels as $channel) {
if ($currentChannelId) {
$channel = [$channel => $currentChannelId];
}
$dncModel->addDncForContact($lead->getId(), $channel, ($this->isPublicView) ? DoNotContact::UNSUBSCRIBED : DoNotContact::MANUAL, 'user');
}
}
$leadModel->setFrequencyRules($lead, $formData, $this->leadLists);
}
#[\Symfony\Contracts\Service\Attribute\Required]
public function setDoNotContactModel(\Mautic\LeadBundle\Model\DoNotContact $doNotContactModel): void
{
$this->doNotContactModel = $doNotContactModel;
}
/**
* The name is different, so it won't collide with other setters.
*/
#[\Symfony\Contracts\Service\Attribute\Required]
public function setRequestStackObject(RequestStack $requestStack): void
{
$this->requestStack = $requestStack;
}
}
|