Skip to content

Instantly share code, notes, and snippets.

@noxify
Last active December 17, 2018 17:26
Show Gist options
  • Save noxify/237b7ee4b5cecffa1feaf5fb5be033fc to your computer and use it in GitHub Desktop.
Save noxify/237b7ee4b5cecffa1feaf5fb5be033fc to your computer and use it in GitHub Desktop.
<?php
namespace App\Repositories;
use A17\Twill\Repositories\Behaviors\HandleBlocks;
use A17\Twill\Repositories\Behaviors\HandleSlugs;
use A17\Twill\Repositories\ModuleRepository;
use App\Models\Client;
use A17\Twill\Repositories\BlockRepository;
class ClientRepository extends ModuleRepository
{
use HandleBlocks, HandleSlugs;
public function __construct(Client $model)
{
$this->model = $model;
}
public function getFormFieldsHandleBlocks($object, $fields)
{
$fields['blocks'] = null;
if ($object->has('blocks')) {
$blocksConfig = config('twill.block_editor');
foreach ($object->blocks as $block) {
$isInRepeater = isset($block->parent_id);
$configKey = $isInRepeater ? 'repeaters' : 'blocks';
$blockTypeConfig = $blocksConfig[$configKey][$block->type] ?? null;
if (is_null($blockTypeConfig)) {
continue;
}
$blockItem = [
'id' => $block->id,
'type' => $blockTypeConfig['component'],
'title' => ($this->getCustomBrowserTitle($block)['name']) ?? $blockTypeConfig['title'],
'attributes' => $blockTypeConfig['attributes'] ?? [],
];
if ($isInRepeater) {
$fields['blocksRepeaters']["blocks-{$block->parent_id}_{$block->child_key}"][] = $blockItem + [
'max' => $blockTypeConfig['max'],
'trigger' => $blockTypeConfig['trigger'],
];
} else {
$fields['blocks'][] = $blockItem + [
'icon' => $blockTypeConfig['icon'],
];
}
$fields['blocksFields'][] = collect($block['content'])->filter(function ($value, $key) {
return $key !== "browsers";
})->map(function ($value, $key) use ($block) {
return [
'name' => "blocks[$block->id][$key]",
'value' => $value,
];
})->filter()->values()->toArray();
$blockFormFields = app(BlockRepository::class)->getFormFields($block);
$medias = $blockFormFields['medias'];
if ($medias) {
$fields['blocksMedias'][] = collect($medias)->mapWithKeys(function ($value, $key) use ($block) {
return [
"blocks[$block->id][$key]" => $value,
];
})->filter()->toArray();
}
$files = $blockFormFields['files'];
if ($files) {
collect($files)->each(function ($rolesWithFiles, $locale) use (&$fields, $block) {
$fields['blocksFiles'][] = collect($rolesWithFiles)->mapWithKeys(function ($files, $role) use ($locale, $block) {
return [
"blocks[$block->id][$role][$locale]" => $files,
];
})->toArray();
});
}
if (isset($block['content']['browsers'])) {
$fields['blocksBrowsers'][] = $this->getBlockBrowsers($block);
}
}
if ($fields['blocksFields'] ?? false) {
$fields['blocksFields'] = call_user_func_array('array_merge', $fields['blocksFields'] ?? []);
}
if ($fields['blocksMedias'] ?? false) {
$fields['blocksMedias'] = call_user_func_array('array_merge', $fields['blocksMedias'] ?? []);
}
if ($fields['blocksFiles'] ?? false) {
$fields['blocksFiles'] = call_user_func_array('array_merge', $fields['blocksFiles'] ?? []);
}
if ($fields['blocksBrowsers'] ?? false) {
$fields['blocksBrowsers'] = call_user_func_array('array_merge', $fields['blocksBrowsers'] ?? []);
}
}
return $fields;
}
/**
* Generates the browser block title based on the selected
* relation
*
* This function is the same as getBlockBrowsers but with
* the filter to return the first found element
*
* @param [?] $block
* @return void
*/
protected function getCustomBrowserTitle($block)
{
//ensure that current browser block exists
//without this, you can create a empty browser block
//but you will get an exception, because the block has no content
if( isset($block['content']['browsers']) ) {
return collect($block['content']['browsers'])->mapWithKeys(function ($ids, $relation) use ($block) {
$relationRepository = $this->getModelRepository($relation);
$relatedItems = $relationRepository->get([], ['id' => $ids], [], -1);
$sortedRelatedItems = array_flip($ids);
foreach ($relatedItems as $item) {
$sortedRelatedItems[$item->id] = $item;
}
$item = collect(array_values($sortedRelatedItems))->filter(function ($value) {
return is_object($value);
})->map(function ($relatedElement) use ($relation) {
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title
];
})->first();
return $item;
})->filter()->toArray();
} else {
return [];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment