Skip to content

Commit

Permalink
Merge branch '3.x' into fix/error-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel authored Jul 2, 2024
2 parents 6d5c1ba + 9b421e8 commit df84398
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Models/Behaviors/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function files()
File::class,
'fileable',
config('twill.fileables_table', 'twill_fileables')
)->withPivot(['role', 'locale'])
)->withPivot(['id', 'role', 'locale'])
->withTimestamps()->orderBy(config('twill.fileables_table', 'twill_fileables') . '.id', 'asc');
}

Expand Down
1 change: 1 addition & 0 deletions src/Models/Behaviors/HasMedias.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function medias()
'mediable',
config('twill.mediables_table', 'twill_mediables')
)->withPivot([
'id',
'crop',
'role',
'crop_w',
Expand Down
21 changes: 13 additions & 8 deletions src/Repositories/Behaviors/HandleFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace A17\Twill\Repositories\Behaviors;

use A17\Twill\Facades\TwillUtil;
use A17\Twill\Models\Behaviors\HasFiles;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\File;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function hydrateHandleFiles($object, $fields)
}

/**
* @param \A17\Twill\Models\Model $object
* @param \A17\Twill\Models\Model|HasFiles $object
* @param array $fields
* @return void
*/
Expand All @@ -47,7 +48,7 @@ public function afterSaveHandleFiles($object, $fields)
return;
}

$object->files()->sync($this->getFiles($fields));
TwillUtil::syncUsingPrimaryKey($object->files(), $this->getFiles($fields));
}

/**
Expand All @@ -73,11 +74,11 @@ private function getFiles($fields)
|| in_array($role, config('twill.block_editor.files', []))
) {
Collection::make($filesForRole)->each(function ($file) use (&$files, $role, $locale) {
$files->push([
$files[$file['pivot_id'] ?? uniqid('file')] = [
'file_id' => $file['id'],
'role' => $role,
'locale' => $locale,
]);
];
});
}
}
Expand All @@ -97,8 +98,8 @@ public function getFormFieldsHandleFiles($object, $fields)
if ($object->has('files')) {
foreach ($object->files->groupBy('pivot.role') as $role => $filesByRole) {
foreach ($filesByRole->groupBy('pivot.locale') as $locale => $filesByLocale) {
$fields['files'][$locale][$role] = $filesByLocale->map(function ($file) {
return $file->toCmsArray();
$fields['files'][$locale][$role] = $filesByLocale->map(function (File $file) {
return $file->toCmsArray() + ['pivot_id' => $file->pivot->id];
});
}
}
Expand All @@ -107,9 +108,13 @@ public function getFormFieldsHandleFiles($object, $fields)
return $fields;
}

/**
* @param HasFiles|TwillModelContract $object
* @param HasFiles|TwillModelContract $newObject
*/
public function afterDuplicateHandleFiles(TwillModelContract $object, TwillModelContract $newObject): void
{
$newObject->files()->sync(
$newObject->files()->attach(
$object->files->mapWithKeys(function ($file) use ($object) {
return [
$file->id => Collection::make($object->files()->getPivotColumns())->mapWithKeys(
Expand Down
16 changes: 10 additions & 6 deletions src/Repositories/Behaviors/HandleMedias.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace A17\Twill\Repositories\Behaviors;

use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Facades\TwillUtil;
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Media;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -43,7 +45,7 @@ public function hydrateHandleMedias($object, $fields)
}

/**
* @param \A17\Twill\Models\Model $object
* @param \A17\Twill\Models\Model|HasMedias $object
* @param array $fields
* @return void
*/
Expand All @@ -53,7 +55,7 @@ public function afterSaveHandleMedias($object, $fields)
return;
}

$object->medias()->sync($this->getMedias($fields));
TwillUtil::syncUsingPrimaryKey($object->medias(), $this->getMedias($fields));
}

/**
Expand All @@ -80,7 +82,7 @@ private function getMedias($fields)
$customMetadatas = $media['metadatas']['custom'] ?? [];
if (isset($media['crops']) && !empty($media['crops'])) {
foreach ($media['crops'] as $cropName => $cropData) {
$medias->push([
$medias[$cropData['pivot_id'] ?? uniqid('media')] = [
'media_id' => $media['id'],
'crop' => $cropName,
'role' => $role,
Expand All @@ -91,11 +93,11 @@ private function getMedias($fields)
'crop_x' => $cropData['x'],
'crop_y' => $cropData['y'],
'metadatas' => json_encode($customMetadatas),
]);
];
}
} else {
foreach ($this->getCrops($role) as $cropName => $cropDefinitions) {
$medias->push([
$medias[$media['pivot_id'] ?? uniqid('media')] = [
'media_id' => $media['id'],
'crop' => $cropName,
'role' => $role,
Expand All @@ -106,7 +108,7 @@ private function getMedias($fields)
'crop_x' => null,
'crop_y' => null,
'metadatas' => json_encode($customMetadatas),
]);
];
}
}
});
Expand Down Expand Up @@ -157,12 +159,14 @@ private function getMediaFormItems($medias)
$item = $mediasById->first();

$itemForForm = $item->toCmsArray();
$itemForForm['pivot_id'] = $item->pivot->id;

$itemForForm['metadatas']['custom'] = json_decode($item->pivot->metadatas, true);

foreach ($mediasById->groupBy('pivot.crop') as $crop => $mediaByCrop) {
$media = $mediaByCrop->first();
$itemForForm['crops'][$crop] = [
'pivot_id' => $media->pivot->id,
'name' => $media->pivot->ratio,
'width' => $media->pivot->crop_w,
'height' => $media->pivot->crop_h,
Expand Down
6 changes: 4 additions & 2 deletions src/Repositories/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill\Repositories;

use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Models\Block;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Repositories\Behaviors\HandleFiles;
use A17\Twill\Repositories\Behaviors\HandleMedias;
Expand Down Expand Up @@ -75,10 +76,11 @@ public function afterSave(TwillModelContract $model, array $fields): void
parent::afterSave($model, $fields);
}

/** @param Block $object */
public function afterDelete(TwillModelContract $object): void
{
$object->medias()->sync([]);
$object->files()->sync([]);
$object->medias()->detach();
$object->files()->detach();

if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$object->clearAllRelated();
Expand Down
52 changes: 52 additions & 0 deletions src/TwillUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill;

use A17\Twill\Models\Contracts\TwillLinkableModel;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Session;

Expand Down Expand Up @@ -103,4 +104,55 @@ private function pushToTempStore(string $key, int $frontendId, int $dbId): void

Session::put(self::SESSION_FIELD, $sessionData);
}

public function syncUsingPrimaryKey(BelongsToMany $relation, $ids, $detaching = true): array
{
return (function ($ids, $detaching = true) {
$changes = [
'attached' => [], 'detached' => [], 'updated' => [],
];

// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->getCurrentlyAttachedPivots()
->pluck('id')->all();

$records = $this->formatRecordsList($this->parseIds($ids));

// Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the
// array of the new IDs given to the method which will complete the sync.
if ($detaching) {
$detach = array_diff($current, array_keys($records));

if (count($detach) > 0) {
$this->newPivotQuery()->whereIn('id', $detach)->delete();

$changes['detached'] = $this->castKeys($detach);
}
}

// 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)
);

// 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
// relationships if they are configured to touch on any database updates.
if (
count($changes['attached']) ||
count($changes['updated']) ||
count($changes['detached'])
) {
$this->touchIfTouching();
}

return $changes;
})->call($relation, $ids, $detaching);
}
}

0 comments on commit df84398

Please sign in to comment.