166 lines
6.7 KiB
PHP
166 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\ManagementTools\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\ManagementTools\Entities\MtScoreCard;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ScoreCardController extends Controller
|
|
{
|
|
protected function authorizeAccess()
|
|
{
|
|
if (! auth()->user()->can('managementtools.access') || ! auth()->user()->can('managementtools.manage_scores')) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
|
|
protected function ensureDefaultCards(int $business_id): void
|
|
{
|
|
if (MtScoreCard::forBusiness($business_id)->exists()) {
|
|
return;
|
|
}
|
|
|
|
$defaults = [
|
|
['title' => __('managementtools::lang.default_card_excellent'), 'color_key' => 'green', 'points' => 10, 'icon' => 'fa-trophy', 'sort_order' => 1],
|
|
['title' => __('managementtools::lang.default_card_good'), 'color_key' => 'blue', 'points' => 5, 'icon' => 'fa-thumbs-up', 'sort_order' => 2],
|
|
['title' => __('managementtools::lang.default_card_warning'), 'color_key' => 'orange', 'points' => -3, 'icon' => 'fa-exclamation-triangle', 'sort_order' => 3],
|
|
['title' => __('managementtools::lang.default_card_penalty'), 'color_key' => 'red', 'points' => -10, 'icon' => 'fa-times-circle', 'sort_order' => 4],
|
|
];
|
|
|
|
foreach ($defaults as $card) {
|
|
MtScoreCard::create(array_merge($card, [
|
|
'business_id' => $business_id,
|
|
'is_active' => true,
|
|
'created_by' => auth()->id(),
|
|
]));
|
|
}
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$this->ensureDefaultCards($business_id);
|
|
|
|
if ($request->ajax()) {
|
|
$cards = MtScoreCard::forBusiness($business_id)->ordered();
|
|
|
|
return DataTables::of($cards)
|
|
->addColumn('card_preview', function ($row) {
|
|
return view('managementtools::score_cards.partials.card_badge', ['card' => $row])->render();
|
|
})
|
|
->addColumn('points_fmt', fn ($row) => $row->pointsFormatted())
|
|
->addColumn('status_label', fn ($row) => $row->is_active
|
|
? '<span class="label label-success">'.__('managementtools::lang.is_active').'</span>'
|
|
: '<span class="label label-default">'.__('managementtools::lang.inactive').'</span>')
|
|
->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_score_card_modal"><i class="glyphicon glyphicon-edit"></i></button>';
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_mt_score_card"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
})
|
|
->editColumn('title', fn ($row) => e($row->title))
|
|
->rawColumns(['card_preview', 'status_label', 'action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('managementtools::score_cards.index', [
|
|
'colors' => MtScoreCard::colorPresets(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeAccess();
|
|
|
|
return view('managementtools::score_cards.create', [
|
|
'colors' => MtScoreCard::colorPresets(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
|
|
$request->validate([
|
|
'title' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'color_key' => 'required|in:'.implode(',', array_keys(MtScoreCard::colorPresets())),
|
|
'points' => 'required|numeric',
|
|
'icon' => 'nullable|string|max:50',
|
|
'sort_order' => 'nullable|integer|min:0|max:999',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
MtScoreCard::create([
|
|
'business_id' => $business_id,
|
|
'title' => $request->title,
|
|
'description' => $request->description,
|
|
'color_key' => $request->color_key,
|
|
'points' => $request->points,
|
|
'icon' => $request->input('icon') ?: 'fa-star',
|
|
'sort_order' => $request->input('sort_order', 0),
|
|
'is_active' => $request->boolean('is_active', true),
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.score_card_added')]);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$card = MtScoreCard::forBusiness($business_id)->findOrFail($id);
|
|
|
|
return view('managementtools::score_cards.edit', [
|
|
'card' => $card,
|
|
'colors' => MtScoreCard::colorPresets(),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$card = MtScoreCard::forBusiness($business_id)->findOrFail($id);
|
|
|
|
$request->validate([
|
|
'title' => 'required|string|max:150',
|
|
'description' => 'nullable|string|max:500',
|
|
'color_key' => 'required|in:'.implode(',', array_keys(MtScoreCard::colorPresets())),
|
|
'points' => 'required|numeric',
|
|
'icon' => 'nullable|string|max:50',
|
|
'sort_order' => 'nullable|integer|min:0|max:999',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
$card->update([
|
|
'title' => $request->title,
|
|
'description' => $request->description,
|
|
'color_key' => $request->color_key,
|
|
'points' => $request->points,
|
|
'icon' => $request->input('icon') ?: 'fa-star',
|
|
'sort_order' => $request->input('sort_order', 0),
|
|
'is_active' => $request->boolean('is_active', true),
|
|
]);
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.score_card_updated')]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$card = MtScoreCard::forBusiness($business_id)->findOrFail($id);
|
|
$card->delete();
|
|
|
|
return response()->json(['success' => true, 'msg' => __('managementtools::lang.score_card_deleted')]);
|
|
}
|
|
}
|