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('
', $parts);
})
->addColumn('action', function ($row) {
$html = '
';
$html .= '';
$html .= ' ';
$html .= '
';
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')]);
}
}