ultimatepos/Modules/ManagementTools/Http/Controllers/ToolController.php

165 lines
5.8 KiB
PHP

<?php
namespace Modules\ManagementTools\Http\Controllers;
use App\Utils\ModuleUtil;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\ManagementTools\Entities\MtTool;
use Modules\ManagementTools\Utils\ManagementToolsUtil;
use Yajra\DataTables\Facades\DataTables;
class ToolController extends Controller
{
protected $moduleUtil;
protected $mtUtil;
public function __construct(ModuleUtil $moduleUtil, ManagementToolsUtil $mtUtil)
{
$this->moduleUtil = $moduleUtil;
$this->mtUtil = $mtUtil;
}
protected function authorizeAccess()
{
if (! auth()->user()->can('managementtools.access') || ! auth()->user()->can('managementtools.manage_tools')) {
abort(403, 'Unauthorized action.');
}
}
public function index()
{
$this->authorizeAccess();
$business_id = request()->session()->get('user.business_id');
if (request()->ajax()) {
$tools = MtTool::forBusiness($business_id)->with('parent')->withCount('children')->orderBy('title');
return DataTables::of($tools)
->addColumn('parent_name', function ($row) {
return $row->parent ? $row->parent->title : '—';
})
->addColumn('attributes_display', function ($row) {
if (empty($row->attributes) || ! is_array($row->attributes)) {
return '—';
}
$parts = [];
foreach ($row->attributes as $key => $val) {
$parts[] = e($key).': '.e($val);
}
return implode('<br>', $parts);
})
->addColumn('action', function ($row) {
$html = '<div class="btn-group">';
$html .= '<button type="button" data-href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary btn-modal" data-container=".mt_tool_modal"><i class="glyphicon glyphicon-edit"></i> '.__('managementtools::lang.edit').'</button>';
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_mt_tool"><i class="glyphicon glyphicon-trash"></i></button>';
$html .= '</div>';
return $html;
})
->rawColumns(['action', 'attributes_display'])
->make(true);
}
return view('managementtools::tools.index');
}
public function create()
{
$this->authorizeAccess();
$business_id = request()->session()->get('user.business_id');
$parents = $this->mtUtil->getToolsDropdown($business_id);
return view('managementtools::tools.create', compact('parents'));
}
public function store(Request $request)
{
$this->authorizeAccess();
$business_id = $request->session()->get('user.business_id');
$request->validate([
'title' => 'required|string|max:150',
'parent_id' => 'nullable|integer|exists:mt_tools,id',
'attribute_name' => 'nullable|array',
'attribute_value' => 'nullable|array',
]);
$attributes = $this->mtUtil->parseToolAttributes(
$request->input('attribute_name', []),
$request->input('attribute_value', [])
);
MtTool::create([
'business_id' => $business_id,
'title' => $request->title,
'parent_id' => $request->parent_id ?: null,
'attributes' => $attributes ?: null,
]);
$output = ['success' => true, 'msg' => __('managementtools::lang.tool_added')];
return response()->json($output);
}
public function edit($id)
{
$this->authorizeAccess();
$business_id = request()->session()->get('user.business_id');
$tool = MtTool::forBusiness($business_id)->findOrFail($id);
$parents = $this->mtUtil->getToolsDropdown($business_id, $tool->id);
return view('managementtools::tools.edit', compact('tool', 'parents'));
}
public function update(Request $request, $id)
{
$this->authorizeAccess();
$business_id = $request->session()->get('user.business_id');
$tool = MtTool::forBusiness($business_id)->findOrFail($id);
$request->validate([
'title' => 'required|string|max:150',
'parent_id' => 'nullable|integer|exists:mt_tools,id',
'attribute_name' => 'nullable|array',
'attribute_value' => 'nullable|array',
]);
if ($request->parent_id == $tool->id) {
return response()->json(['success' => false, 'msg' => __('managementtools::lang.something_went_wrong')]);
}
$attributes = $this->mtUtil->parseToolAttributes(
$request->input('attribute_name', []),
$request->input('attribute_value', [])
);
$tool->update([
'title' => $request->title,
'parent_id' => $request->parent_id ?: null,
'attributes' => $attributes ?: null,
]);
$output = ['success' => true, 'msg' => __('managementtools::lang.tool_updated')];
return response()->json($output);
}
public function destroy($id)
{
$this->authorizeAccess();
$business_id = request()->session()->get('user.business_id');
$tool = MtTool::forBusiness($business_id)->withCount('children')->findOrFail($id);
if ($tool->children_count > 0) {
return response()->json(['success' => false, 'msg' => __('managementtools::lang.cannot_delete_has_children')]);
}
$tool->delete();
return response()->json(['success' => true, 'msg' => __('managementtools::lang.tool_deleted')]);
}
}