Added filearea import

This commit is contained in:
Deon George 2023-09-14 23:42:25 +10:00
parent ec5c28a03e
commit 2f878b6e64
3 changed files with 148 additions and 2 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Domain;
use App\Jobs\FileareaImport as Job;
class FileareaImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'filearea: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 Filearea';
/**
* 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'));
}
}

View File

@ -90,6 +90,7 @@ class EchoareaImport implements ShouldQueue
$o->description = ($this->prefix.' ' ?: '').ucfirst($description);
$o->active = TRUE;
$o->show = TRUE;
$o->security = 9;
$this->do->echoareas()->save($o);
}
@ -97,8 +98,8 @@ class EchoareaImport implements ShouldQueue
fclose($fh);
if ($this->delete_file and $c)
unlink($file);
unlink($this->file);
Log::info(sprintf('%s:Updated %d records',self::LOGKEY,$p));
Log::info(sprintf('%s:= Updated %d records',self::LOGKEY,$p));
}
}

105
app/Jobs/FileareaImport.php Normal file
View File

@ -0,0 +1,105 @@
<?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,Filearea};
use App\Traits\Import as ImportTrait;
class FileareaImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ImportTrait;
protected const LOGKEY = 'JFI';
private const importkey = 'filearea';
private string $file;
private Domain $do;
private string $prefix;
private bool $delete_file;
private bool $delete_recs;
/**
* Import Filearea 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;
}
public function __get($key): mixed
{
switch ($key) {
case 'subject':
return sprintf('%s-%s',$this->do->name,$this->file);
default:
return NULL;
}
}
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle()
{
$fh = fopen($this->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++;
if (str_contains($line,' '))
list($area,$description) = preg_split('/[\s|\t]+/',$line,2);
else {
$area = $line;
$description = '[No Description]';
}
$o = Filearea::whereRaw(sprintf("LOWER(name)='%s'",strtolower($area)))->firstOrNew();
$o->name = strtoupper($area);
$o->description = ($this->prefix.' ' ?: '').ucfirst($description);
$o->active = TRUE;
$o->show = TRUE;
$o->security = 9;
$this->do->fileareas()->save($o);
}
fclose($fh);
if ($this->delete_file and $c)
unlink($this->file);
Log::info(sprintf('%s:= Updated %d records',self::LOGKEY,$p));
}
}