<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Twig\Extension;
use Twig\Extension\AbstractExtension;
#use Twig\TwigFilter;
use Twig\TwigFunction;
use \Pimcore\Model\WebsiteSetting;
use SaintElmos\BaseBundle\Services\ImageResponsive;
use App\Services\Table;
use App\AppBundle;
use App\Services\Tracking;
class GlanzerExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('admin_tabs', [$this, 'getAdminTabs']),
new TwigFunction('codevorendehead_allgemein', [$this, 'getMetaVerifictionText']),
new TwigFunction('codenachanfangbody_allgemein', [$this, 'getFooterText']),
new TwigFunction('show_includes', [$this, 'showIncludes']),
new TwigFunction('codevorendebody_allgemein', [$this, 'getCodeNachanfangbody']),
new TwigFunction('description_default', [$this, 'getDefaultDescription']),
new TwigFunction('render_snippets', [$this, 'getSnippets']),
new TwigFunction('show_snippet', [$this, 'showSnippet']),
new TwigFunction('checkMultiselect', [$this, 'checkMultiselect']),
new TwigFunction('changeTextarea', [$this, 'changeTextarea']),
new TwigFunction('createMultiselect', [$this, 'createMultiselect']),
new TwigFunction('imageResponsive', [$this, 'getImgResponsive']),
new TwigFunction('table_data', [$this, 'getTableData']),
new TwigFunction('app_value', [$this, 'getAppValue']),
new TwigFunction('allow_tracking', [$this, 'getTracking']),
new TwigFunction('a_tag', [$this, 'getA']),
new TwigFunction('compress', [$this, 'getCompress'])
];
}
public function getTracking()
{
return Tracking::allowTrackingSocialmedia();
}
public function getAppValue($trackingObj, $styleclass)
{
return (string) AppBundle::getValue($trackingObj, $styleclass);
}
public function getA($href,$target,$styleclass)
{
$c = ($styleclass != '')? ' class="'.$styleclass.'"' : '';
$t = ($target != '')? $target : '_self';
$a = '<a class="no-underline" href="'.$href.'" target="'.$t.'"'.$c.'>';
$a = ($href !='') ? $a : '<a>';
return $a;
}
public function getTableData($objId, $spalten_str, $lang)
{
/* $obj = \Pimcore\Model\DataObject::getById($objId);
$spalten_str = '';
if( method_exists($obj,'getSpalten_str' ) )
{
$spalten_str = trim($obj->getSpalten_str());
} */
$zebraStripe = (empty($spalten_str)) ? true : false;
if(is_numeric($objId) && ($objId != 0))
{
// $lg = $this->document->getProperty('language');
return Table::getTable($this, $objId, $lang, $zebraStripe);
}
return "";
}
/**
* Returns responsive image
* @param $propertyArray
* @return string
*/
public function getImgResponsive($propertyArray)
{
/**
* @var $service ImageResponsive
*/
$service = \Pimcore::getContainer()->get(ImageResponsive::class);
return $service->getImgResponsive($propertyArray);
}
public function getAdminTabs($nr,$tabContentArray)
{
$setChecked = ($nr == 1)? ' checked' : '';
$result = '<input class="tabContent" id="'.$nr.'" type="radio" name="grp" '.$setChecked.' />';
$result .= '<label for="'.$nr.'">'.$tabContentArray[$nr].'</label>';
return $result;
}
public function getMetaVerifictionText(){
$konfiguration_basic = WebsiteSetting::getByName('konfiguration_basic')->getData();
$codevorendehead_globalallgemein_text = trim((string)$konfiguration_basic->getCodevorendehead_globalallgemein());
$codevorendehead_allgemein = "";
if ($codevorendehead_globalallgemein_text != '') {
$codevorendehead_globalallgemein = htmlspecialchars_decode($codevorendehead_globalallgemein_text);
$codevorendehead_allgemein = "\n\r" . trim(html_entity_decode($codevorendehead_globalallgemein)) . "\n\r";
}
return $codevorendehead_allgemein;
}
public function getCodeNachanfangbody(){
$konfiguration_basic = WebsiteSetting::getByName('konfiguration_basic')->getData();
$codevorendebody_globalallgemein_text = trim((string)$konfiguration_basic->getCodevorendebody_globalallgemein());
$codevorendebody_allgemein = "";
if ($codevorendebody_globalallgemein_text != '') {
$codevorendebody_globalallgemein = htmlspecialchars_decode($codevorendebody_globalallgemein_text);
$codevorendebody_allgemein = "\n\r" . trim(html_entity_decode($codevorendebody_globalallgemein)) . "\n\r";
}
return $codevorendebody_allgemein;
}
public function getDefaultDescription(){
$konfiguration_basic = WebsiteSetting::getByName('konfiguration_basic')->getData();
$localizedFields = $konfiguration_basic->getLocalizedfields();
$description_default = trim((string)$localizedFields->getLocalizedValue('metabeschreibung'));
return $description_default;
}
public function getFooterText(){
$konfiguration_basic = WebsiteSetting::getByName('konfiguration_basic')->getData();
$codenachanfangbody_globalallgemein_text = trim((string)$konfiguration_basic->getCodenachanfangbody_globalallgemein());
$codenachanfangbody_allgemein = "";
if ($codenachanfangbody_globalallgemein_text != '') {
$codenachanfangbody_globalallgemein = htmlspecialchars_decode($codenachanfangbody_globalallgemein_text);
$codenachanfangbody_allgemein = "\n\r" . trim(html_entity_decode($codenachanfangbody_globalallgemein)) . "\n\r";
}
return $codenachanfangbody_allgemein;
}
/**
* Gibt den Templat-Namen eines Sippets zurück
*
* @param int $id
* @return string
*/
private function showTemplateName($template)
{
//$document = \Pimcore\Model\Document::getById($id);
//$templatepath = $document->getTemplate();
$file = explode('/', $template);
$count_elemente = count($file)-1;
$name = str_replace('.html.twig', '', $file[$count_elemente]);
return $name;
}
public function getSnippets($document, $frontendParam){
$frontendParam = $frontendParam;
$contentModuleAusgabeArray = array();
$parentObj = $document;
$renderTemplate = [];
if(is_object($parentObj))
{
$parentChildren = $parentObj->getChildren();
if(!empty($parentChildren)){
foreach ($parentChildren as $key => $child)
{
if ($child->getType() === 'snippet')
{
$exclude = $child->getproperty('snippet_exclude');
if ($child->isPublished() && !$exclude)
{
$properieslist = $child->getProperties();
//echo \Pimcore\Model\Document\Service::render($child, [], false);
//$count++;
$contentModuleAusgabeArray[$child->getId()] = array(
'ausgabetyp' => $child->getType(),
'id' => $child->getId(),
'path' => $child->getPath(),
'properieslist' => $properieslist,
'template' => $child->getTemplate(),
);
}
}
}
}
}
if (is_array($contentModuleAusgabeArray))
{
$break = false;
$show_modulBreakpoint = false;
foreach ($contentModuleAusgabeArray as $key => $moduleArray)
{
$snippet_exclude ="";
if(!empty($moduleArray['properieslist']) && isset($moduleArray['properieslist']['snippet_exclude'])){
$snippet_exclude = (string)$moduleArray['properieslist']['snippet_exclude']->getData();
}
// $templateName = trim((string)$this->showTemplateName($key));
$templateName = trim((string)$this->showTemplateName(trim($moduleArray['template'])));
if($templateName == 'breakpoint-snippetmodule')
{
$break = true;
}
if ($frontendParam == 'module')
{
if(!$snippet_exclude && $break == true)
{
if($templateName != 'breakpoint-snippetmodule' || $show_modulBreakpoint == false)
$show_modulBreakpoint = true;
$renderTemplate[] =$this->showSnippet($key,['parameter1'=>'ID:'.$key]);
//echo $this->showSnippet($key,['parameter1'=>'ID:'.$key]);
}
}
else
{
if(!$snippet_exclude && $break == false)
{
$optionalParamsArray = array(
'parameter1'=>'ID='.$key
);
// $optionalParamsArray = array_merge($optionalParamsArray,[]);
$renderTemplate[] =$this->showSnippet($key,$optionalParamsArray);
// echo $this->showSnippet($key,$optionalParamsArray);
}
}
}
//-----------------------------------------------------------------------------------------
if ($frontendParam == 'module')
{
echo '[endstring]';
}
}// End of contentModuleAusgabeArray
return $renderTemplate;
}
/**
* includiert gerendertes Snippet
*
* @param mixed $datei
* @return
*
* Ausgabebeispiel: echo $this->frontendHelper()->getSnippet('6',['parameter1' => 'Ausgabe Param 1', 'parameter2'=>'Ausgabe Param 1']);
* Ausgabebeispiel Parameter im Snippet: echo $this->getParam('parameter1') / oder print_r(getParams());
*/
public function showSnippet($file,$optionalParamsArray=array(),$useLayout=false)
{
$file = trim($file);
$path = "";
$id =0;
$isFileExists = false;
$docObj = "";
if (is_numeric((int)$file) && (int)$file != 0)
{
$id = (int)$file;
$docObj = \Pimcore\Model\Document::getById($id);
if (is_object($docObj) && !empty($docObj))
{
$path = $docObj->getFullPath();
$isFileExists = true;
}
}
else
{
$docObj = \Pimcore\Model\Document::getByPath($file);
if (is_object($docObj) && !empty($docObj))
{
$id = $docObj->getId();
$path = $file;
$isFileExists = true;
}
}
/* if (\Pimcore\Model\Document\Service::pathExists($path))
{
$isFileExists = true;
}
else
{
$isFileExists = false;
} */
if($isFileExists)
{
// $doc = \Pimcore\Model\Document::getById($id);
$mySnippet = \Pimcore\Model\Document\Service::render($docObj, $optionalParamsArray, $useLayout);
return $mySnippet;
}
else
{
return 'ERROR => File not exist!';
}
}
public function showIncludes()
{
/* ********************************************************************************************************************* */
/*
* B U N D L E I N C L U D E - A R R A Y S
*/
global $kernel;
$items = $kernel->getBundleCollection()->getItems();
$docroot = str_replace("web", "", $_SERVER["DOCUMENT_ROOT"]);
$incl_kopf_array = [];
$incl_fuss_array = [];
$saintElmosItems = array_filter($items, function($item){
$isSaintElmosBundle = strpos($item->getBundle()->getName(), 'SaintElmos') !== false;
$isAppBundle = strpos($item->getBundle()->getName(), 'AppBundle') !== false;
return $isSaintElmosBundle || $isAppBundle;
});
$incl_body_array = $incl_kopf_array = $incl_fuss_array = [];
$extension = [];
/**
* @var $item Item
*/
foreach ($saintElmosItems as $item){
$bundle = $item->getBundle();
$bundleName = $bundle->getName();
$bundleNamespace = $bundle->getNamespace();
$class = $bundle->getName();
$bundleClass = $bundleNamespace.'\\'.$class;
$interfaces = class_implements($bundleClass);
if(in_array(IncludeInterface::class, $interfaces)){
$incl_kopf_array[$bundleName] = $bundleClass::getHeaderIncludes();
$incl_body_array[$bundleName] = $bundleClass::getBodyIncludes();
$incl_fuss_array[$bundleName] = $bundleClass::getFooterIncludes();
}
}
//echo "Inside forLoop"; die;
//print_r($extension); die;
//Ausgabe
$this->baseparams['incl_kopf_array'] = $incl_kopf_array;
$this->baseparams['incl_body_array'] = $incl_body_array;
$this->baseparams['incl_fuss_array'] = $incl_fuss_array;
}
/**
* Prüft ein Mehrfach Auswahl Feld
*
* @param string $inputStr
* @param string $check
* @return bool
*/
public function checkMultiselect($inputStr, $check)
{
$inputArray = explode(',', $inputStr);
$result = (in_array($check, $inputArray)) ? true : false;
return $result;
}
/**
* gibt Textarea bereinigt (Entities umwandeln) zurück
*
* @param mixed $that
* @param string $textareaName
* @return string
*/
public function changeTextarea($text)
{
return trim(nl2br(html_entity_decode($text)));
}
/**
* Erzeugt ein Mehrfach Auswahl Feld
*
* @param mixed $that
* @param int $selectID
* @param mixed $optonProprtyArray
* @param string $display
* @return string
*/
public function createMultiselect($that, $selectID, $optonProprtyArray, $display = 'none')
{
if ($display == 'block')
{
$position = '0px';
} else
{
$position = '-5000px';
}
//select + input => müssen den selben Namen haben!
$outputStr = $that->input($selectID)->text;
$inputArray = explode(',', $outputStr);
ob_start();
echo '<select multiple class="' . $selectID . '" name="' . $selectID . '[]">' . PHP_EOL;
foreach ($optonProprtyArray as $key => $value)
{
$selected = (in_array($key, $inputArray)) ? ' selected="selected"' : '';
echo '<option value="' . $key . '"' . $selected . '>' . $value . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
echo '<div style="position:absolute; left:' . $position . ';"><span class="parentbox">' . PHP_EOL;
echo $that->input($selectID, array("width" => "430", "height" => 25)) . PHP_EOL;
echo '</span></div>' . PHP_EOL;
?>
<script type="text/javascript">
$(document).ready(function () {
$('.<?=$selectID?>').multiSelect({
afterSelect: function (values) {
var $container = $('#<?=$selectID?>'),
v = new Array();
$container.find('.ms-selection li:visible').each(function () {
var value = $container.find('select > option').eq($(this).index()).val();
v.push(value);
});
$('#pimcore_editable_<?=$selectID;?>').text(v.join(','));
},
afterDeselect: function (values) {
var $container = $('#<?=$selectID?>'),
v = new Array();
$container.find('.ms-selection li:visible').each(function () {
var value = $container.find('select > option').eq($(this).index()).val();
v.push(value);
});
$('#pimcore_editable_<?=$selectID;?>').text(v.join(','));
}
});
});
</script>
<?php
$select = ob_get_contents();
ob_end_clean();
return $select;
}
/**
* Entfernung von Zeilenumbrüchen und Kommentare => Ausgabe in einer Zeile
*
* @param string $buffer
* @return string
*/
public function getCompress($buffer)
{
// remove comments
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
// remove tabs, newlines, etc.
$buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);
//remove multiple spaces
$buffer = preg_replace('/\s\s+/', ' ', $buffer);
return $buffer;
}
}