Skip to content

Commit

Permalink
Vue badge page added
Browse files Browse the repository at this point in the history
  • Loading branch information
usp-npe committed Apr 19, 2022
1 parent 91718e5 commit 859f870
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 85 deletions.
77 changes: 75 additions & 2 deletions app/Http/Controllers/Badges/BadgeMakerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function selection(Request $request)
$persons[] = [
'name' => $row['name'],
'position' => $row['position'],
'code' => $row->code ?? null,
];
}
}
Expand All @@ -110,7 +109,6 @@ public function selection(Request $request)
'name' => $request->name[$i],
'position' => $request->position[$i] ?? null,
'picture' => $picture,
'code' => null,
];
}
}
Expand Down Expand Up @@ -185,6 +183,81 @@ public function make(Request $request)
}
}

public function make2(Request $request)
{
$request->validate([
'elements' => [
'required',
'array',
'min:1',
],
'elements.*.name' => [
'string',
'filled',
],
'elements.*.file' => [
'file',
'image',
],
'alt_logo' => [
'file',
'image',
],
]);

$title = __('Badges');

$badgeCreator = new BadgeCreator($request->elements);
if ($request->hasFile('alt_logo')) {
$badgeCreator->logo = $request->file('alt_logo');
} elseif (Setting::has('badges.logo_file')) {
$badgeCreator->logo = Storage::path(Setting::get('badges.logo_file'));
}
try {
$badgeCreator->createPdf($title);
} catch (Exception $e) {
return redirect()->route('badges.index')
->with('error', $e->getMessage());
}
}

public function parseSpreadsheet(Request $request)
{
$request->validate([
'file' => [
'required',
'file',
'mimes:xlsx,xls,csv',
],
]);

$elements = [];
$sheets = (new BadgeImport())->toArray($request->file('file'));
foreach ($sheets as $rows) {
foreach ($rows as $row) {
if (isset($row['name']) && isset($row['position'])) {
$elements[] = [
'name' => $row['name'],
'position' => $row['position'],
];
}
}
}

return response()->json($elements);
}

public function fetchCommunityVolunteers()
{
$this->authorize('viewAny', CommunityVolunteer::class);

$elements = CommunityVolunteer::workStatus('active')
->get()
->map(fn ($cmtyvol) => self::communityVolunteerToBadgePerson($cmtyvol));

return response()->json($elements);
}

private static function addCommunityVolunteerData($person)
{
if (isset($person['type']) && $person['type'] == 'cmtyvol' && isset($person['id'])) {
Expand Down
46 changes: 0 additions & 46 deletions app/Util/Badges/BadgeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public function createPdf($title)
text-align: right;
font-size: 6pt;
}
.code_no {
font-size: 8pt;
}
', 1);

$pageWidth = $mpdf->w;
Expand Down Expand Up @@ -138,13 +135,6 @@ public function createPdf($title)
$yp = $y + $this->padding;
$wp = $this->badgeWidth - (2 * $this->padding);
$hp = $this->badgeHeight - (2 * $this->padding);
// $xpb = $x + $this->badgeWidth + $this->padding;
// $ypb = $pageHeight - $yp - $hp;
// $rx0 = $pageWidth - ($this->badgeWidth * 2 * $badgesX);
// $rx = $rx0 + ($badgeFrontAndBackWidth * ($badgesX - 1- ($i % $badgesX)));

// Page number
// $page = floor($i / $badgesPerPage) + 1;

// Line on the right
$mpdf->Line($x + $badgeFrontAndBackWidth, $y, $x + $badgeFrontAndBackWidth, $y + $this->badgeHeight);
Expand Down Expand Up @@ -193,44 +183,8 @@ public function createPdf($title)

// Punch hole
$mpdf->writeHTML('<div style="position: absolute; left: '. ($x + ($this->badgeWidth / 2) - ($this->punchHoleSize / 2)) .'mm; top: '. ($y + $this->punchHoleDistanceCenter - ($this->punchHoleSize / 2)) .'mm; width: '. $this->punchHoleSize .'mm; height: ' . $this->punchHoleSize . 'mm; border-radius: ' . ($this->punchHoleSize / 2) .'mm; border: 1px dotted black;"></div>');

// // QR Code of ID
// if (! empty($persons[$i]['code'])) {
// $mpdf->WriteFixedPosHTML('<barcode code="'. $persons[$i]['code'] .'" type="QR" class="barcode" size="0.5" error="M" disableborder="1" /><br><small class="code_no">' . $persons[$i]['code'].'</small>', $xpb - 2, $yp + $hp - 13, 30, 30, 'auto');
// }
}

$mpdf->Output($title . ' ' .Carbon::now()->toDateString() . '.pdf', Destination::DOWNLOAD);
}

