117 lines
3.7 KiB
PHP
117 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Models\SofaScoreTask;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\File;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
trait GrappingToExcel
|
|
{
|
|
protected int $max = 2000;
|
|
|
|
protected string $titel = '';
|
|
|
|
public function import_to_excel(string $folder, $title = '')
|
|
{
|
|
ini_set('memory_limit', -1);
|
|
|
|
if ($this->debug) {
|
|
Notice::note("Import $folder to Excel", 'bb');
|
|
}
|
|
|
|
switch ($folder) {
|
|
case 'daily':
|
|
$directories = File::directories($this->data_dir);
|
|
foreach ($directories as $directory) {
|
|
$day = substr(strrchr($directory, '/'), 1);
|
|
$this->titel = ucfirst($folder).'-'.$day;
|
|
$this->make_file($this->data_dir.'/'.$day);
|
|
}
|
|
break;
|
|
|
|
case 'range':
|
|
$task = SofaScoreTask::with('sport')->where('id', 3)->first();
|
|
$this->titel = ucfirst($folder).'-'.$this->clean_string($task->name);
|
|
$this->make_file($this->data_dir);
|
|
break;
|
|
|
|
case 'rose':
|
|
$day = Carbon::now()->subDay()->format('Y-m-d');
|
|
|
|
$this->titel = ucfirst($title).'-'.$day;
|
|
$this->make_file($this->data_dir);
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected function make_file(string $directory)
|
|
{
|
|
$files = $this->get_tree($directory);
|
|
|
|
// only json max 2000
|
|
$files = array_chunk($this->filter($files), $this->max);
|
|
|
|
foreach ($files as $key => $value) {
|
|
|
|
$has = count($value);
|
|
if (! $key && $has < $this->max) {
|
|
$count_from = 1;
|
|
$count_to = $has;
|
|
} elseif (! $key) {
|
|
$count_from = 1;
|
|
$key++;
|
|
$count_to = $key * $this->max;
|
|
} elseif ($has == $this->max) {
|
|
$count_from = $key * $this->max + 1;
|
|
$key++;
|
|
$count_to = $key * $this->max;
|
|
} else {
|
|
$count_from = $key * $this->max + 1;
|
|
$count_to = $key * $this->max + $has;
|
|
}
|
|
|
|
$spreadsheet = new Spreadsheet;
|
|
$spreadsheet->getProperties()->setTitle($this->titel.'-'.$count_from.'-'.$count_to);
|
|
$activeWorksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$column = 1;
|
|
foreach ($value as $file) {
|
|
$data = file_get_contents($file);
|
|
$data = json_decode($data, true);
|
|
$json_string = json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
$arr = explode(PHP_EOL, $json_string);
|
|
$row = 0;
|
|
foreach ($arr as $string) {
|
|
$row++;
|
|
$replacedString = preg_replace('/\\\\u([0-9abcdef]{4})/', '&#x$1;', $string);
|
|
$utf8string = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');
|
|
$activeWorksheet->setCellValue([$column, $row], $utf8string);
|
|
}
|
|
$column++;
|
|
}
|
|
|
|
$spreadsheet->setActiveSheetIndex(0);
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
$writer->save("$this->data_dir/$this->titel-$count_from-$count_to.xlsx");
|
|
}
|
|
}
|
|
|
|
protected function filter(array $files, array $types = ['json'])
|
|
{
|
|
$filtered = [];
|
|
foreach ($files as $file) {
|
|
$path_parts = pathinfo($file);
|
|
if (isset($path_parts['extension']) && in_array($path_parts['extension'], $types)) {
|
|
$filtered[] = $file;
|
|
}
|
|
}
|
|
|
|
return $filtered;
|
|
}
|
|
}
|