ultimatepos/Modules/IntegrationHub/Http/Controllers/InstallController.php

86 lines
2.8 KiB
PHP

<?php
namespace Modules\IntegrationHub\Http\Controllers;
use App\System;
use Composer\Semver\Comparator;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
class InstallController extends Controller
{
protected $module_name = 'integrationhub';
protected $appVersion;
public function __construct()
{
$this->appVersion = config('integrationhub.module_version', '1.0');
}
public function index()
{
if (! auth()->user()->can('superadmin')) {
abort(403, 'Unauthorized action.');
}
if (! empty(System::getProperty($this->module_name.'_version'))) {
abort(404);
}
return view('install.install-module')->with([
'action_url' => action([self::class, 'install']),
'intruction_type' => 'uf',
]);
}
public function install()
{
try {
DB::beginTransaction();
request()->validate(['license_code' => 'required', 'login_username' => 'required']);
DB::statement('SET default_storage_engine=INNODB;');
Artisan::call('module:migrate', ['module' => 'IntegrationHub', '--force' => true]);
System::addProperty($this->module_name.'_version', $this->appVersion);
DB::commit();
$output = ['success' => 1, 'msg' => __('integrationhub::lang.module_installed')];
} catch (\Exception $e) {
DB::rollBack();
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
$output = ['success' => false, 'msg' => $e->getMessage()];
}
return redirect()->action([\App\Http\Controllers\Install\ModulesController::class, 'index'])->with('status', $output);
}
public function uninstall()
{
if (! auth()->user()->can('superadmin')) {
abort(403, 'Unauthorized action.');
}
System::removeProperty($this->module_name.'_version');
return redirect()->back()->with(['status' => ['success' => true, 'msg' => __('integrationhub::lang.success')]]);
}
public function update()
{
if (! auth()->user()->can('superadmin')) {
abort(403, 'Unauthorized action.');
}
$current = System::getProperty($this->module_name.'_version');
if (! Comparator::greaterThan($this->appVersion, (string) $current)) {
abort(404);
}
DB::statement('SET default_storage_engine=INNODB;');
Artisan::call('module:migrate', ['module' => 'IntegrationHub', '--force' => true]);
System::setProperty($this->module_name.'_version', $this->appVersion);
return redirect()->back()->with(['status' => ['success' => 1, 'msg' => __('integrationhub::lang.module_updated', ['version' => $this->appVersion])]]);
}
}