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: CMS editor import/export #197

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 routes/cms/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

Route::post('template-export', [CmsEditorImportExportController::class, 'exportAllTemplates']);
// table
Route::prefix('table')->group(function () {
Route::get('', [CmsEditorTableController::class, 'index']);
Expand Down
4 changes: 2 additions & 2 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use NotFound\Framework\Helpers\CmsImportHelper;
use NotFound\Framework\Services\CmsExchange\ExchangeConsoleService;
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 @@ -15,7 +15,7 @@

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

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

return Command::SUCCESS;
Expand Down
18 changes: 18 additions & 0 deletions src/Http/Controllers/CmsEditor/CmsEditorImportExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use NotFound\Framework\Http\Controllers\Controller;
use NotFound\Framework\Models\Table;
use NotFound\Framework\Models\Template;
use NotFound\Layout\LayoutResponse;
use NotFound\Layout\Responses\Toast;

Expand All @@ -25,4 +26,21 @@ public function exportAllTables()

return $response->build();
}

public function exportAllTemplates()
{
$response = new LayoutResponse();
$templates = Template::all();

foreach ($templates as $template) {
// TODO: catch exceptions
$template->exportToFile();
}

$response->addAction(
new Toast($templates->count().' templates exported successfully')
);

return $response->build();
}
}
6 changes: 3 additions & 3 deletions src/Http/Controllers/CmsEditor/CmsEditorTableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use NotFound\Framework\Helpers\CmsImportHelper;
use NotFound\Framework\Http\Requests\FormDataRequest;
use NotFound\Framework\Models\Table;
use NotFound\Framework\Services\CmsExchange\TableExchangeService;
use NotFound\Framework\Services\Editor\FieldsProperties;
use NotFound\Layout\Elements\LayoutBreadcrumb;
use NotFound\Layout\Elements\LayoutButton;
Expand Down Expand Up @@ -121,8 +121,8 @@ public function readOne(Table $table)
$breadcrumbs->addItem($table->name ?? 'New table');
$page->addBreadCrumb($breadcrumbs);

$importHelper = new CmsImportHelper();
$hasChanges = $importHelper->hasChanges($table);
$tableExchangeService = new TableExchangeService();
$hasChanges = $tableExchangeService->hasChanges($table);

$widget1 = new LayoutWidget($table->name ?? 'New table', $hasChanges ? 12 : 6);

Expand Down
10 changes: 7 additions & 3 deletions src/Http/Controllers/CmsEditor/CmsEditorTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function index()
$form->addButton(new LayoutButton('Add new template'));
$widget2->addForm($form);

$form = new LayoutForm('/app/editor/template-export/');

$form->addInput((new LayoutInputCheckbox('name', 'I know this will overwrite the template files from my database'))->setRequired());

$form->addButton(new LayoutButton('Export all templates to files'));
$widget2->addForm($form);

$page->addWidget($widget2);

$response->addUIElement($page);
Expand Down Expand Up @@ -170,9 +177,6 @@ public function readOne(Template $table)

$page->addWidget($widget2);

$page->addWidget(CmsEditorImportExportController::getExport($tables));
$page->addWidget(CmsEditorImportExportController::getImport($table->id, 'page'));

$response->addUIElement($page);

return $response->build();
Expand Down
1 change: 0 additions & 1 deletion src/Models/Editor/AbstractEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abstract class AbstractEditor
{
public function __construct(protected TableService $ts)
{

}

public function getBar(): LayoutBar
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use NotFound\Framework\Services\Legacy\StatusColumn;
use NotFound\Framework\Traits\Exportable;
use NotFound\Framework\Traits\Exchangeable;

/**
* NotFound\Framework\Models\Table
Expand Down Expand Up @@ -56,7 +56,7 @@
*/
class Table extends AssetModel
{
use Exportable;
use Exchangeable;
use SoftDeletes;

protected $table = 'cms_table';
Expand Down
10 changes: 9 additions & 1 deletion src/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use NotFound\Framework\Traits\Exchangeable;

/**
* NotFound\Framework\Models\Template
Expand Down Expand Up @@ -50,7 +51,9 @@
*/
class Template extends AssetModel
{
use HasFactory, SoftDeletes;
use HasFactory;
use SoftDeletes;
use Exchangeable;

protected $table = 'cms_template';

Expand Down Expand Up @@ -79,4 +82,9 @@ public function getIdentifier()
{
return strtolower($this->attributes['filename']);
}

private function getSiteTableName()
{
return strtolower($this->filename);
}
}
37 changes: 37 additions & 0 deletions src/Services/CmsExchange/AbstractExchangeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace NotFound\Framework\Services\CmsExchange;

abstract class AbstractExchangeService
{
protected bool $debug;

protected bool $dryRun;

protected string $exportTypeName;

abstract public function runImport(): void;

abstract public function exportRetainIds(): bool;

abstract public function exportTypeName(): string;

public function exportTypeNamePlural(): string
{
return $this->exportTypeName().'s';
}

public function import(bool $debug = false, bool $dryRun = false): void
{
$this->debug = $debug;
$this->dryRun = $dryRun;
$this->runImport();
}

protected function debug($text, $force = false)
{
if ($this->debug || $force) {
printf("\n - %s", $text);
}
}
}
34 changes: 34 additions & 0 deletions src/Services/CmsExchange/ExchangeConsoleService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NotFound\Framework\Services\CmsExchange;

class ExchangeConsoleService
{
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);
}

$tableExchangeService = new TableExchangeService();
$tableExchangeService->import($this->debug, $this->dryRun);
$templateExchangeService = new TemplateExchangeService();
$templateExchangeService->import($this->debug, $this->dryRun);

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

private function debug($text, $force = false)
{
if ($this->debug || $force) {
printf("\n - %s", $text);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<?php

namespace NotFound\Framework\Helpers;
namespace NotFound\Framework\Services\CmsExchange;

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

class CmsImportHelper
class TableExchangeService extends AbstractExchangeService
{
public function __construct(
private bool $debug = false,
private bool $dryRun = false
) {
}
protected string $exportTypeName = 'table';

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

$this->importTables('cms_users');
$this->debug('DONE');
$this->debug('Starting CMS Table Import');
$this->importTables();
}

public function hasChanges(Table $table): bool
Expand All @@ -39,7 +30,7 @@ public function hasChanges(Table $table): bool
return $data != $fileData;
}

private function importTables(string $tableName): object
private function importTables(): object
{
$path = resource_path('siteboss/tables');
if (! File::exists($path)) {
Expand Down Expand Up @@ -117,13 +108,6 @@ private function importTables(string $tableName): object
return (object) [];
}

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

private function createImportTables(): void
{
if ($this->dryRun) {
Expand Down Expand Up @@ -171,4 +155,14 @@ private function createImportTables(): void
});

}

public function exportTypeName(): string
{
return 'table';
}

public function exportRetainIds(): bool
{
return config('siteboss.export_retain_ids') ?? false;
}
}
Loading
Loading