sarga/packages/Webkul/Admin/src/Http/Controllers/Customer/CustomerController.php

177 lines
4.4 KiB
PHP
Raw Normal View History

2018-09-08 08:41:36 +00:00
<?php
2018-10-17 07:40:08 +00:00
namespace Webkul\Admin\Http\Controllers\Customer;
2018-09-08 08:41:36 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2018-10-22 05:47:42 +00:00
use Webkul\Admin\Http\Controllers\Controller;
2018-09-11 04:11:48 +00:00
use Webkul\Customer\Repositories\CustomerRepository as Customer;
use Webkul\Customer\Repositories\CustomerGroupRepository as CustomerGroup;
2018-10-22 05:47:42 +00:00
use Webkul\Core\Repositories\ChannelRepository as Channel;
2018-09-08 08:41:36 +00:00
/**
2018-10-22 05:47:42 +00:00
* Customer controlller
2018-09-08 08:41:36 +00:00
*
* @author Rahul Shukla <rahulshukla.symfony517@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CustomerController extends Controller
{
/**
2018-09-11 04:11:48 +00:00
* Contains route related configuration
2018-09-08 08:41:36 +00:00
*
2018-09-11 04:11:48 +00:00
* @var array
2018-09-08 08:41:36 +00:00
*/
protected $_config;
2018-09-11 04:11:48 +00:00
/**
* CustomerRepository object
*
* @var array
*/
protected $customer;
/**
* CustomerGroupRepository object
*
* @var array
*/
protected $customerGroup;
2018-10-22 05:47:42 +00:00
/**
* ChannelRepository object
*
* @var array
*/
protected $channel;
2018-09-11 04:11:48 +00:00
/**
* Create a new controller instance.
*
* @param Webkul\Customer\Repositories\CustomerRepository as customer;
* @param Webkul\Customer\Repositories\CustomerGroupRepository as customerGroup;
2018-10-22 05:47:42 +00:00
* @param Webkul\Core\Repositories\ChannelRepository as Channel;
2018-09-11 04:11:48 +00:00
* @return void
*/
2018-10-22 05:47:42 +00:00
public function __construct(Customer $customer, CustomerGroup $customerGroup, Channel $channel )
2018-09-08 08:41:36 +00:00
{
$this->_config = request('_config');
2018-09-11 04:11:48 +00:00
$this->customer = $customer;
$this->customerGroup = $customerGroup;
2018-10-22 05:47:42 +00:00
$this->channel = $channel;
2018-09-08 08:41:36 +00:00
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view($this->_config['view']);
}
2018-09-11 04:11:48 +00:00
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$customerGroup = $this->customerGroup->all();
2018-10-22 05:47:42 +00:00
$channelName = $this->channel->all();
return view($this->_config['view'],compact('customerGroup','channelName'));
2018-09-11 04:11:48 +00:00
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
2018-10-22 05:47:42 +00:00
public function store()
2018-09-11 04:11:48 +00:00
{
2018-10-22 05:47:42 +00:00
$this->validate(request(), [
'channel_id' => 'required',
2018-09-11 04:11:48 +00:00
'first_name' => 'string|required',
'last_name' => 'string|required',
2018-10-12 11:31:14 +00:00
'gender' => 'required',
2018-10-22 05:47:42 +00:00
'phone' => 'nullable|numeric|unique:customers,phone',
2018-10-12 11:31:14 +00:00
'email' => 'required|unique:customers,email'
2018-09-11 04:11:48 +00:00
]);
2018-10-22 05:47:42 +00:00
$data=request()->all();
2018-09-11 04:11:48 +00:00
$password = bcrypt(rand(100000,10000000));
$data['password']=$password;
$this->customer->create($data);
session()->flash('success', 'Customer created successfully.');
return redirect()->route($this->_config['redirect']);
}
2018-09-08 08:41:36 +00:00
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
2018-09-11 04:11:48 +00:00
$customer = $this->customer->findOneWhere(['id'=>$id]);
2018-09-08 08:41:36 +00:00
2018-09-11 04:11:48 +00:00
$customerGroup = $this->customerGroup->all();
2018-09-08 08:41:36 +00:00
2018-10-22 05:47:42 +00:00
$channelName = $this->channel->all();
return view($this->_config['view'],compact('customer', 'customerGroup', 'channelName'));
2018-09-08 08:41:36 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
2018-10-22 05:47:42 +00:00
$this->validate(request(), [
'channel_id' => 'required',
2018-10-12 11:31:14 +00:00
'first_name' => 'string|required',
'last_name' => 'string|required',
'gender' => 'required',
2018-10-22 05:47:42 +00:00
'phone' => 'nullable|numeric|unique:customers,phone,'. $id,
2018-10-12 11:31:14 +00:00
'email' => 'required|unique:customers,email,'. $id
]);
2018-09-08 08:41:36 +00:00
2018-09-11 04:11:48 +00:00
$this->customer->update(request()->all(),$id);
2018-09-08 08:41:36 +00:00
session()->flash('success', 'Customer updated successfully.');
return redirect()->route($this->_config['redirect']);
}
2018-10-17 10:47:03 +00:00
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$this->customer->delete($id);
session()->flash('success', 'Customer deleted successfully.');
return redirect()->back();
}
2018-09-08 08:41:36 +00:00
}