sarga/packages/Webkul/Shop/src/Http/Controllers/SubscriptionController.php

144 lines
3.6 KiB
PHP
Raw Normal View History

2018-11-05 05:51:55 +00:00
<?php
namespace Webkul\Shop\Http\Controllers;
use Webkul\Shop\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Mail;
use Webkul\Shop\Mail\SubscriptionEmail;
use Webkul\Customer\Repositories\CustomerRepository as Customer;
use Webkul\Core\Repositories\SubscribersListRepository as Subscription;
2018-11-05 05:51:55 +00:00
/**
* Subscription controller
*
* @author Prashant Singh <prashant.singh852@webkul.com> @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;
2018-11-05 05:51:55 +00:00
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(Customer $customer, Subscription $subscription)
2018-11-05 05:51:55 +00:00
{
$this->user = auth()->guard('customer')->user();
$this->customer = $customer;
$this->subscription = $subscription;
2018-11-05 05:51:55 +00:00
$this->_config = request('_config');
}
/**
* Subscribes Customers and Guests to subscription mailing list and checks if already subscribed
2018-11-05 05:51:55 +00:00
*/
public function subscribe()
2018-11-05 05:51:55 +00:00
{
$this->validate(request(), [
'email' => 'email|required'
2018-11-05 05:51:55 +00:00
]);
$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 {
2018-11-16 12:58:41 +00:00
$alreadySubscribed = $this->subscription->findWhere(['email' => $email]);
2018-11-05 05:51:55 +00:00
2018-11-16 12:58:41 +00:00
$unique = function() use($alreadySubscribed){
if($alreadySubscribed->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();
}
2018-11-16 13:19:28 +00:00
$subscriptionData['email'] = $email;
$subscriptionData['token'] = $token;
Mail::send(new SubscriptionEmail($subscriptionData));
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');
2018-11-05 05:51:55 +00:00
}
}