From 2f878b6e647be20f6fb3c7bb75111a9edd4d8d12 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 14 Sep 2023 23:42:25 +1000 Subject: [PATCH] Added filearea import --- app/Console/Commands/FileareaImport.php | 40 +++++++++ app/Jobs/EchoareaImport.php | 5 +- app/Jobs/FileareaImport.php | 105 ++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/FileareaImport.php create mode 100644 app/Jobs/FileareaImport.php diff --git a/app/Console/Commands/FileareaImport.php b/app/Console/Commands/FileareaImport.php new file mode 100644 index 0000000..bfeb07a --- /dev/null +++ b/app/Console/Commands/FileareaImport.php @@ -0,0 +1,40 @@ +argument('domain')))->singleOrFail(); + return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix'),$this->option('unlink')); + } +} \ No newline at end of file diff --git a/app/Jobs/EchoareaImport.php b/app/Jobs/EchoareaImport.php index 2dea0ea..5fc282c 100644 --- a/app/Jobs/EchoareaImport.php +++ b/app/Jobs/EchoareaImport.php @@ -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)); } } diff --git a/app/Jobs/FileareaImport.php b/app/Jobs/FileareaImport.php new file mode 100644 index 0000000..65fce5f --- /dev/null +++ b/app/Jobs/FileareaImport.php @@ -0,0 +1,105 @@ +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)); + } +}