Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Version 0.14.0 #179

Merged
merged 20 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"mcamara/laravel-localization": "^1.8",
"xenolope/quahog": "^3.0",
"firebase/php-jwt": "^6.3",
"intervention/image": "^2.7",
"intervention/image": "^3.0",
"php": "^8.1",
"doctrine/dbal": "^3.6"
},
Expand Down
11 changes: 11 additions & 0 deletions config/siteboss.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@

'frontend_api_prefix' => env('SB_FRONTEND_API_PREFIX', 'api'),

/*
|--------------------------------------------------------------------------
| CMS importer
|--------------------------------------------------------------------------
|
| Do you want to retain the database id's for tables and table items.
|
*/

'export_retain_ids' => env('SB_EXPORT_RETAIN_IDS', false),

];
28 changes: 28 additions & 0 deletions database/migrations/2024_01_02_142937_update_cms_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('cms_config', function (Blueprint $table) {
$table->renameColumn('rights', 'editable');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('cms_config', function (Blueprint $table) {
$table->renameColumn('editable', 'rights');
});
}
};
31 changes: 31 additions & 0 deletions database/migrations/2024_01_10_122234_update_cms_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('cms_user', function (Blueprint $table) {
$table->string('sub')->nullable()->unique()->change();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};
2 changes: 1 addition & 1 deletion phpinsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'requirements' => [
'min-quality' => 60,
'min-complexity' => 60,
'min-architecture' => 60,
'min-architecture' => 55,
'min-style' => 80,
],
];
4 changes: 3 additions & 1 deletion routes/cms/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

Route::get('', [CmsEditorController::class, 'index']);

// import/export
Route::post('table-export', [CmsEditorImportExportController::class, 'exportAllTables']);

// table
Route::prefix('table')->group(function () {
Route::get('', [CmsEditorTableController::class, 'index']);
Expand All @@ -20,7 +23,6 @@
Route::prefix('{table}')->group(function () {
Route::get('', [CmsEditorTableController::class, 'readOne']);
Route::post('', [CmsEditorTableController::class, 'update']);
Route::post('import', [CmsEditorImportExportController::class, 'importTable']);

Route::put('move', [CmsEditorTableController::class, 'updatePosition']);
Route::post('add-field', [CmsEditorTableController::class, 'addField']);
Expand Down
9 changes: 9 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use NotFound\Framework\Helpers\CmsImportHelper;
use NotFound\Framework\Services\Indexer\IndexBuilderService;

Artisan::command('siteboss:index-site {--debug : Whether debug messages should be displayed} {--clean : Truncate search table}', function ($debug, $clean) {
Expand All @@ -11,3 +12,11 @@

return Command::SUCCESS;
})->purpose('Index site for local search');

Artisan::command('siteboss:cms-import {--debug : Whether debug messages should be displayed} {--dry : Dry Run}', function ($debug, $dry) {

$indexer = new CmsImportHelper($debug, $dry);
$indexer->import();

return Command::SUCCESS;
})->purpose('Import CMS changes to the database');
172 changes: 172 additions & 0 deletions src/Helpers/CmsImportHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace NotFound\Framework\Helpers;

use File;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use NotFound\Framework\Models\Table;
use NotFound\Framework\Models\TableItem;

class CmsImportHelper
{
public function __construct(
private bool $debug = false,
private bool $dryRun = false
) {
}

public function import(): void
{
$this->debug('Starting CMS Import');
if ($this->dryRun) {
$this->debug('Dry Run: true', force: true);
}

$this->importTables('cms_users');
$this->debug('DONE');
}

public function hasChanges(Table $table): bool
{
$path = resource_path('siteboss/tables/'.$table->table.'.json');
if (! File::exists($path)) {
return false;
}
$data = $table->exportToObject();
$fileData = json_decode(file_get_contents($path));

return $data != $fileData;
}

private function importTables(string $tableName): object
{
$path = resource_path('siteboss/tables');
if (! File::exists($path)) {
$this->debug('No export files found in '.$path);

return (object) [];
}

// Create temp table to import into
$this->createImportTables();

$fileSources = [];
$files = File::files($path);
foreach ($files as $file) {
$index = str_replace('.json', '', $file->getFilename());
$fileSources[$index] = json_decode(file_get_contents($file->getPathname()));
}
$this->debug('== Read '.count($fileSources).' files from '.$path);

$order = 1;
foreach ($fileSources as $tableName => $fileSource) {
if ($this->dryRun) {
$this->debug('CREATE TABLE '.$tableName);
} else {

$table = new Table();
if (isset($fileSource->id)) {
$table->id = $fileSource->id;
}
$table->name = $fileSource->name;
$table->url = $fileSource->url;
$table->rights = $fileSource->rights;

$table->comments = $fileSource->comments;
$table->allow_create = $fileSource->allow_create;
$table->allow_delete = $fileSource->allow_delete;
$table->allow_sort = $fileSource->allow_sort;
$table->properties = $fileSource->properties;
$table->order = $order++;
$table->enabled = $fileSource->enabled;
$table->table = $tableName;
$table->save();

$tableId = $table->id;
}
$itemOrder = 1;
foreach ($fileSource->items as $item) {
if ($this->dryRun) {
$this->debug(' [x] '.$item->name);

continue;
}
$tableItem = new TableItem();
if (isset($item->id)) {
$tableItem->id = $item->id;
}
$tableItem->table_id = $tableId;
$tableItem->name = $item->name;
$tableItem->type = $item->type;
$tableItem->internal = $item->internal;
$tableItem->description = $item->description;
$tableItem->properties = $item->properties;
$tableItem->server_properties = $item->server_properties;
$tableItem->order = $itemOrder++;
$tableItem->enabled = $item->enabled;
$tableItem->rights = $item->rights;

$tableItem->save();

}

}

return (object) [];
}

private function debug($text, $force = false)
{
if ($this->debug || $force) {
printf("\n - %s", $text);
}
}

private function createImportTables(): void
{
if ($this->dryRun) {
$this->debug('Dry run: skipping table creation');

return;
}
$this->debug('Creating import tables');
Schema::dropIfExists('cms_table_backup');
Schema::rename('cms_table', 'cms_table_backup');
Schema::create('cms_table', function (Blueprint $table) {
$table->id();
$table->string('name', 128)->nullable();
$table->string('table', 128)->nullable();
$table->string('url', 128)->nullable();
$table->string('rights', 128)->nullable();
$table->text('comments')->nullable();
$table->boolean('allow_create')->default(true);
$table->boolean('allow_delete')->default(true);
$table->boolean('allow_sort')->default(true);
$table->json('properties')->nullable();
$table->integer('order')->nullable();
$table->tinyInteger('enabled')->default(1);
$table->softDeletes();
$table->timestamps();
});

Schema::dropIfExists('cms_tableitem_backup');
Schema::rename('cms_tableitem', 'cms_tableitem_backup');
Schema::create('cms_tableitem', function (Blueprint $table) {
$table->id();
$table->string('rights', 128)->default('');
$table->foreignIdFor(Table::class, 'table_id')->nullable();
$table->string('type', 64)->nullable();
$table->string('internal', 64)->nullable();
$table->string('name', 128)->nullable();
$table->text('description')->nullable();
$table->json('properties')->nullable();
$table->json('server_properties')->nullable();
$table->integer('order')->nullable(); //TODO: FIX
$table->tinyInteger('enabled')->nullable()->default(1);
$table->softDeletes();
$table->timestamps();
});

}
}
Loading
Loading