Completed
Push — development ( 08cf53...7490ee )
by Ashutosh
08:45
created

updateSubscriberForPaidProduct()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
cc 2
nc 5
nop 2
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\MailchimpGroupAgoraRelation;
9
use App\Model\Common\Mailchimp\MailchimpLists;
10
use App\Model\Common\Mailchimp\MailchimpSetting;
11
use App\Model\Common\Setting;
12
use App\Model\Common\Mailchimp\MailchimpGroup;
13
use App\Model\Product\Product;
14
use App\User;
15
use Exception;
16
use Illuminate\Http\Request;
17
18
class MailChimpController extends Controller
19
{
20
    protected $mail_api_key;
21
    protected $mailchimp;
22
    protected $mailchimp_field_model;
23
    protected $mailchimp_set;
24
    protected $list_id;
25
    protected $lists;
26
    protected $relation;
27
28
    public function __construct()
29
    {
30
        $mailchimp_set = new MailchimpSetting();
31
        $this->mailchimp_set = $mailchimp_set->firstOrFail();
32
        $this->mail_api_key = $this->mailchimp_set->api_key;
33
        $this->list_id = $this->mailchimp_set->list_id;
34
        $this->product_group_id =  $this->mailchimp_set->group_id_products;
0 ignored issues
show
Bug introduced by
The property group_id_products does not seem to exist on App\Model\Common\Mailchimp\MailchimpSetting. 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...
35
36
        $mailchimp_filed_model = new MailchimpField();
37
        $this->mailchimp_field_model = $mailchimp_filed_model;
38
39
40
41
        $lists = new MailchimpLists();
42
        $this->lists = $lists;
43
44
        $groups = new MailchimpGroup();
45
        $this->groups = $groups;
46
47
        $relation = new MailchimpFieldAgoraRelation();
48
        $this->relation = $relation->firstOrFail();
49
50
        $groupRelation =  new MailchimpGroupAgoraRelation();
51
        $this->groupRelation = $groupRelation;
52
53
        $this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key);
54
    }
55
56
    public function getLists()
57
    {
58
        try {
59
            $result = $this->mailchimp->request('lists');
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 getListById()
70
    {
71
        try {
72
            $result = $this->mailchimp->request("lists/$this->list_id");
73
74
            return $result;
75
        } catch (Exception $ex) {
76
            return redirect()->back()->with('fails', $ex->getMessage());
77
        }
78
    }
79
80
    public function addSubscriber($email)
81
    {
82
        try {
83
            $merge_fields = $this->field($email);
84
              $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No
0 ignored issues
show
Bug introduced by
The property is_paid_no 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...
85
              $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
0 ignored issues
show
Bug introduced by
The property is_paid_yes 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...
86
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
87
                'status'        => $this->mailchimp_set->subscribe_status,
88
                'email_address' => $email,
89
                'merge_fields'  => $merge_fields,
90
91
                'interests'         => array( $interestGroupIdForNo => true , $interestGroupIdForYes=>false ),
92
93
            ]);
94
95
            return $result;
96
        } catch (Exception $ex) {
97
            $exe = json_decode($ex->getMessage(), true);
98
            if ($exe['status'] == 400) {
99
                throw new Exception("$email is already subscribed to newsletter", 400);
100
            }
101
        }
102
    }
103
    
104
    //Update to Mailchimp For Free Product
105
    public function updateSubscriberForFreeProduct($email,$productid)
106
    {
107
        try {
108
             $merge_fields = $this->field($email);
109
             $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No
0 ignored issues
show
Bug introduced by
The property is_paid_no 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...
110
              $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
0 ignored issues
show
Bug introduced by
The property is_paid_yes 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...
111
              $productGroupId = $this->groupRelation->where('agora_product_id',$productid)
112
              ->pluck('mailchimp_group_cat_id')->first();
113
             $hash = md5($email);
114
              $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash",[
115
                 'interests'         => array( $interestGroupIdForNo => true , $interestGroupIdForYes=>false, $productGroupId =>true),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
116
                 //refer to https://us7.api.mailchimp.com/playground
117
              ]);
118
             } catch (Exception $ex) {
119
            $exe = json_decode($ex->getMessage(), true);
120
           
121
        }
