2021-10-29 18:05:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Kirby\Cms;
|
|
|
|
|
|
|
|
use Kirby\Data\Data;
|
2022-08-31 16:08:03 +02:00
|
|
|
use Kirby\Toolkit\Str;
|
2021-10-29 18:05:46 +02:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of layouts
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @package Kirby Cms
|
|
|
|
* @author Bastian Allgeier <bastian@getkirby.com>
|
|
|
|
* @link https://getkirby.com
|
2022-03-22 15:39:39 +01:00
|
|
|
* @copyright Bastian Allgeier
|
2021-10-29 18:05:46 +02:00
|
|
|
* @license https://getkirby.com/license
|
|
|
|
*/
|
|
|
|
class Layouts extends Items
|
|
|
|
{
|
2022-12-19 16:26:24 +01:00
|
|
|
public const ITEM_CLASS = Layout::class;
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
public static function factory(array $items = null, array $params = [])
|
|
|
|
{
|
|
|
|
$first = $items[0] ?? [];
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
// if there are no wrapping layouts for blocks yet …
|
|
|
|
if (array_key_exists('content', $first) === true || array_key_exists('type', $first) === true) {
|
|
|
|
$items = [
|
|
|
|
[
|
|
|
|
'id' => Str::uuid(),
|
|
|
|
'columns' => [
|
|
|
|
[
|
|
|
|
'width' => '1/1',
|
|
|
|
'blocks' => $items
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
return parent::factory($items, $params);
|
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
/**
|
|
|
|
* Checks if a given block type exists in the layouts collection
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasBlockType(string $type): bool
|
|
|
|
{
|
|
|
|
return $this->toBlocks()->hasType($type);
|
|
|
|
}
|
2021-11-18 17:44:47 +01:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
/**
|
|
|
|
* Parse layouts data
|
|
|
|
*
|
|
|
|
* @param array|string $input
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function parse($input): array
|
|
|
|
{
|
|
|
|
if (empty($input) === false && is_array($input) === false) {
|
|
|
|
try {
|
|
|
|
$input = Data::decode($input, 'json');
|
2022-12-19 16:26:24 +01:00
|
|
|
} catch (Throwable) {
|
2022-08-31 16:08:03 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
if (empty($input) === true) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
return $input;
|
|
|
|
}
|
2021-11-18 17:44:47 +01:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
/**
|
|
|
|
* Converts layouts to blocks
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
|
|
|
* @param bool $includeHidden Sets whether to include hidden blocks
|
|
|
|
* @return \Kirby\Cms\Blocks
|
|
|
|
*/
|
|
|
|
public function toBlocks(bool $includeHidden = false)
|
|
|
|
{
|
|
|
|
$blocks = [];
|
2021-11-18 17:44:47 +01:00
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
if ($this->isNotEmpty() === true) {
|
|
|
|
foreach ($this->data() as $layout) {
|
|
|
|
foreach ($layout->columns() as $column) {
|
|
|
|
foreach ($column->blocks($includeHidden) as $block) {
|
|
|
|
$blocks[] = $block->toArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 17:44:47 +01:00
|
|
|
|
2023-04-14 16:30:28 +02:00
|
|
|
return Blocks::factory($blocks, [
|
|
|
|
'field' => $this->field,
|
|
|
|
'parent' => $this->parent
|
|
|
|
]);
|
2022-08-31 16:08:03 +02:00
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
}
|