Merge pull request #1913 from SevanNerse/export-events

headings and data of exports converted to event-driven
This commit is contained in:
Denis Duliçi 2021-03-06 16:57:33 +03:00 committed by GitHub
commit c05274221e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 2 deletions

View File

@ -2,6 +2,8 @@
namespace App\Abstracts; namespace App\Abstracts;
use App\Events\Export\HeadingsPreparing;
use App\Events\Export\RowsPreparing;
use App\Utilities\Date; use App\Utilities\Date;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\FromCollection;
@ -15,9 +17,12 @@ abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, W
{ {
public $ids; public $ids;
public $fields;
public function __construct($ids = null) public function __construct($ids = null)
{ {
$this->ids = $ids; $this->ids = $ids;
$this->fields = $this->fields();
} }
public function title(): string public function title(): string
@ -38,7 +43,7 @@ abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, W
$evil_chars = ['=', '+', '-', '@']; $evil_chars = ['=', '+', '-', '@'];
foreach ($this->fields() as $field) { foreach ($this->fields as $field) {
$value = $model->$field; $value = $model->$field;
if (in_array($field, $date_fields)) { if (in_array($field, $date_fields)) {
@ -58,6 +63,15 @@ abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, W
public function headings(): array public function headings(): array
{ {
return $this->fields(); event(new HeadingsPreparing($this));
return $this->fields;
}
public function prepareRows($rows)
{
event(new RowsPreparing($this, $rows));
return $rows;
} }
} }

View File

@ -0,0 +1,20 @@
<?php
namespace App\Events\Export;
use App\Abstracts\Event;
class HeadingsPreparing extends Event
{
public $class;
/**
* Create a new event instance.
*
* @param $class
*/
public function __construct($class)
{
$this->class = $class;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Events\Export;
use App\Abstracts\Event;
class RowsPreparing extends Event
{
public $class;
public $rows;
/**
* Create a new event instance.
*
* @param $class
* @param $rows
*/
public function __construct($class, $rows)
{
$this->class = $class;
$this->rows = $rows;
}
}