Skip to content
Open
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"@ckeditor/ckeditor5-vue": "^1.0.3",
"@fortawesome/fontawesome-free": "^5.12.0",
"@fortawesome/fontawesome-svg-core": "^1.2.0",
"@fortawesome/free-brands-svg-icons": "^5.12.0",
"@fortawesome/free-regular-svg-icons": "^5.12.0",
"@fortawesome/free-solid-svg-icons": "^5.12.0",
"@fortawesome/free-brands-svg-icons": "^5.15.4",
"@fortawesome/free-regular-svg-icons": "^5.15.4",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/vue-fontawesome": "^0.1.0",
"@popperjs/core": "^2.0.6",
"@shopify/draggable": "^1.0.0-beta.8",
Expand Down
2 changes: 1 addition & 1 deletion public/css/gravity.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/js/chunks/1505.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/gravity.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"/js/gravity.js": "/js/gravity.js?id=2b068722e2a1f6b4c4c5",
"/css/gravity.css": "/css/gravity.css?id=f4312103778cfc5c63f0",
"/js/gravity.js": "/js/gravity.js?id=854e62944ae6ef28d5df",
"/css/gravity.css": "/css/gravity.css?id=865176a63047b81aa599",
"/img/audio-large.svg": "/img/audio-large.svg?id=fca6a67c7ef06d00ef4a",
"/img/audio-small.svg": "/img/audio-small.svg?id=48f5a5c5ff1cfd2cb375",
"/img/document-large.svg": "/img/document-large.svg?id=5c7cacec26ee17c609d5",
Expand Down
4 changes: 2 additions & 2 deletions resources/js/components/Fieldtypes/Replicator/Settings.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section-builder v-model="settings.sections"></section-builder>
<field-builder v-model="settings.sections"></field-builder>
</template>

<script>
Expand All @@ -15,7 +15,7 @@
if (this.settings.replicator) {
axios.get(`/api/replicators/${this.settings.replicator}`)
.then((response) => {
this.$set(this.settings, 'sections', response.data.data.sections)
this.$set(this.settings, 'sections', response.data.data.sections[0].fields)
})
} else {
this.settings.sections = []
Expand Down
50 changes: 33 additions & 17 deletions src/Fieldtypes/ReplicatorFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public function persistRelationship($model, Field $field)
$model->{$handle}()->attach($attached);
});
}

public function destroyRelationship($model, Field $field)
{
$replicator = Replicator::find($field->settings['replicator']);
$sections = $replicator->sections;
$sections->each(function ($section) use ($model, $replicator, $field) {
$handle = "rp_{$section->handle}_{$replicator->uniqid}";
$model->{$handle}()->wherePivot('section_id', $section->id)->detach();
});
}

/**
* Get custom rules when saving field.
Expand All @@ -196,16 +206,20 @@ public function rules(Field $field, $value = null)
{
$rules = [];

foreach ($value as $key => $input) {
$section = Section::find($input['section']['id']);
$prefix = "{$field->handle}.{$key}.fields.";
if (isset($value)) {
foreach ($value as $key => $input) {
$section = Section::find($input['section']['id']);
$prefix = "{$field->handle}.{$key}.fields.";

foreach ($section->fields as $sub) {
$rule = $sub->type()->rules($sub, $value[$key]['fields'][$sub->handle]);
$handle = key($rule);
$validation = current($rule);
foreach ($section->fields as $sub) {
$rule = $sub->type()->rules($sub, $value[$key]['fields'][$sub->handle]);
$handle = key($rule);
$validation = current($rule);

$rules[$prefix.$handle] = $validation;
if ($validation !== false) {
$rules[$prefix.$handle] = $validation;
}
}
}
}

Expand All @@ -224,12 +238,14 @@ public function attributes(Field $field, $value = null)
{
$attributes = [];

foreach ($value as $key => $input) {
$section = Section::find($input['section']['id']);
$prefix = "{$field->handle}.{$key}.fields.";
if (isset($value)) {
foreach ($value as $key => $input) {
$section = Section::find($input['section']['id']);
$prefix = "{$field->handle}.{$key}.fields.";

foreach ($section->fields as $sub) {
$attributes[$prefix.$sub->handle] = $sub->name;
foreach ($section->fields as $sub) {
$attributes[$prefix.$sub->handle] = $sub->name;
}
}
}

Expand Down Expand Up @@ -283,11 +299,11 @@ private function persistReplicants(Replicator $replicator, Field $field)
/**
* Merge replicator field into Request object.
*/
request()->merge([
$field->handle => $input['fields'][$field->handle],
]);
// request()->merge([
// $field->handle => $input['fields'][$field->handle],
// ]);

$field->type()->persistRelationship($replicant, $field);
$field->type()->persistRelationship($replicant, $field, $input['fields'][$field->handle] ?? null);
}
});

Expand Down
13 changes: 13 additions & 0 deletions src/Models/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Fusion\Models;

use Fusion\Concerns\HasBlueprint;
use Fusion\Concerns\HasActivity;
use Fusion\Concerns\HasBuilder;
use Fusion\Contracts\Structure;
use Fusion\Database\Eloquent\Model;
use Fusion\Services\Builders;

class Replicator extends Model implements Structure
{
use HasBlueprint;
use HasBuilder;

/**
* The attributes that are fillable via mass assignment.
Expand All @@ -20,6 +23,8 @@ class Replicator extends Model implements Structure

protected $structure = 'Replicators';

protected $blueprintHidden = true;

/**
* @param \Fusion\Models\Section $section
*
Expand Down Expand Up @@ -49,4 +54,12 @@ public function field()
{
return $this->belongsTo(Field::class);
}

public function getBuilderTable($section)
{
$namespace = $this->getBuilderNamespace();
$prefix = $namespace::prefix();

return "{$prefix}_{$this->handle}_{$section->handle}_{$this->uniqid}";
}
}
2 changes: 1 addition & 1 deletion src/Models/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public function blueprint()
*/
public function getBuilderTable()
{
return $this->blueprint->blueprintable->getBuilderTable();
return $this->blueprint->blueprintable->getBuilderTable($this);
}
}
60 changes: 24 additions & 36 deletions src/Observers/ReplicatorObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Fusion\Observers;

