Issues (3627)

LeadBundle/Controller/FrequencyRuleTrait.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://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\LeadBundle\Entity\DoNotContact;
15
use Mautic\LeadBundle\Entity\Lead;
16
use Mautic\LeadBundle\Form\Type\ContactFrequencyType;
17
use Mautic\LeadBundle\Model\LeadModel;
18
use Symfony\Component\Form\Form;
19
20
trait FrequencyRuleTrait
21
{
22
    protected $leadLists;
23
24
    protected $dncChannels;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $isPublicView = false;
30
31
    /**
32
     * @param       $lead
33
     * @param array $viewParameters
34
     * @param null  $data
35
     * @param bool  $isPublic
36
     * @param null  $action
37
     * @param bool  $isPreferenceCenter
38
     *
39
     * @return bool|Form
40
     */
41
    protected function getFrequencyRuleForm($lead, &$viewParameters = [], &$data = null, $isPublic = false, $action = null, $isPreferenceCenter = false)
42
    {
43
        /** @var LeadModel $model */
44
        $model = $this->getModel('lead');
45
46
        $leadChannels = $model->getContactChannels($lead);
47
        $allChannels  = $model->getPreferenceChannels();
48
        $leadLists    = $model->getLists($lead, true, true, $isPublic, $isPreferenceCenter);
49
50
        $viewParameters = array_merge(
51
            $viewParameters,
52
            [
53
                'leadsLists'   => $leadLists,
54
                'channels'     => $allChannels,
55
                'leadChannels' => $leadChannels,
56
            ]
57
        );
58
59
        //find the email
60
        $currentChannelId = null;
61
        if (!empty($viewParameters['idHash'])) {
62
            $emailModel = $this->getModel('email');
63
            if ($stat = $emailModel->getEmailStatus($viewParameters['idHash'])) {
64
                if ($email = $stat->getEmail()) {
65
                    $currentChannelId = $email->getId();
66
                }
67
            }
68
        }
69
70
        if (null == $data) {
71
            $data = $this->getFrequencyRuleFormData($lead, $allChannels, $leadChannels, $isPublic, null, $isPreferenceCenter);
72
        }
73
        /** @var Form $form */
74
        $form = $this->get('form.factory')->create(
75
            ContactFrequencyType::class,
76
            $data,
77
            [
78
                'action'                   => $action,
79
                'channels'                 => $allChannels,
80
                'public_view'              => $isPublic,
81
                'preference_center_only'   => $isPreferenceCenter,
82
                'allow_extra_fields'       => true,
83
            ]
84
        );
85
86
        $method = $this->request->getMethod();
87
        if ('GET' !== $method) {
88
            if (!$this->isFormCancelled($form)) {
89
                if ($this->isFormValid($form, $data)) {
90
                    $this->persistFrequencyRuleFormData($lead, $form->getData(), $allChannels, $leadChannels, $currentChannelId);
91
92
                    return true;
93
                }
94
            }
95
        }
96
97
        return $form;
98
    }
99
100
    /**
101
     * @param null $leadChannels
102
     * @param bool $isPublic
103
     * @param null $frequencyRules
104
     *
105
     * @return array
106
     */
107
    protected function getFrequencyRuleFormData(Lead $lead, array $allChannels = null, $leadChannels = null, $isPublic = false, $frequencyRules = null, $isPreferenceCenter = false)
108
    {
109
        $data = [];
110
111
        /** @var LeadModel $model */
112
        $model = $this->getModel('lead');
113
        if (null === $allChannels) {
114
            $allChannels = $model->getPreferenceChannels();
115
        }
116
117
        if (null === $leadChannels) {
118
            $leadChannels = $model->getContactChannels($lead);
119
        }
120
121
        if (null === $frequencyRules) {
122
            $frequencyRules = $model->getFrequencyRules($lead);
123
        }
124
125
        foreach ($allChannels as $channel) {
126
            if (isset($frequencyRules[$channel])) {
127
                $frequencyRule                                       = $frequencyRules[$channel];
128
                $data['lead_channels']['frequency_number_'.$channel] = $frequencyRule['frequency_number'];
129
                $data['lead_channels']['frequency_time_'.$channel]   = $frequencyRule['frequency_time'];
130
                if ($frequencyRule['pause_from_date']) {
131
                    $data['lead_channels']['contact_pause_start_date_'.$channel] = new \DateTime($frequencyRule['pause_from_date']);
132
                }
133
134
                if ($frequencyRule['pause_to_date']) {
135
                    $data['lead_channels']['contact_pause_end_date_'.$channel] = new \DateTime($frequencyRule['pause_to_date']);
136
                }
137
138
                if (!empty($frequencyRule['preferred_channel'])) {
139
                    $data['lead_channels']['preferred_channel'] = $channel;
140
                }
141
            }
142
        }
143
144
        $data['global_categories'] = (isset($frequencyRules['global_categories']))
145
            ? $frequencyRules['global_categories']
146
            : $model->getLeadCategories(
147
                $lead
148
            );
149
        $this->leadLists    = $model->getLists($lead, false, false, $isPublic, $isPreferenceCenter);
150
        $data['lead_lists'] = [];
151
        foreach ($this->leadLists as $leadList) {
152
            $data['lead_lists'][] = $leadList->getId();
153
        }
154
155
        $data['lead_channels']['subscribed_channels'] = $leadChannels;
156
        $this->isPublicView                           = $isPublic;
157
158
        return $data;
159
    }
160
161
    /**
162
     * @param     $leadChannels
163
     * @param int $currentChannelId
164
     */
165
    protected function persistFrequencyRuleFormData(Lead $lead, array $formData, array $allChannels, $leadChannels, $currentChannelId = null)
166
    {
167
        /** @var LeadModel $leadModel */
168
        $leadModel = $this->getModel('lead.lead');
169
170
        /** @var \Mautic\LeadBundle\Model\DoNotContact $dncModel */
171
        $dncModel = $this->getModel('lead.dnc');
172
173
        // iF subscribed_channels are enabled in form, then touch DNC
174
        if (isset($this->request->request->get('lead_contact_frequency_rules')['lead_channels'])) {
175
            foreach ($formData['lead_channels']['subscribed_channels'] as $contactChannel) {
176
                if (!isset($leadChannels[$contactChannel])) {
177
                    $contactable = $dncModel->isContactable($lead, $contactChannel);
178
                    if (DoNotContact::UNSUBSCRIBED == $contactable || DoNotContact::MANUAL == $contactable) {
179
                        $dncModel->removeDncForContact($lead->getId(), $contactChannel);
180
                    }
181
                }
182
            }
183
            $dncChannels = array_diff($allChannels, $formData['lead_channels']['subscribed_channels']);
184
            if (!empty($dncChannels)) {
185
                foreach ($dncChannels as $channel) {
186
                    if ($currentChannelId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentChannelId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
187
                        $channel = [$channel => $currentChannelId];
188
                    }
189
                    $dncModel->addDncForContact($lead->getId(), $channel, ($this->isPublicView) ? DoNotContact::UNSUBSCRIBED : DoNotContact::MANUAL, 'user');
190
                }
191
            }
192
        }
193
        $leadModel->setFrequencyRules($lead, $formData, $this->leadLists);
194
    }
195
}
196