2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace Modules\Importer\Http\Controllers;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
use App\Contracts\Controller;
|
2019-12-13 04:07:35 +08:00
|
|
|
use App\Services\Installer\DatabaseService;
|
|
|
|
use App\Services\Installer\InstallerService;
|
|
|
|
use App\Support\Utils;
|
2019-11-27 22:19:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Services\ImporterService;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
class ImporterController extends Controller
|
|
|
|
{
|
2019-12-13 04:07:35 +08:00
|
|
|
private $dbSvc;
|
2019-11-27 22:19:20 +08:00
|
|
|
private $importerSvc;
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
public function __construct(DatabaseService $dbSvc, ImporterService $importerSvc)
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
2019-12-13 04:07:35 +08:00
|
|
|
$this->dbSvc = $dbSvc;
|
2019-11-27 22:19:20 +08:00
|
|
|
$this->importerSvc = $importerSvc;
|
2019-12-12 01:57:18 +08:00
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
Utils::disableDebugToolbar();
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the main page for the importer; show form for the admin email
|
|
|
|
* and the credentials for the other database
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2019-12-13 04:07:35 +08:00
|
|
|
* @return mixed
|
2019-11-27 22:19:20 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2019-12-13 04:07:35 +08:00
|
|
|
return view('importer::step1-configure');
|
|
|
|
}
|
|
|
|
|
2020-02-29 07:56:01 +08:00
|
|
|
protected function testDb(Request $request)
|
|
|
|
{
|
|
|
|
$this->dbSvc->checkDbConnection(
|
|
|
|
$request->post('db_conn'),
|
|
|
|
$request->post('db_host'),
|
|
|
|
$request->post('db_port'),
|
|
|
|
$request->post('db_name'),
|
|
|
|
$request->post('db_user'),
|
|
|
|
$request->post('db_pass')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
/**
|
|
|
|
* Check the database connection
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function dbtest(Request $request)
|
|
|
|
{
|
|
|
|
$status = 'success'; // success|warn|danger
|
|
|
|
$message = 'Database connection looks good!';
|
|
|
|
|
2019-12-12 04:12:31 +08:00
|
|
|
try {
|
2020-02-29 07:56:01 +08:00
|
|
|
$this->testDb($request);
|
2019-12-13 04:07:35 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status = 'danger';
|
|
|
|
$message = 'Failed! '.$e->getMessage();
|
2019-12-12 04:12:31 +08:00
|
|
|
}
|
2019-12-02 22:57:35 +08:00
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
return view('importer::dbtest', [
|
|
|
|
'status' => $status,
|
|
|
|
'message' => $message,
|
|
|
|
]);
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post from the above
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2019-12-13 04:07:35 +08:00
|
|
|
* @return mixed
|
2019-11-27 22:19:20 +08:00
|
|
|
*/
|
|
|
|
public function config(Request $request)
|
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
try {
|
2020-02-29 07:56:01 +08:00
|
|
|
$this->testDb($request);
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
// Save the credentials to use later
|
|
|
|
$this->importerSvc->saveCredentialsFromRequest($request);
|
|
|
|
|
|
|
|
// Generate the import manifest
|
|
|
|
$manifest = $this->importerSvc->generateImportManifest();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e->getMessage());
|
2019-12-13 04:07:35 +08:00
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
// Send it to run, step1
|
2019-12-13 04:07:35 +08:00
|
|
|
return view('importer::error', [
|
2019-12-02 22:57:35 +08:00
|
|
|
'error' => $e->getMessage(),
|
|
|
|
]);
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
// Send it to run, step1
|
2019-12-13 04:07:35 +08:00
|
|
|
return view('importer::step2-processing', [
|
2019-12-02 22:57:35 +08:00
|
|
|
'manifest' => $manifest,
|
|
|
|
]);
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the importer. Pass in query string with a few different parameters:
|
|
|
|
*
|
|
|
|
* stage=STAGE NAME
|
|
|
|
* start=record_start
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2019-12-02 22:57:35 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*
|
2019-12-13 04:07:35 +08:00
|
|
|
* @return mixed
|
2019-11-27 22:19:20 +08:00
|
|
|
*/
|
|
|
|
public function run(Request $request)
|
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
$importer = $request->input('importer');
|
|
|
|
$start = $request->input('start');
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
Log::info('Starting stage '.$importer.' from offset '.$start);
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
$this->importerSvc->run($importer, $start);
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
return response()->json([
|
|
|
|
'message' => 'completed',
|
|
|
|
]);
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
/**
|
|
|
|
* Complete the import
|
|
|
|
*/
|
|
|
|
public function complete()
|
|
|
|
{
|
2019-12-13 04:07:35 +08:00
|
|
|
$installerSvc = app(InstallerService::class);
|
|
|
|
$installerSvc->disableInstallerModules();
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
return redirect('/');
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
}
|