@prashant-webkul * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) */ class SubscriptionController extends Controller { /** * User object * * @var array */ protected $user; /** * Customer Repository object * * @var array */ protected $customer; /** * Subscription List Repository object * * @var array */ protected $subscription; /** * Create a new controller instance. * * @return void */ public function __construct(Customer $customer, Subscription $subscription) { $this->user = auth()->guard('customer')->user(); $this->customer = $customer; $this->subscription = $subscription; $this->_config = request('_config'); } /** * Subscribes Customers and Guests to subscription mailing list and checks if already subscribed */ public function subscribe() { $this->validate(request(), [ 'email' => 'email|required' ]); $email = request()->input('email'); $unique = 0; if(auth()->guard('customer')->check()) { $unique = function() use($email) { $count = $this->customer->findWhere(['email' => $email]); if($count->count() > 0 && $count->first()->subscribed_to_news_letter == 1) { return 0; } else { return 1; } }; } else { $unique = $this->subscription->findWhere(['email' => $email]); if($unique->count() > 0) { return 0; } else { return 1; } } if($unique()) { $token = uniqid(); $result = false; if(!auth()->guard('customer')->check()) { $result = $this->subscription->create([ 'email' => $email, 'channel_id' => core()->getCurrentChannel()->id, 'is_subscribed' => 1, 'token' => $token ]); } else { $user = auth()->guard('customer')->user(); $result = $user->update(['subscribed_to_news_letter' => 1]); } if(!$result) { session()->flash('error', trans('shop::app.subscription.not-subscribed')); return redirect()->back(); } Mail::send(new SubscriptionEmail); session()->flash('success', trans('shop::app.subscription.subscribed')); return redirect()->back(); } else { session()->flash('error', trans('shop::app.subscription.already')); return redirect()->back(); } return redirect()->back(); } /** * To unsubscribe from a the subcription list * * @var string $token */ public function unsubscribe($token) { dd('unsubscribing'); } }