2017-04-02 15:54:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use App\Services\ImportService;
|
|
|
|
use App\Ninja\Mailers\UserMailer;
|
2017-04-23 19:51:04 +02:00
|
|
|
use App\Models\User;
|
2017-04-02 15:54:07 +02:00
|
|
|
use Auth;
|
2017-04-27 10:23:01 +02:00
|
|
|
use App;
|
2017-06-27 14:53:54 +02:00
|
|
|
use Utils;
|
2017-07-18 11:22:04 +02:00
|
|
|
use Exception;
|
2017-04-02 15:54:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SendInvoiceEmail.
|
|
|
|
*/
|
|
|
|
class ImportData extends Job implements ShouldQueue
|
|
|
|
{
|
|
|
|
use InteractsWithQueue, SerializesModels;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $settings;
|
|
|
|
|
2017-05-04 19:08:27 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
2017-04-02 15:54:07 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param mixed $files
|
|
|
|
* @param mixed $settings
|
|
|
|
*/
|
2017-04-23 19:51:04 +02:00
|
|
|
public function __construct(User $user, $type, $settings)
|
2017-04-02 15:54:07 +02:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->type = $type;
|
|
|
|
$this->settings = $settings;
|
2017-05-04 19:08:27 +02:00
|
|
|
$this->server = config('database.default');
|
2017-04-02 15:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @param ContactMailer $mailer
|
|
|
|
*/
|
|
|
|
public function handle(ImportService $importService, UserMailer $userMailer)
|
|
|
|
{
|
|
|
|
$includeSettings = false;
|
|
|
|
|
2017-04-26 08:16:36 +02:00
|
|
|
if (App::runningInConsole()) {
|
|
|
|
Auth::onceUsingId($this->user->id);
|
|
|
|
$this->user->account->loadLocalizationSettings();
|
|
|
|
}
|
2017-04-02 15:54:07 +02:00
|
|
|
|
2017-06-26 22:44:55 +02:00
|
|
|
try {
|
|
|
|
if ($this->type === IMPORT_JSON) {
|
|
|
|
$includeData = $this->settings['include_data'];
|
|
|
|
$includeSettings = $this->settings['include_settings'];
|
|
|
|
$files = $this->settings['files'];
|
|
|
|
$results = $importService->importJSON($files[IMPORT_JSON], $includeData, $includeSettings);
|
|
|
|
} elseif ($this->type === IMPORT_CSV) {
|
|
|
|
$map = $this->settings['map'];
|
|
|
|
$headers = $this->settings['headers'];
|
|
|
|
$timestamp = $this->settings['timestamp'];
|
|
|
|
$results = $importService->importCSV($map, $headers, $timestamp);
|
|
|
|
} else {
|
|
|
|
$source = $this->settings['source'];
|
|
|
|
$files = $this->settings['files'];
|
|
|
|
$results = $importService->importFiles($source, $files);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject = trans('texts.import_complete');
|
|
|
|
$message = $importService->presentResults($results, $includeSettings);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$subject = trans('texts.import_failed');
|
|
|
|
$message = $exception->getMessage();
|
2017-08-16 09:28:40 +02:00
|
|
|
Utils::logError($subject . ': ' . $message);
|
2017-04-02 15:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$userMailer->sendMessage($this->user, $subject, $message);
|
2017-04-24 21:27:35 +02:00
|
|
|
|
2017-04-26 08:16:36 +02:00
|
|
|
if (App::runningInConsole()) {
|
|
|
|
Auth::logout();
|
|
|
|
}
|
2017-04-02 15:54:07 +02:00
|
|
|
}
|
|
|
|
}
|