<?php
namespace FoxHabbit\BasisBundle\Document;
use Pimcore\Extension\Document\Areabrick\AbstractTemplateAreabrick;
use Pimcore\Model\Document\Editable\Area\Info;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
//use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use FoxHabbit\BasisBundle\Manager\ConfigManager;
abstract class AreabrickTwig extends AbstractTemplateAreabrick
{
/**
* @var ConfigManager
*/
protected $configManager;
/**
* @var TokenStorage
*/
protected $tokenStorage ;
public function getTemplateLocation()
{
return static::TEMPLATE_LOCATION_BUNDLE;
}
public function getTemplateSuffix()
{
return static::TEMPLATE_SUFFIX_TWIG;
}
protected function getTemplateSubfolder() {
return '';
}
public function getName() {
$name = $this->getId();
$name = preg_replace( '/^([^-]+)-(.+)$/', '$2-($1)', $this->getId());
$name = ucwords( str_replace( '-',' ', $name));
return $name;
}
/**
* @param Info $info
*
* @throws \Exception
*/
public function action(Info $info)
{
$this->configManager = $this->container->get(ConfigManager::class);
//$info->getView()->configManager = $this->configManager;
$siteId = null;
if($info->getDocument()) {
// Get site of current document for site specifig config
$site = \Pimcore\Tool\Frontend::getSiteForDocument($info->getDocument());
if( $site) {
$siteId = $site->getId();
}
}
if( $info->getEditable()->getEditmode()) {
$config = $this->configManager->getAreaConfigEditmode($this->getId(), $siteId);
} else {
$config = $this->configManager->getAreaConfig($this->getId(), $siteId);
}
$info->setParam('config', $config);
$this->tokenStorage = $this->container->get('security.token_storage');
}
// Hide the wrapper-div
public function getHtmlTagOpen(Info $info)
{
//return '<fieldset class="pimcore-area-outline"><legend>'.$info->getId().'</legend>';
return '<!-- START Brick '.$info->getId().' -->';
}
public function getHtmlTagClose(Info $info)
{
//return '</fieldset>';
return '<!-- END Brick '.$info->getId().' -->';
}
// Allow subfolder for the view scripts
public function getTemplate() { // For Pimcore 10
return $this->buildBrickTemplateReference( 'view');
}
public function getViewTemplate() { // For Pimcore 6
return $this->buildBrickTemplateReference( 'view');
}
public function getEditTemplate() { // For Pimcore 6
return $this->buildBrickTemplateReference( 'edit');
}
protected function buildBrickTemplateReference( $type) {
$subfolder = $this->getTemplateSubfolder();
if ($this->getTemplateLocation() === static::TEMPLATE_LOCATION_BUNDLE) {
return sprintf(
'%s/Areas/%s%s/%s.%s',
'@FoxHabbitBasis',
$subfolder,
$this->getId(),
$type,
$this->getTemplateSuffix()
);
} else {
return sprintf(
'Areas/%s/%s%s.%s',
$subfolder,
$this->getId(),
$type,
$this->getTemplateSuffix()
);
}
}
protected function getUser() {
if( $this->tokenStorage) {
if( $this->tokenStorage->getToken()) {
return $this->tokenStorage->getToken()->getUser();
}
}
return null;
}
}