2021-10-01 08:27:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Role;
|
|
|
|
|
use App\Http\Requests\EraseCellContentRequest;
|
|
|
|
|
use App\Http\Requests\SetCellContentRequest;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\WeekPlanning;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
|
|
class PlanningController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$date = Carbon::now()->startOfWeek();
|
|
|
|
|
if ($request->p) {
|
|
|
|
|
$date = Carbon::createFromTimestamp($request->p)->startOfWeek();
|
|
|
|
|
}
|
|
|
|
|
$userIds = User::where('role', Role::developper())->pluck('id');
|
|
|
|
|
$weeks = WeekPlanning::whereDate('start', $date)
|
|
|
|
|
->with('user')->get();
|
|
|
|
|
foreach ($userIds as $id) {
|
|
|
|
|
// auto create missing planning for user
|
2021-10-02 14:04:43 +02:00
|
|
|
if ($date >= Carbon::now()->startOfWeek() and !$weeks->contains('user.id', $id)) {
|
2021-10-01 08:27:06 +02:00
|
|
|
$wp = new WeekPlanning([
|
|
|
|
|
'start' => $date,
|
|
|
|
|
'planning' => WeekPlanning::getWeekBlock($date),
|
|
|
|
|
'user_id' => $id
|
|
|
|
|
]);
|
|
|
|
|
$wp->saveOrFail();
|
|
|
|
|
$weeks->push($wp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return view('planning.index', compact('weeks', 'date'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setCellContent(SetCellContentRequest $request)
|
|
|
|
|
{
|
|
|
|
|
$wp = WeekPlanning::findOrFail($request->id);
|
|
|
|
|
if (!isset($wp->planning[$request->day][$request->row])) {
|
|
|
|
|
abort(422, trans('Ce jour et ce moment n\'existent pas.'));
|
|
|
|
|
}
|
|
|
|
|
$planning = $wp->planning;
|
|
|
|
|
$planning[$request->day][$request->row] =
|
|
|
|
|
array_merge(WeekPlanning::getCell(), array_intersect_key($request->all(), WeekPlanning::getCell()));
|
|
|
|
|
$wp->planning = $planning;
|
|
|
|
|
$wp->saveOrFail();
|
|
|
|
|
return \response()->json(['status' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function eraseCellContent(EraseCellContentRequest $request)
|
|
|
|
|
{
|
|
|
|
|
$wp = WeekPlanning::findOrFail($request->id);
|
|
|
|
|
if (!isset($wp->planning[$request->day][$request->row])) {
|
|
|
|
|
abort(422, trans('Ce jour et ce moment n\'existent pas.'));
|
|
|
|
|
}
|
|
|
|
|
$planning = $wp->planning;
|
|
|
|
|
$planning[$request->day][$request->row] = WeekPlanning::getCell();
|
|
|
|
|
$wp->planning = $planning;
|
|
|
|
|
$wp->saveOrFail();
|
|
|
|
|
return \response()->json(['status' => true]);
|
|
|
|
|
}
|
|
|
|
|
}
|