Loading app/Http/Controllers/SecuritySheets/SecuritySheetController.php +57 −5 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 @@ -30,10 +30,45 @@ public function __construct(){ * @return string|\View what to display on the page (what the strategy returns) */ public function generate(string $ds_code){ $html = self::callPdfBuilderView($ds_code); $html = self::callView($ds_code, 'securitySheet.pdf'); 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 @@ -49,7 +84,7 @@ public function setStrategy(SecuritySheetStrategy $strategy){ * @param $ds_code the code of the diving session * @return string the HTML code */ private function callPdfBuilderView(string $ds_code){ private function callView(string $ds_code, string $viewName){ $dive = DivingSession::find($ds_code); $director = User::find($dive->US_ID_CAR_DIRECT); Loading @@ -69,7 +104,7 @@ private function callPdfBuilderView(string $ds_code){ ]; } return view('securitySheet.pdf', [ return view($viewName, [ 'dive' => $dive, 'director' => $director, 'surfaceSecurity' => $surfaceSecurity, Loading @@ -78,4 +113,21 @@ private function callPdfBuilderView(string $ds_code){ '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 @@ -11,6 +11,8 @@ class DivingGroup extends Model protected $table = 'CAR_DIVING_GROUP'; protected $fillable = ['DS_CODE', 'DG_NUMBER']; public $timestamps = false; use HasFactory; public function getDivers(){ Loading public/js/securitySheets/edit.js 0 → 100644 +101 −0 Original line number Diff line number Diff line // DOM Elements const observationField = document.getElementById('observation-field'); const startTimes = document.getElementsByClassName('dg-start'); const endTimes = document.getElementsByClassName('dg-end'); 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 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 json = JSON.stringify(data); sendRequest(json, diveId); } /** * 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); Array.prototype.push.apply(elements, endTimes); Array.prototype.push.apply(elements, expectedTimes); Array.prototype.push.apply(elements, actualTimes); Array.prototype.push.apply(elements, expectedDepths); Array.prototype.push.apply(elements, actualDepths); 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/components/security-sheets/cell.blade.php 0 → 100644 +3 −0 Original line number Diff line number Diff line <td class="border-2 border-solid border-black" colspan="{{$colspan ?? ''}}"> {{$slot}} </td> No newline at end of file resources/views/components/security-sheets/divers-header.blade.php 0 → 100644 +3 −0 Original line number Diff line number Diff line <tr class="bg-[#D8D8D8]"> {{$slot}} </tr> No newline at end of file Loading
app/Http/Controllers/SecuritySheets/SecuritySheetController.php +57 −5 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 @@ -30,10 +30,45 @@ public function __construct(){ * @return string|\View what to display on the page (what the strategy returns) */ public function generate(string $ds_code){ $html = self::callPdfBuilderView($ds_code); $html = self::callView($ds_code, 'securitySheet.pdf'); 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 @@ -49,7 +84,7 @@ public function setStrategy(SecuritySheetStrategy $strategy){ * @param $ds_code the code of the diving session * @return string the HTML code */ private function callPdfBuilderView(string $ds_code){ private function callView(string $ds_code, string $viewName){ $dive = DivingSession::find($ds_code); $director = User::find($dive->US_ID_CAR_DIRECT); Loading @@ -69,7 +104,7 @@ private function callPdfBuilderView(string $ds_code){ ]; } return view('securitySheet.pdf', [ return view($viewName, [ 'dive' => $dive, 'director' => $director, 'surfaceSecurity' => $surfaceSecurity, Loading @@ -78,4 +113,21 @@ private function callPdfBuilderView(string $ds_code){ '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 @@ -11,6 +11,8 @@ class DivingGroup extends Model protected $table = 'CAR_DIVING_GROUP'; protected $fillable = ['DS_CODE', 'DG_NUMBER']; public $timestamps = false; use HasFactory; public function getDivers(){ Loading
public/js/securitySheets/edit.js 0 → 100644 +101 −0 Original line number Diff line number Diff line // DOM Elements const observationField = document.getElementById('observation-field'); const startTimes = document.getElementsByClassName('dg-start'); const endTimes = document.getElementsByClassName('dg-end'); 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 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 json = JSON.stringify(data); sendRequest(json, diveId); } /** * 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); Array.prototype.push.apply(elements, endTimes); Array.prototype.push.apply(elements, expectedTimes); Array.prototype.push.apply(elements, actualTimes); Array.prototype.push.apply(elements, expectedDepths); Array.prototype.push.apply(elements, actualDepths); 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/components/security-sheets/cell.blade.php 0 → 100644 +3 −0 Original line number Diff line number Diff line <td class="border-2 border-solid border-black" colspan="{{$colspan ?? ''}}"> {{$slot}} </td> No newline at end of file
resources/views/components/security-sheets/divers-header.blade.php 0 → 100644 +3 −0 Original line number Diff line number Diff line <tr class="bg-[#D8D8D8]"> {{$slot}} </tr> No newline at end of file