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

201 lines
5.9 KiB
PHP
Raw Normal View History

2018-07-17 13:28:34 +00:00
<?php
namespace Webkul\Attribute\Http\Controllers;
2019-07-01 11:33:36 +00:00
use Webkul\Attribute\Repositories\AttributeFamilyRepository;
use Webkul\Attribute\Repositories\AttributeRepository;
2018-07-17 13:28:34 +00:00
/**
* Catalog family controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeFamilyController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
2018-07-17 13:28:34 +00:00
/**
* AttributeFamilyRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
*/
protected $attributeFamilyRepository;
/**
* AttributeRepository object
*
* @var Object
2018-07-17 13:28:34 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $attributeRepository;
2018-07-17 13:28:34 +00:00
/**
* Create a new controller instance.
*
2019-07-01 11:33:36 +00:00
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
2018-07-17 13:28:34 +00:00
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(
AttributeFamilyRepository $attributeFamilyRepository,
AttributeRepository $attributeRepository
)
2018-07-17 13:28:34 +00:00
{
2019-07-01 11:33:36 +00:00
$this->attributeFamilyRepository = $attributeFamilyRepository;
$this->attributeRepository = $attributeRepository;
2018-07-17 13:28:34 +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-17 13:28:34 +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-17 13:28:34 +00:00
*/
2019-07-01 11:33:36 +00:00
public function create()
2018-07-17 13:28:34 +00:00
{
2019-07-01 11:33:36 +00:00
$attributeFamily = $this->attributeFamilyRepository->with(['attribute_groups.custom_attributes'])->findOneByField('code', 'default');
2019-07-01 11:33:36 +00:00
$custom_attributes = $this->attributeRepository->all(['id', 'code', 'admin_name', 'type']);
2018-07-17 13:28:34 +00:00
2018-08-09 04:35:13 +00:00
return view($this->_config['view'], compact('custom_attributes', 'attributeFamily'));
2018-07-17 13:28:34 +00:00
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
$this->validate(request(), [
2018-08-09 04:35:13 +00:00
'code' => ['required', 'unique:attribute_families,code', new \Webkul\Core\Contracts\Validations\Code],
2020-03-04 06:32:53 +00:00
'name' => 'required',
2018-07-17 13:28:34 +00:00
]);
2019-07-01 11:33:36 +00:00
$attributeFamily = $this->attributeFamilyRepository->create(request()->all());
2018-12-21 12:48:34 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Family']));
2018-07-17 13:28:34 +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-17 13:28:34 +00:00
*/
2019-07-01 11:33:36 +00:00
public function edit($id)
2018-07-17 13:28:34 +00:00
{
2019-07-01 11:33:36 +00:00
$attributeFamily = $this->attributeFamilyRepository->with(['attribute_groups.custom_attributes'])->findOrFail($id, ['*']);
2018-07-17 13:28:34 +00:00
2019-07-01 11:33:36 +00:00
$custom_attributes = $this->attributeRepository->all(['id', 'code', 'admin_name', 'type']);
2018-07-17 13:28:34 +00:00
2018-08-09 04:35:13 +00:00
return view($this->_config['view'], compact('attributeFamily', 'custom_attributes'));
2018-07-17 13:28:34 +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-17 13:28:34 +00:00
{
$this->validate(request(), [
2018-08-09 04:35:13 +00:00
'code' => ['required', 'unique:attribute_families,code,' . $id, new \Webkul\Core\Contracts\Validations\Code],
2020-03-04 06:32:53 +00:00
'name' => 'required',
2018-07-17 13:28:34 +00:00
]);
2019-07-01 11:33:36 +00:00
$attributeFamily = $this->attributeFamilyRepository->update(request()->all(), $id);
2018-12-21 12:48:34 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Family']));
2018-07-17 13:28:34 +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
$attributeFamily = $this->attributeFamilyRepository->findOrFail($id);
2019-07-01 11:33:36 +00:00
if ($this->attributeFamilyRepository->count() == 1) {
2019-01-16 08:38:39 +00:00
session()->flash('error', trans('admin::app.response.last-delete-error', ['name' => 'Family']));
2019-04-09 01:01:52 +00:00
2020-02-24 12:19:12 +00:00
} elseif ($attributeFamily->products()->count()) {
2019-01-16 08:38:39 +00:00
session()->flash('error', trans('admin::app.response.attribute-product-error', ['name' => 'Attribute family']));
2018-10-17 07:21:47 +00:00
} else {
2019-04-09 01:01:52 +00:00
try {
2019-07-01 11:33:36 +00:00
$this->attributeFamilyRepository->delete($id);
2019-04-09 01:01:52 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Family']));
2018-10-17 07:21:47 +00:00
return response()->json(['message' => true], 200);
2019-04-09 01:01:52 +00:00
} catch (\Exception $e) {
2020-01-27 16:06:03 +00:00
report($e);
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Family']));
2019-04-09 01:01:52 +00:00
}
2018-10-17 07:21:47 +00:00
}
return response()->json(['message' => false], 400);
2018-07-17 13:28:34 +00:00
}
/**
* Remove the specified resources from database
*
* @return response \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function massDestroy()
{
$suppressFlash = false;
if (request()->isMethod('delete')) {
$indexes = explode(',', request()->input('indexes'));
foreach ($indexes as $key => $value) {
try {
2019-07-01 11:33:36 +00:00
$this->attributeFamilyRepository->delete($value);
} catch (\Exception $e) {
2020-01-27 16:06:03 +00:00
report($e);
$suppressFlash = true;
continue;
}
}
2020-03-04 06:32:53 +00:00
if (! $suppressFlash) {
2019-01-02 14:42:49 +00:00
session()->flash('success', ('admin::app.datagrid.mass-ops.delete-success'));
2020-01-27 16:06:03 +00:00
} else {
session()->flash('info', trans('admin::app.datagrid.mass-ops.partial-action', ['resource' => 'Attribute Family']));
2020-01-27 16:06:03 +00:00
}
return redirect()->back();
} else {
session()->flash('error', trans('admin::app.datagrid.mass-ops.method-error'));
return redirect()->back();
}
}
2018-07-17 13:28:34 +00:00
}