Skip to content

Instantly share code, notes, and snippets.

@delphinpro
Last active January 14, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delphinpro/14dc6a3d79fe06edba3b3cd437fce3bf to your computer and use it in GitHub Desktop.
Save delphinpro/14dc6a3d79fe06edba3b3cd437fce3bf to your computer and use it in GitHub Desktop.
Наследование блоков PageBuilder (EvolutionCMS 2.x)
<?php
/**
* PageBuilderInheritable
*
* @author delphinpro <delphinpro@gmail.com>
* @copyright copyright © 2019 delphinpro
* @license licensed under the MIT license
*/
namespace EvolutionCMS\Custom;
use Illuminate\Support\Facades\DB;
class PageBuilderInheritable
{
public static function make(array $pbBlockDefinition, string $titleFieldName, string $blockId)
{
$evo = evolutionCMS();
$parentIds = $evo->getParentIds($evo->documentObject['id']);
$containers = !empty($pbBlockDefinition['container']) ? $pbBlockDefinition['container'] : 'default';
$pbBlocks = DB::table('pagebuilder')
->select()
->whereIn('document_id', $parentIds)
->whereIn('container', $containers)
->where('config', $blockId)
->orderBy('document_id')
->get()
->toArray();
$parentDocs = DB::table('site_content')
->select(['id', 'pagetitle'])
->whereIn('id', $parentIds)
->get()
->toArray();
$docTitles = [];
foreach ($parentDocs as $item) {
$docTitles[$item->id] = $item->pagetitle;
}
$ddElements = [
-1 => '--- Выберите блок, из которого следует унаследовать содержимое ---',
];
foreach ($pbBlocks as $block) {
$value = json_decode($block->values, true);
$title = self::findDescription($value, $titleFieldName) ?: '* Без описания *';
$substring = mb_substr(strip_tags($title), 0, 100);
$title = $substring.(mb_strlen($substring) < mb_strlen($title) ? '...' : '');
$ddElements[$block->id] = '[ '.$docTitles[$block->document_id].' ] '
.($title ?: $pbBlockDefinition['title']);
}
$inheritableSupport = !empty($pbBlocks) ? [
'inherit' => [
'caption' => '',
'type' => 'checkbox',
'layout' => 'horizontal',
'elements' => [1 => 'Наследовать'],
'default' => [],
],
'inheritId' => [
'type' => 'dropdown',
'elements' => $ddElements,
'default' => 1,
],
] : [];
$pbBlockDefinition['fields'] = array_merge($inheritableSupport, $pbBlockDefinition['fields']);
return $pbBlockDefinition;
}
private static function findDescription($fieldsValues, $titleFieldName)
{
$description = '';
if (array_key_exists($titleFieldName, $fieldsValues) && trim($fieldsValues[$titleFieldName]) != '') {
return $fieldsValues[$titleFieldName];
}
foreach ($fieldsValues as $name => $value) {
if (is_string($value) && trim($value) != '' && !is_numeric($value)) {
$description .= ' '.$value;
}
if (is_array($value)) {
$_description = self::findDescription($value, $titleFieldName);
if ($_description) {
$description .= ' '.$_description;
}
}
}
return $description;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment