Completed
Push — development ( 55bb16...2dbf65 )
by Ashutosh
09:53
created

MailChimpController::field()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 23
rs 9.4555
c 0
b 0
f 0
cc 5
nc 10
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Common\Mailchimp\MailchimpField;
7
use App\Model\Common\Mailchimp\MailchimpFieldAgoraRelation;
8
use App\Model\Common\Mailchimp\MailchimpLists;
9
use App\Model\Common\Mailchimp\MailchimpSetting;
10
use App\User;
11
use Exception;
12
use Illuminate\Http\Request;
13
14
class MailChimpController extends Controller
15
{
16
    protected $mail_api_key;
17
    protected $mailchimp;
18
    protected $mailchimp_field_model;
19
    protected $mailchimp_set;
20
    protected $list_id;
21
    protected $lists;
22
    protected $relation;
23
24
    public function __construct()
25
    {
26
        $mailchimp_set = new MailchimpSetting();
27
        $this->mailchimp_set = $mailchimp_set->firstOrFail();
28
        $this->mail_api_key = $this->mailchimp_set->api_key;
29
        $this->list_id = $this->mailchimp_set->list_id;
30
31
        $mailchimp_filed_model = new MailchimpField();
32
        $this->mailchimp_field_model = $mailchimp_filed_model;
33
34
        $lists = new MailchimpLists();
35
        $this->lists = $lists;
36
37
        $relation = new MailchimpFieldAgoraRelation();
38
        $this->relation = $relation->firstOrFail();
39
40
        $this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key);
41
    }
42
43
    public function getLists()
44
    {
45
        try {
46
            $result = $this->mailchimp->request('lists');
47
48
            return $result;
49
        } catch (Exception $ex) {
50
            dd($ex);
51
52
            return redirect()->back()->with('fails', $ex->getMessage());
53
        }
54
    }
55
56
    public function getListById()
57
    {
58
        try {
59
            $result = $this->mailchimp->request("lists/$this->list_id");
60
61
            return $result;
62
        } catch (Exception $ex) {
63
            dd($ex);
64
65
            return redirect()->back()->with('fails', $ex->getMessage());
66
        }
67
    }
68
69
    public function addSubscriber($email)
70
    {
71
        try {
72
            //dd($email);
73
            $merge_fields = $this->field($email);
74
            //dd($merge_fields);
75
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
76
                'status'        => $this->mailchimp_set->subscribe_status,
77
                'email_address' => $email,
78
                'merge_fields'  => $merge_fields,
79
            ]);
80
81
            return $result;
82
        } catch (Exception $ex) {
83
            $exe = json_decode($ex->getMessage(), true);
84
            if ($exe['status'] == 400) {
85
                throw new Exception("$email is already subscribed to newsletter", 400);
86
            }
87
        }
88
    }
89
90
    public function addSubscriberByClientPanel(Request $request)
91
    {
92
        $this->validate($request, [
93
            'email' => 'required|email',
94
        ]);
95
96
        try {
97
            $email = $request->input('email');
98
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
99
                'status'        => $this->mailchimp_set->subscribe_status,
100
                'email_address' => $email,
101
102
            ]);
103
104
            return redirect()->back()->with('success', 'email added to mailchimp');
105
        } catch (Exception $ex) {
106
            $exe = json_decode($ex->getMessage(), true);
107
            if ($exe['status'] == 400) {
108
                $error = "$email is already subscribed to newsletter";
109
110
                return redirect()->back()->with('warning', $error);
111
            }
112
113
            return redirect()->back()->with('fails', $ex->getMessage());
114
        }
115
    }
116
117
    public function field($email)
118
    {
119
        try {
120
            $user = new User();
121
            $user = $user->where('email', $email)->first();
122
            if ($user) {
123
                $fields = ['first_name','last_name','company','mobile','address','town','state','zip','active','role'];
124
                $relation = $this->relation;
125
                $merge_fields = array();
126
                foreach ($fields as $field) {
127
                    if($relation->$field){
128
                    $merge_fields[$relation->field] = $user->field;
0 ignored issues
show
Bug introduced by
The property field does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property field does not seem to exist on App\Model\Common\Mailchi...chimpFieldAgoraRelation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
129
130
                    }
131
                }
132
133
                return $merge_fields;
134
            } else {
135
                return redirect()->back()->with('fails', 'user not found');
136
            }
137
        } catch (Exception $ex) {
138
            //dd($ex);
139
            return redirect()->back()->with('fails', $ex->getMessage());
140
        }
141
    }
