Added Echoarea NA file import
This commit is contained in:
parent
85d9dd1545
commit
6b8704b1cf
40
app/Console/Commands/EchoareaImport.php
Normal file
40
app/Console/Commands/EchoareaImport.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Domain;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
use App\Jobs\EchoareaImport as Job;
|
||||
|
||||
class EchoareaImport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'echoarea:import'
|
||||
.' {file : NA File}'
|
||||
.' {domain : Domain}'
|
||||
.' {--P|prefix= : Add prefix to description}'
|
||||
.' {--U|unlink : Delete file after import}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Import Echoarea';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$do = Domain::where('name',strtolower($this->argument('domain')))->singleOrFail();
|
||||
return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix'),$this->option('unlink'));
|
||||
}
|
||||
}
|
98
app/Jobs/EchoareaImport.php
Normal file
98
app/Jobs/EchoareaImport.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Models\{Domain,Echoarea};
|
||||
use App\Traits\Import as ImportTrait;
|
||||
|
||||
class EchoareaImport implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
use ImportTrait;
|
||||
|
||||
protected const LOGKEY = 'JIE';
|
||||
private const importkey = 'echoarea';
|
||||
|
||||
private string $file;
|
||||
private Domain $do;
|
||||
private string $prefix;
|
||||
private bool $delete_file;
|
||||
private bool $delete_recs;
|
||||
|
||||
/**
|
||||
* Import Echoarea constructor.
|
||||
*
|
||||
* @param string $file
|
||||
* @param Domain $do
|
||||
* @param string $prefix
|
||||
* @param bool $delete_recs
|
||||
* @param bool $delete_file
|
||||
*/
|
||||
public function __construct(string $file,Domain $do,string $prefix,bool $delete_recs=FALSE,bool $delete_file=FALSE)
|
||||
{
|
||||
$this->file = $file;
|
||||
$this->do = $do;
|
||||
$this->prefix = $prefix;
|
||||
$this->delete_file = $delete_file;
|
||||
$this->delete_recs = $delete_recs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
// Get the file from the host
|
||||
$file = $this->getFileFromHost('',self::importkey,$this->file);
|
||||
$lines = $this->getFileLines($file);
|
||||
Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines));
|
||||
|
||||
$fh = fopen($file,'r');
|
||||
|
||||
$p = $c = 0;
|
||||
while (! feof($fh)) {
|
||||
$line = stream_get_line($fh, 0, "\n");
|
||||
|
||||
// Remove any embedded CR and BOM
|
||||
$line = str_replace("\r",'',$line);
|
||||
$line = preg_replace('/^\x{feff}/u','',$line);
|
||||
|
||||
if (! $line)
|
||||
continue;
|
||||
|
||||
$c++;
|
||||
|
||||
list($area,$description) = preg_split('/[\s|\t]+/',$line,2);
|
||||
|
||||
if ($this->do->echoareas->pluck('name')->search($area) !== FALSE) {
|
||||
Log::info(sprintf('%s: Area [%s] already exists.',self::LOGKEY,$area));
|
||||
continue;
|
||||
}
|
||||
|
||||
$o = new Echoarea;
|
||||
$o->name = $area;
|
||||
$o->description = ($this->prefix ?: '').$description;
|
||||
$o->active = TRUE;
|
||||
$o->public = TRUE;
|
||||
|
||||
$this->do->echoareas()->save($o);
|
||||
}
|
||||
|
||||
fclose($fh);
|
||||
|
||||
if ($this->delete_file and $c)
|
||||
unlink($file);
|
||||
|
||||
Log::info(sprintf('%s:Updated %d records',self::LOGKEY,$p));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user