103 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2020-01-20 02:05:40 +03:00
<?php
namespace App\Abstracts;
use App\Events\Export\HeadingsPreparing;
use App\Events\Export\RowsPreparing;
2021-04-16 00:59:43 +03:00
use App\Notifications\Common\ExportFailed;
2020-03-25 20:21:42 +03:00
use App\Utilities\Date;
2021-04-16 00:59:43 +03:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
2020-01-20 02:05:40 +03:00
use Illuminate\Support\Str;
2021-04-16 00:59:43 +03:00
use Maatwebsite\Excel\Concerns\Exportable;
2020-01-20 02:05:40 +03:00
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
2023-06-19 17:17:26 +03:00
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
2020-01-20 02:05:40 +03:00
2023-06-19 17:17:26 +03:00
abstract class Export implements FromCollection, HasLocalePreference, ShouldAutoSize, ShouldQueue, WithHeadings, WithMapping, WithTitle, WithStrictNullComparison
2020-01-20 02:05:40 +03:00
{
2021-04-16 00:59:43 +03:00
use Exportable;
2020-01-20 02:05:40 +03:00
public $ids;
public $fields;
2021-04-16 00:59:43 +03:00
public $user;
public function __construct($ids = null)
2020-01-20 02:05:40 +03:00
{
$this->ids = $ids;
$this->fields = $this->fields();
2021-04-16 00:59:43 +03:00
$this->user = user();
2020-01-20 02:05:40 +03:00
}
public function title(): string
{
return Str::snake((new \ReflectionClass($this))->getShortName());
}
public function fields(): array
{
return [];
}
public function map($model): array
{
$map = [];
$date_fields = ['paid_at', 'invoiced_at', 'billed_at', 'due_at', 'issued_at', 'transferred_at'];
2020-01-20 02:05:40 +03:00
2020-04-24 23:28:43 +03:00
$evil_chars = ['=', '+', '-', '@'];
foreach ($this->fields as $field) {
2020-01-20 02:05:40 +03:00
$value = $model->$field;
2023-06-16 17:14:35 +03:00
// created_by is equal to the owner id. Therefore, the value in export is owner email.
if ($field == 'created_by') {
$value = $model->owner->email ?? null;
}
2020-01-20 02:05:40 +03:00
if (in_array($field, $date_fields)) {
$value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d'));
2020-01-20 02:05:40 +03:00
}
2020-04-24 23:28:43 +03:00
// Prevent CSV injection https://security.stackexchange.com/a/190848
if (Str::startsWith($value, $evil_chars)) {
$value = "'" . $value;
}
2020-01-20 02:05:40 +03:00
$map[] = $value;
}
return $map;
}
public function headings(): array
{
event(new HeadingsPreparing($this));
return $this->fields;
}
public function prepareRows($rows)
{
event(new RowsPreparing($this, $rows));
return $rows;
2020-01-20 02:05:40 +03:00
}
2021-04-16 00:59:43 +03:00
public function preferredLocale()
{
return $this->user->locale;
}
public function failed(\Throwable $exception): void
{
2021-06-19 18:16:09 +03:00
$this->user->notify(new ExportFailed($exception->getMessage()));
2021-04-16 00:59:43 +03:00
}
2020-01-20 02:05:40 +03:00
}