Completed
Push — development ( a546b2...2b11c6 )
by Ashutosh
09:55
created

SettingsController::settingsMail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\ApiKey;
6
use App\Model\Common\Setting;
7
use App\Model\Common\Template;
8
use App\Model\Plugin;
9
use App\User;
10
use Bugsnag;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Input;
13
use Spatie\Activitylog\Models\Activity;
14
use GrahamCampbell\Markdown\Facades\Markdown;
15
16
class SettingsController extends BaseSettingsController
17
{
18
    public $apikey;
19
20
    public function __construct()
21
    {
22
        $this->middleware('auth', ['except' => 'checkPaymentGateway']);
23
        $this->middleware('admin', ['except' => 'checkPaymentGateway']);
24
25
        $apikey = new ApiKey();
26
        $this->apikey = $apikey;
27
    }
28
29
    public function settings(Setting $settings)
30
    {
31
        if (!$settings->where('id', '1')->first()) {
32
            $settings->create(['company' => '']);
33
        }
34
35
        return view('themes.default1.common.admin-settings');
36
        //return view('themes.default1.common.settings', compact('setting', 'template'));
37
    }
38
39
    public function plugins()
40
    {
41
        return view('themes.default1.common.plugins');
42
    }
43
44
    public function getKeys(ApiKey $apikeys)
45
    {
46
        try {
47
            $model = $apikeys->find(1);
48
49
            return view('themes.default1.common.apikey', compact('model'));
50
        } catch (\Exception $ex) {
51
            return redirect('/')->with('fails', $ex->getMessage());
52
        }
53
    }
54
55
    public function postKeys(ApiKey $apikeys, Request $request)
56
    {
57
        try {
58
            $keys = $apikeys->find(1);
59
            $keys->fill($request->input())->save();
60
61
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
62
        } catch (\Exception $ex) {
63
            return redirect()->back()->with('fails', $ex->getMessage());
64
        }
65
    }
66
67
    public static function checkPaymentGateway($currency)
68
    {
69
        try {
70
            $plugins = new Plugin();
71
            $models = [];
72
            $gateways = 'Razorpay';
73
74
            return $gateways;
75
        } catch (\Exception $ex) {
76
            return redirect()->back()->with('fails', $ex->getMessage());
77
        }
78
    }
79
80
    public function settingsSystem(Setting $settings)
81
    {
82
        try {
83
            $set = $settings->find(1);
84
85
            return view('themes.default1.common.setting.system', compact('set'));
86
        } catch (\Exception $ex) {
87
            return redirect()->back()->with('fails', $ex->getMessage());
88
        }
89
    }
90
91
    public function postSettingsSystem(Setting $settings, Request $request)
92
    {
93
        try {
94
            $setting = $settings->find(1);
95
            if ($request->hasFile('logo')) {
96
                $name = $request->file('logo')->getClientOriginalName();
97
                $destinationPath = public_path('cart/img/logo');
98
                $request->file('logo')->move($destinationPath, $name);
99
                $setting->logo = $name;
100
            }
101
            $setting->fill($request->except('password', 'logo'))->save();
102
103
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
104
        } catch (\Exception $ex) {
105
            return redirect()->back()->with('fails', $ex->getMessage());
106
        }
107
    }
108
109
    public function settingsEmail(Setting $settings)
110
    {
111
        try {
112
            $set = $settings->find(1);
113
114
            return view('themes.default1.common.setting.email', compact('set'));
115
        } catch (\Exception $ex) {
116
            return redirect()->back()->with('fails', $ex->getMessage());
117
        }
118
    }
119
120
    public function postSettingsEmail(Setting $settings, Request $request)
121
    {
122
        $this->validate($request, [
123
                'email'    => 'required',
124
                'password' => 'required',
125
            ]);
126
127
        try {
128
            $setting = $settings->find(1);
129
            $setting->fill($request->input())->save();
130
131
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
132
        } catch (\Exception $ex) {
133
            return redirect()->back()->with('fails', $ex->getMessage());
134
        }
135
    }
136
137
    public function settingsTemplate(Setting $settings)
138
    {
139
        try {
140
            $set = $settings->find(1);
141
            $template = new Template();
142
            //$templates = $template->lists('name', 'id')->toArray();
143
            return view('themes.default1.common.setting.template', compact('set', 'template'));
144
        } catch (\Exception $ex) {
145
            return redirect()->back()->with('fails', $ex->getMessage());
146
        }
147
    }
148
149
    public function postSettingsTemplate(Setting $settings, Request $request)
150
    {
151
        try {
152
            $setting = $settings->find(1);
153
            $setting->fill($request->input())->save();
154
155
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
156
        } catch (\Exception $ex) {
157
            return redirect()->back()->with('fails', $ex->getMessage());
158
        }
159
    }
160
161
    public function settingsError(Setting $settings)
162
    {
163
        try {
164
            $set = $settings->find(1);
165
166
            return view('themes.default1.common.setting.error-log', compact('set'));
167
        } catch (\Exception $ex) {
168
            return redirect()->back()->with('fails', $ex->getMessage());
169
        }
170
    }
171
172
    public function settingsActivity(Activity $activities)
173
    {
174
        try {
175
            $activity = $activities->all();
176
177
            return view('themes.default1.common.Activity-Log', compact('activity'));
178
        } catch (\Exception $ex) {
179
            return redirect()->back()->with('fails', $ex->getMessage());
180
        }
181
    }
182
183
184
    public function settingsMail()
185
    {
186
        try {
187
           return view('themes.default1.common.email-log');
188
        } catch (\Exception $ex) {
189
            return redirect()->back()->with('fails', $ex->getMessage());
190
        }
191
    }
192
193
    public function getActivity()
194
    {
195
        try {
196
            $activity_log = Activity::select('id', 'log_name', 'description', 'subject_id', 'subject_type', 'causer_id', 'properties', 'created_at')->get();
197
198
            return\ DataTables::of($activity_log)
199
             ->addColumn('checkbox', function ($model) {
200
                 return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>';
201
             })
202
                           ->addColumn('name', function ($model) {
203
                               return ucfirst($model->log_name);
204
                           })
205
                             ->addColumn('description', function ($model) {
206
                                 return ucfirst($model->description);
207
                             })
208
                          ->addColumn('username', function ($model) {
209
                              $causer_id = $model->causer_id;
210
                              $names = User::where('id', $causer_id)->pluck('last_name', 'first_name');
211
                              foreach ($names as $key => $value) {
212
                                  $fullName = $key.' '.$value;
213
214
                                  return $fullName;
215
                              }
216
                          })
217
                              ->addColumn('role', function ($model) {
218
                                  $causer_id = $model->causer_id;
219
                                  $role = User::where('id', $causer_id)->pluck('role');
220
221
                                  return json_decode($role);
222
                              })
223
                               ->addColumn('new', function ($model) {
224
                                   $properties = ($model->properties);
225
                                   $newEntry = $this->getNewEntry($properties, $model);
226
227
                                   return $newEntry;
228
                               })
229
                                ->addColumn('old', function ($model) {
230
                                    $data = ($model->properties);
231
                                    $oldEntry = $this->getOldEntry($data, $model);
232
233
                                    return $oldEntry;
234
                                })
235
                                ->addColumn('created_at', function ($model) {
236
                                    $newDate = $this->getDate($model->created_at);
237
238
                                    return $newDate;
239
                                })
240
241
                            ->rawColumns(['checkbox', 'name', 'description',   'username', 'role', 'new', 'old', 'created_at'])
242
                            ->make(true);
243
        } catch (\Exception $e) {
244
            Bugsnag::notifyException($e);
245
246
            return redirect()->back()->with('fails', $e->getMessage());
247
        }
248
    }
249
250
251
    public function getMails()
252
    {
253
        try {
254
            $email_log = \DB::table('email_log')->get();
255
256
            return\ DataTables::of($email_log)
257
             ->addColumn('checkbox', function ($model) {
258
                 return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>';
259
             })
260
                           ->addColumn('date', function ($model) {
261
                               return ucfirst($model->date);
262
                           })
263
                             ->addColumn('from', function ($model) {
264
                                $from =  Markdown::convertToHtml(ucfirst($model->from));
265
                                 return $from;
266
                             })
267
                              ->addColumn('to', function ($model) {
268
                                  $to = Markdown::convertToHtml(ucfirst($model->to));
269
                                   return $to;
270
                             })
271
                             ->addColumn('cc', function ($model) {
272
                                 $cc ='--';
273
                                 return $cc;
274
                                
275
                             })
276
                         
277
                               ->addColumn('subject', function ($model) {
278
                                  return ucfirst($model->subject);
279
                               
280
                              })
281
                               
282
                              ->addColumn('headers', function ($model) {
283
                                  $headers= Markdown::convertToHtml(ucfirst($model->headers));
284
                                  return $headers;
285
                               
286
                              })
287
                              ->addColumn('status', function ($model) {
288
                                   return ucfirst($model->status);
289
                               
290
                              })
291
                            
292
293
                            ->rawColumns(['checkbox', 'date', 'from', 'to', 'cc', 'bcc', 'subject', 'headers','status'])
294
                            ->make(true);
295
        } catch (\Exception $e) {
296
            Bugsnag::notifyException($e);
297
298
            return redirect()->back()->with('fails', $e->getMessage());
299
        }
300
    }
301
302
    public function destroy(Request $request)
303
    {
304
        try {
305
            $ids = $request->input('select');
306
            if (!empty($ids)) {
307
                foreach ($ids as $id) {
308
                    $activity = Activity::where('id', $id)->first();
309
                    if ($activity) {
310
                        $activity->delete();
311
                    } else {
312
                        echo "<div class='alert alert-danger alert-dismissable'>
313
                        <i class='fa fa-ban'></i>
314
315
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */     \Lang::get('message.failed').'
316
317
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
318
                            './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
319
                    </div>';
320
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
321
                    }
322
                }
323
                echo "<div class='alert alert-success alert-dismissable'>
324
                        <i class='fa fa-ban'></i>
325
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.success').'
326
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
327
                            './* @scrutinizer ignore-type */ \Lang::get('message.deleted-successfully').'
328
                    </div>';
329
            } else {
330
                echo "<div class='alert alert-danger alert-dismissable'>
331
                        <i class='fa fa-ban'></i>
332
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
333
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
334
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
335
                    </div>';
336
                //echo \Lang::get('message.select-a-row');
337
            }
338
        } catch (\Exception $e) {
339
            echo "<div class='alert alert-danger alert-dismissable'>
340
                        <i class='fa fa-ban'></i>
341
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
342
                        /* @scrutinizer ignore-type */\Lang::get('message.failed').'
343
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
344
                            '.$e->getMessage().'
345
                    </div>';
346
        }
347
    }
348
349
    public function postSettingsError(Setting $settings, Request $request)
350
    {
351
        try {
352
            $setting = $settings->find(1);
353
            $setting->fill($request->input())->save();
354
355
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
356
        } catch (\Exception $ex) {
357
            return redirect()->back()->with('fails', $ex->getMessage());
358
        }
359
    }
360
}
361