%PDF- %PDF-
Direktori : /home/nginx/.vscode-server/data/User/History/-2430a80a/ |
Current File : //home/nginx/.vscode-server/data/User/History/-2430a80a/kMux.php |
<?php namespace App\Workers; use App\Models\Contest; use Illuminate\Support\Str; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use PhpOffice\PhpSpreadsheet\Reader\Xls; use ZipArchive; class RegistrationExporter { protected $headers = [ 'Shooter#', 'First Name', 'Last Name', 'Email', 'IPSC#', 'Division', 'Squad', 'Region', 'Power Factor', 'NotCompeting' ]; public string $filename; function __construct(Contest $contest, string $type, public bool $all) { $now = Carbon::now(); $this->filename = $contest->id . "/" . $now->isoFormat("YYYY-MM-DD") . "_" . str_replace(".", "-", Str::ucfirst(Str::camel(Str::ascii($contest->contestname)))); switch ($type) { case "pscsv": $this->filename .= ".csv"; $content = $this->buildCSV($contest); break; case "excel": $this->filename .= ".xlsx"; $content = $this->buildExcel($contest); break; case "psc": $this->filename .= ".psc"; $content = $this->buildPractiscore($contest); break; } Storage::disk('contests')->put($this->filename, $content, 'public'); } protected function buildCSV(Contest $contest) { $data = "\xEF\xBB\xBF"; //BOM $data .= join(',', $this->headers) . "\n"; $regs = $this->all ? $contest->registrations : $contest->registrations->where('present', 1); foreach ($regs as $reg) { $data .= join(',', [ $reg->id, $reg->user->firstname, ($reg->notcomp ? "(MZ) " : "") . $reg->user->lastname . ($reg->user->suffix ? " " . $reg->user->suffix : ""), $reg->user->email, strtoupper($reg->user->username), $reg->division->bgdivision, $reg->squad, "CZE", "MINOR", $reg->notcomp ? "YES" : "" ]) . "\n"; } return $data; } protected function buildPractiscore(Contest $contest) { $txt = $contest->isLosik() ? Storage::disk('templates')->get("losik.match_def.json") : Storage::disk('templates')->get("match_def.json"); $def = json_decode($txt); $def->match_id = $contest->psmatchguid; $def->match_name = $contest->contestname; $def->match_date = $contest->date->format("Y-m-d"); $def->match_modifieddate = date("Y-m-d H:i:s.v"); $regs = $this->all ? $contest->registrations : $contest->registrations->where('present', 1); foreach ($regs as $reg) { $date = date("Y-m-d H:i:s.v"); $chkins = array(); if($reg->present) $chkins[] = "Checked in"; if($reg->notcomp) $chkins[] = "NotCompeting"; mt_srand($reg->id); $def->match_shooters[] = (object) array( "sh_uuid" => $reg->canceltoken, "sh_uid" => $reg->canceltoken, "sh_ln" => ($reg->notcomp ? "(MZ)" : "") . $reg->user->lastname . ($reg->user->suffix ? " " . $reg->user->suffix : ""), "sh_fn" => $reg->user->firstname, "sh_id" => strtoupper($reg->user->username), "sh_num" => $reg->id, "sh_random" => mt_rand(), "sh_sqd" => $reg->squad, "sh_del" => false, "sh_eml" => $reg->user->email, "sh_dvp" => $reg->division->bgdivision, "sh_pf" => "MINOR", "sh_grd" => "", "mod_ctgs" => $date, "mod_chkins" => $date, "sh_chkins" => $chkins, "sh_wlk" => false, "sh_mod" => $date, "mod_pr" => $date, "mod_dv" => $date, "mod_pf" => $date, "mod_sq" => $date, "mod_dl" => $date, "mod_dq" => $date, "sh_dq" => false, "sh_cc" => "CZE" ); } $txt = $contest->isLosik() ? Storage::disk('templates')->get("losik.match_scores.json") : Storage::disk('templates')->get("match_scores.json"); $scores = json_decode($txt); $scores->match_id = $contest->psmatchguid; $tmpName = tempnam(sys_get_temp_dir(), 'practiscore.zip'); $zip = new ZipArchive; $res = $zip->open($tmpName, ZipArchive::CREATE); $zip->addFromString("match_def.json", json_encode($def, JSON_PRETTY_PRINT)); $zip->addFromString("match_scores.json", json_encode($scores, JSON_PRETTY_PRINT)); $zip->close(); $contents = file_get_contents($tmpName); unlink($tmpName); return $contents; } protected function buildExcel(Contest $contest) { $fp = Storage::disk('templates')->readStream($contest->isLosik() ? "LOSik_2017-template.xls" : "los-tabulka-" . $contest->stages . ".xls"); $templateFile = stream_get_meta_data($fp)["uri"]; $regs = $this->all ? $contest->registrations : $contest->registrations->where('present', 1); $reader = new Xls(); $spreadsheet = $reader->load($templateFile); $ss = $spreadsheet->setActiveSheetIndex(0); $pos = 3; $date = date("d. m. Y"); $ss->setCellValue("A1", $contest->contestname); if ($contest->isLosik()) { foreach ($regs as $reg) { if($reg->notcomp) $ss->setCellValue("A" . $pos, "MZ"); $ss->setCellValue("B" . $pos, $reg->user->email . "/" . strtoupper($reg->user->username)); $ss->setCellValue("C" . $pos, $reg->user->firstname . " " . $reg->user->lastname . ($reg->user->suffix ? " " . $reg->user->suffix : "")); $ss->setCellValue("D" . $pos++, $date); } } else { $pos = array(); foreach ($regs as $reg) { $div = strtoupper($reg->division->bgdivision); if (!isset($pos[$div])) { $pos[$div] = ExcelHelper::getDefaultPosForDivision($div, $contest->stages); } if($reg->notcomp) $ss->setCellValue("A" . $pos[$div], "MZ"); $ss->setCellValue("B" . $pos[$div], $reg->user->email . "/" . strtoupper($reg->user->username)); $ss->setCellValue("C" . $pos[$div]++, $reg->user->firstname . " " . $reg->user->lastname . ($reg->user->suffix ? " " . $reg->user->suffix : "")); } } $tmpName = tempnam(sys_get_temp_dir(), 'export.xlsx'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); $writer->save($tmpName); $excel = file_get_contents($tmpName); unlink($tmpName); return $excel; } }