78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Imports\Sheets;
|
|
|
|
use App\Models\BackupImport;
|
|
use App\Models\Holding;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Maatwebsite\Excel\Concerns\WithValidation;
|
|
use Maatwebsite\Excel\Events\BeforeSheet;
|
|
|
|
class ConfigSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
|
|
{
|
|
public function __construct(
|
|
public BackupImport $backupImport
|
|
) {}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
BeforeSheet::class => function (BeforeSheet $event) {
|
|
DB::commit();
|
|
$this->backupImport->update([
|
|
'message' => __('Importing configurations...'),
|
|
]);
|
|
DB::beginTransaction();
|
|
},
|
|
];
|
|
}
|
|
|
|
public function collection(Collection $configs)
|
|
{
|
|
foreach ($configs as $config) {
|
|
|
|
switch ($config['key']) {
|
|
case 'name':
|
|
$this->backupImport->user->setAttribute('name', $config['value']);
|
|
$this->backupImport->user->save();
|
|
break;
|
|
|
|
case 'locale':
|
|
$this->backupImport->user->setOption('locale', $config['value']);
|
|
$this->backupImport->user->save();
|
|
break;
|
|
|
|
case 'display_currency':
|
|
$this->backupImport->user->setOption('display_currency', $config['value']);
|
|
$this->backupImport->user->save();
|
|
break;
|
|
|
|
case 'reinvest_dividends':
|
|
|
|
Holding::myHoldings()->where('id', $config['value'])->update([
|
|
'reinvest_dividends' => true,
|
|
]);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'key' => ['required', 'string'],
|
|
'value' => ['required', 'string'],
|
|
];
|
|
}
|
|
}
|