Skip to content

Commit

Permalink
Support multi-browsers in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ifox committed Nov 19, 2019
1 parent c7b4bf2 commit f6cea4e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/Models/Behaviors/HasRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public function getRelated($browser_name)

public function loadRelated($browser_name)
{
$this->load('relatedItems');
if (!isset($this->relatedItems)) {
$this->load('relatedItems');
}

return $this->relatedCache[$browser_name] = $this->relatedItems
->where('browser_name', $browser_name)
Expand All @@ -36,7 +38,7 @@ public function loadRelated($browser_name)
});
}

public function sync($items, $browser_name)
public function saveRelated($items, $browser_name)
{
RelatedItem::where([
'browser_name' => $browser_name,
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use A17\Twill\Models\Behaviors\HasFiles;
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Models\Behaviors\HasPresenter;
use A17\Twill\Models\Behaviors\HasRelated;
use Illuminate\Database\Eloquent\Model as BaseModel;

class Block extends BaseModel
{
use HasMedias, HasFiles, HasPresenter;
use HasMedias, HasFiles, HasPresenter, HasRelated;

public $timestamps = false;

Expand Down
2 changes: 1 addition & 1 deletion src/Models/RelatedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function subject()

public function getTable()
{
return 'related';
return config('twill.related_table', 'related');
}
}
40 changes: 22 additions & 18 deletions src/Repositories/Behaviors/HandleBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Repositories\BlockRepository;
use Illuminate\Support\Collection;
use Schema;

trait HandleBlocks
{
Expand Down Expand Up @@ -248,26 +249,29 @@ public function getFormFieldsHandleBlocks($object, $fields)
protected function getBlockBrowsers($block)
{
return Collection::make($block['content']['browsers'])->mapWithKeys(function ($ids, $relation) use ($block) {
$relationRepository = $this->getModelRepository($relation);
$relatedItems = $relationRepository->get([], ['id' => $ids], [], -1);
$sortedRelatedItems = array_flip($ids);
if (Schema::hasTable(config('twill.related_table', 'related')) && $block->getRelated($relation)->isNotEmpty()) {
$items = $this->getFormFieldsForRelatedBrowser($block, $relation);;
} else {
$relationRepository = $this->getModelRepository($relation);
$relatedItems = $relationRepository->get([], ['id' => $ids], [], -1);
$sortedRelatedItems = array_flip($ids);

foreach ($relatedItems as $item) {
$sortedRelatedItems[$item->id] = $item;
}

foreach ($relatedItems as $item) {
$sortedRelatedItems[$item->id] = $item;
$items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) {
return is_object($value);
})->map(function ($relatedElement) use ($relation) {
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
'edit' => moduleRoute($relation, config('twill.block_editor.browser_route_prefixes.' . $relation), 'edit', $relatedElement->id),
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
})->toArray();
}

$items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) {
return is_object($value);
})->map(function ($relatedElement) use ($relation) {
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
'edit' => moduleRoute($relation, config('twill.block_editor.browser_route_prefixes.' . $relation), 'edit', $relatedElement->id),
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
})->toArray();

return [
"blocks[$block->id][$relation]" => $items,
];
Expand Down
44 changes: 44 additions & 0 deletions src/Repositories/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use A17\Twill\Repositories\Behaviors\HandleMedias;
use Illuminate\Config\Repository as Config;
use Illuminate\Support\Collection;
use Log;
use ReflectionException;
use Schema;

class BlockRepository extends ModuleRepository
{
Expand Down Expand Up @@ -37,14 +40,55 @@ public function getCrops($role)
return $this->config->get('twill.block_editor.crops')[$role];
}

public function hydrate($object, $fields)
{
if (Schema::hasTable(config('twill.related_table', 'related'))) {
$relatedItems = Collection::make();

Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object, &$relatedItems) {
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
try {
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
$relatedItems->push((object) [
'related' => $repository->getById($item['id']),
'browser_name' => $browserName,
]);

} catch (ReflectionException $e) {
Log::error($e);
}
});
});

$object->setRelation('relatedItems', $relatedItems);
}

return parent::hydrate($object, $fields);
}

/**
* @param HasMedias|HasFiles $object
* @return void
*/
public function afterSave($object, $fields)
{
if (Schema::hasTable(config('twill.related_table', 'related'))) {
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object) {
$object->saveRelated($items, $browserName);
});
}

parent::afterSave($object, $fields);
}

public function afterDelete($object)
{
$object->medias()->sync([]);
$object->files()->sync([]);

if (Schema::hasTable(config('twill.related_table', 'related'))) {
$object->relatedItems()->delete();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ public function updateBrowser($object, $fields, $relationship, $positionAttribut
*/
public function updateRelatedBrowser($object, $fields, $browserName)
{
$object->sync($fields['browsers'][$browserName] ?? [], $browserName);
$object->saveRelated($fields['browsers'][$browserName] ?? [], $browserName);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion views/partials/form/_browser.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
@php
$name = $name ?? $moduleName;
$label = $label ?? 'Missing browser label';
$endpoints = $endpoints ?? [];
$endpointsFromModules = isset($modules) ? collect($modules)->map(function ($module) {
return [
'label' => $module['label'] ?? ucfirst($module['name']),
'value' => moduleRoute($module['name'], $module['routePrefix'] ?? null, 'browser', $module['params'] ?? [], false)
];
})->toArray() : null;
$endpoints = $endpoints ?? $endpointsFromModules ?? [];
$endpoint = $endpoint ?? (!empty($endpoints) ? null : moduleRoute($moduleName, $routePrefix ?? null, 'browser', $params ?? [], false));
$max = $max ?? 1;
$note = $note ?? 'Add' . ($max > 1 ? " up to $max ". strtolower($label) : ' one ' . str_singular(strtolower($label)));
$itemLabel = $itemLabel ?? strtolower($label);
Expand Down

0 comments on commit f6cea4e

Please sign in to comment.