use Fusion\Models\Field;
use Fusion\Models\Fieldset;
use Fusion\Models\Blueprint as FusionBlueprint;
use Fusion\Models\Replicator;
use Fusion\Models\Section;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -39,7 +39,7 @@ public function saved(Replicator $replicator)
$this->suffix = $replicator->uniqid;

$replicator->withoutEvents(function () use ($replicator) {
if ($replicator->fieldset == null) {
if ($replicator->sections == null || $replicator->sections->isEmpty()) {
$this->createFieldset($replicator);
} else {
$this->updateFieldset($replicator);
Expand Down Expand Up @@ -73,14 +73,8 @@ public function deleted(Replicator $replicator)
*/
private function createFieldset(Replicator $replicator)
{
$fieldset = $replicator->fieldsets()->create([
'name' => ($name = "Replicator: {$replicator->name}"),
'handle' => str_handle("{$replicator->name}_{$replicator->uniqid}"),
'hidden' => true,
]);

$this->createSections(
$fieldset,
$replicator->blueprint,
collect($replicator->field->settings['sections'])
);
}
Expand All @@ -94,18 +88,14 @@ private function createFieldset(Replicator $replicator)
*/
private function updateFieldset(Replicator $replicator)
{
$fieldset = $replicator->fieldset;
$fieldset->update([
'name' => ($name = "Replicator: {$replicator->name}"),
'handle' => str_handle("{$replicator->name}_{$replicator->uniqid}"),
]);
$blueprint = $replicator->blueprint;

$sections = collect($replicator->field->settings['sections']);
$existing = $fieldset->sections->pluck('id');
$existing = $blueprint->sections->pluck('id');

$this->deleteSections($fieldset, $this->getDetachedItems($existing, $sections));
$this->updateSections($fieldset, $this->getUpdatedItems($sections));
$this->createSections($fieldset, $this->getAttachedItems($sections));
$this->deleteSections($blueprint, $this->getDetachedItems($existing, $sections));
$this->updateSections($blueprint, $this->getUpdatedItems($sections));
$this->createSections($blueprint, $this->getAttachedItems($sections));
}

/**
Expand All @@ -132,20 +122,18 @@ private function deleteFieldset(Replicator $replicator)
*
* @return void
*/
private function createSections(Fieldset $fieldset, Collection $toCreate)
private function createSections(FusionBlueprint $blueprint, Collection $toCreate)
{
$toCreate->each(function ($data, $index) use ($fieldset) {
$section = $fieldset->sections()->create([
'name' => $data['name'],
'handle' => $data['handle'],
'description' => $data['description'],
'placement' => $data['placement'],
'order' => ($index + 1),
]);
$section = $blueprint->sections()->create([
'name' => 'General',
'handle' => 'general',
'description' => null,
'placement' => 'body',
'order' => 0,
]);

$this->createReplicantTable($section);
$this->createFields($section, collect($data['fields']));
});
$this->createReplicantTable($section);
$this->createFields($section, $toCreate);
}

/**
Expand All @@ -156,10 +144,10 @@ private function createSections(Fieldset $fieldset, Collection $toCreate)
*
* @return void
*/
private function updateSections(Fieldset $fieldset, Collection $toUpdate)
private function updateSections(FusionBlueprint $blueprint, Collection $toUpdate)
{
$toUpdate->each(function ($data, $index) use ($fieldset) {
$newSection = $fieldset->sections()->find($data['id']);
$toUpdate->each(function ($data, $index) use ($blueprint, $toUpdate) {
$newSection = $blueprint->sections()->find($data['id']);
$oldSection = $newSection->replicate();
$newSection->update([
'name' => $data['name'],
Expand Down Expand Up @@ -189,9 +177,9 @@ private function updateSections(Fieldset $fieldset, Collection $toUpdate)
*
* @return void
*/
private function deleteSections(Fieldset $fieldset, Collection $toDelete)
private function deleteSections(FusionBlueprint $blueprint, Collection $toDelete)
{
$sections = $fieldset->sections()->whereIn('id', $toDelete);
$sections = $blueprint->sections()->whereIn('id', $toDelete);
$sections->each(function ($section) {
$this->deleteFields($section, $section->fields->pluck('id'));
$this->deleteReplicantTable($section);
Expand All @@ -213,7 +201,7 @@ private function createFields(Section $section, Collection $toCreate)
$field = $section->fields()->create([
'name' => $data['name'],
'handle' => $data['handle'],
'help' => $data['help'],
'help' => $data['help'] ?? null,
'settings' => $data['settings'],
'type' => is_string($data['type']) ? $data['type'] : $data['type']['handle'],
'order' => ($index + 1),
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function getPlaceholders()
*
* @return string
*/
private function getBuildTable()
protected function getBuildTable()
{
$prefix = static::prefix();

Expand Down
13 changes: 12 additions & 1 deletion src/Services/Builders/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,24 @@ protected function getPlaceholders()
];
}

protected function getBuildTable()
{
$prefix = static::prefix();

if (is_null($prefix)) {
$prefix = Str::lower($this->source->getClassName());
}

return "{$prefix}_{$this->handle}";
}

/**
* Builder table prefix.
*
* @var string
*/
public static function prefix()
{
return 'replicator';
return 'rp';
}
}