Installing and managing modules from admin panel (#847)
This commit is contained in:
146
app/Services/ImporterService.php
Normal file
146
app/Services/ImporterService.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Repositories\KvpRepository;
|
||||
use App\Services\Importers\AircraftImporter;
|
||||
use App\Services\Importers\AirlineImporter;
|
||||
use App\Services\Importers\AirportImporter;
|
||||
use App\Services\Importers\ClearDatabase;
|
||||
use App\Services\Importers\ExpenseImporter;
|
||||
use App\Services\Importers\FinalizeImporter;
|
||||
use App\Services\Importers\FlightImporter;
|
||||
use App\Services\Importers\GroupImporter;
|
||||
use App\Services\Importers\LedgerImporter;
|
||||
use App\Services\Importers\PirepImporter;
|
||||
use App\Services\Importers\RankImport;
|
||||
use App\Services\Importers\SettingsImporter;
|
||||
use App\Services\Importers\UserImport;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ImporterService extends Service
|
||||
{
|
||||
private $CREDENTIALS_KEY = 'legacy.importer.db';
|
||||
|
||||
/**
|
||||
* @var KvpRepository
|
||||
*/
|
||||
private $kvpRepo;
|
||||
|
||||
/**
|
||||
* The list of importers, in proper order
|
||||
*/
|
||||
private $importList = [
|
||||
ClearDatabase::class,
|
||||
RankImport::class,
|
||||
GroupImporter::class,
|
||||
AirlineImporter::class,
|
||||
AircraftImporter::class,
|
||||
AirportImporter::class,
|
||||
FlightImporter::class,
|
||||
UserImport::class,
|
||||
PirepImporter::class,
|
||||
ExpenseImporter::class,
|
||||
LedgerImporter::class,
|
||||
SettingsImporter::class,
|
||||
FinalizeImporter::class,
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->kvpRepo = app(KvpRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the credentials from a request
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function saveCredentialsFromRequest(Request $request)
|
||||
{
|
||||
$creds = [
|
||||
'host' => $request->post('db_host'),
|
||||
'port' => $request->post('db_port'),
|
||||
'name' => $request->post('db_name'),
|
||||
'user' => $request->post('db_user'),
|
||||
'pass' => $request->post('db_pass'),
|
||||
'table_prefix' => $request->post('db_prefix'),
|
||||
];
|
||||
|
||||
$this->saveCredentials($creds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the given credentials
|
||||
*
|
||||
* @param array $creds
|
||||
*/
|
||||
public function saveCredentials(array $creds)
|
||||
{
|
||||
$creds = array_merge([
|
||||
'admin_email' => '',
|
||||
'host' => '',
|
||||
'port' => '',
|
||||
'name' => '',
|
||||
'user' => '',
|
||||
'pass' => 3306,
|
||||
'table_prefix' => 'phpvms_',
|
||||
], $creds);
|
||||
|
||||
$this->kvpRepo->save($this->CREDENTIALS_KEY, $creds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the saved credentials
|
||||
*/
|
||||
public function getCredentials()
|
||||
{
|
||||
return $this->kvpRepo->get($this->CREDENTIALS_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a manifest of the import. Creates an array with the importer name,
|
||||
* which then has a subarray of all of the different steps/stages it needs to run
|
||||
*/
|
||||
public function generateImportManifest()
|
||||
{
|
||||
$manifest = [];
|
||||
|
||||
foreach ($this->importList as $importerKlass) {
|
||||
/** @var BaseImporter $importer */
|
||||
$importer = new $importerKlass();
|
||||
$manifest = array_merge($manifest, $importer->getManifest());
|
||||
}
|
||||
|
||||
return $manifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a given stage
|
||||
*
|
||||
* @param $importer
|
||||
* @param int $start
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return int|void
|
||||
*/
|
||||
public function run($importer, $start = 0)
|
||||
{
|
||||
if (!in_array($importer, $this->importList, true)) {
|
||||
throw new Exception('Unknown importer "'.$importer.'"');
|
||||
}
|
||||
|
||||
/** @var $importerInst BaseImporter */
|
||||
$importerInst = new $importer();
|
||||
|
||||
try {
|
||||
$importerInst->run($start);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error running importer: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
97
app/Services/Importers/AircraftImporter.php
Normal file
97
app/Services/Importers/AircraftImporter.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Subfleet;
|
||||
|
||||
class AircraftImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'aircraft';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRCRAFT IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'id',
|
||||
'icao',
|
||||
'name',
|
||||
'fullname',
|
||||
'registration',
|
||||
'enabled',
|
||||
];
|
||||
|
||||
// See if there is an airline column
|
||||
$columns = $this->db->getColumns($this->table);
|
||||
if (in_array('airline', $columns, true)) {
|
||||
$fields[] = 'airline';
|
||||
}
|
||||
|
||||
if (in_array('location', $columns, true)) {
|
||||
$fields[] = 'location';
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
||||
foreach ($rows as $row) {
|
||||
$subfleet_name = $row->icao;
|
||||
|
||||
$airline_id = null;
|
||||
if (!empty($row->airline)) {
|
||||
$subfleet_name = $row->airline.' - '.$row->icao;
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->airline);
|
||||
}
|
||||
|
||||
$subfleet = $this->getSubfleet($subfleet_name, $row->icao, $airline_id);
|
||||
|
||||
$where = [
|
||||
'registration' => $row->registration,
|
||||
];
|
||||
|
||||
$cols = [
|
||||
'icao' => $row->icao,
|
||||
'name' => $row->fullname,
|
||||
'subfleet_id' => $subfleet->id,
|
||||
'active' => $row->enabled,
|
||||
];
|
||||
|
||||
if (!empty($row->location)) {
|
||||
$cols['airport_id'] = $row->location;
|
||||
}
|
||||
|
||||
$aircraft = Aircraft::firstOrCreate($where, $cols);
|
||||
|
||||
$this->idMapper->addMapping('aircraft', $row->id, $aircraft->id);
|
||||
|
||||
if ($aircraft->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' aircraft');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subfleet
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $icao ICAO of the subfleet
|
||||
* @param int $airline_id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getSubfleet($name, $icao, $airline_id = null)
|
||||
{
|
||||
if (empty($airline_id)) {
|
||||
$airline = Airline::first();
|
||||
$airline_id = $airline->id;
|
||||
}
|
||||
|
||||
return Subfleet::firstOrCreate([
|
||||
'airline_id' => $airline_id,
|
||||
'name' => $name,
|
||||
], ['type' => $icao]);
|
||||
}
|
||||
}
|
||||
45
app/Services/Importers/AirlineImporter.php
Normal file
45
app/Services/Importers/AirlineImporter.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Airline;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AirlineImporter extends BaseImporter
|
||||
{
|
||||
public $table = 'airlines';
|
||||
|
||||
/**
|
||||
* @param int $start
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRLINE IMPORT ---');
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$attrs = [
|
||||
'iata' => $row->code,
|
||||
'name' => $row->name,
|
||||
'active' => $row->enabled,
|
||||
];
|
||||
|
||||
$w = ['icao' => $row->code];
|
||||
|
||||
//$airline = Airline::firstOrCreate($w, $attrs);
|
||||
$airline = Airline::create(array_merge($w, $attrs));
|
||||
|
||||
$this->idMapper->addMapping('airlines', $row->id, $airline->id);
|
||||
$this->idMapper->addMapping('airlines', $row->code, $airline->id);
|
||||
|
||||
Log::debug('Mapping '.$row->id.'/'.$row->code.' to ID '.$airline->id);
|
||||
|
||||
if ($airline->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' airlines');
|
||||
}
|
||||
}
|
||||
63
app/Services/Importers/AirportImporter.php
Normal file
63
app/Services/Importers/AirportImporter.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Airport;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AirportImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'airports';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRPORT IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'icao',
|
||||
'name',
|
||||
'country',
|
||||
'lat',
|
||||
'lng',
|
||||
'hub',
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
||||
foreach ($rows as $row) {
|
||||
$attrs = [
|
||||
'id' => trim($row->icao),
|
||||
'icao' => trim($row->icao),
|
||||
'name' => $row->name,
|
||||
'country' => $row->country,
|
||||
'lat' => $row->lat,
|
||||
'lon' => $row->lng,
|
||||
'hub' => $row->hub,
|
||||
];
|
||||
|
||||
$w = ['id' => $attrs['id']];
|
||||
//$airport = Airport::updateOrCreate($w, $attrs);
|
||||
|
||||
try {
|
||||
$airport = Airport::create(array_merge($w, $attrs));
|
||||
} catch (QueryException $e) {
|
||||
$sqlState = $e->errorInfo[0];
|
||||
$errorCode = $e->errorInfo[1];
|
||||
if ($sqlState === '23000' && $errorCode === 1062) {
|
||||
Log::info('Found duplicate for '.$row->icao.', ignoring');
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($airport->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' airports');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
150
app/Services/Importers/BaseImporter.php
Normal file
150
app/Services/Importers/BaseImporter.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Services\ImporterService;
|
||||
use App\Services\Installer\LoggerTrait;
|
||||
use App\Utils\IdMapper;
|
||||
use App\Utils\ImporterDB;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
abstract class BaseImporter
|
||||
{
|
||||
use LoggerTrait;
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
/**
|
||||
* Holds the connection to the legacy database
|
||||
*
|
||||
* @var ImporterDB
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* The mapper class used for old IDs to new IDs
|
||||
*
|
||||
* @var IdMapper
|
||||
*/
|
||||
protected $idMapper;
|
||||
|
||||
/**
|
||||
* The legacy table this importer targets
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The column used for the ID, used for the ORDER BY
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $idField = 'id';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$importerService = app(ImporterService::class);
|
||||
$this->db = new ImporterDB($importerService->getCredentials());
|
||||
$this->idMapper = app(IdMapper::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* The start method. Takes the offset to start from
|
||||
*
|
||||
* @param int $start
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function run($start = 0);
|
||||
|
||||
/**
|
||||
* Return a manifest of the import tasks to run. Returns an array of objects,
|
||||
* which contain a start and end row
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getManifest(): array
|
||||
{
|
||||
$manifest = [];
|
||||
|
||||
// Ensure that the table exists; if it doesn't skip it from the manifest
|
||||
if (!$this->db->tableExists($this->table)) {
|
||||
Log::info('Table '.$this->table.' doesn\'t exist');
|
||||
return [];
|
||||
}
|
||||
|
||||
$start = 0;
|
||||
$total_rows = $this->db->getTotalRows($this->table);
|
||||
Log::info('Found '.$total_rows.' rows for '.$this->table);
|
||||
|
||||
do {
|
||||
$end = $start + $this->db->batchSize;
|
||||
if ($end > $total_rows) {
|
||||
$end = $total_rows;
|
||||
}
|
||||
|
||||
$idx = $start + 1;
|
||||
|
||||
$manifest[] = [
|
||||
'importer' => static::class,
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
'message' => 'Importing '.$this->table.' ('.$idx.' - '.$end.' of '.$total_rows.')',
|
||||
];
|
||||
|
||||
$start += $this->db->batchSize;
|
||||
} while ($start < $total_rows);
|
||||
|
||||
return $manifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine what columns exist, can be used for feature testing between v2/v5
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseDate($date)
|
||||
{
|
||||
return Carbon::parse($date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a decimal duration and convert it to minutes
|
||||
*
|
||||
* @param $duration
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
protected function convertDuration($duration)
|
||||
{
|
||||
if (strpos($duration, '.') !== false) {
|
||||
$delim = '.';
|
||||
} elseif (strpos($duration, ':')) {
|
||||
$delim = ':';
|
||||
} else {
|
||||
// no delimiter, assume it's just a straight hour
|
||||
return (int) $duration * 60;
|
||||
}
|
||||
|
||||
$hm = explode($delim, $duration);
|
||||
$hours = (int) $hm[0] * 60;
|
||||
$mins = (int) $hm[1];
|
||||
|
||||
return $hours + $mins;
|
||||
}
|
||||
}
|
||||
94
app/Services/Importers/ClearDatabase.php
Normal file
94
app/Services/Importers/ClearDatabase.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Acars;
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Airport;
|
||||
use App\Models\Bid;
|
||||
use App\Models\Expense;
|
||||
use App\Models\File;
|
||||
use App\Models\Flight;
|
||||
use App\Models\FlightField;
|
||||
use App\Models\FlightFieldValue;
|
||||
use App\Models\Journal;
|
||||
use App\Models\JournalTransaction;
|
||||
use App\Models\Ledger;
|
||||
use App\Models\News;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\Subfleet;
|
||||
use App\Models\User;
|
||||
use App\Models\UserAward;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClearDatabase extends BaseImporter
|
||||
{
|
||||
/**
|
||||
* Returns a default manifest just so this step gets run
|
||||
*/
|
||||
public function getManifest(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'importer' => static::class,
|
||||
'start' => 0,
|
||||
'end' => 1,
|
||||
'message' => 'Clearing database',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->cleanupDb();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup the local database of any users and other data that might conflict
|
||||
* before running the importer
|
||||
*/
|
||||
protected function cleanupDb()
|
||||
{
|
||||
$this->info('Running database cleanup/empty before starting');
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=0');
|
||||
|
||||
Bid::truncate();
|
||||
File::truncate();
|
||||
News::truncate();
|
||||
|
||||
Expense::truncate();
|
||||
JournalTransaction::truncate();
|
||||
Journal::truncate();
|
||||
Ledger::truncate();
|
||||
|
||||
// Clear flights
|
||||
DB::table('flight_fare')->truncate();
|
||||
DB::table('flight_subfleet')->truncate();
|
||||
FlightField::truncate();
|
||||
FlightFieldValue::truncate();
|
||||
Flight::truncate();
|
||||
Subfleet::truncate();
|
||||
Aircraft::truncate();
|
||||
|
||||
Airline::truncate();
|
||||
Airport::truncate();
|
||||
Acars::truncate();
|
||||
Pirep::truncate();
|
||||
|
||||
UserAward::truncate();
|
||||
User::truncate();
|
||||
|
||||
// Clear permissions
|
||||
DB::table('permission_role')->truncate();
|
||||
DB::table('permission_user')->truncate();
|
||||
DB::table('role_user')->truncate();
|
||||
|
||||
// Role::truncate();
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=1');
|
||||
|
||||
$this->idMapper->clear();
|
||||
}
|
||||
}
|
||||
47
app/Services/Importers/ExpenseImporter.php
Normal file
47
app/Services/Importers/ExpenseImporter.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Enums\ExpenseType;
|
||||
use App\Models\Expense;
|
||||
|
||||
class ExpenseImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'expenses';
|
||||
|
||||
private $expense_types = [
|
||||
'M' => ExpenseType::MONTHLY,
|
||||
'F' => ExpenseType::FLIGHT,
|
||||
'P' => ExpenseType::MONTHLY, // percent, monthly
|
||||
'G' => ExpenseType::FLIGHT, // percent, per-flight
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- EXPENSES IMPORT ---');
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$attrs = [
|
||||
'airline_id' => null,
|
||||
'name' => $row->name,
|
||||
'amount' => $row->amount,
|
||||
'type' => $this->expense_types[$row->type],
|
||||
'active' => 1,
|
||||
'ref_model' => Expense::class,
|
||||
];
|
||||
|
||||
$expense = Expense::updateOrCreate(['name' => $row->name], $attrs);
|
||||
$this->idMapper->addMapping('expenses', $row->id, $expense->id);
|
||||
$this->idMapper->addMapping('expenses', $row->name, $expense->id);
|
||||
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' expenses');
|
||||
}
|
||||
}
|
||||
50
app/Services/Importers/ExpenseLogImporter.php
Normal file
50
app/Services/Importers/ExpenseLogImporter.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Airline;
|
||||
use App\Models\Expense;
|
||||
use App\Services\FinanceService;
|
||||
use App\Support\Money;
|
||||
use Prettus\Validator\Exceptions\ValidatorException;
|
||||
|
||||
class ExpenseLogImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'expenselog';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- EXPENSE LOG IMPORT ---');
|
||||
|
||||
/** @var FinanceService $financeSvc */
|
||||
$financeSvc = app(FinanceService::class);
|
||||
$airline = Airline::first();
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$expense_id = $this->idMapper->getMapping('expenses', $row->name);
|
||||
$expense = Expense::find($expense_id);
|
||||
|
||||
$debit = Money::createFromAmount($expense->amount);
|
||||
|
||||
$financeSvc->debitFromJournal(
|
||||
$airline->journal,
|
||||
$debit,
|
||||
$airline,
|
||||
'Expense: '.$expense->name,
|
||||
$expense->transaction_group ?? 'Expenses',
|
||||
'expense'
|
||||
);
|
||||
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' expense logs');
|
||||
}
|
||||
}
|
||||
82
app/Services/Importers/FinalizeImporter.php
Normal file
82
app/Services/Importers/FinalizeImporter.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\AircraftService;
|
||||
use App\Services\UserService;
|
||||
|
||||
class FinalizeImporter extends BaseImporter
|
||||
{
|
||||
/**
|
||||
* Returns a default manifest just so this step gets run
|
||||
*/
|
||||
public function getManifest(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'importer' => static::class,
|
||||
'start' => 0,
|
||||
'end' => 1,
|
||||
'message' => 'Finalizing import',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The start method. Takes the offset to start from
|
||||
*
|
||||
* @param int $start
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->findLastPireps();
|
||||
$this->recalculateUserStats();
|
||||
$this->clearValueStore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Go through and set the last PIREP ID for the users
|
||||
*/
|
||||
protected function findLastPireps()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate all of the user stats
|
||||
*/
|
||||
protected function recalculateUserStats()
|
||||
{
|
||||
$this->comment('--- RECALCULATING USER STATS ---');
|
||||
|
||||
/** @var UserService $userSvc */
|
||||
$userSvc = app(UserService::class);
|
||||
|
||||
User::all()->each(function ($user) use ($userSvc) {
|
||||
$userSvc->recalculateStats($user);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the aircraft stats with the newest/latest PIREPs
|
||||
*/
|
||||
protected function recalculateAircraftStats()
|
||||
{
|
||||
$this->comment('--- RECALCULATING AIRCRAFT STATS ---');
|
||||
|
||||
/** @var AircraftService $aircraftSvc */
|
||||
$aircraftSvc = app(AircraftService::class);
|
||||
$aircraftSvc->recalculateStats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the value store of any old value mappings
|
||||
*/
|
||||
protected function clearValueStore()
|
||||
{
|
||||
$this->idMapper->clear();
|
||||
}
|
||||
}
|
||||
70
app/Services/Importers/FlightImporter.php
Normal file
70
app/Services/Importers/FlightImporter.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Flight;
|
||||
|
||||
class FlightImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'schedules';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- FLIGHT SCHEDULE IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'id',
|
||||
'code',
|
||||
'flightnum',
|
||||
'depicao',
|
||||
'arricao',
|
||||
'route',
|
||||
'distance',
|
||||
'flightlevel',
|
||||
'deptime',
|
||||
'arrtime',
|
||||
'flighttime',
|
||||
'notes',
|
||||
'enabled',
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
||||
foreach ($rows as $row) {
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
|
||||
$flight_num = trim($row->flightnum);
|
||||
|
||||
$attrs = [
|
||||
'dpt_airport_id' => $row->depicao,
|
||||
'arr_airport_id' => $row->arricao,
|
||||
'route' => $row->route ?: '',
|
||||
'distance' => round($row->distance ?: 0, 2),
|
||||
'level' => $row->flightlevel ?: 0,
|
||||
'dpt_time' => $row->deptime ?: '',
|
||||
'arr_time' => $row->arrtime ?: '',
|
||||
'flight_time' => $this->convertDuration($row->flighttime) ?: '',
|
||||
'notes' => $row->notes ?: '',
|
||||
'active' => $row->enabled ?: true,
|
||||
];
|
||||
|
||||
try {
|
||||
$w = ['airline_id' => $airline_id, 'flight_number' => $flight_num];
|
||||
// $flight = Flight::updateOrCreate($w, $attrs);
|
||||
$flight = Flight::create(array_merge($w, $attrs));
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e);
|
||||
}
|
||||
|
||||
$this->idMapper->addMapping('flights', $row->id, $flight->id);
|
||||
|
||||
// TODO: deserialize route_details into ACARS table
|
||||
|
||||
if ($flight->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' flights');
|
||||
}
|
||||
}
|
||||
165
app/Services/Importers/GroupImporter.php
Normal file
165
app/Services/Importers/GroupImporter.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Services\RoleService;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Imports the groups into the permissions feature(s)
|
||||
*/
|
||||
class GroupImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'groups';
|
||||
protected $idField = 'groupid';
|
||||
|
||||
/**
|
||||
* Permissions in the legacy system, mapping them to the current system
|
||||
*/
|
||||
protected $legacy_permission_set = [
|
||||
'EDIT_NEWS' => 0x1,
|
||||
'EDIT_PAGES' => 0x2,
|
||||
'EDIT_DOWNLOADS' => 0x4,
|
||||
'EMAIL_PILOTS' => 0x8,
|
||||
'EDIT_AIRLINES' => 0x10, //
|
||||
'EDIT_FLEET' => 0x20, //
|
||||
'EDIT_SCHEDULES' => 0x80, //
|
||||
'IMPORT_SCHEDULES' => 0x100, //
|
||||
'MODERATE_REGISTRATIONS' => 0x200,
|
||||
'EDIT_PILOTS' => 0x400, //
|
||||
'EDIT_GROUPS' => 0x800,
|
||||
'EDIT_RANKS' => 0x1000, //
|
||||
'EDIT_AWARDS' => 0x2000, //
|
||||
'MODERATE_PIREPS' => 0x4000, //
|
||||
'VIEW_FINANCES' => 0x8000, //
|
||||
'EDIT_EXPENSES' => 0x10000, //
|
||||
'EDIT_SETTINGS' => 0x20000, //
|
||||
'EDIT_PIREPS_FIELDS' => 0x40000, //
|
||||
'EDIT_PROFILE_FIELDS' => 0x80000, //
|
||||
'EDIT_VACENTRAL' => 0x100000,
|
||||
'ACCESS_ADMIN' => 0x2000000,
|
||||
'FULL_ADMIN' => 35651519, //
|
||||
];
|
||||
|
||||
/**
|
||||
* Map a legacy value over to one of the current permission values
|
||||
*/
|
||||
protected $legacy_to_permission = [
|
||||
'FULL_ADMIN' => 'admin',
|
||||
'EDIT_AIRLINES' => 'airlines',
|
||||
'EDIT_AWARDS' => 'awards',
|
||||
'EDIT_FLEET' => 'fleet',
|
||||
'EDIT_EXPENCES' => 'finances',
|
||||
'VIEW_FINANCES' => 'finances',
|
||||
'EDIT_SCHEDULES' => 'flights',
|
||||
'EDIT_PILOTS' => 'users',
|
||||
'EDIT_PROFILE_FIELDS' => 'users',
|
||||
'EDIT_SETTINGS' => 'settings',
|
||||
'MODERATE_PIREPS' => 'pireps',
|
||||
'EDIT_PIREPS_FIELDS' => 'pireps',
|
||||
'EDIT_RANKS' => 'ranks',
|
||||
'MODERATE_REGISTRATIONS' => 'users',
|
||||
];
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- ROLES/GROUPS IMPORT ---');
|
||||
|
||||
/** @var \App\Services\RoleService $roleSvc */
|
||||
$roleSvc = app(RoleService::class);
|
||||
$permMappings = $this->getPermissions();
|
||||
|
||||
$count = 0;
|
||||
$permCount = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
// Legacy "administrator" role is now "admin", just map that 1:1
|
||||
if (strtolower($row->name) === 'administrators') {
|
||||
$role = Role::where('name', 'admin')->first();
|
||||
$this->idMapper->addMapping('group', $row->groupid, $role->id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Map the "core" roles, which are active/inactive pilots to a new ID of
|
||||
// -1; so then we can ignore/not add these groups, and then ignore them
|
||||
// for any of the users that are being imported. these groups are unused
|
||||
if ($row->core === 1 || $row->core === '1') {
|
||||
$this->idMapper->addMapping('group', $row->groupid, -1);
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = str_slug($row->name);
|
||||
$role = Role::firstOrCreate(
|
||||
['name' => $name],
|
||||
['display_name' => $row->name]
|
||||
);
|
||||
|
||||
$this->idMapper->addMapping('group', $row->groupid, $role->id);
|
||||
|
||||
// See if the permission set mask contains one of the mappings above
|
||||
// Add all of the ones which apply, and then set them on the new role
|
||||
$permissions = [];
|
||||
foreach ($this->legacy_permission_set as $legacy_name => $mask) {
|
||||
$val = $row->permissions & $mask;
|
||||
if ($val === $mask) {
|
||||
// Map this legacy permission to what it is under the new system
|
||||
if (!array_key_exists($legacy_name, $this->legacy_to_permission)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the ID of the permission
|
||||
try {
|
||||
$permName = $this->legacy_to_permission[$legacy_name];
|
||||
if ($permName === 'admin') {
|
||||
foreach ($permMappings as $name => $value) {
|
||||
if (!in_array($value, $permissions, true)) {
|
||||
$permissions[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$permMapId = $permMappings[$permName];
|
||||
if (!in_array($permMapId, $permissions, true)) {
|
||||
$permissions[] = $permMapId;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($permissions) > 0) {
|
||||
$roleSvc->setPermissionsForRole($role, $permissions);
|
||||
$permCount += count($permissions);
|
||||
}
|
||||
|
||||
if ($role->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' roles, synced '.$permCount.' permissions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the permissions from locally and return a kvp with the
|
||||
* key being the permission short-name and the value being the ID
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getPermissions(): array
|
||||
{
|
||||
$mappings = [];
|
||||
$permissions = Permission::all();
|
||||
/** @var \App\Models\Permission $p */
|
||||
foreach ($permissions as $p) {
|
||||
$mappings[$p->name] = $p->id;
|
||||
}
|
||||
|
||||
return $mappings;
|
||||
}
|
||||
}
|
||||
74
app/Services/Importers/LedgerImporter.php
Normal file
74
app/Services/Importers/LedgerImporter.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Pirep;
|
||||
use App\Services\FinanceService;
|
||||
use App\Support\Money;
|
||||
|
||||
class LedgerImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'ledger';
|
||||
|
||||
/**
|
||||
* Legacy ID to the current ledger ref_model mapping
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $legacy_paysource = [
|
||||
1 => Pirep::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
if (!$this->db->tableExists('ledger')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->comment('--- LEDGER IMPORT ---');
|
||||
|
||||
/** @var FinanceService $financeSvc */
|
||||
$financeSvc = app(FinanceService::class);
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$pirep = Pirep::find($this->idMapper->getMapping('pireps', $row->pirepid));
|
||||
if (!$pirep) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$pilot_pay = Money::createFromAmount($row->amount * 100);
|
||||
$memo = 'Pilot payment';
|
||||
|
||||
$financeSvc->debitFromJournal(
|
||||
$pirep->airline->journal,
|
||||
$pilot_pay,
|
||||
$pirep,
|
||||
$memo,
|
||||
'Pilot Pay',
|
||||
'pilot_pay',
|
||||
$row->submitdate
|
||||
);
|
||||
|
||||
$financeSvc->creditToJournal(
|
||||
$pirep->user->journal,
|
||||
$pilot_pay,
|
||||
$pirep,
|
||||
$memo,
|
||||
'Pilot Pay',
|
||||
'pilot_pay',
|
||||
$row->submitdate
|
||||
);
|
||||
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' ledger entries');
|
||||
}
|
||||
}
|
||||
152
app/Services/Importers/PirepImporter.php
Normal file
152
app/Services/Importers/PirepImporter.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Pirep;
|
||||
|
||||
class PirepImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'pireps';
|
||||
protected $idField = 'pirepid';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- PIREP IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'pirepid',
|
||||
'pilotid',
|
||||
'code',
|
||||
'aircraft',
|
||||
'flightnum',
|
||||
'depicao',
|
||||
'arricao',
|
||||
'fuelused',
|
||||
'route',
|
||||
'source',
|
||||
'accepted',
|
||||
'submitdate',
|
||||
'distance',
|
||||
'flighttime_stamp',
|
||||
'flighttype',
|
||||
];
|
||||
|
||||
// See if there's a flightlevel column, export that if there is
|
||||
$columns = $this->db->getColumns($this->table);
|
||||
if (in_array('flightlevel', $columns, true)) {
|
||||
$fields[] = 'flightlevel';
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
||||
foreach ($rows as $row) {
|
||||
$pirep_id = $row->pirepid;
|
||||
$user_id = $this->idMapper->getMapping('users', $row->pilotid);
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
$aircraft_id = $this->idMapper->getMapping('aircraft', $row->aircraft);
|
||||
|
||||
$attrs = [
|
||||
'user_id' => $user_id,
|
||||
'airline_id' => $airline_id,
|
||||
'aircraft_id' => $aircraft_id,
|
||||
'flight_number' => $row->flightnum ?: '',
|
||||
'dpt_airport_id' => $row->depicao,
|
||||
'arr_airport_id' => $row->arricao,
|
||||
'block_fuel' => $row->fuelused,
|
||||
'route' => $row->route ?: '',
|
||||
'source_name' => $row->source,
|
||||
'state' => $this->mapState($row->accepted),
|
||||
'created_at' => $this->parseDate($row->submitdate),
|
||||
'updated_at' => $this->parseDate($row->submitdate),
|
||||
];
|
||||
|
||||
// Set the distance
|
||||
$distance = round($row->distance ?: 0, 2);
|
||||
$attrs['distance'] = $distance;
|
||||
$attrs['planned_distance'] = $distance;
|
||||
|
||||
// Set the flight time properly
|
||||
$duration = $this->convertDuration($row->flighttime_stamp);
|
||||
$attrs['flight_time'] = $duration;
|
||||
$attrs['planned_flight_time'] = $duration;
|
||||
|
||||
// Set how it was filed
|
||||
if (strtoupper($row->source) === 'MANUAL') {
|
||||
$attrs['source'] = PirepSource::MANUAL;
|
||||
} else {
|
||||
$attrs['source'] = PirepSource::ACARS;
|
||||
}
|
||||
|
||||
// Set the flight type
|
||||
$row->flighttype = strtoupper($row->flighttype);
|
||||
if ($row->flighttype === 'P') {
|
||||
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
||||
} elseif ($row->flighttype === 'C') {
|
||||
$attrs['flight_type'] = FlightType::SCHED_CARGO;
|
||||
} else {
|
||||
$attrs['flight_type'] = FlightType::CHARTER_PAX_ONLY;
|
||||
}
|
||||
|
||||
// Set the flight level of the PIREP is set
|
||||
if (property_exists($row, 'flightlevel')) {
|
||||
$attrs['level'] = $row->flightlevel;
|
||||
} else {
|
||||
$attrs['level'] = 0;
|
||||
}
|
||||
|
||||
$w = ['id' => $pirep_id];
|
||||
|
||||
$pirep = Pirep::updateOrCreate($w, $attrs);
|
||||
//$pirep = Pirep::create(array_merge($w, $attrs));
|
||||
|
||||
//Log::debug('pirep oldid='.$pirep_id.', olduserid='.$row->pilotid
|
||||
// .'; new id='.$pirep->id.', user id='.$user_id);
|
||||
|
||||
$source = strtoupper($row->source);
|
||||
if ($source === 'SMARTCARS') {
|
||||
// TODO: Parse smartcars log into the acars table
|
||||
} elseif ($source === 'KACARS') {
|
||||
// TODO: Parse kACARS log into acars table
|
||||
} elseif ($source === 'XACARS') {
|
||||
// TODO: Parse XACARS log into acars table
|
||||
}
|
||||
|
||||
// TODO: Add extra fields in as PIREP fields
|
||||
$this->idMapper->addMapping('pireps', $row->pirepid, $pirep->id);
|
||||
|
||||
if ($pirep->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' pireps');
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the old status to the current
|
||||
* https://github.com/nabeelio/phpvms_v2/blob/master/core/app.config.php#L450
|
||||
*
|
||||
* @param int $old_state
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function mapState($old_state)
|
||||
{
|
||||
$map = [
|
||||
0 => PirepState::PENDING,
|
||||
1 => PirepState::ACCEPTED,
|
||||
2 => PirepState::REJECTED,
|
||||
3 => PirepState::IN_PROGRESS,
|
||||
];
|
||||
|
||||
$old_state = (int) $old_state;
|
||||
if (!in_array($old_state, $map, true)) {
|
||||
return PirepState::PENDING;
|
||||
}
|
||||
|
||||
return $map[$old_state];
|
||||
}
|
||||
}
|
||||
36
app/Services/Importers/RankImport.php
Normal file
36
app/Services/Importers/RankImport.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Rank;
|
||||
|
||||
class RankImport extends BaseImporter
|
||||
{
|
||||
protected $table = 'ranks';
|
||||
protected $idField = 'rankid';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- RANK IMPORT ---');
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$rank = Rank::updateOrCreate(['name' => $row->rank], [
|
||||
'image_url' => $row->rankimage,
|
||||
'hours' => $row->minhours,
|
||||
'acars_base_payrate' => $row->payrate,
|
||||
'manual_base_payrate' => $row->payrate,
|
||||
]);
|
||||
|
||||
$this->idMapper->addMapping('ranks', $row->rankid, $rank->id);
|
||||
$this->idMapper->addMapping('ranks', $row->rank, $rank->id);
|
||||
|
||||
if ($rank->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' ranks');
|
||||
}
|
||||
}
|
||||
30
app/Services/Importers/SettingsImporter.php
Normal file
30
app/Services/Importers/SettingsImporter.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Repositories\SettingRepository;
|
||||
|
||||
class SettingsImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'settings';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- SETTINGS IMPORT ---');
|
||||
|
||||
/** @var SettingRepository $settingsRepo */
|
||||
$settingsRepo = app(SettingRepository::class);
|
||||
|
||||
$count = 0;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
switch ($row->name) {
|
||||
case 'ADMIN_EMAIL':
|
||||
$settingsRepo->store('general.admin_email', $row->value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' settings');
|
||||
}
|
||||
}
|
||||
164
app/Services/Importers/UserImport.php
Normal file
164
app/Services/Importers/UserImport.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Importers;
|
||||
|
||||
use App\Models\Enums\UserState;
|
||||
use App\Models\User;
|
||||
use App\Services\UserService;
|
||||
use App\Support\Units\Time;
|
||||
use App\Support\Utils;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserImport extends BaseImporter
|
||||
{
|
||||
protected $table = 'pilots';
|
||||
protected $idField = 'pilotid';
|
||||
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
private $userSvc;
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- USER IMPORT ---');
|
||||
|
||||
$this->userSvc = app(UserService::class);
|
||||
|
||||
$count = 0;
|
||||
$first_row = true;
|
||||
$rows = $this->db->readRows($this->table, $this->idField, $start);
|
||||
foreach ($rows as $row) {
|
||||
$pilot_id = $row->pilotid; // This isn't their actual ID
|
||||
$name = $row->firstname.' '.$row->lastname;
|
||||
|
||||
// Figure out which airline, etc, they belong to
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
// Log::info('User airline from '.$row->code.' to ID '.$airline_id);
|
||||
|
||||
$rank_id = $this->idMapper->getMapping('ranks', $row->rank);
|
||||
$state = $this->getUserState($row->retired);
|
||||
|
||||
if ($first_row) {
|
||||
$new_password = 'admin';
|
||||
$first_row = false;
|
||||
} else {
|
||||
$new_password = Str::random(60);
|
||||
}
|
||||
|
||||
// Look for a user with that pilot ID already. If someone has it
|
||||
if ($this->userSvc->isPilotIdAlreadyUsed($pilot_id)) {
|
||||
Log::info('User with pilot id '.$pilot_id.' exists');
|
||||
|
||||
// Is this the same user? If not, get a new pilot ID
|
||||
$user_exist = User::where('pilot_id', $pilot_id)->first();
|
||||
if ($user_exist->email !== $row->email) {
|
||||
$pilot_id = $this->userSvc->getNextAvailablePilotId();
|
||||
}
|
||||
}
|
||||
|
||||
$attrs = [
|
||||
'pilot_id' => $pilot_id,
|
||||
'name' => $name,
|
||||
'password' => Hash::make($new_password),
|
||||
'api_key' => Utils::generateApiKey(),
|
||||
'airline_id' => $airline_id,
|
||||
'rank_id' => $rank_id,
|
||||
'home_airport_id' => $row->hub,
|
||||
'curr_airport_id' => $row->hub,
|
||||
'country' => $row->location,
|
||||
'flights' => (int) $row->totalflights,
|
||||
'flight_time' => Time::hoursToMinutes($row->totalhours),
|
||||
'state' => $state,
|
||||
'created_at' => $this->parseDate($row->joindate),
|
||||
];
|
||||
|
||||
$user = User::updateOrCreate(['email' => $row->email], $attrs);
|
||||
$this->idMapper->addMapping('users', $row->pilotid, $user->id);
|
||||
|
||||
$this->updateUserRoles($user, $row->pilotid);
|
||||
|
||||
if ($user->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user's roles and add them to the proper ones
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $old_pilot_id
|
||||
*/
|
||||
protected function updateUserRoles(User $user, $old_pilot_id)
|
||||
{
|
||||
// Be default add them to the user role, and then determine if they
|
||||
// belong to any other groups, and add them to that
|
||||
$newRoles = [];
|
||||
|
||||
// Figure out what other groups they belong to... read from the old table, and map
|
||||
// them to the new group(s)
|
||||
$old_user_groups = $this->db->findBy('groupmembers', ['pilotid' => $old_pilot_id]);
|
||||
foreach ($old_user_groups as $oldGroup) {
|
||||
$newRoleId = $this->idMapper->getMapping('group', $oldGroup->groupid);
|
||||
|
||||
// This role should be ignored
|
||||
if ($newRoleId === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newRoles[] = $newRoleId;
|
||||
}
|
||||
|
||||
// Assign the groups to the new user
|
||||
$user->attachRoles($newRoles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's new state from their original state
|
||||
*
|
||||
* @param $state
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getUserState($state)
|
||||
{
|
||||
// Return active for now, let the stats/cron determine the status later
|
||||
return UserState::ACTIVE;
|
||||
/*$state = (int) $state;
|
||||
|
||||
// Declare array of classic states
|
||||
$phpvms_classic_states = [
|
||||
'ACTIVE' => 0,
|
||||
'INACTIVE' => 1,
|
||||
'BANNED' => 2,
|
||||
'ON_LEAVE' => 3,
|
||||
];
|
||||
|
||||
// Decide which state they will be in accordance with v7
|
||||
if ($state === $phpvms_classic_states['ACTIVE']) {
|
||||
return UserState::ACTIVE;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['INACTIVE']) {
|
||||
// TODO: Make an inactive state?
|
||||
return UserState::REJECTED;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['BANNED']) {
|
||||
return UserState::SUSPENDED;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['ON_LEAVE']) {
|
||||
return UserState::ON_LEAVE;
|
||||
}
|
||||
|
||||
$this->error('Unknown status: '.$state);
|
||||
|
||||
return UserState::ACTIVE;*/
|
||||
}
|
||||
}
|
||||
252
app/Services/Installer/ConfigService.php
Normal file
252
app/Services/Installer/ConfigService.php
Normal file
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Installer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use Exception;
|
||||
use function extension_loaded;
|
||||
use Illuminate\Encryption\Encrypter;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use function is_bool;
|
||||
use Nwidart\Modules\Support\Stub;
|
||||
use PDO;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
class ConfigService extends Service
|
||||
{
|
||||
/**
|
||||
* Create the .env file
|
||||
*
|
||||
* @param $attrs
|
||||
*
|
||||
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createConfigFiles($attrs): bool
|
||||
{
|
||||
$opts = [
|
||||
'APP_ENV' => 'dev',
|
||||
'APP_KEY' => $this->createAppKey(),
|
||||
'SITE_NAME' => '',
|
||||
'SITE_URL' => 'http://phpvms.test',
|
||||
'CACHE_PREFIX' => '',
|
||||
'DB_CONN' => '',
|
||||
'DB_HOST' => '',
|
||||
'DB_PORT' => 3306,
|
||||
'DB_NAME' => '',
|
||||
'DB_USER' => '',
|
||||
'DB_PASS' => '',
|
||||
'DB_PREFIX' => '',
|
||||
'DB_EMULATE_PREPARES' => false,
|
||||
];
|
||||
|
||||
$opts = array_merge($opts, $attrs);
|
||||
|
||||
$opts = $this->determinePdoOptions($opts);
|
||||
$opts = $this->configCacheDriver($opts);
|
||||
$opts = $this->configQueueDriver($opts);
|
||||
|
||||
$this->writeConfigFiles($opts);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the environment file and update certain keys/values
|
||||
*
|
||||
* @param array $kvp
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateKeysInEnv(array $kvp)
|
||||
{
|
||||
$app = app();
|
||||
|
||||
$env_file = file_get_contents($app->environmentFilePath());
|
||||
foreach ($kvp as $key => $value) {
|
||||
$key = strtoupper($key);
|
||||
|
||||
// cast for any boolean values
|
||||
if (is_bool($value)) {
|
||||
$value = $value === true ? 'true' : 'false';
|
||||
}
|
||||
|
||||
// surround by quotes if there are any spaces in the value
|
||||
if (strpos($value, ' ') !== false) {
|
||||
$value = '"'.$value.'"';
|
||||
}
|
||||
|
||||
Log::info('Replacing "'.$key.'" with '.$value);
|
||||
|
||||
$env_file = preg_replace(
|
||||
'/^'.$key.'(.*)?/m',
|
||||
$key.'='.$value,
|
||||
$env_file
|
||||
);
|
||||
}
|
||||
|
||||
file_put_contents($app->environmentFilePath(), $env_file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a fresh new APP_KEY
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createAppKey(): string
|
||||
{
|
||||
return base64_encode(Encrypter::generateKey(config('app.cipher')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a few options within the PDO driver, depending on the version
|
||||
* of mysql/maria, etc used. ATM, only make a change for MariaDB
|
||||
*
|
||||
* @param $opts
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function determinePdoOptions($opts)
|
||||
{
|
||||
if ($opts['DB_CONN'] !== 'mysql') {
|
||||
return $opts;
|
||||
}
|
||||
|
||||
$dsn = "mysql:host=$opts[DB_HOST];port=$opts[DB_PORT];";
|
||||
Log::info('Connection string: '.$dsn);
|
||||
|
||||
$conn = new PDO($dsn, $opts['DB_USER'], $opts['DB_PASS']);
|
||||
$version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION));
|
||||
Log::info('Detected DB Version: '.$version);
|
||||
|
||||
// If it's mariadb, enable the emulation for prepared statements
|
||||
// seems to be throwing a problem on 000webhost
|
||||
// https://github.com/nabeelio/phpvms/issues/132
|
||||
if (strpos($version, 'mariadb') !== false) {
|
||||
Log::info('Detected MariaDB, setting DB_EMULATE_PREPARES to true');
|
||||
$opts['DB_EMULATE_PREPARES'] = true;
|
||||
}
|
||||
|
||||
return $opts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine is APC is installed, if so, then use it as a cache driver
|
||||
*
|
||||
* @param $opts
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function configCacheDriver($opts)
|
||||
{
|
||||
// Set the cache prefix
|
||||
$opts['CACHE_PREFIX'] = uniqid($opts['SITE_NAME'].'_');
|
||||
|
||||
// Figure out what cache driver to initially use, depending on
|
||||
// what is installed. It won't detect redis or anything, though
|
||||
foreach (config('installer.cache.drivers') as $ext => $driver) {
|
||||
if (extension_loaded($ext)) {
|
||||
Log::info('Detected extension "'.$ext.'", setting driver to "'.$driver.'"');
|
||||
$opts['CACHE_DRIVER'] = $driver;
|
||||
return $opts;
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('No extension detected, using file cache');
|
||||
$opts['CACHE_DRIVER'] = config('installer.cache.default');
|
||||
return $opts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a queue driver that's not the default "sync"
|
||||
* driver, if a database is being used
|
||||
*
|
||||
* @param $opts
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function configQueueDriver($opts)
|
||||
{
|
||||
// If we're setting up a database, then also setup
|
||||
// the default queue driver to use the database
|
||||
if ($opts['DB_CONN'] === 'mysql' || $opts['DB_CONN'] === 'postgres') {
|
||||
$opts['QUEUE_DRIVER'] = 'database';
|
||||
} else {
|
||||
$opts['QUEUE_DRIVER'] = 'sync';
|
||||
}
|
||||
|
||||
return $opts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the config files
|
||||
*/
|
||||
public function removeConfigFiles()
|
||||
{
|
||||
$env_file = App::environmentFilePath();
|
||||
$config_file = App::environmentPath().'/config.php';
|
||||
|
||||
if (file_exists($env_file)) {
|
||||
try {
|
||||
unlink($env_file);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (file_exists($config_file)) {
|
||||
try {
|
||||
unlink($config_file);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template file name and write it out
|
||||
*
|
||||
* @param $opts
|
||||
*
|
||||
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
||||
*/
|
||||
protected function writeConfigFiles($opts)
|
||||
{
|
||||
Stub::setBasePath(resource_path('/stubs/installer'));
|
||||
|
||||
$env_file = App::environmentFilePath();
|
||||
|
||||
if (file_exists($env_file) && !is_writable($env_file)) {
|
||||
Log::error('Permissions on existing env.php is not writable');
|
||||
|
||||
throw new FileException('Can\'t write to the env.php file! Check the permissions');
|
||||
}
|
||||
|
||||
/*
|
||||
* First write out the env file
|
||||
*/
|
||||
try {
|
||||
$stub = new Stub('/env.stub', $opts);
|
||||
$stub->render();
|
||||
$stub->saveTo(App::environmentPath(), App::environmentFile());
|
||||
} catch (Exception $e) {
|
||||
throw new FileException('Couldn\'t write env.php. ('.$e.')');
|
||||
}
|
||||
|
||||
/*
|
||||
* Next write out the config file. If there's an error here,
|
||||
* then throw an exception but delete the env file first
|
||||
*/
|
||||
try {
|
||||
$stub = new Stub('/config.stub', $opts);
|
||||
$stub->render();
|
||||
$stub->saveTo(App::environmentPath(), 'config.php');
|
||||
} catch (Exception $e) {
|
||||
unlink(App::environmentPath().'/'.App::environmentFile());
|
||||
|
||||
throw new FileException('Couldn\'t write config.php. ('.$e.')');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ namespace App\Services\Installer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
|
||||
class InstallerService extends Service
|
||||
{
|
||||
@@ -44,27 +43,6 @@ class InstallerService extends Service
|
||||
*/
|
||||
public function clearCaches(): void
|
||||
{
|
||||
$commands = [
|
||||
'clear-compiled',
|
||||
'cache:clear',
|
||||
'route:clear',
|
||||
'view:clear',
|
||||
];
|
||||
|
||||
foreach ($commands as $cmd) {
|
||||
Artisan::call($cmd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the installer and importer modules
|
||||
*/
|
||||
public function disableInstallerModules()
|
||||
{
|
||||
$module = Module::find('installer');
|
||||
$module->disable();
|
||||
|
||||
$module = Module::find('importer');
|
||||
$module->disable();
|
||||
Artisan::call('optimize:clear');
|
||||
}
|
||||
}
|
||||
|
||||
77
app/Services/Installer/RequirementsService.php
Normal file
77
app/Services/Installer/RequirementsService.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Installer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
|
||||
class RequirementsService extends Service
|
||||
{
|
||||
/**
|
||||
* Check the PHP version that it meets the minimum requirement
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function checkPHPVersion(): array
|
||||
{
|
||||
$passed = false;
|
||||
if (version_compare(PHP_VERSION, config('installer.php.version')) >= 0) {
|
||||
$passed = true;
|
||||
}
|
||||
|
||||
return ['version' => PHP_VERSION, 'passed' => $passed];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure the minimal extensions required are loaded
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function checkExtensions(): array
|
||||
{
|
||||
$extensions = [];
|
||||
foreach (config('installer.extensions') as $ext) {
|
||||
$pass = true;
|
||||
if (!\extension_loaded($ext)) {
|
||||
$pass = false;
|
||||
}
|
||||
|
||||
$extensions[] = [
|
||||
'ext' => $ext,
|
||||
'passed' => $pass,
|
||||
];
|
||||
}
|
||||
|
||||
return $extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the permissions for the directories specified
|
||||
* Make sure they exist and are writable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function checkPermissions(): array
|
||||
{
|
||||
clearstatcache();
|
||||
|
||||
$directories = [];
|
||||
foreach (config('installer.permissions') as $path) {
|
||||
$pass = true;
|
||||
|
||||
if (!file_exists($path)) {
|
||||
$pass = false;
|
||||
}
|
||||
|
||||
if (!is_writable($path)) {
|
||||
$pass = false;
|
||||
}
|
||||
|
||||
$directories[] = [
|
||||
'dir' => $path,
|
||||
'passed' => $pass,
|
||||
];
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Services\Installer;
|
||||
use App\Contracts\Service;
|
||||
use App\Models\Setting;
|
||||
use App\Services\DatabaseService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -42,6 +43,10 @@ class SeederService extends Service
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->moduleSeedsPending()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,6 +59,7 @@ class SeederService extends Service
|
||||
$this->syncAllYamlFileSeeds();
|
||||
$this->syncAllSettings();
|
||||
$this->syncAllPermissions();
|
||||
$this->syncAllModules();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,6 +89,24 @@ class SeederService extends Service
|
||||
});
|
||||
}
|
||||
|
||||
public function syncAllModules(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/modules.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $module) {
|
||||
$module['updated_at'] = Carbon::now();
|
||||
$count = DB::table('modules')->where('name', $module['name'])->count('name');
|
||||
if ($count === 0) {
|
||||
$module['created_at'] = Carbon::now();
|
||||
DB::table('modules')->insert($module);
|
||||
} else {
|
||||
DB::table('modules')
|
||||
->where('name', $module['name'])
|
||||
->update($module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncAllSettings(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
||||
@@ -286,4 +310,28 @@ class SeederService extends Service
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function moduleSeedsPending(): bool
|
||||
{
|
||||
$all_modules = DB::table('modules')->get();
|
||||
|
||||
$data = file_get_contents(database_path('/seeds/modules.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
|
||||
foreach ($yml as $perm) {
|
||||
$row = $all_modules->firstWhere('name', $perm['name']);
|
||||
if (!$row) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// See if any of these column values have changed
|
||||
foreach (['name', 'enabled'] as $column) {
|
||||
if ($row->{$column} !== $perm[$column]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,22 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Exceptions\ModuleExistsException;
|
||||
use App\Exceptions\ModuleInstallationError;
|
||||
use App\Exceptions\ModuleInvalidFileType;
|
||||
use App\Models\Module;
|
||||
use Exception;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Laracasts\Flash\FlashNotifier;
|
||||
use Madnest\Madzipper\Madzipper;
|
||||
use Nwidart\Modules\Json;
|
||||
use PharData;
|
||||
|
||||
class ModuleService extends Service
|
||||
{
|
||||
@@ -22,14 +38,14 @@ class ModuleService extends Service
|
||||
* @param string $title
|
||||
* @param string $url
|
||||
* @param string $icon
|
||||
* @param mixed $logged_in
|
||||
* @param bool $logged_in
|
||||
*/
|
||||
public function addFrontendLink(string $title, string $url, string $icon = '', $logged_in = true)
|
||||
public function addFrontendLink(string $title, string $url, string $icon = 'pe-7s-users', $logged_in = true)
|
||||
{
|
||||
self::$frontendLinks[$logged_in][] = [
|
||||
'title' => $title,
|
||||
'url' => $url,
|
||||
'icon' => 'pe-7s-users',
|
||||
'icon' => $icon,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -52,12 +68,12 @@ class ModuleService extends Service
|
||||
* @param string $url
|
||||
* @param string $icon
|
||||
*/
|
||||
public function addAdminLink(string $title, string $url, string $icon = '')
|
||||
public function addAdminLink(string $title, string $url, string $icon = 'pe-7s-users')
|
||||
{
|
||||
self::$adminLinks[] = [
|
||||
'title' => $title,
|
||||
'url' => $url,
|
||||
'icon' => 'pe-7s-users',
|
||||
'icon' => $icon,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -70,4 +86,217 @@ class ModuleService extends Service
|
||||
{
|
||||
return self::$adminLinks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get All modules from Database
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getAllModules(): object
|
||||
{
|
||||
return Module::all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Module Information from Database.
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getModule($id): object
|
||||
{
|
||||
return Module::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding installed module to the database
|
||||
*
|
||||
* @param $module_name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function addModule($module_name): bool
|
||||
{
|
||||
/*Check if module already exists*/
|
||||
$module = Module::where('name', $module_name);
|
||||
if (!$module->exists()) {
|
||||
Module::create([
|
||||
'name' => $module_name,
|
||||
'enabled' => 1,
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* User's uploaded file is passed into this method
|
||||
* to install module in the Storage.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
*
|
||||
* @return FlashNotifier
|
||||
*/
|
||||
public function installModule(UploadedFile $file): FlashNotifier
|
||||
{
|
||||
$file_ext = $file->getClientOriginalExtension();
|
||||
$allowed_extensions = ['zip', 'tar', 'gz'];
|
||||
|
||||
if (!in_array($file_ext, $allowed_extensions, true)) {
|
||||
throw new ModuleInvalidFileType();
|
||||
}
|
||||
|
||||
$module = null;
|
||||
|
||||
$new_dir = rand();
|
||||
|
||||
File::makeDirectory(
|
||||
storage_path('app/tmp/modules/'.$new_dir),
|
||||
0777,
|
||||
true
|
||||
);
|
||||
$temp_ext_folder = storage_path('app/tmp/modules/'.$new_dir);
|
||||
$temp = storage_path('app/tmp/modules/'.$new_dir);
|
||||
|
||||
$zipper = null;
|
||||
|
||||
if ($file_ext === 'tar' || $file_ext === 'gz') {
|
||||
$zipper = new PharData($file);
|
||||
$zipper->decompress();
|
||||
}
|
||||
|
||||
if ($file_ext === 'zip') {
|
||||
$madZipper = new Madzipper();
|
||||
|
||||
try {
|
||||
$zipper = $madZipper->make($file);
|
||||
} catch (Exception $e) {
|
||||
throw new ModuleInstallationError();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$zipper->extractTo($temp);
|
||||
} catch (Exception $e) {
|
||||
throw new ModuleInstallationError();
|
||||
}
|
||||
|
||||
if (!File::exists($temp.'/module.json')) {
|
||||
$directories = Storage::directories('tmp/modules/'.$new_dir);
|
||||
$temp = storage_path('app/'.$directories[0]);
|
||||
}
|
||||
|
||||
$json_file = $temp.'/module.json';
|
||||
|
||||
if (File::exists($json_file)) {
|
||||
$json = json_decode(file_get_contents($json_file), true);
|
||||
$module = $json['name'];
|
||||
} else {
|
||||
File::deleteDirectory($temp_ext_folder);
|
||||
return flash()->error('Module Structure Not Correct!');
|
||||
}
|
||||
|
||||
if (!$module) {
|
||||
File::deleteDirectory($temp_ext_folder);
|
||||
return flash()->error('Not a Valid Module File.');
|
||||
}
|
||||
|
||||
$toCopy = base_path().'/modules/'.$module;
|
||||
|
||||
if (File::exists($toCopy)) {
|
||||
File::deleteDirectory($temp_ext_folder);
|
||||
|
||||
throw new ModuleExistsException($module);
|
||||
}
|
||||
|
||||
File::moveDirectory($temp, $toCopy);
|
||||
|
||||
File::deleteDirectory($temp_ext_folder);
|
||||
|
||||
try {
|
||||
$this->addModule($module);
|
||||
} catch (Exception $e) {
|
||||
throw new ModuleExistsException($module);
|
||||
}
|
||||
|
||||
Artisan::call('config:cache');
|
||||
Artisan::call('module:migrate '.$module);
|
||||
|
||||
return flash()->success('Module Installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update module with the status passed by user.
|
||||
*
|
||||
* @param $id
|
||||
* @param $status
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function updateModule($id, $status): bool
|
||||
{
|
||||
$module = Module::find($id);
|
||||
$module->update([
|
||||
'enabled' => $status,
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Module from the Storage & Database.
|
||||
*
|
||||
* @param $id
|
||||
* @param $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteModule($id, $data): bool
|
||||
{
|
||||
$module = Module::find($id);
|
||||
if ($data['verify'] === strtoupper($module->name)) {
|
||||
try {
|
||||
$module->delete();
|
||||
} catch (Exception $e) {
|
||||
Log::emergency('Cannot Delete Module!');
|
||||
}
|
||||
$moduleDir = base_path().'/modules/'.$module->name;
|
||||
|
||||
try {
|
||||
File::deleteDirectory($moduleDir);
|
||||
} catch (Exception $e) {
|
||||
Log::info('Folder Deleted Manually for Module : '.$module->name);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get & scan all modules.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scan()
|
||||
{
|
||||
$modules_path = base_path('modules/*');
|
||||
$path = Str::endsWith($modules_path, '/*') ? $modules_path : Str::finish($modules_path, '/*');
|
||||
|
||||
$modules = [];
|
||||
|
||||
$manifests = (new Filesystem())->glob("{$path}/module.json");
|
||||
|
||||
is_array($manifests) || $manifests = [];
|
||||
|
||||
foreach ($manifests as $manifest) {
|
||||
$name = Json::make($manifest)->get('name');
|
||||
$module = Module::where('name', $name);
|
||||
if (!$module->exists()) {
|
||||
array_push($modules, $name);
|
||||
}
|
||||
}
|
||||
|
||||
return $modules;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user