142
143
    public function getMergeFields()
144
    {
145
        try {
146
            $result = $this->mailchimp->get("lists/$this->list_id/merge-fields");
147
148
            return $result;
149
        } catch (Exception $ex) {
150
            return redirect()->back()->with('fails', $ex->getMessage());
151
        }
152
    }
153
154
    public function addFieldsToAgora()
155
    {
156
        try {
157
            /** @scrutinizer ignore-call */
158
            $fields = $this->getMergeFields($this->list_id);
159
            $mailchimp_field_in_agora = $this->mailchimp_field_model->get();
160
            if (count($mailchimp_field_in_agora) > 0) {
161
                foreach ($mailchimp_field_in_agora as $field) {
162
                    $field->delete();
163
                }
164
            }
165
            foreach ($fields['merge_fields'] as $key => $value) {
166
                $merge_id = $value->merge_id;
167
                $name = $value->name;
168
                $type = $value->type;
169
                $required = $value->required;
170
                $list_id = $value->list_id;
171
                $tag = $value->tag;
172
173
                $this->mailchimp_field_model->create([
174
                    'merge_id' => $merge_id,
175
                    'tag'      => $tag,
176
                    'name'     => $name,
177
                    'type'     => $type,
178
                    'required' => $required,
179
                    'list_id'  => $list_id,
180
                ]);
181
            }
182
        } catch (Exception $ex) {
183
            dd($ex);
184
185
            return redirect()->back()->with('fails', $ex->getMessage());
186
        }
187
    }
188
189
    public function addListsToAgora()
190
    {
191
        try {
192
            $lists = $this->getLists();
193
            $agora_lists = $this->lists->get();
194
            if (count($agora_lists) > 0) {
195
                foreach ($agora_lists as $agora) {
196
                    $agora->delete();
197
                }
198
            }
199
            foreach ($lists['lists'] as $list) {
200
                $name = $list->name;
201
                $list_id = $list->id;
202
                $this->lists->create([
203
                    'name'    => $name,
204
                    'list_id' => $list_id,
205
                ]);
206
            }
207
            //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora'));
208
        } catch (Exception $ex) {
209
            dd($ex);
210
211
            return redirect()->back()->with('fails', $ex->getMessage());
212
        }
213
    }
214
215
    public function mailChimpSettings()
216
    {
217
        try {
218
            $set = $this->mailchimp_set;
219
            $lists = $this->lists->pluck('name', 'list_id')->toArray();
220
221
            return view('themes.default1.common.mailchimp.settings', compact('set', 'lists'));
222
        } catch (Exception $ex) {
223
            return redirect()->back()->with('fails', $ex->getMessage());
224
        }
225
    }
226
227
    public function postMailChimpSettings(Request $request)
228
    {
229
        $this->validate($request, [
230
            'api_key' => 'required',
231
            //'list_id'=>'required',
232
        ]);
233
234
        try {
235
            $this->mailchimp_set->fill($request->input())->save();
236
            $this->addListsToAgora();
237
238
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
239
        } catch (Exception $ex) {
240
            dd($ex);
241
242
            return redirect()->back()->with('fails', $ex->getMessage());
243
        }
244
    }
245
246
    public function mapField()
247
    {
248
        try {
249
            $model = $this->relation;
250
            $this->addFieldsToAgora();
251
            $mailchimp_fields = $this->mailchimp_field_model->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray();
252
253
            return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model'));
254
        } catch (Exception $ex) {
255
            dd($ex);
256
257
            return redirect()->back()->with('fails', $ex->getMessage());
258
        }
259
    }
260
261
    public function postMapField(Request $request)
262
    {
263
        try {
264
            $this->relation->fill($request->input())->save();
265
266
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
267
        } catch (Exception $ex) {
268
            dd($ex);
269
270
            return redirect()->back()->with('fails', $ex->getMessage());
271
        }
272
    }
273
}
274