122
    }
123
    
124
    //Update to Mailchimp For Paid Product
125
    public function updateSubscriberForPaidProduct($email,$productid)
126
    {
127
        try {
128
             $merge_fields = $this->field($email);
129
             $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No
0 ignored issues
show
Bug introduced by
The property is_paid_no 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...
130
              $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
0 ignored issues
show
Bug introduced by
The property is_paid_yes 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...
131
               $productGroupId = $this->groupRelation->where('agora_product_id',$productid)
132
              ->pluck('mailchimp_group_cat_id')->first();
133
             $hash = md5($email);
134
              $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash",[
135
                 'interests'         => array( $interestGroupIdForNo => false , $interestGroupIdForYes=>true ,$productGroupId =>true),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
136
                 //refer to https://us7.api.mailchimp.com/playground
137
              ]);
138
             } catch (Exception $ex) {
139
            $exe = json_decode($ex->getMessage(), true);
140
           
141
        }
142
    }
143
144
    public function addSubscriberByClientPanel(Request $request)
145
    {
146
        $this->validate($request, [
147
            'email' => 'required|email',
148
        ]);
149
150
        try {
151
          
152
            $email = $request->input('email');
153
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
154
                'status'        => $this->mailchimp_set->subscribe_status,
155
                'email_address' => $email,
156
157
            ]);
158
159
            return redirect()->back()->with('success', 'email added to mailchimp');
160
        } catch (Exception $ex) {
161
            $exe = json_decode($ex->getMessage(), true);
162
            if ($exe['status'] == 400) {
163
                $error = "$email is already subscribed to newsletter";
164
165
                return redirect()->back()->with('warning', $error);
166
            }
167
168
            return redirect()->back()->with('fails', $ex->getMessage());
169
        }
170
    }
171
172
    public function field($email)
173
    {
174
        try {
175
            $user = new User();
176
            $setting = new Setting();
177
            $user = $user->where('email', $email)->first();
178
            if ($user) {
179
                $fields = ['first_name', 'last_name', 'company', 'mobile',
180
                 'address', 'town', 'state', 'zip', 'active', 'role','source' ];
181
                $relation = $this->relation;
182
                $merge_fields = [];
183
                foreach ($fields as $field) {
184
                    if ($relation->$field) {
185
                        $merge_fields[$relation->$field] = $user->$field;
186
                    }
187
                }
188
                $merge_fields[$relation->source] =  $setting->findorFail(1)->title;
0 ignored issues
show
Bug introduced by
The property source 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...
189
                return $merge_fields;
190
            } else {
191
                return redirect()->back()->with('fails', 'user not found');
192
            }
193
        } catch (Exception $ex) {
194
            //dd($ex);
195
            return redirect()->back()->with('fails', $ex->getMessage());
196
        }
197
    }
198
199
    public function getMergeFields()
200
    {
201
        try {
202
            $result = $this->mailchimp->get("lists/$this->list_id/merge-fields");
203
       
204
            return $result;
205
        } catch (Exception $ex) {
206
            return redirect()->back()->with('fails', $ex->getMessage());
207
        }
208
    }
209
210
    public function addFieldsToAgora()
211
    {
212
        try {
213
            /** @scrutinizer ignore-call */
214
            $fields = $this->getMergeFields($this->list_id);
215
            $mailchimp_field_in_agora = $this->mailchimp_field_model->get();
216
            if (count($mailchimp_field_in_agora) > 0) {
217
                foreach ($mailchimp_field_in_agora as $field) {
218
                    $field->delete();
219
                }
220
            }
221
            foreach ($fields['merge_fields'] as $key => $value) {
222
                $merge_id = $value->merge_id;
223
                $name = $value->name;
224
                $type = $value->type;
225
                $required = $value->required;
226
                $list_id = $value->list_id;
227
                $tag = $value->tag;
228
229
                $this->mailchimp_field_model->create([
230
                    'merge_id' => $merge_id,
231
                    'tag'      => $tag,
232
                    'name'     => $name,
233
                    'type'     => $type,
234
                    'required' => $required,
235
                    'list_id'  => $list_id,
236
                ]);
237
            }
238
        } catch (Exception $ex) {
239
             return redirect()->back()->with('fails', $ex->getMessage());
240
        }
241
    }
