File: D:/HostingSpaces/SBogers10/ijzerenman.komma.pro/app/Custom/Pages/DynamicPage.php
<?php
namespace Komma\Pages;
class DynamicPage
{
private $blockCount = 1;
/**
* @var PageRepository
*/
private $pageRepository;
private $holderOpened = false;
private $groupPageBlock = ['page-link-block','file-block','google-maps-block'];
/**
* @param PageRepository $pageRepository
*/
public function __construct(PageRepository $pageRepository)
{
$this->pageRepository = $pageRepository;
}
/**
* @param PageEntity $entity
* @return bool|string
*/
public function createFromEntity(PageEntity $entity, $allow = [])
{
if (!$entity->description) return false;
$dynamic = json_decode($entity->description);
// Create output
$output = '';
$activeDynamic = [];
// Loop through blocks
foreach ($dynamic as $key => $block)
{
if( ! empty($allow) && ! in_array($block->typeSlug,$allow)) continue;
if (!isset($block->status) || !$block->status) continue;
$activeDynamic[] = $block;
}
// Loop through active blocks
foreach($activeDynamic as $key => $block)
{
$output .= $this->checkCloseHolder($block);
$output .= $this->checkOpenHolder($block);
// Call method dynamic
//cally
$methodName = 'create' . ucfirst(camel_case($block->typeSlug));
if (!method_exists($this, $methodName)) continue;
$block->switch = $this->checkSwitch($activeDynamic, $key);
// Call function
$output .= $this->{$methodName}((array)$block,$entity);
// Check for separator
if ($block->typeSlug == 'content-block' &&
$key != count($activeDynamic) - 1)
$output .= $this->createSeparator();
$this->blockCount++;
}
// close last opened block
$output .= '</div>';
$output .= $this->createSeparator();
return $output;
}
/**
* For a form, get the first content block
* Only display this from the KMS, form is static
*
* @param PageEntity $entity
* @return bool
*/
public function createFormFromEntity(PageEntity $entity)
{
if (!$entity->description) return false;
$dynamic = json_decode($entity->description);
if (!isset($dynamic[0])) return false;
return $dynamic[0];
}
/*
* Return View from a content block
*
* @return String
*/
protected function createContentBlock($block,$entity = null)
{
// Get image
if (isset($block['images'][0]->large)) $block['image'] = $block['images'][0]->large;
// Check for call to action
$cta = '';
if(isset($block['cta']) && ! empty($block['cta']))
{
$view = 'layouts.partials.buttons.' . $block['cta'];
if( \View::exists($view))
$cta = \View::make($view)
->with(['class' => 'arrow'])
->render();
}
// Create view
return \View::make('layouts.partials.dynamicPage.contentBlock')->with([
'image' => $block['image'],
'title' => $block['title'],
'description' => $block['description'],
'switch' => $block['switch'],
'entity' => $entity,
'cta' => $cta,
'count' => $this->blockCount
])->render();
}
/*
* Return View from a page link entity
*/
protected function createPageLinkBlock($block,$entity = null)
{
// If we have a custom image, use custom, elke use image from linked page
$image = '';
if(isset($block['images'][0]) && ! empty($block['images'][0]))
{
$image = $block['images'][0]->medium;
}
// Title
$title = '';
if(isset($block['title']) && $block['title'] != '')
$title = $block['title'];
// Description
$description = '';
if(isset($block['short_description']) && $block['short_description'] != '')
$description = $block['short_description'];
// If we have custom text, use custom, else use default
$buttonText = 'Lees meer';
if(isset($block['link_text']) && $block['link_text'] != '')
$buttonText = $block['link_text'];
// Route
$route = '#';
$linkedEntity = $this->pageRepository->getRouteByPageId($block['pageId']);
if($linkedEntity != null) $route = $linkedEntity->route;
$class = 'default';
if(isset($block['special']) && $block['special'] )
$class = 'dark';
return \View::make('layouts.partials.dynamicPage.pageLink')
->with([
'route' => $route,
'colorClass' => $class,
'title' => $title,
'description' => $description,
'image' => $image,
'buttonText' => $buttonText,
'switch' => $block['switch']
])
->render();
}
/*
* Return View from a page link entity
*/
protected function createFileBlock($block,$entity = null)
{
// If we have a custom image, use custom, elke use image from linked page
$image = '';
if(isset($block['images'][0]) && ! empty($block['images'][0]))
{
$image = $block['images'][0]->medium;
}
// Tile
$title = '';
if(isset($block['title']) && $block['title'] != '')
$title = $block['title'];
// Description
$description = '';
if(isset($block['short_description']) && $block['short_description'] != '')
$description = $block['short_description'];
// If we have custom text, use custom, else use default
$buttonText = 'Lees meer';
if(isset($block['download_text']) && $block['download_text'] != '')
$buttonText = $block['download_text'];
$class = 'default';
if(isset($block['special']) && $block['special'] )
$class = 'dark';
$route = '';
if(isset($block['file']))
$route = $block['file'];
return \View::make('layouts.partials.dynamicPage.file')
->with([
'route' => $route,
'colorClass' => $class,
'title' => $title,
'description' => $description,
'image' => $image,
'buttonText' => $buttonText,
'switch' => $block['switch']
])
->render();
}
/*
* Return View from a page link entity
*/
protected function createGoogleMapsBlock($block,$entity = null)
{
return \View::make('layouts.partials.dynamicPage.googleMaps')
->with([
'switch' => $block['switch']
])
->render();
}
/*
* Return View for an album
*/
protected function createImageSliderBlock($block,$entity = null)
{
return \View::make('layouts.partials.dynamicPage.photoAlbumBlock')
->with([
'tabs' => $block['tab']
])
->render();
}
/*
* Return View for an youtube video
*/
protected function createVideoBlock($block,$entity = null)
{
return \View::make('layouts.partials.dynamicPage.videoBlock')->with($block)->render();
}
/*
* Return View from a page separator
*/
protected function createSeparator()
{
return \View::make('layouts.partials.dynamicPage.separator')->render();
}
/**
* Open a holder that contains all blocks of the same type
* This can be useful for styling
*
* @param $block
* @return bool|string
*/
protected function checkOpenHolder($block)
{
// Don't open if already opened
if ($this->holderOpened) return false;
$slug = $block->typeSlug;
if(in_array($slug,$this->groupPageBlock)) $slug = 'page-link-block';
$this->holderOpened = $slug;
return '<div class="' . $slug . '-holder">';
}
/**
* Close the holder that contains all blocks of the same type
*
* @param $block
* @return bool|string
*/
protected function checkCloseHolder($block)
{
// Don't close if already closed
if ( ! $this->holderOpened) return false;
// Don't close if block is the type of block we're currently holding
// Or both types are in the same group
if ($this->holderOpened == $block->typeSlug || (in_array($this->holderOpened,$this->groupPageBlock) && in_array($block->typeSlug,$this->groupPageBlock))) return false;
$output = '</div>';
// Check for separator
if ($this->holderOpened != 'content-block')
$output .= $this->createSeparator();
$this->holderOpened = false;
return $output;
}
/**
* See if the block needs to be switched (in design)
*
* @param $dynamic
* @param $key
*
* @return bool
*/
protected function checkSwitch($dynamic, $key)
{
$currentBlock = $dynamic[$key];
// Check the switch scope for each block
switch ($currentBlock->typeSlug) {
case 'page-link-block':
case 'file-block':
case 'google-maps-block':
$switchScope = 2;
break;
default:
$switchScope = 1;
}
// If earlier block doesn't exist return false
if (!isset($dynamic[$key - $switchScope])) return false;
// Switch scope can be two, but in that case the previous item should also be from the same type
// For example: page-link, content, page-link (this last one should start over)
if($switchScope == 2)
{
if (!isset($dynamic[$key - 1])) return false;
if( ! $this->sameType($dynamic[$key - 1], $currentBlock)) return false;
}
// The Block to compare with is the earlier block
// This is the block {switchScope} steps back in the array
$checkBlock = $dynamic[$key - $switchScope];
// So if one of both statements is true, the check block from the same type
// If not, never add a switch
if( ! $this->sameType($checkBlock, $currentBlock)) return false;
// Same type and the check block doesn't have a switch, this one needs one
return ! $checkBlock->switch;
}
/**
* Compare types of two blocks
*
* @param $block1
* @param $block2
* @return bool
*/
private function sameType($block1, $block2)
{
// The check block should be of the same type as the current block
// The string could be equal (for example for content-block) ...
$sameTypeString = $block1->typeSlug == $block2->typeSlug;
// ... or the block have different typeSlugs but are in the same group
$sameTypeArray = in_array($block1->typeSlug,$this->groupPageBlock) &&
in_array($block2->typeSlug,$this->groupPageBlock);
// So if one of both statements is true, the check block from the same type
// If not, never add a switch
return ($sameTypeString || $sameTypeArray);
}
}