sarga/packages/Webkul/Attribute/src/Http/Controllers/AttributeController.php

188 lines
5.1 KiB
PHP
Raw Normal View History

2018-07-11 05:41:27 +00:00
<?php
namespace Webkul\Attribute\Http\Controllers;
2019-07-01 11:33:36 +00:00
use Webkul\Attribute\Repositories\AttributeRepository;
2018-07-17 13:28:34 +00:00
2018-07-11 05:41:27 +00:00
class AttributeController extends Controller
{
/**
* Contains route related configuration.
2018-07-11 05:41:27 +00:00
*
* @var array
*/
protected $_config;
2018-07-17 13:28:34 +00:00
/**
* Attribute repository instance.
2018-07-17 13:28:34 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Attribute\Repositories\AttributeRepository
2018-07-17 13:28:34 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $attributeRepository;
2018-07-11 05:41:27 +00:00
/**
* Create a new controller instance.
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
2018-07-11 05:41:27 +00:00
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(AttributeRepository $attributeRepository)
2018-07-11 05:41:27 +00:00
{
2019-07-01 11:33:36 +00:00
$this->attributeRepository = $attributeRepository;
2018-07-17 13:28:34 +00:00
2018-07-11 05:41:27 +00:00
$this->_config = request('_config');
}
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-11 05:41:27 +00:00
*/
public function index()
{
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-11 05:41:27 +00:00
*/
public function create()
{
2018-07-17 13:28:34 +00:00
return view($this->_config['view']);
2018-07-11 05:41:27 +00:00
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
$this->validate(request(), [
2020-02-19 11:39:17 +00:00
'code' => ['required', 'unique:attributes,code', new \Webkul\Core\Contracts\Validations\Code],
2018-07-17 13:28:34 +00:00
'admin_name' => 'required',
2020-03-04 06:32:53 +00:00
'type' => 'required',
2018-07-11 05:41:27 +00:00
]);
$data = request()->all();
$data['is_user_defined'] = 1;
$this->attributeRepository->create($data);
2018-12-21 12:48:34 +00:00
2019-02-13 12:12:07 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Attribute']));
2018-07-11 05:41:27 +00:00
return redirect()->route($this->_config['redirect']);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-11 05:41:27 +00:00
*/
public function edit($id)
{
2019-07-01 11:33:36 +00:00
$attribute = $this->attributeRepository->findOrFail($id);
2018-07-11 05:41:27 +00:00
2018-07-17 13:28:34 +00:00
return view($this->_config['view'], compact('attribute'));
2018-07-11 05:41:27 +00:00
}
/**
* Get attribute options associated with attribute.
*
* @param int $id
* @return \Illuminate\View\View
*/
public function getAttributeOptions($id)
{
$attribute = $this->attributeRepository->findOrFail($id);
return $attribute->options()->paginate(50);
}
2018-07-11 05:41:27 +00:00
/**
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function update($id)
2018-07-11 05:41:27 +00:00
{
$this->validate(request(), [
2020-02-19 11:39:17 +00:00
'code' => ['required', 'unique:attributes,code,' . $id, new \Webkul\Core\Contracts\Validations\Code],
2018-07-17 13:28:34 +00:00
'admin_name' => 'required',
2020-03-04 06:32:53 +00:00
'type' => 'required',
2018-07-11 05:41:27 +00:00
]);
$this->attributeRepository->update(request()->all(), $id);
2018-12-21 12:48:34 +00:00
2019-02-13 12:12:07 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Attribute']));
2018-07-11 05:41:27 +00:00
return redirect()->route($this->_config['redirect']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2019-07-01 11:33:36 +00:00
$attribute = $this->attributeRepository->findOrFail($id);
2019-04-09 06:35:52 +00:00
if (! $attribute->is_user_defined) {
2019-02-13 12:12:07 +00:00
session()->flash('error', trans('admin::app.response.user-define-error', ['name' => 'Attribute']));
2018-10-17 07:21:47 +00:00
} else {
try {
2019-07-01 11:33:36 +00:00
$this->attributeRepository->delete($id);
2018-10-17 07:21:47 +00:00
2019-02-13 12:12:07 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Attribute']));
return response()->json(['message' => true], 200);
} catch (\Exception $e) {
2019-04-09 01:01:52 +00:00
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Attribute']));
}
2018-10-17 07:21:47 +00:00
}
return response()->json(['message' => false], 400);
2018-07-11 05:41:27 +00:00
}
/**
* Remove the specified resources from database.
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
*/
public function massDestroy()
{
if (request()->isMethod('post')) {
$indexes = explode(',', request()->input('indexes'));
2021-12-20 10:26:24 +00:00
foreach ($indexes as $index) {
$attribute = $this->attributeRepository->find($index);
2020-03-04 06:32:53 +00:00
2021-12-20 10:26:24 +00:00
if (! $attribute->is_user_defined) {
session()->flash('error', trans('admin::app.response.user-define-error', ['name' => 'Attribute']));
2020-02-19 11:39:17 +00:00
2021-12-20 10:26:24 +00:00
return redirect()->back();
}
}
2021-12-20 10:26:24 +00:00
foreach ($indexes as $index) {
$this->attributeRepository->delete($index);
2020-02-19 11:39:17 +00:00
}
2021-12-20 10:26:24 +00:00
session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success', ['resource' => 'attributes']));
return redirect()->back();
} else {
session()->flash('error', trans('admin::app.datagrid.mass-ops.method-error'));
return redirect()->back();
}
}
}