Akaunting/app/Abstracts/Export.php

295 lines
8.9 KiB
PHP
Raw Permalink Normal View History

2020-01-19 23:05:40 +00:00
<?php
namespace App\Abstracts;
2024-04-24 15:10:36 +00:00
use App\Abstracts\Http\FormRequest;
use App\Events\Export\HeadingsPreparing;
use App\Events\Export\RowsPreparing;
2021-04-15 21:59:43 +00:00
use App\Notifications\Common\ExportFailed;
2020-03-25 17:21:42 +00:00
use App\Utilities\Date;
2021-04-15 21:59:43 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
2020-01-19 23:05:40 +00:00
use Illuminate\Support\Str;
2021-04-15 21:59:43 +00:00
use Maatwebsite\Excel\Concerns\Exportable;
2020-01-19 23:05:40 +00:00
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
2024-04-24 15:10:36 +00:00
use Maatwebsite\Excel\Concerns\WithEvents;
2020-01-19 23:05:40 +00:00
use Maatwebsite\Excel\Concerns\WithTitle;
2023-06-19 14:17:26 +00:00
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
2024-04-24 15:10:36 +00:00
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeSheet;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
2024-04-24 15:10:36 +00:00
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
2020-01-19 23:05:40 +00:00
2024-04-24 15:10:36 +00:00
abstract class Export implements FromCollection, HasLocalePreference, ShouldAutoSize, ShouldQueue, WithHeadings, WithMapping, WithTitle, WithStrictNullComparison, WithEvents
2020-01-19 23:05:40 +00:00
{
2021-04-15 21:59:43 +00:00
use Exportable;
2020-01-19 23:05:40 +00:00
public $ids;
public $fields;
2021-04-15 21:59:43 +00:00
public $user;
2024-04-24 15:10:36 +00:00
public $request_class = null;
2024-04-25 09:18:25 +00:00
public $column_count; //number of columns to be auto sized
2024-04-24 15:10:36 +00:00
public $column_validations; //selects should have column_name and options
2024-04-25 09:18:25 +00:00
public $row_count; //number of rows that will have the dropdown
public function __construct($ids = null)
2020-01-19 23:05:40 +00:00
{
$this->ids = $ids;
$this->fields = $this->fields();
2024-04-24 15:10:36 +00:00
$this->column_validations = $this->columnValidations();
2024-04-25 09:18:25 +00:00
$this->column_count = config('excel.exports.column_count');
$this->row_count = config('excel.exports.row_count');
2021-04-15 21:59:43 +00:00
$this->user = user();
2020-01-19 23:05:40 +00: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-19 23:05:40 +00:00
2020-04-24 20:28:43 +00:00
$evil_chars = ['=', '+', '-', '@'];
foreach ($this->fields as $field) {
2020-01-19 23:05:40 +00:00
$value = $model->$field;
2023-06-16 14:14:35 +00: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-19 23:05:40 +00:00
if (in_array($field, $date_fields)) {
$value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d'));
2020-01-19 23:05:40 +00:00
}
2020-04-24 20:28:43 +00:00
// Prevent CSV injection https://security.stackexchange.com/a/190848
if (Str::startsWith($value, $evil_chars)) {
$value = "'" . $value;
}
2020-01-19 23:05:40 +00: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-19 23:05:40 +00:00
}
2021-04-15 21:59:43 +00:00
public function preferredLocale()
{
if (! $this->user) {
return setting('default.locale');
}
2021-04-15 21:59:43 +00:00
return $this->user->locale;
}
public function failed(\Throwable $exception): void
{
if (! $this->user) {
return;
}
2021-06-19 15:16:09 +00:00
$this->user->notify(new ExportFailed($exception->getMessage()));
2021-04-15 21:59:43 +00:00
}
2024-04-24 15:10:36 +00:00
public function columnValidations(): array
{
return [];
}
public function afterSheet($event)
{
$condition = class_exists($this->request_class)
? ! ($request = new $this->request_class) instanceof FormRequest
: true;
if (empty($this->column_validations) && $condition) {
return [];
}
$alphas = range('A', 'Z');
foreach ($this->fields as $key => $value) {
$drop_column = $alphas[$key];
if ($this->setColumnValidations($drop_column, $event, $value)) {
continue;
};
$this->validationWarning($drop_column, $event, $value, $request);
}
}
public function setColumnValidations($drop_column, $event, $value)
{
if (! isset($this->column_validations[$value])) {
return false;
}
$column_validation = $this->column_validations[$value];
// set dropdown list for first data row
$validation = $event->sheet->getCell("{$drop_column}2")->getDataValidation();
$validation->setType($column_validation['type'] ?? DataValidation::TYPE_LIST);
if (empty($column_validation['hide_prompt'])) {
$validation->setAllowBlank($column_validation['allow_blank'] ?? false);
$validation->setShowInputMessage($column_validation['show_input_message'] ?? true);
$validation->setPromptTitle($column_validation['prompt_title'] ?? null);
$validation->setPrompt($column_validation['prompt'] ?? null);
}
if (empty($column_validation['hide_error'])) {
$validation->setErrorStyle($column_validation['error_style'] ?? DataValidation::STYLE_INFORMATION);
$validation->setShowErrorMessage($column_validation['show_error_message'] ?? true);
$validation->setErrorTitle($column_validation['error_title'] ?? null);
$validation->setError($column_validation['error'] ?? null);
}
if (! empty($column_validation['options'])) {
$validation->setFormula1(sprintf('"%s"', implode(',', $column_validation['options'])));
$validation->setShowDropDown($column_validation['show_dropdown'] ?? true);
}
// clone validation to remaining rows
for ($i = 3; $i <= $this->row_count; $i++) {
$event->sheet->getCell("{$drop_column}{$i}")->setDataValidation(clone $validation);
}
2024-04-24 15:10:36 +00:00
// set columns to autosize
for ($i = 1; $i <= $this->column_count; $i++) {
$column = Coordinate::stringFromColumnIndex($i);
$event->sheet->getColumnDimension($column)->setAutoSize(true);
}
return true;
}
public function validationWarning($drop_column, $event, $value, $request)
{
$rules = $this->prepareRules($request->rules());
if (! isset($rules[$value])) {
return false;
}
$rule = explode('|', $rules[$value]);
$prompt = '';
foreach ($rule as $r) {
if (strpos($r, 'unique') !== false) {
$r = 'unique';
}
if (strpos($r, 'amount') !== false) {
2024-04-25 09:20:44 +00:00
$r = 'double';
2024-04-24 15:10:36 +00:00
}
if (strpos($r, 'date_format') !== false) {
$prompt = $prompt . trans('validation.date_format', [
'attribute' => $value,
2024-04-24 15:10:36 +00:00
'format' => str_replace('date_format:', '', $r)
]) . ' ';
}
if (strpos($r, 'required_without') !== false) {
$prompt = $prompt . trans('validation.required_without', [
'attribute' => $value,
2024-04-24 15:10:36 +00:00
'values' => str_replace('required_without:', '', $r)
]) . ' ';
}
2024-04-25 09:20:44 +00:00
if (in_array($r, ['required', 'email', 'integer', 'unique', 'date_format', 'double'])) {
2024-04-24 15:10:36 +00:00
$prompt = $prompt . trans('validation.' . $r, ['attribute' => $value]) . ' ';
}
}
$validation = $event->sheet->getCell("{$drop_column}2")->getDataValidation();
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setPromptTitle(trans('general.validation_warning'));
$validation->setPrompt($prompt ?? null);
2024-04-24 15:10:36 +00:00
for ($i = 3; $i <= $this->row_count; $i++) {
$event->sheet->getCell("{$drop_column}{$i}")->setDataValidation(clone $validation);
}
for ($i = 1; $i <= $this->column_count; $i++) {
$column = Coordinate::stringFromColumnIndex($i);
$event->sheet->getColumnDimension($column)->setAutoSize(true);
}
}
public function getDropdownOptions($model, $select): array
{
$limit = 253;
$selects = [];
$totalLength = 0;
$model::select($select)->each(function ($row) use (&$selects, &$totalLength, $limit, $select) {
$nameLength = mb_strlen($row->$select);
2024-04-24 15:10:36 +00:00
if ($totalLength + $nameLength <= $limit && $nameLength !== 0) {
$selects[] = $row->$select;
$totalLength += $nameLength;
}
});
return $selects;
}
/**
* You can override this method to add custom rules for each row.
*/
public function prepareRules(array $rules): array
{
return $rules;
}
public function registerEvents(): array
{
return [
2024-04-24 15:10:36 +00:00
AfterSheet::class => function(AfterSheet $event) {
$this->afterSheet($event);
},
];
}
2020-01-19 23:05:40 +00:00
}