1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Common; |
4
|
|
|
|
5
|
|
|
use App\Model\Common\Country; |
6
|
|
|
use App\Model\Common\Mailchimp\MailchimpField; |
7
|
|
|
use App\Model\Common\Mailchimp\MailchimpFieldAgoraRelation; |
8
|
|
|
use App\Model\Common\Mailchimp\MailchimpGroup; |
9
|
|
|
use App\Model\Common\Mailchimp\MailchimpGroupAgoraRelation; |
10
|
|
|
use App\Model\Common\Mailchimp\MailchimpLists; |
11
|
|
|
use App\Model\Common\Mailchimp\MailchimpSetting; |
12
|
|
|
use App\Model\Common\Setting; |
13
|
|
|
use App\Model\Common\StatusSetting; |
14
|
|
|
use App\Model\Product\Product; |
15
|
|
|
use App\User; |
16
|
|
|
use Exception; |
17
|
|
|
use Illuminate\Http\Request; |
18
|
|
|
|
19
|
|
|
class MailChimpController extends BaseMailChimpController |
20
|
|
|
{ |
21
|
|
|
protected $mail_api_key; |
22
|
|
|
protected $mailchimp; |
23
|
|
|
protected $mailchimp_field_model; |
24
|
|
|
protected $mailchimp_set; |
25
|
|
|
protected $list_id; |
26
|
|
|
protected $lists; |
27
|
|
|
protected $relation; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->middleware('auth', ['except'=>['addSubscriberByClientPanel']]); |
32
|
|
|
$mailchimp_set = new MailchimpSetting(); |
33
|
|
|
$this->mailchimp_set = $mailchimp_set->firstOrFail(); |
34
|
|
|
$this->mail_api_key = $this->mailchimp_set->api_key; |
35
|
|
|
$this->list_id = $this->mailchimp_set->list_id; |
36
|
|
|
$this->product_group_id = $this->mailchimp_set->group_id_products; |
37
|
|
|
$this->is_paid_group_id = $this->mailchimp_set->group_id_is_paid; |
38
|
|
|
|
39
|
|
|
$mailchimp_filed_model = new MailchimpField(); |
40
|
|
|
$this->mailchimp_field_model = $mailchimp_filed_model; |
41
|
|
|
|
42
|
|
|
$lists = new MailchimpLists(); |
43
|
|
|
$this->lists = $lists; |
44
|
|
|
|
45
|
|
|
$groups = new MailchimpGroup(); |
46
|
|
|
$this->groups = $groups; |
47
|
|
|
|
48
|
|
|
$relation = new MailchimpFieldAgoraRelation(); |
49
|
|
|
$this->relation = $relation->firstOrFail(); |
50
|
|
|
|
51
|
|
|
$groupRelation = new MailchimpGroupAgoraRelation(); |
52
|
|
|
$this->groupRelation = $groupRelation; |
53
|
|
|
|
54
|
|
|
$this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addSubscriber($email) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$merge_fields = $this->field($email); |
61
|
|
|
$interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No |
62
|
|
|
$interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
63
|
|
|
$result = $this->mailchimp->post("lists/$this->list_id/members", [ |
64
|
|
|
'status' => $this->mailchimp_set->subscribe_status, |
65
|
|
|
'email_address' => $email, |
66
|
|
|
'merge_fields' => $merge_fields, |
67
|
|
|
|
68
|
|
|
'interests' => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false], |
69
|
|
|
|
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} catch (Exception $ex) { |
74
|
|
|
$exe = json_decode($ex->getMessage(), true); |
75
|
|
|
if ($exe['status'] == 400) { |
76
|
|
|
throw new Exception("$email is already subscribed to newsletter", 400); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
//Update to Mailchimp For Paid Product |
82
|
|
|
public function addSubscriberByClientPanel(Request $request) |
83
|
|
|
{ |
84
|
|
|
$this->validate($request, [ |
85
|
|
|
'email' => 'required|email', |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
$email = $request->input('email'); |
90
|
|
|
$result = $this->mailchimp->post("lists/$this->list_id/members", [ |
91
|
|
|
'status' => $this->mailchimp_set->subscribe_status, |
92
|
|
|
'email_address' => $email, |
93
|
|
|
|
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
return successResponse('Email added to mailchimp'); |
97
|
|
|
} catch (Exception $ex) { |
98
|
|
|
$exe = json_decode($ex->getMessage(), true); |
99
|
|
|
// dd($exe); |
100
|
|
|
if ($exe['status'] == 400) { |
101
|
|
|
$error = $exe['detail']; |
102
|
|
|
|
103
|
|
|
return errorResponse($error, 400); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return errorResponse($ex->getMessage()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function field($email) |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
$user = new User(); |
114
|
|
|
$setting = new Setting(); |
115
|
|
|
$user = $user->where('email', $email)->first(); |
116
|
|
|
$country = Country::where('country_code_char2', $user->country)->pluck('nicename')->first(); |
117
|
|
|
if ($user) { |
118
|
|
|
$fields = ['first_name', 'last_name', 'company', 'mobile', |
119
|
|
|
'address', 'town', 'country', 'state', 'zip', 'active', 'role', 'source', ]; |
120
|
|
|
$relation = $this->relation; |
121
|
|
|
$merge_fields = []; |
122
|
|
|
foreach ($fields as $field) { |
123
|
|
|
if ($relation->$field) { |
124
|
|
|
$merge_fields[$relation->$field] = $user->$field; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$merge_fields[$relation->source] = $setting->findorFail(1)->title; |
128
|
|
|
|
129
|
|
|
return $merge_fields; |
130
|
|
|
} else { |
131
|
|
|
return redirect()->back()->with('fails', 'user not found'); |
132
|
|
|
} |
133
|
|
|
} catch (Exception $ex) { |
134
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function mapField() |
139
|
|
|
{ |
140
|
|
|
try { |
141
|
|
|
$model = $this->relation; |
142
|
|
|
$model2 = $this->groupRelation; |
143
|
|
|
$this->addFieldsToAgora(); |
144
|
|
|
|
145
|
|
|
$mailchimp_fields = $this->mailchimp_field_model |
146
|
|
|
->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray(); |
147
|
|
|
$agoraProducts = Product::pluck('name', 'id')->toArray(); |
148
|
|
|
|
149
|
|
|
$mailchimpProducts = $this->mailchimp->get("lists/$this->list_id/interest-categories"); |
150
|
|
|
$selectedProducts = MailchimpGroupAgoraRelation::select('agora_product_id', 'mailchimp_group_cat_id')->orderBy('id', 'asc')->get()->toArray(); |
151
|
|
|
$allGroups = $this->mailchimp->get("lists/$this->list_id/interest-categories"); //Get all the groups(interest-categories for a list) |
152
|
|
|
foreach ($allGroups['categories'] as $key => $value) { |
153
|
|
|
$display[] = (['id'=> $value->id, 'title'=>$value->title]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->addProductInterestFieldsToAgora(); //add all the fields in Product Section of Groups to the db |
157
|
|
|
$group_fields = $this->groups->where('list_id', $this->list_id) |
158
|
|
|
->select('category_name', 'category_option_id', 'category_id')->get()->toArray(); |
159
|
|
|
// dd($group_fields[0]); |
160
|
|
|
$relations = MailchimpGroupAgoraRelation::where('id', '!=', 0) |
161
|
|
|
->select('agora_product_id', 'mailchimp_group_cat_id') |
162
|
|
|
->orderBy('id', 'asc')->get()->toArray(); |
163
|
|
|
$productList = []; |
164
|
|
|
$categoryList = []; |
165
|
|
|
if (count($relations) != 0) { |
166
|
|
|
foreach ($relations as $key => $value) { |
167
|
|
|
$categoryList[] = $this->groups->where('category_option_id', $value['mailchimp_group_cat_id'])->pluck('category_name')->first(); |
168
|
|
|
$productList[] = Product::where('id', $value['agora_product_id'])->pluck('name')->first(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$isPaidYesId = MailchimpFieldAgoraRelation::first()->pluck('is_paid_yes')->toArray(); |
172
|
|
|
$selectedIsPaid[] = $isPaidYesId ? MailchimpGroup::where('category_option_id', $isPaidYesId)->pluck('category_id')->first() : ''; |
|
|
|
|
173
|
|
|
$status = StatusSetting::select('mailchimp_product_status', 'mailchimp_ispaid_status')->first(); |
174
|
|
|
|
175
|
|
|
return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model2', 'model', 'agoraProducts', 'display', 'selectedProducts', 'relations', 'group_fields', 'categoryList', 'productList', 'status', 'selectedIsPaid')); |
176
|
|
|
} catch (Exception $ex) { |
177
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function addFieldsToAgora() |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
|
|
/** @scrutinizer ignore-call */ |
185
|
|
|
$fields = $this->getMergeFields($this->list_id); |
186
|
|
|
$mailchimp_field_in_agora = $this->mailchimp_field_model->get(); |
187
|
|
|
if (count($mailchimp_field_in_agora) > 0) { |
188
|
|
|
foreach ($mailchimp_field_in_agora as $field) { |
189
|
|
|
$field->delete(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
foreach ($fields['merge_fields'] as $key => $value) { |
193
|
|
|
$merge_id = $value->merge_id; |
194
|
|
|
$name = $value->name; |
195
|
|
|
$type = $value->type; |
196
|
|
|
$required = $value->required; |
197
|
|
|
$list_id = $value->list_id; |
198
|
|
|
$tag = $value->tag; |
199
|
|
|
|
200
|
|
|
$this->mailchimp_field_model->create([ |
201
|
|
|
'merge_id' => $merge_id, |
202
|
|
|
'tag' => $tag, |
203
|
|
|
'name' => $name, |
204
|
|
|
'type' => $type, |
205
|
|
|
'required' => $required, |
206
|
|
|
'list_id' => $list_id, |
207
|
|
|
]); |
208
|
|
|
} |
209
|
|
|
} catch (Exception $ex) { |
210
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function addInterestFieldsToAgora(Request $request, $value) |
215
|
|
|
{ |
216
|
|
|
$groupInterests = $this->mailchimp->get("lists/$this->list_id/interest-categories/$value/interests?count=20"); |
217
|
|
|
echo '<option value="">Choose a Category</option>'; |
218
|
|
|
if (count($groupInterests) > 0) { |
219
|
|
|
foreach ($groupInterests['interests'] as $key=>$value) { |
|
|
|
|
220
|
|
|
$fields[] = (['category_id' => $value->category_id, |
221
|
|
|
'list_id' => $value->list_id, |
222
|
|
|
'category_option_id' => $value->id, |
223
|
|
|
'category_option_name' => $value->name, |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
foreach ($fields as $field) { |
|
|
|
|
227
|
|
|
$selectedCategory = MailchimpGroupAgoraRelation::where('mailchimp_group_cat_id', $field['category_option_id'])->pluck('mailchimp_group_cat_id')->first(); |
228
|
|
|
echo '<option value='.$field['category_option_id'].'>'.$field['category_option_name'].'</option>'; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function addProductInterestFieldsToAgora() |
234
|
|
|
{ |
235
|
|
|
$checkCategory = ($this->mailchimp->get("lists/$this->list_id/interest-categories")['categories']); |
236
|
|
|
if (count($checkCategory) > 0) { |
237
|
|
|
foreach ($checkCategory as $interest) { |
238
|
|
|
$groupInterests = $this->mailchimp->get("lists/$this->list_id/interest-categories/$interest->id/interests?count=20"); |
239
|
|
|
if (count($groupInterests['interests']) > 0) { |
240
|
|
|
foreach ($groupInterests['interests'] as $key => $value) { |
241
|
|
|
$category_id = $value->category_id; |
242
|
|
|
$list_id = $value->list_id; |
243
|
|
|
$category_option_id = $value->id; |
244
|
|
|
$category_option_name = $value->name; |
245
|
|
|
$this->groups->updateOrCreate([ |
246
|
|
|
'category_id' => $category_id, |
247
|
|
|
'list_id' => $list_id, |
248
|
|
|
'category_option_id' => $category_option_id, |
249
|
|
|
'category_name' => $category_option_name, |
250
|
|
|
]); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function postMapField(Request $request) |
258
|
|
|
{ |
259
|
|
|
try { |
260
|
|
|
$this->relation->fill($request->input())->save(); |
261
|
|
|
|
262
|
|
|
return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
263
|
|
|
} catch (Exception $ex) { |
264
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function postGroupMapField(Request $request) |
269
|
|
|
{ |
270
|
|
|
try { |
271
|
|
|
MailchimpGroupAgoraRelation::where('id', '!=', 0)->delete(); |
272
|
|
|
foreach ($request->row as $key => $value) { |
273
|
|
|
MailchimpGroupAgoraRelation::create(['agora_product_id'=> $value[0], |
274
|
|
|
'mailchimp_group_cat_id' => $value[1], ]); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
278
|
|
|
} catch (Exception $ex) { |
279
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function postIsPaidMapField(Request $request) |
284
|
|
|
{ |
285
|
|
|
try { |
286
|
|
|
$group = $request->input('group'); |
287
|
|
|
$groupInterests = $this->mailchimp->get("lists/$this->list_id/interest-categories/$group/interests?count=20")['interests']; |
288
|
|
|
foreach ($groupInterests as $interest) { |
289
|
|
|
//IS Paid Should be either Yes/No or True/False |
290
|
|
|
if ((strcasecmp($interest->name, 'Yes') == 0) || (strcasecmp($interest->name, 'True') == 0)) { |
291
|
|
|
MailchimpFieldAgoraRelation::find(1)->update(['is_paid_yes'=>$interest->id]); |
292
|
|
|
} elseif ((strcasecmp($interest->name, 'No') == 0) || (strcasecmp($interest->name, 'False') == 0)) { |
293
|
|
|
MailchimpFieldAgoraRelation::find(1)->update(['is_paid_no'=>$interest->id]); |
294
|
|
|
} else { |
295
|
|
|
return redirect()->back()->with('fails', 'The Group Should have Dropdown with values either Yes/No or True/False'); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return redirect()->back()->with('success', 'Settings Updated Successfully'); |
300
|
|
|
} catch (Exception $ex) { |
301
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|