// private static function fitImage($mpdf, $imageSource, $x, $y, $containerWidth, $containerHeight)
// {
// // Get image width
// $imageWidth = getimagesize($imageSource)[0];
// // Get image height
// $imageHeight = getimagesize($imageSource)[1];
// // Get image aspect ratio
// $imageRatio = $imageWidth / $imageHeight;
// // Get container aspect ratio
// $containerRatio = $containerWidth / $containerHeight;

// // Decide if image should increase in height or width
// if ($imageRatio > $containerRatio) {
// $width = $containerWidth;
// $height = $containerWidth / $imageRatio;
// } elseif ($imageRatio < $containerRatio) {
// $width = $containerHeight * $imageRatio;
// $height = $containerHeight;
// } else {
// $width = $containerWidth;
// $height = $containerHeight;
// }

// // Center image
// $offsetX = ($containerWidth / 2) - ($width / 2);
// $offsetY = ($containerHeight / 2) - ($height / 2);

// // Write image to PDF
// $mpdf->Image($imageSource, $x + $offsetX, $y + $offsetY, $width, $height);
// }
}
6 changes: 4 additions & 2 deletions lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@
"Contact information": "Kontaktinformationen",
"Include Portraits": "Inklusive Portraits",
"Age distribution": "Altersverteilung",
"File must be in Excel or CSV format and contain the columns 'Name', 'Position' and optional 'Code'.": "Datei muss im Excel oder CSV Format vorliegen und die Spalten 'Name', 'Position' und optional 'Code' beinhalten.",
"File must be in Excel or CSV format and contain the columns 'Name', 'Position'.": "Datei muss im Excel oder CSV Format vorliegen und die Spalten 'Name', 'Position' beinhalten.",
"No starting date has been set.": "Kein Startdatum gesetzt.",
"persons": "Personen",
"Accounting": "Buchhaltung",
Expand Down Expand Up @@ -776,5 +776,7 @@
"Visitor deleted.": "Besucher gelöscht.",
":count check-ins today": ":count check-ins heute",
"Inactive visitor records will be anonymized after :days days.": "Inaktive Besucherdaten werden nach :days Tagen gelöscht.",
"Edit metadata": "Metadaten bearbeiten"
"Edit metadata": "Metadaten bearbeiten",
"Add row": "Zeile hinzufügen",
"Load data": "Daten laden"
}
17 changes: 17 additions & 0 deletions resources/js/api/badges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { api, route } from "@/api/baseApi";
export default {
async make(params) {
const url = route("api.badges.make", params);
return await api.download(url, "POST");
},
async fetchCommunityVolunteers() {
const url = route("api.badges.fetchCommunityVolunteers");
return await api.get(url);
},
async parseSpreadsheet(file) {
const formData = new FormData();
formData.append('file', file)
const url = route("api.badges.parseSpreadsheet");
return await api.postFormData(url, formData);
}
};
50 changes: 30 additions & 20 deletions resources/js/api/baseApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ import ziggyMixin from "@/mixins/ziggyMixin";
export const route = ziggyMixin.methods.route;

export const getAjaxErrorMessage = function(err) {
var msg;
if (err.response) {
if (err.response.data.message) {
msg = err.response.data.message;
}
if (err.response.data.errors) {
msg +=
"\n" +
Object.entries(err.response.data.errors).map(([k, v]) => {
return v.join(". ");
});
} else if (err.response.data.error) {
msg = err.response.data.error;
}
let msg = parseErrorResponse(err.response.data);
if (!msg) {
msg = `Error ${err.response.status}: ${err.response.statusText}`;
return `Error ${err.response.status}: ${err.response.statusText}`;
}
} else {
msg = err;
return msg
}
return msg;
return err
};

const parseErrorResponse = function(data) {
let msg
if (data.message) {
msg = data.message;
}
if (data.errors) {
msg += "\n" +
Object.entries(data.errors)
.map(([, v]) => v.filter(e => !msg.includes(e)).join(", "))
.join(' ');
} else if (data.error) {
msg = data.error;
}
return msg
}

const parseBlobDataJson = async function(data) {
if (data instanceof Blob && data.type == 'application/json') {
return JSON.parse(await data.text());
}
return data
}

const handleError = function(err) {
console.error(err);
throw getAjaxErrorMessage(err);
Expand Down Expand Up @@ -88,11 +98,11 @@ export const api = {
handleError(err);
}
},
async download(downloadUrl) {
async download(downloadUrl, method="GET") {
try {
let response = await axios({
url: downloadUrl,
method: "GET",
method: method,
responseType: "blob"
});

Expand All @@ -113,7 +123,7 @@ export const api = {
document.body.appendChild(link);
link.click();
} catch (err) {
handleError(err);
handleError(err.response ? parseErrorResponse(await parseBlobDataJson(err.response.data)) : err);
}
}
};
Loading

0 comments on commit 859f870

Please sign in to comment.