242
243
    public function addListsToAgora()
244
    {
245
        try {
246
            $lists = $this->getLists();
247
            $agora_lists = $this->lists->get();
248
            if (count($agora_lists) > 0) {
249
                foreach ($agora_lists as $agora) {
250
                    $agora->delete();
251
                }
252
            }
253
            foreach ($lists['lists'] as $list) {
254
                $name = $list->name;
255
                $list_id = $list->id;
256
                $this->lists->create([
257
                    'name'    => $name,
258
                    'list_id' => $list_id,
259
                ]);
260
            }
261
            //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora'));
262
        } catch (Exception $ex) {
263
            return redirect()->back()->with('fails', $ex->getMessage());
264
        }
265
    }
266
267
    public function mailChimpSettings()
268
    {
269
        try {
270
            $set = $this->mailchimp_set;
271
            $lists = $this->lists->pluck('name', 'list_id')->toArray();
272
273
            return view('themes.default1.common.mailchimp.settings', compact('set', 'lists'));
274
        } catch (Exception $ex) {
275
            return redirect()->back()->with('fails', $ex->getMessage());
276
        }
277
    }
278
279
    public function postMailChimpSettings(Request $request)
280
    {
281
        $this->validate($request, [
282
            'api_key' => 'required',
283
            //'list_id'=>'required',
284
        ]);
285
286
        try {
287
            $this->mailchimp_set->fill($request->input())->save();
288
            $this->addListsToAgora();
289
290
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
291
        } catch (Exception $ex) {
292
            return redirect()->back()->with('fails', $ex->getMessage());
293
        }
294
    }
295
296
    public function mapField()
297
    {
298
        try {
299
            $model = $this->relation;
300
            $model2 = $this->groupRelation;
301
            $this->addFieldsToAgora();
302
            $mailchimp_fields = $this->mailchimp_field_model
303
            ->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray();
304
            $agoraProducts = Product::pluck('name','id')->toArray();
305
         $groupInterests =$this->mailchimp->get("lists/$this->list_id/interest-categories/$this->product_group_id/interests");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
306
         //get all the fields for Product Group
307
         if(count($groupInterests)>0){
308
             foreach ($groupInterests['interests'] as $key=>$value) {
309
310
             $category_id = $value->category_id;
311
             $list_id    = $value->list_id;
312
             $category_option_id = $value->id;
313
             $category_option_name = $value->name;
314
             $this->groups->create([
315
                    'category_id' =>  $category_id,
316
                    'list_id'      =>  $list_id,
317
                    'category_option_id' => $category_option_id,
318
                    'category_name'     => $category_option_name,
319
                ]);
320
            }
321
           }
322
            
323
          $product_group_fields = $this->groups->where('list_id', $this->list_id)
324
          ->pluck('category_name','category_option_id','category_id')->toArray();
325
          $relations = MailchimpGroupAgoraRelation::where('id','!=',0)
326
          ->select('agora_product_id','mailchimp_group_cat_id')
327
          ->orderBy('id','asc')->get()->toArray();
328
        return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model2','model','product_group_fields','agoraProducts','relations'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 158 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
329
        } catch (Exception $ex) {
330
            return redirect()->back()->with('fails', $ex->getMessage());
331
        }
332
    }
333
334
    public function postMapField(Request $request)
335
    {
336
        try {
337
            $this->relation->fill($request->input())->save();
338
339
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
340
        } catch (Exception $ex) {
341
            dd($ex);
342
343
            return redirect()->back()->with('fails', $ex->getMessage());
344
        }
345
    }
346
347
      public function postGroupMapField(Request $request)
348
    {
349
350
        try {
351
            MailchimpGroupAgoraRelation::where('id','!=',0)->delete();
352
            foreach ($request->row as $key => $value) {
353
            MailchimpGroupAgoraRelation::create(['agora_product_id'=> $value[0] ,
354
             'mailchimp_group_cat_id' => $value[1]]);
355
            }
356
           return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
357
        } catch (Exception $ex) {
358
            return redirect()->back()->with('fails', $ex->getMessage());
359
        }
360
    }
361
 
362
}
363