store-manage/app/Admin/Controllers/AdminController.php

107 lines
2.6 KiB
PHP

<?php
namespace App\Admin\Controllers;
use App\Traits\CustomActionTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Slowlyo\OwlAdmin\Controllers\AdminController as Controller;
use Throwable;
/**
* @property \App\Admin\Services\BaseService $service
*/
abstract class AdminController extends Controller
{
use CustomActionTrait;
/**
* {@inheritdoc}
*/
public function store(Request $request)
{
try {
DB::beginTransaction();
if ($this->actionOfQuickEdit()) {
$result = $this->service->quickEdit($request->all());
} elseif ($this->actionOfQuickEditItem()) {
$result = $this->service->quickEditItem($request->all());
} else {
$result = $this->service->store($request->all());
}
if (! $result) {
admin_abort($this->service->getError() ?: __('admin.save') . __('admin.failed'));
}
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
throw $th;
}
return $this->response()->successMessage(__('admin.save') . __('admin.successfully'));
}
/**
* {@inheritdoc}
*/
public function update(Request $request)
{
$input = Arr::except($request->all(), $this->service->primaryKey());
if ($request->filled('_fields')) {
$input = Arr::only($input, explode(',', $request->input('_fields')));
}
try {
DB::beginTransaction();
$result = $this->service->update($this->getPrimaryValue($request), $input);
if (! $result) {
admin_abort($this->service->getError() ?: __('admin.save') . __('admin.failed'));
}
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
throw $th;
}
return $this->response()->successMessage(__('admin.save') . __('admin.successfully'));
}
/**
* {@inheritdoc}
*/
public function destroy($ids)
{
try {
DB::beginTransaction();
$this->service->delete($ids);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
throw $th;
}
return $this->response()->successMessage(__('admin.delete') . __('admin.successfully'));
}
public function getQuickEditItemPath(array $fields = ['*'])
{
$path = $this->getUpdatePath();
if ($fields != ['*']) {
$path .= '?_fields='.implode(',', $fields);
}
return $path;
}
}