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

Allow positioning media files #2694

Merged
merged 5 commits into from
Dec 4, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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
{
$twillMediablesTable = config('twill.mediables_table', 'twill_mediables');

if (!Schema::hasColumn($twillMediablesTable, 'position')) {
Schema::table($twillMediablesTable, function (Blueprint $table) {
$table->integer('position')->default(1);
});
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$twillMediablesTable = config('twill.mediables_table', 'twill_mediables');

Schema::table($twillMediablesTable, function (Blueprint $table) {
$table->dropColumn('position');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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
{
$twillFileablesTable = config('twill.fileables_table', 'twill_fileables');

if (!Schema::hasColumn($twillFileablesTable, 'position')) {
Schema::table($twillFileablesTable, function (Blueprint $table) {
$table->integer('position')->default(1);
});
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$twillFileablesTable = config('twill.fileables_table', 'twill_fileables');

Schema::table($twillFileablesTable, function (Blueprint $table) {
$table->dropColumn('position');
});
}
};
4 changes: 3 additions & 1 deletion src/Models/Behaviors/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function files()
'fileable',
config('twill.fileables_table', 'twill_fileables')
)->withPivot(['id', 'role', 'locale'])
->withTimestamps()->orderBy(config('twill.fileables_table', 'twill_fileables') . '.id', 'asc');
->withTimestamps()
->orderBy(config('twill.fileables_table', 'twill_fileables') . '.position', 'asc')
->orderBy(config('twill.fileables_table', 'twill_fileables') . '.id', 'asc');
}

private function findFile($role, $locale)
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Behaviors/HasMedias.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function medias()
'ratio',
'metadatas',
'locale',
])->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc');
])->withTimestamps()
->orderBy(config('twill.mediables_table', 'twill_mediables') . '.position')
->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id');
}

private function findMedia($role, $crop = 'default')
Expand Down
3 changes: 2 additions & 1 deletion src/Repositories/Behaviors/HandleFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ private function getFiles($fields)
in_array($role, $this->model->filesParams ?? [])
|| in_array($role, config('twill.block_editor.files', []))
) {
Collection::make($filesForRole)->each(function ($file) use (&$files, $role, $locale) {
Collection::make($filesForRole)->each(function ($file, $index) use (&$files, $role, $locale) {
$files[$file['pivot_id'] ?? uniqid('file')] = [
'file_id' => $file['id'],
'role' => $role,
'locale' => $locale,
'position' => $index + 1
];
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/Repositories/Behaviors/HandleMedias.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function getMedias($fields)
$locale = $locale ?? config('app.locale');

if ($this->hasRole($role) || $this->hasJsonRepeaterRole($role)) {
Collection::make($mediasForRole)->each(function ($media) use (&$medias, $role, $locale) {
Collection::make($mediasForRole)->each(function ($media, $index) use (&$medias, $role, $locale) {
$customMetadatas = $media['metadatas']['custom'] ?? [];
if (isset($media['crops']) && !empty($media['crops'])) {
foreach ($media['crops'] as $cropName => $cropData) {
Expand All @@ -93,6 +93,7 @@ private function getMedias($fields)
'crop_x' => $cropData['x'],
'crop_y' => $cropData['y'],
'metadatas' => json_encode($customMetadatas),
'position' => $index + 1,
];
}
} else {
Expand All @@ -108,6 +109,7 @@ private function getMedias($fields)
'crop_x' => null,
'crop_y' => null,
'metadatas' => json_encode($customMetadatas),
'position' => $index + 1,
];
}
}
Expand Down
30 changes: 26 additions & 4 deletions src/TwillUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,32 @@ public function syncUsingPrimaryKey(BelongsToMany $relation, $ids, $detaching =
// Now we are finally ready to attach the new records. Note that we'll disable
// touching until after the entire operation is complete so we don't fire a
// ton of touch operations until we are totally done syncing the records.
$changes = array_merge(
$changes,
$this->attachNew($records, $current, false)
);
foreach ($records as $id => $attributes) {
// If the ID is not in the list of existing pivot IDs, we will insert a new pivot
// record, otherwise, we will just update this existing record on this joining
// table, so that the developers will easily update these records pain free.
if (!in_array($id, $current)) {
$this->attach($id, $attributes, false);

$changes['attached'][] = $this->castKey($id);
} elseif (count($attributes) > 0) {
// Now we'll try to update an existing pivot record with the attributes that were
// given to the method. If the model is actually updated we will add it to the
// list of updated pivot records so we return them back out to the consumer.
if ($this->hasPivotColumn($this->updatedAt())) {
$attributes = $this->addTimestampsToAttachment($attributes, true);
}

$updated = $this->newPivotQuery()->whereIn('id', $this->parseIds($id))->update(
$this->castAttributes($attributes)
);

if ($updated) {
$changes['updated'][] = $this->castKey($id);
}
}
}


// Once we have finished attaching or detaching the records, we will see if we
// have done any attaching or detaching, and if we have we will touch these
Expand Down
Loading