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

feature/FOUR-20311 Add Settings to a Bundle from the ellipsis #7811

Merged
merged 14 commits into from
Dec 10, 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
26 changes: 23 additions & 3 deletions ProcessMaker/Http/Controllers/Api/DevLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use ProcessMaker\Exception\ValidationException;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Http\Resources\ApiCollection;
use ProcessMaker\Jobs\DevLinkInstall;
use ProcessMaker\Exception\ValidationException;
use ProcessMaker\Models\Bundle;
use ProcessMaker\Models\BundleAsset;
use ProcessMaker\Models\BundleSetting;
use ProcessMaker\Models\DevLink;
use ProcessMaker\Models\Setting;

Expand Down Expand Up @@ -119,7 +120,7 @@ public function localBundles(Request $request)

public function showBundle(Bundle $bundle)
{
return $bundle->load('assets');
return $bundle->load('assets')->load('settings');
}

public function remoteBundles(Request $request, DevLink $devLink)
Expand All @@ -131,6 +132,7 @@ public function createBundle(Request $request)
{
$bundle = new Bundle();
$bundle->name = $request->input('name');
$bundle->description = $request->input('description');
$bundle->published = (bool) $request->input('published', false);
$bundle->version = 1;
$bundle->saveOrFail();
Expand All @@ -143,6 +145,7 @@ public function updateBundle(Request $request, Bundle $bundle)
$bundle->validateEditable();

$bundle->name = $request->input('name');
$bundle->description = $request->input('description');
$bundle->published = (bool) $request->input('published', false);
$bundle->saveOrFail();

Expand Down Expand Up @@ -200,6 +203,11 @@ public function exportLocalBundle(Bundle $bundle)
return ['payloads' => $bundle->export()];
}

public function exportLocalBundleSettings(Bundle $bundle)
{
return ['settings' => $bundle->exportSettings()];
}

public function exportLocalAsset(Request $request)
{
$asset = $request->input('class')::findOrFail($request->input('id'));
Expand All @@ -212,7 +220,12 @@ public function addAsset(Request $request, Bundle $bundle)
$asset = $request->input('type')::findOrFail($request->input('id'));
$bundle->addAsset($asset);
}


public function addSettings(Request $request, Bundle $bundle)
{
$bundle->addSettings($request->input('setting'), $request->input('config'));
}

public function addAssetToBundles(Request $request)
{
$bundles = $request->input('bundles');
Expand Down Expand Up @@ -290,4 +303,11 @@ public function deleteBundleAsset(BundleAsset $bundleAsset)

return response()->json(['message' => 'Bundle asset association deleted.'], 200);
}

public function deleteBundleSetting(BundleSetting $bundleSetting)
{
$bundleSetting->delete();

return response()->json(['message' => 'Bundle setting deleted.'], 200);
}
}
44 changes: 43 additions & 1 deletion ProcessMaker/Models/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function assets()
return $this->hasMany(BundleAsset::class);
}

public function settings()
{
return $this->hasMany(BundleSetting::class);
}

public function devLink()
{
return $this->belongsTo(DevLink::class, 'dev_link_id');
Expand Down Expand Up @@ -72,6 +77,13 @@ public function export()
return $exports;
}

public function exportSettings()
{
return $this->settings()->get()->map(function ($setting) {
return $setting->export();
});
}

public function syncAssets($assets)
{
$assetKeys = [];
Expand Down Expand Up @@ -125,7 +137,23 @@ public function addAsset(ProcessMakerModel $asset)
'asset_id' => $asset->id,
]);
}


public function addSettings($setting, $config)
{
$exists = $this->settings()->where('setting', $setting)->exists();
if ($exists) {
$this->settings()->where('setting', $setting)->update([
'config' => $config,
]);
} else {
BundleSetting::create([
'bundle_id' => $this->id,
'setting' => $setting,
'config' => $config,
]);
}
}

public function addAssetToBundles(ProcessMakerModel $asset)
{
$message = null;
Expand All @@ -134,6 +162,7 @@ public function addAssetToBundles(ProcessMakerModel $asset)
} catch (ValidationException $ve) {
$message = $ve->getMessage();
}

return $message;
}

Expand Down Expand Up @@ -184,6 +213,19 @@ public function savePayloadsToFile(array $payloads)
}
}

public function installSettings($settings)
{
$newSettingsKeys = collect($settings)->pluck('setting')->toArray();

$this->settings()
->whereNotIn('setting', $newSettingsKeys)
->delete();

foreach ($settings as $setting) {
$this->addSettings($setting['setting'], $setting['config']);
}
}

