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

193 lines
5.1 KiB
PHP
Raw Normal View History

2018-07-11 05:41:27 +00:00
<?php
namespace Webkul\Attribute\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2018-07-17 13:28:34 +00:00
use Webkul\Attribute\Repositories\AttributeRepository as Attribute;
use Event;
2018-07-17 13:28:34 +00:00
2018-07-11 05:41:27 +00:00
/**
* Catalog attribute controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
2018-07-17 13:28:34 +00:00
/**
* AttributeRepository object
*
* @var array
*/
protected $attribute;
2018-07-11 05:41:27 +00:00
/**
* Create a new controller instance.
*
* @param \Webkul\Attribute\Repositories\AttributeRepository $attribute
2018-07-11 05:41:27 +00:00
* @return void
*/
2018-07-17 13:28:34 +00:00
public function __construct(Attribute $attribute)
2018-07-11 05:41:27 +00:00
{
2018-07-17 13:28:34 +00:00
$this->attribute = $attribute;
2018-07-11 05:41:27 +00:00
$this->_config = request('_config');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
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(), [
2018-08-09 04:35:13 +00:00
'code' => ['required', 'unique:attributes,code', new \Webkul\Core\Contracts\Validations\Code],
2018-07-17 13:28:34 +00:00
'admin_name' => 'required',
2018-07-11 05:41:27 +00:00
'type' => 'required'
]);
$data = request()->all();
$data['is_user_defined'] = 1;
$attribute = $this->attribute->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
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$attribute = $this->attribute->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
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate(request(), [
2018-08-09 04:35:13 +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',
'type' => 'required'
2018-07-11 05:41:27 +00:00
]);
2018-12-21 12:48:34 +00:00
$attribute = $this->attribute->update(request()->all(), $id);
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)
{
return request()->all();
2018-10-17 07:21:47 +00:00
$attribute = $this->attribute->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 {
$this->attribute->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']));
} catch(\Exception $e) {
2019-01-16 08:38:39 +00:00
session()->flash('error', trans('admin::app.response.attribute-error', ['name' => 'Attribute']));
}
2018-10-17 07:21:47 +00:00
}
return redirect()->back();
2018-07-11 05:41:27 +00:00
}
/**
* Remove the specified resources from database
*
* @return response \Illuminate\Http\Response
*/
public function massDestroy()
{
$suppressFlash = false;
if (request()->isMethod('post')) {
$indexes = explode(',', request()->input('indexes'));
foreach ($indexes as $key => $value) {
2018-12-19 11:12:29 +00:00
$attribute = $this->attribute->findOrFail($value);
try {
2019-01-15 11:54:41 +00:00
if (! $attribute->is_user_defined) {
continue;
2018-12-21 12:48:34 +00:00
} else {
2018-12-19 11:12:29 +00:00
$this->attribute->delete($value);
2018-12-21 12:48:34 +00:00
}
} catch (\Exception $e) {
$suppressFlash = true;
continue;
}
}
2019-01-15 11:54:41 +00:00
if (! $suppressFlash)
2018-12-19 11:12:29 +00:00
session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success', ['resource' => 'attributes']));
else
session()->flash('info', trans('admin::app.datagrid.mass-ops.partial-action', ['resource' => 'attributes']));
return redirect()->back();
} else {
session()->flash('error', trans('admin::app.datagrid.mass-ops.method-error'));
return redirect()->back();
}
}
}