Loading app/Http/Controllers/SecuritySheets/SecuritySheetController.php +51 −2 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Http\Controllers\SecuritySheets; use App\Models\User; use App\Models\DivingGroup; use Illuminate\Http\Request; use App\Models\DivingSession; use App\Models\DivingLocation; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; use App\Http\Controllers\SecuritySheets\StoreSilentStrategy; Loading @@ -33,10 +34,41 @@ public function generate(string $ds_code){ return $this->strategy->generatePdf($html, $ds_code); } /** * Shows a edition form for the security sheet of the specified diving session. * @param string $ds_code the code of the diving session * @return string a view of the editing form */ public function edit(string $ds_code){ return self::callView($ds_code, 'securitySheet.edit'); } /** * Updates the diving session and each of its diving groups based on the editing form. * Also generates the corresponding pdf file. * @param string $ds_code the code of the diving session * @param Request $request the HTTP request received by the server */ public function update(string $ds_code, Request $request){ $data = $request->json()->all(); Log::info($data); $divingSession = DivingSession::find($ds_code); $divingSession->DS_OBSERVATION_FIELD = $data['observation']; $divingSession->save(); foreach($data as $groupNumber => $groupData){ if(! is_array($groupData)){ continue; } self::updateDivingGroup($ds_code, $groupNumber, $groupData); } self::setStrategy(new StoreSilentStrategy); self::generate($ds_code); } /** * Sets the strategy for the security sheet and returns this instance. * @param $strategy the strategy Loading Loading @@ -81,4 +113,21 @@ private function callView(string $ds_code, string $viewName){ 'divingGroups' => $divingGroupsForView ]); } /** * Update a single diving group. * @param string $ds_code */ private function updateDivingGroup(string $ds_code, $dg_number, array $divingGroup){ DivingGroup::where('DS_CODE', $ds_code) ->where('DG_NUMBER', $dg_number) ->update([ 'DG_BEGINNING_OF_DIVING_HOUR' => $divingGroup['dg-start'], 'DG_END_OF_DIVING_HOUR' => $divingGroup['dg-end'], 'DG_MAX_DURATION' => $divingGroup['dg-exp-time'], 'DG_MAX_DEPTH' => $divingGroup['dg-exp-dep'], 'DG_EFFECTIVE_DIVING_DURATION' => $divingGroup['dg-act-time'], 'DG_MAX_EFFECTIVE_DEPTH' => $divingGroup['dg-act-dep'] ]); } } app/Models/DivingGroup.php +2 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ class DivingGroup extends Model { protected $table = 'CAR_DIVING_GROUP'; public $timestamps = false; use HasFactory; public function getDivers(){ Loading public/js/securitySheets/edit.js +71 −13 Original line number Diff line number Diff line Loading @@ -6,30 +6,57 @@ const expectedTimes = document.getElementsByClassName('dg-exp-time'); const actualTimes = document.getElementsByClassName('dg-act-time'); const expectedDepths = document.getElementsByClassName('dg-exp-dep'); const actualDepths = document.getElementsByClassName('dg-act-dep'); const inputsElement = gatherInputsElement(); const generateButton = document.getElementById('button'); const inputsElement = gatherInputsElement(); // Meta data const diveId = document.getElementById('divingSessionId').getAttribute('content'); const csrfToken = document.getElementById('csrf-token').getAttribute('content') generateButton.addEventListener('click', generate); /** * Saves the edited data to the database and generate. */ function generate(){ let data = {}; inputsElement.forEach((element) => { fetchField(data, element); }); data['observation'] = observationField.value; let group = { 'start' : startTimes[5].value, 'end' : endTimes[5].value, 'exp-time' : expectedTimes[5].value, 'act-time' : actualTimes[5].value, 'exp-dep' : expectedDepths[5].value, 'act-dep' : actualDepths[5].value }; let json = JSON.stringify(data); sendRequest(json, diveId); } console.log(JSON.stringify(group)); /** * Send the HTTP request to our server. * @param {*} json the data to send to the server * @param {*} diveId the code of the diving session */ function sendRequest(json, diveId){ fetch('/dives/' + diveId + '/security-sheet/update', { method: "POST", body: json, headers: { "Content-type": "application/json; charset=UTF-8", "x-csrf-token" : csrfToken } }) .then((response) => notify(response)); } //TODO display the notification on the screen function notify(response){ console.table(response); } /** * Puts all the input elements in the same array. * @returns an array containing all the input elements */ function gatherInputsElement(){ let elements = []; Array.prototype.push.apply(elements, startTimes); Loading @@ -38,6 +65,37 @@ function gatherInputsElement(){ Array.prototype.push.apply(elements, actualTimes); Array.prototype.push.apply(elements, expectedDepths); Array.prototype.push.apply(elements, actualDepths); console.log(elements.length); return elements; } /** * Fills the given array to build the json with the data of the form. * @param {*} array the array to write the data to * @param {*} element the input element the data must be written from */ function fetchField(array, element){ let key = getKeyFromClassList(element); let id = element.id.match(/\d+/)[0]; if(! (id in array)){ array[id] = {}; } array[id][key] = element.value; } /** * Reads the classList of an input element to retrieve the key (i.e. its the field it's for) * @param {*} element the input element * @returns the key */ function getKeyFromClassList(element){ let classList = element.classList; for(let i = 0; i < classList.length; i++){ if(classList[i].includes('dg')){ return classList[i] } } return false; } No newline at end of file resources/views/securitySheet/edit.blade.php +1 −0 Original line number Diff line number Diff line <script defer src="{{asset('js/securitySheets/edit.js')}}"></script> <meta id="divingSessionId" content="{{$dive->DS_CODE}}"> <meta id="csrf-token" content="{{csrf_token()}}"> <x-layout> <x-security-sheets.table> Loading routes/web.php +11 −2 Original line number Diff line number Diff line Loading @@ -89,6 +89,17 @@ */ Route::get('/dives/{ds_code}/security-sheet/generate', [SecuritySheetController::class, 'generate']); /** * Leads to a page that allows to edit a security sheet and generate it again. */ Route::get('/dives/{ds_code}/security-sheet/edit', [SecuritySheetController::class, 'edit']); /** * Updates the security sheet and generates it again. * @param $ds_code the code of the diving session */ Route::post('/dives/{ds_code}/security-sheet/update', [SecuritySheetController::class, 'update']); /** * Tries to register the user to a diving session. * Redirects to /dives afterhand. Loading Loading @@ -217,5 +228,3 @@ Route::middleware('App\Http\Middleware\rightChecker') ->get('manage/dives', [DivesList::class, 'showManagementList']) ->name('manage_dives_dir'); Route::get('/view/{ds_code}', [SecuritySheetController::class, 'edit']); Loading
app/Http/Controllers/SecuritySheets/SecuritySheetController.php +51 −2 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace App\Http\Controllers\SecuritySheets; use App\Models\User; use App\Models\DivingGroup; use Illuminate\Http\Request; use App\Models\DivingSession; use App\Models\DivingLocation; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; use App\Http\Controllers\SecuritySheets\StoreSilentStrategy; Loading @@ -33,10 +34,41 @@ public function generate(string $ds_code){ return $this->strategy->generatePdf($html, $ds_code); } /** * Shows a edition form for the security sheet of the specified diving session. * @param string $ds_code the code of the diving session * @return string a view of the editing form */ public function edit(string $ds_code){ return self::callView($ds_code, 'securitySheet.edit'); } /** * Updates the diving session and each of its diving groups based on the editing form. * Also generates the corresponding pdf file. * @param string $ds_code the code of the diving session * @param Request $request the HTTP request received by the server */ public function update(string $ds_code, Request $request){ $data = $request->json()->all(); Log::info($data); $divingSession = DivingSession::find($ds_code); $divingSession->DS_OBSERVATION_FIELD = $data['observation']; $divingSession->save(); foreach($data as $groupNumber => $groupData){ if(! is_array($groupData)){ continue; } self::updateDivingGroup($ds_code, $groupNumber, $groupData); } self::setStrategy(new StoreSilentStrategy); self::generate($ds_code); } /** * Sets the strategy for the security sheet and returns this instance. * @param $strategy the strategy Loading Loading @@ -81,4 +113,21 @@ private function callView(string $ds_code, string $viewName){ 'divingGroups' => $divingGroupsForView ]); } /** * Update a single diving group. * @param string $ds_code */ private function updateDivingGroup(string $ds_code, $dg_number, array $divingGroup){ DivingGroup::where('DS_CODE', $ds_code) ->where('DG_NUMBER', $dg_number) ->update([ 'DG_BEGINNING_OF_DIVING_HOUR' => $divingGroup['dg-start'], 'DG_END_OF_DIVING_HOUR' => $divingGroup['dg-end'], 'DG_MAX_DURATION' => $divingGroup['dg-exp-time'], 'DG_MAX_DEPTH' => $divingGroup['dg-exp-dep'], 'DG_EFFECTIVE_DIVING_DURATION' => $divingGroup['dg-act-time'], 'DG_MAX_EFFECTIVE_DEPTH' => $divingGroup['dg-act-dep'] ]); } }
app/Models/DivingGroup.php +2 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ class DivingGroup extends Model { protected $table = 'CAR_DIVING_GROUP'; public $timestamps = false; use HasFactory; public function getDivers(){ Loading
public/js/securitySheets/edit.js +71 −13 Original line number Diff line number Diff line Loading @@ -6,30 +6,57 @@ const expectedTimes = document.getElementsByClassName('dg-exp-time'); const actualTimes = document.getElementsByClassName('dg-act-time'); const expectedDepths = document.getElementsByClassName('dg-exp-dep'); const actualDepths = document.getElementsByClassName('dg-act-dep'); const inputsElement = gatherInputsElement(); const generateButton = document.getElementById('button'); const inputsElement = gatherInputsElement(); // Meta data const diveId = document.getElementById('divingSessionId').getAttribute('content'); const csrfToken = document.getElementById('csrf-token').getAttribute('content') generateButton.addEventListener('click', generate); /** * Saves the edited data to the database and generate. */ function generate(){ let data = {}; inputsElement.forEach((element) => { fetchField(data, element); }); data['observation'] = observationField.value; let group = { 'start' : startTimes[5].value, 'end' : endTimes[5].value, 'exp-time' : expectedTimes[5].value, 'act-time' : actualTimes[5].value, 'exp-dep' : expectedDepths[5].value, 'act-dep' : actualDepths[5].value }; let json = JSON.stringify(data); sendRequest(json, diveId); } console.log(JSON.stringify(group)); /** * Send the HTTP request to our server. * @param {*} json the data to send to the server * @param {*} diveId the code of the diving session */ function sendRequest(json, diveId){ fetch('/dives/' + diveId + '/security-sheet/update', { method: "POST", body: json, headers: { "Content-type": "application/json; charset=UTF-8", "x-csrf-token" : csrfToken } }) .then((response) => notify(response)); } //TODO display the notification on the screen function notify(response){ console.table(response); } /** * Puts all the input elements in the same array. * @returns an array containing all the input elements */ function gatherInputsElement(){ let elements = []; Array.prototype.push.apply(elements, startTimes); Loading @@ -38,6 +65,37 @@ function gatherInputsElement(){ Array.prototype.push.apply(elements, actualTimes); Array.prototype.push.apply(elements, expectedDepths); Array.prototype.push.apply(elements, actualDepths); console.log(elements.length); return elements; } /** * Fills the given array to build the json with the data of the form. * @param {*} array the array to write the data to * @param {*} element the input element the data must be written from */ function fetchField(array, element){ let key = getKeyFromClassList(element); let id = element.id.match(/\d+/)[0]; if(! (id in array)){ array[id] = {}; } array[id][key] = element.value; } /** * Reads the classList of an input element to retrieve the key (i.e. its the field it's for) * @param {*} element the input element * @returns the key */ function getKeyFromClassList(element){ let classList = element.classList; for(let i = 0; i < classList.length; i++){ if(classList[i].includes('dg')){ return classList[i] } } return false; } No newline at end of file
resources/views/securitySheet/edit.blade.php +1 −0 Original line number Diff line number Diff line <script defer src="{{asset('js/securitySheets/edit.js')}}"></script> <meta id="divingSessionId" content="{{$dive->DS_CODE}}"> <meta id="csrf-token" content="{{csrf_token()}}"> <x-layout> <x-security-sheets.table> Loading
routes/web.php +11 −2 Original line number Diff line number Diff line Loading @@ -89,6 +89,17 @@ */ Route::get('/dives/{ds_code}/security-sheet/generate', [SecuritySheetController::class, 'generate']); /** * Leads to a page that allows to edit a security sheet and generate it again. */ Route::get('/dives/{ds_code}/security-sheet/edit', [SecuritySheetController::class, 'edit']); /** * Updates the security sheet and generates it again. * @param $ds_code the code of the diving session */ Route::post('/dives/{ds_code}/security-sheet/update', [SecuritySheetController::class, 'update']); /** * Tries to register the user to a diving session. * Redirects to /dives afterhand. Loading Loading @@ -217,5 +228,3 @@ Route::middleware('App\Http\Middleware\rightChecker') ->get('manage/dives', [DivesList::class, 'showManagementList']) ->name('manage_dives_dir'); Route::get('/view/{ds_code}', [SecuritySheetController::class, 'edit']);