public function install(array $payloads, $mode, $logger = null)
{
if ($logger === null) {
Expand Down
50 changes: 48 additions & 2 deletions ProcessMaker/Models/BundleAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class BundleAsset extends ProcessMakerModel

protected $guarded = ['id'];

protected $appends = ['name', 'url', 'type'];
protected $appends = ['name', 'url', 'type', 'owner_name', 'categories'];

const DATA_SOURCE_CLASS = 'ProcessMaker\Packages\Connectors\DataSources\Models\DataSource';

const COLLECTION_CLASS = 'ProcessMaker\Plugins\Collections\Models\Collection';

const DECISION_TABLE_CLASS = 'ProcessMaker\Package\PackageDecisionEngine\Models\DecisionTable';

const FLOW_GENIE_CLASS = 'ProcessMaker\Package\PackageAi\Models\FlowGenie';

protected static function boot()
{
Expand Down Expand Up @@ -53,7 +61,7 @@ public function getNameAttribute()
$this->asset_type === Screen::class ||
$this->asset_type === Script::class
) {
return $this->title;
return $this->asset->title;
}

return $this->asset->name;
Expand All @@ -68,6 +76,14 @@ public function getUrlAttribute()
return "/designer/scripts/{$this->asset_id}/builder";
case Process::class:
return "/modeler/{$this->asset_id}";
case self::DATA_SOURCE_CLASS:
return "/designer/data-sources/{$this->asset_id}/edit";
case self::COLLECTION_CLASS:
return "/collections/{$this->asset_id}/edit";
case self::DECISION_TABLE_CLASS:
return "/designer/decision-tables/table-builder/{$this->asset_id}/edit";
case self::FLOW_GENIE_CLASS:
return "/designer/flow-genies/{$this->asset_id}/edit";
default:
return null;
}
Expand All @@ -82,8 +98,38 @@ public function getTypeAttribute()
return 'Script';
case Process::class:
return 'Process';
case self::DATA_SOURCE_CLASS:
return 'data_source';
case self::COLLECTION_CLASS:
return 'collection';
case self::DECISION_TABLE_CLASS:
return 'decision_table';
case self::FLOW_GENIE_CLASS:
return 'flow_genie';
default:
return null;
}
}

public function getOwnerNameAttribute()
{
if ($this->asset && method_exists($this->asset, 'user')) {
return $this->asset->user->firstname . ' ' . $this->asset->user->lastname;
}

return null;
}

public function getCategoriesAttribute()
{
if ($this->asset_type === self::COLLECTION_CLASS) {
return [];
}

if ($this->asset && method_exists($this->asset, 'categories')) {
return $this->asset->categories->pluck('name')->toArray();
}

return [];
}
}
34 changes: 34 additions & 0 deletions ProcessMaker/Models/BundleSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ProcessMaker\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use ProcessMaker\Models\ProcessMakerModel;

class BundleSetting extends ProcessMakerModel
{
use HasFactory;

protected $fillable = [
'bundle_id',
'setting',
'config',
];

protected $casts = [
'config' => 'array',
];

public function bundle()
{
return $this->belongsTo(Bundle::class);
}

public function export()
{
return [
'setting' => $this->setting,
'config' => $this->config,
];
}
}
6 changes: 5 additions & 1 deletion ProcessMaker/Models/DevLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public function installRemoteBundle($remoteBundleId, $updateType)
route('api.devlink.export-local-bundle', ['bundle' => $remoteBundleId], false)
)->json();

$bundleSettingsExport = $this->client()->get(
route('api.devlink.export-local-bundle-settings', ['bundle' => $remoteBundleId], false)
)->json();

$bundle = Bundle::updateOrCreate(
[
'remote_id' => $remoteBundleId,
Expand All @@ -156,7 +160,7 @@ public function installRemoteBundle($remoteBundleId, $updateType)
);

$bundle->install($bundleExport['payloads'], $updateType, $this->logger);

$bundle->installSettings($bundleSettingsExport['settings']);
$this->logger->setStatus('done');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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(): void
{
Schema::table('bundles', function (Blueprint $table) {
$table->text('description')->after('name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('bundles', function (Blueprint $table) {
$table->dropColumn('description');
});
}
};
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.
*/
public function up(): void
{
Schema::create('bundle_settings', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('bundle_id');
$table->string('setting');
$table->json('config')->nullable();
$table->timestamps();

$table->index(['bundle_id', 'setting']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('bundle_settings');
}
};
3 changes: 3 additions & 0 deletions devhub/pm-font/svg/edit-outline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions devhub/pm-font/svg/link-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions devhub/pm-font/svg/plus-thin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions devhub/pm-font/svg/remove-outlined.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading