mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-15 02:07:56 -04:00
255 lines
8.0 KiB
PHP
255 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\BHDModuleManager\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Brian2694\Toastr\Facades\Toastr;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Log;
|
|
use ZipArchive;
|
|
use Modules\BHDModuleManager\Traits\Upload;
|
|
use Modules\UserActivityLog\Traits\LogActivity;
|
|
|
|
class BHDModuleManagerController extends Controller
|
|
{
|
|
use Upload;
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
* @return Renderable
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('bhdmodulemanager::index');
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
* @return Renderable
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bhdmodulemanager::create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
* @param Request $request
|
|
* @return Renderable
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
* @param int $id
|
|
* @return Renderable
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return view('bhdmodulemanager::show');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
* @param int $id
|
|
* @return Renderable
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
return view('bhdmodulemanager::edit');
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
* @param Request $request
|
|
* @param int $id
|
|
* @return Renderable
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
* @param int $id
|
|
* @return Renderable
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
public function uploadModule(Request $request)
|
|
{
|
|
$request->validate([
|
|
'module' => 'required|mimes:zip',
|
|
]);
|
|
|
|
|
|
try {
|
|
$path = $request->module->store('updateFile');
|
|
$request->module->getClientOriginalName();
|
|
$zip = new ZipArchive;
|
|
$res = $zip->open(storage_path('app/' . $path));
|
|
if ($res === true) {
|
|
$zip->extractTo(storage_path('app/tempUpdate'));
|
|
$zip->close();
|
|
} else {
|
|
abort(500, 'Error! Could not open File');
|
|
}
|
|
|
|
|
|
$src = storage_path('app/tempUpdate');
|
|
|
|
$dir = opendir($src);
|
|
|
|
$module = '';
|
|
while ($file = readdir($dir)) {
|
|
if ($file != "." && $file != "..") {
|
|
$module = $file;
|
|
}
|
|
}
|
|
|
|
|
|
$dataPath = $src.'/'.$module . '/' . $module . '.json';
|
|
$strJsonFileContents = file_get_contents($dataPath);
|
|
$array = json_decode($strJsonFileContents, true);
|
|
|
|
if (!empty($array)) {
|
|
$json = $array[$module];
|
|
if (!empty($json['min_system_version']) && app('general_setting')->system_version < $json['min_system_version']) {
|
|
if (storage_path('app/updateFile')) {
|
|
$this->delete_directory(storage_path('app/updateFile'));
|
|
}
|
|
if (storage_path('app/tempUpdate')) {
|
|
$this->delete_directory(storage_path('app/tempUpdate'));
|
|
}
|
|
Toastr::error($json['min_system_version'] . ' or greater system version is required for this version', trans('common.operation_failed'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
if(empty($json['min_system_version'])){
|
|
if (storage_path('app/updateFile')) {
|
|
$this->delete_directory(storage_path('app/updateFile'));
|
|
}
|
|
if (storage_path('app/tempUpdate')) {
|
|
$this->delete_directory(storage_path('app/tempUpdate'));
|
|
}
|
|
Toastr::error('This version is not suitable for current system version. Please check update log for minimum required version.', trans('common.operation_failed'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
} else {
|
|
if (storage_path('app/updateFile')) {
|
|
$this->delete_directory(storage_path('app/updateFile'));
|
|
}
|
|
if (storage_path('app/tempUpdate')) {
|
|
$this->delete_directory(storage_path('app/tempUpdate'));
|
|
}
|
|
Toastr::error('Config File Missing', trans('common.operation_failed'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
$dst = base_path('/Modules/');
|
|
$this->recurse_copy($src, $dst);
|
|
$this->migrateModule($module);
|
|
$moduleCheck = \Nwidart\Modules\Facades\Module::find($module);
|
|
$moduleCheck->enable();
|
|
|
|
$this->migrateModule($module);
|
|
|
|
if (storage_path('app/updateFile')) {
|
|
$this->delete_directory(storage_path('app/updateFile'));
|
|
}
|
|
if (storage_path('app/tempUpdate')) {
|
|
$this->delete_directory(storage_path('app/tempUpdate'));
|
|
}
|
|
|
|
|
|
Toastr::success("Your module successfully uploaded", 'Success');
|
|
return redirect()->back();
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
LogActivity::errorLog($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function enableModule(Request $request) {
|
|
$name = $request->name;
|
|
if ($name) {
|
|
$moduleCheck = \Nwidart\Modules\Facades\Module::find($name);
|
|
$moduleCheck->enable();
|
|
return response()->json([
|
|
'message' => 'Module has been enabled.'
|
|
]);
|
|
}
|
|
return response()->json([
|
|
'message' => 'Failed to enable the module.'
|
|
], 400);
|
|
}
|
|
|
|
public function disableModule(Request $request) {
|
|
$name = $request->name;
|
|
if ($name) {
|
|
$moduleCheck = \Nwidart\Modules\Facades\Module::find($name);
|
|
$moduleCheck->disable();
|
|
return response()->json([
|
|
'message' => 'Module has been disabled.'
|
|
]);
|
|
}
|
|
return response()->json([
|
|
'message' => 'Failed to disable the module'
|
|
], 400);
|
|
}
|
|
|
|
public function migrateModule($module)
|
|
{
|
|
//$dataPath = 'Modules/' . $module . '/' . $module . '.json'; // // Get the contents of the JSON file
|
|
//$strJsonFileContents = file_get_contents($dataPath);
|
|
//$array = json_decode($strJsonFileContents, true);
|
|
//$migrations = $array[$module]['migration'] ?? '';
|
|
|
|
$migrations = File::files('Modules/' . $module . '/Database/Migrations');
|
|
$module_tables = [];
|
|
|
|
if (count($migrations) > 0) {
|
|
foreach ($migrations as $migration) {
|
|
$module_tables[] = $migration->getPathname();
|
|
}
|
|
}
|
|
|
|
$is_module_available = 'Modules/' . $module . '/Providers/' . $module . 'ServiceProvider.php';
|
|
|
|
if (file_exists($is_module_available)) {
|
|
try {
|
|
if (!empty($module_tables)) {
|
|
foreach ($module_tables as $path) {
|
|
if (file_exists($path)) {
|
|
try {
|
|
$test = Artisan::call('migrate',
|
|
array(
|
|
'--path' => $path,
|
|
'--force' => true
|
|
));
|
|
} catch (\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|