src/Twig/Extension/GlanzerExtension.php line 178

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Twig\Extension;
  15. use Twig\Extension\AbstractExtension;
  16. #use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. use \Pimcore\Model\WebsiteSetting;
  19. use SaintElmos\BaseBundle\Services\ImageResponsive;
  20. use App\Services\Table;
  21. use App\AppBundle;
  22. use App\Services\Tracking;
  23. class GlanzerExtension extends AbstractExtension
  24. {
  25.     
  26.     public function getFunctions()
  27.     {
  28.         return [
  29.             new TwigFunction('admin_tabs', [$this'getAdminTabs']),
  30.             new TwigFunction('codevorendehead_allgemein', [$this'getMetaVerifictionText']),
  31.             new TwigFunction('codenachanfangbody_allgemein', [$this'getFooterText']),
  32.             new TwigFunction('show_includes', [$this'showIncludes']),
  33.             new TwigFunction('codevorendebody_allgemein', [$this'getCodeNachanfangbody']),
  34.             new TwigFunction('description_default', [$this'getDefaultDescription']),
  35.             new TwigFunction('render_snippets', [$this'getSnippets']),
  36.             new TwigFunction('show_snippet', [$this'showSnippet']),
  37.             new TwigFunction('checkMultiselect', [$this'checkMultiselect']),
  38.             new TwigFunction('changeTextarea', [$this'changeTextarea']),
  39.             new TwigFunction('createMultiselect', [$this'createMultiselect']),
  40.             new TwigFunction('imageResponsive', [$this'getImgResponsive']),
  41.             new TwigFunction('table_data', [$this'getTableData']),
  42.             new TwigFunction('app_value', [$this'getAppValue']),
  43.             new TwigFunction('allow_tracking', [$this'getTracking']),
  44.             new TwigFunction('a_tag', [$this'getA']),
  45.             new TwigFunction('compress', [$this'getCompress'])            
  46.         ];
  47.     }
  48.     public function getTracking()
  49.     {
  50.         return Tracking::allowTrackingSocialmedia();
  51.     }
  52.     public function getAppValue($trackingObj$styleclass)
  53.     {
  54.         return (string) AppBundle::getValue($trackingObj$styleclass);
  55.     }
  56.     public function getA($href,$target,$styleclass)
  57.     {
  58.         $c = ($styleclass != '')? ' class="'.$styleclass.'"' '';
  59.         $t = ($target != '')? $target '_self';
  60.         $a '<a class="no-underline" href="'.$href.'" target="'.$t.'"'.$c.'>';
  61.         $a = ($href !='') ? $a '<a>';
  62.         return $a;
  63.     }
  64.     public function getTableData($objId$spalten_str$lang)
  65.     {
  66. /*         $obj    = \Pimcore\Model\DataObject::getById($objId);
  67.         $spalten_str = '';
  68.         if( method_exists($obj,'getSpalten_str' ) )
  69.         {
  70.           $spalten_str = trim($obj->getSpalten_str());
  71.         } */
  72.         $zebraStripe = (empty($spalten_str)) ? true false;
  73.         if(is_numeric($objId) && ($objId != 0))
  74.         {
  75.            // $lg = $this->document->getProperty('language');
  76.             return Table::getTable($this$objId$lang$zebraStripe);
  77.         }
  78.         return "";
  79.     }
  80.     /**
  81.      * Returns responsive image
  82.      * @param $propertyArray
  83.      * @return string
  84.      */
  85.     public function getImgResponsive($propertyArray)
  86.     {
  87.         /**
  88.          * @var $service ImageResponsive
  89.          */
  90.         $service \Pimcore::getContainer()->get(ImageResponsive::class);
  91.         return $service->getImgResponsive($propertyArray);
  92.     }
  93.     public function getAdminTabs($nr,$tabContentArray)
  94.     {
  95.         $setChecked = ($nr == 1)? ' checked' '';
  96.         $result '<input class="tabContent" id="'.$nr.'" type="radio" name="grp" '.$setChecked.' />';
  97.         $result .= '<label for="'.$nr.'">'.$tabContentArray[$nr].'</label>';
  98.         return $result;
  99.     }
  100.     public function getMetaVerifictionText(){
  101.         $konfiguration_basic WebsiteSetting::getByName('konfiguration_basic')->getData();
  102.         $codevorendehead_globalallgemein_text trim((string)$konfiguration_basic->getCodevorendehead_globalallgemein());
  103.         $codevorendehead_allgemein "";
  104.         if ($codevorendehead_globalallgemein_text != '') {
  105.             $codevorendehead_globalallgemein htmlspecialchars_decode($codevorendehead_globalallgemein_text);
  106.             $codevorendehead_allgemein "\n\r" trim(html_entity_decode($codevorendehead_globalallgemein)) . "\n\r";
  107.         }
  108.         return $codevorendehead_allgemein;
  109.     }
  110.     public function getCodeNachanfangbody(){
  111.         $konfiguration_basic WebsiteSetting::getByName('konfiguration_basic')->getData();
  112.         $codevorendebody_globalallgemein_text trim((string)$konfiguration_basic->getCodevorendebody_globalallgemein());
  113.         $codevorendebody_allgemein "";
  114.         if ($codevorendebody_globalallgemein_text != '') {
  115.             $codevorendebody_globalallgemein htmlspecialchars_decode($codevorendebody_globalallgemein_text);
  116.             $codevorendebody_allgemein "\n\r" trim(html_entity_decode($codevorendebody_globalallgemein)) . "\n\r";
  117.         }
  118.         return $codevorendebody_allgemein;
  119.     }
  120.     public function getDefaultDescription(){
  121.         $konfiguration_basic WebsiteSetting::getByName('konfiguration_basic')->getData();
  122.         $localizedFields $konfiguration_basic->getLocalizedfields();
  123.         $description_default trim((string)$localizedFields->getLocalizedValue('metabeschreibung'));
  124.         return $description_default;
  125.     }
  126.     public function getFooterText(){
  127.         $konfiguration_basic WebsiteSetting::getByName('konfiguration_basic')->getData();
  128.         $codenachanfangbody_globalallgemein_text trim((string)$konfiguration_basic->getCodenachanfangbody_globalallgemein());
  129.         $codenachanfangbody_allgemein "";
  130.         if ($codenachanfangbody_globalallgemein_text != '') {
  131.             $codenachanfangbody_globalallgemein htmlspecialchars_decode($codenachanfangbody_globalallgemein_text);
  132.             $codenachanfangbody_allgemein "\n\r" trim(html_entity_decode($codenachanfangbody_globalallgemein)) . "\n\r";
  133.         }
  134.         return $codenachanfangbody_allgemein;
  135.     }
  136.     /**
  137.      * Gibt den Templat-Namen eines Sippets zurück
  138.      *
  139.      * @param int $id
  140.      * @return string
  141.      */
  142.     private function showTemplateName($template)
  143.     {
  144.         //$document = \Pimcore\Model\Document::getById($id);
  145.         //$templatepath  = $document->getTemplate();
  146.         $file explode('/'$template);
  147.         $count_elemente count($file)-1;
  148.         $name str_replace('.html.twig'''$file[$count_elemente]);
  149.         return $name;
  150.     }
  151.     public function getSnippets($document$frontendParam){
  152.         $frontendParam $frontendParam;
  153.         $contentModuleAusgabeArray = array();
  154.         $parentObj $document;
  155.         $renderTemplate = [];
  156.         if(is_object($parentObj))
  157.         {
  158.             $parentChildren $parentObj->getChildren();
  159.             if(!empty($parentChildren)){
  160.                 foreach ($parentChildren as $key => $child)
  161.                 {
  162.                     if ($child->getType() === 'snippet')
  163.                     {
  164.                         $exclude $child->getproperty('snippet_exclude');
  165.                         if ($child->isPublished() && !$exclude)
  166.                         {
  167.                             $properieslist $child->getProperties();
  168.                             //echo \Pimcore\Model\Document\Service::render($child, [], false);
  169.                             //$count++;
  170.                            $contentModuleAusgabeArray[$child->getId()] = array(
  171.                                 'ausgabetyp' => $child->getType(),
  172.                                 'id' => $child->getId(),
  173.                                 'path' => $child->getPath(),
  174.                                 'properieslist' => $properieslist,
  175.                                 'template' => $child->getTemplate(),
  176.                             );
  177.                             
  178.                         }
  179.                     }
  180.                 }
  181.             }    
  182.         }
  183.         
  184.         if (is_array($contentModuleAusgabeArray))
  185.         {
  186.                 $break false;
  187.                 $show_modulBreakpoint false;
  188.                 foreach ($contentModuleAusgabeArray as $key => $moduleArray)
  189.                 {
  190.                    
  191.                     $snippet_exclude ="";
  192.                     if(!empty($moduleArray['properieslist']) && isset($moduleArray['properieslist']['snippet_exclude'])){
  193.                         $snippet_exclude = (string)$moduleArray['properieslist']['snippet_exclude']->getData();
  194.                     }
  195.                     // $templateName = trim((string)$this->showTemplateName($key));
  196.                     $templateName trim((string)$this->showTemplateName(trim($moduleArray['template'])));
  197.                     if($templateName == 'breakpoint-snippetmodule')
  198.                     {
  199.                         $break =  true;
  200.                     }
  201.                     if ($frontendParam == 'module')
  202.                     {
  203.                         if(!$snippet_exclude && $break == true)
  204.                         {
  205.                             if($templateName != 'breakpoint-snippetmodule' || $show_modulBreakpoint == false)
  206.                             $show_modulBreakpoint true;
  207.                             $renderTemplate[] =$this->showSnippet($key,['parameter1'=>'ID:'.$key]);
  208.                             //echo $this->showSnippet($key,['parameter1'=>'ID:'.$key]);
  209.                         }
  210.                     }
  211.                     else
  212.                     {
  213.                         if(!$snippet_exclude && $break == false)
  214.                         {
  215.                             $optionalParamsArray = array(
  216.                                 'parameter1'=>'ID='.$key
  217.                             );
  218.                            // $optionalParamsArray = array_merge($optionalParamsArray,[]);                            
  219.                             $renderTemplate[] =$this->showSnippet($key,$optionalParamsArray);
  220.                            // echo $this->showSnippet($key,$optionalParamsArray);
  221.                         }
  222.                     }
  223.                        
  224.                 }
  225.                  
  226.                 //-----------------------------------------------------------------------------------------
  227.                 if ($frontendParam == 'module')
  228.                 {
  229.                     echo '[endstring]';
  230.                 }
  231.         }// End of contentModuleAusgabeArray
  232.         return $renderTemplate;
  233.     }
  234.     /**
  235.      * includiert gerendertes Snippet
  236.      *
  237.      * @param mixed $datei
  238.      * @return
  239.      *
  240.      * Ausgabebeispiel:  echo $this->frontendHelper()->getSnippet('6',['parameter1' => 'Ausgabe Param 1', 'parameter2'=>'Ausgabe Param 1']);
  241.      * Ausgabebeispiel Parameter im Snippet:  echo  $this->getParam('parameter1') /  oder print_r(getParams());
  242.      */
  243.      public function showSnippet($file,$optionalParamsArray=array(),$useLayout=false)
  244.      {
  245.  
  246.          $file trim($file);
  247.          $path "";
  248.          $id =0;
  249.          $isFileExists false;
  250.          $docObj "";
  251.          if (is_numeric((int)$file) && (int)$file != 0)
  252.          {
  253.             $id = (int)$file;
  254.             $docObj \Pimcore\Model\Document::getById($id);
  255.              if (is_object($docObj) && !empty($docObj))
  256.              {
  257.                 $path $docObj->getFullPath();
  258.                 $isFileExists true;
  259.              }
  260.  
  261.          }
  262.          else
  263.          {
  264.             $docObj \Pimcore\Model\Document::getByPath($file);
  265.             if (is_object($docObj) && !empty($docObj))
  266.             {
  267.                 $id $docObj->getId();
  268.                 $path $file;
  269.                 $isFileExists true;
  270.             }
  271.          }
  272.  
  273. /*          if (\Pimcore\Model\Document\Service::pathExists($path))
  274.          {
  275.              $isFileExists = true;
  276.          }
  277.          else
  278.          {
  279.              $isFileExists = false;
  280.          } */
  281.  
  282.  
  283.          if($isFileExists)
  284.          {
  285.             // $doc =  \Pimcore\Model\Document::getById($id);
  286.             $mySnippet \Pimcore\Model\Document\Service::render($docObj$optionalParamsArray$useLayout);
  287.             return $mySnippet;
  288.          }
  289.          else
  290.          {
  291.              return 'ERROR => File not exist!';
  292.  
  293.          }
  294.      }
  295.     public function showIncludes()
  296.     {
  297.         /* ********************************************************************************************************************* */
  298.         /*
  299.          * B U N D L E   I N C L U D E - A R R A Y S
  300.          */
  301.         global $kernel;
  302.         $items $kernel->getBundleCollection()->getItems();
  303.         $docroot str_replace("web"""$_SERVER["DOCUMENT_ROOT"]);
  304.         $incl_kopf_array = [];
  305.         $incl_fuss_array = [];
  306.         
  307.         $saintElmosItems array_filter($items, function($item){
  308.            $isSaintElmosBundle strpos($item->getBundle()->getName(), 'SaintElmos') !== false;
  309.            $isAppBundle strpos($item->getBundle()->getName(), 'AppBundle') !== false;
  310.            return $isSaintElmosBundle || $isAppBundle;
  311.         });
  312.         
  313.         $incl_body_array $incl_kopf_array $incl_fuss_array  = [];
  314.         $extension = [];
  315.         /**
  316.          * @var $item Item
  317.          */
  318.         foreach ($saintElmosItems as $item){
  319.             $bundle $item->getBundle();
  320.             $bundleName $bundle->getName();
  321.             $bundleNamespace $bundle->getNamespace();
  322.             $class $bundle->getName();
  323.             $bundleClass $bundleNamespace.'\\'.$class;
  324.             $interfaces class_implements($bundleClass);
  325.             if(in_array(IncludeInterface::class, $interfaces)){
  326.                 $incl_kopf_array[$bundleName] = $bundleClass::getHeaderIncludes();
  327.                 $incl_body_array[$bundleName] = $bundleClass::getBodyIncludes();
  328.                 $incl_fuss_array[$bundleName] = $bundleClass::getFooterIncludes();
  329.             }
  330.         }
  331.           //echo "Inside forLoop"; die;
  332.           //print_r($extension); die;
  333.         //Ausgabe
  334.         $this->baseparams['incl_kopf_array'] =  $incl_kopf_array;
  335.         $this->baseparams['incl_body_array'] =  $incl_body_array;
  336.         $this->baseparams['incl_fuss_array'] =  $incl_fuss_array;
  337.     }
  338.     /**
  339.      * Prüft ein Mehrfach Auswahl Feld
  340.      *
  341.      * @param string $inputStr
  342.      * @param string $check
  343.      * @return bool
  344.      */
  345.      public function checkMultiselect($inputStr$check)
  346.      {
  347.          $inputArray explode(','$inputStr);
  348.          $result = (in_array($check$inputArray)) ? true false;
  349.  
  350.          return $result;
  351.      }
  352.      /**
  353.      * gibt Textarea bereinigt (Entities umwandeln) zurück
  354.      *
  355.      * @param mixed $that
  356.      * @param string $textareaName
  357.      * @return string
  358.      */
  359.      public function changeTextarea($text)
  360.      {
  361.          return trim(nl2br(html_entity_decode($text)));
  362.      }
  363.     
  364.     /**
  365.      * Erzeugt ein Mehrfach Auswahl Feld
  366.      *
  367.      * @param mixed $that
  368.      * @param int $selectID
  369.      * @param mixed $optonProprtyArray
  370.      * @param string $display
  371.      * @return string
  372.      */
  373.      public function createMultiselect($that$selectID$optonProprtyArray$display 'none')
  374.      {
  375.          if ($display == 'block')
  376.          {
  377.              $position '0px';
  378.          } else
  379.          {
  380.              $position '-5000px';
  381.          }
  382.  
  383.          //select + input => müssen den selben Namen haben!
  384.          $outputStr $that->input($selectID)->text;
  385.          $inputArray explode(','$outputStr);
  386.          ob_start();
  387.  
  388.          echo '<select multiple class="' $selectID '" name="' $selectID '[]">' PHP_EOL;
  389.  
  390.  
  391.          foreach ($optonProprtyArray as $key => $value)
  392.          {
  393.  
  394.              $selected = (in_array($key$inputArray)) ? ' selected="selected"' '';
  395.  
  396.              echo '<option value="' $key '"' $selected '>' $value '</option>' PHP_EOL;
  397.  
  398.          }
  399.  
  400.          echo '</select>' PHP_EOL;
  401.  
  402.          echo '<div style="position:absolute; left:' $position ';"><span class="parentbox">' PHP_EOL;
  403.          echo $that->input($selectID, array("width" => "430""height" => 25)) . PHP_EOL;
  404.          echo '</span></div>' PHP_EOL;
  405.  
  406.          ?>
  407.          <script type="text/javascript">
  408.  
  409.              $(document).ready(function () {
  410.  
  411.                  $('.<?=$selectID?>').multiSelect({
  412.  
  413.                      afterSelect: function (values) {
  414.  
  415.                          var $container = $('#<?=$selectID?>'),
  416.                              v = new Array();
  417.  
  418.                          $container.find('.ms-selection li:visible').each(function () {
  419.                              var value = $container.find('select > option').eq($(this).index()).val();
  420.  
  421.                              v.push(value);
  422.                          });
  423.                          $('#pimcore_editable_<?=$selectID;?>').text(v.join(','));
  424.  
  425.                      },
  426.  
  427.                      afterDeselect: function (values) {
  428.  
  429.                          var $container = $('#<?=$selectID?>'),
  430.                              v = new Array();
  431.                          $container.find('.ms-selection li:visible').each(function () {
  432.                              var value = $container.find('select > option').eq($(this).index()).val();
  433.  
  434.                              v.push(value);
  435.                          });
  436.  
  437.                          $('#pimcore_editable_<?=$selectID;?>').text(v.join(','));
  438.  
  439.                      }
  440.                  });
  441.  
  442.              });
  443.  
  444.          </script>
  445.  
  446.          <?php
  447.  
  448.          $select ob_get_contents();
  449.          ob_end_clean();
  450.  
  451.          return $select;
  452.      }
  453.      /**
  454.      * Entfernung von Zeilenumbrüchen und Kommentare => Ausgabe in einer Zeile
  455.      *
  456.      * @param string $buffer
  457.      * @return string
  458.      */
  459.     public function getCompress($buffer)
  460.     {
  461.         // remove comments
  462.         $buffer preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!'''$buffer);
  463.         // remove tabs, newlines, etc.
  464.         $buffer str_replace(array("\r\n""\r""\n""\t"), ''$buffer);
  465.         //remove multiple spaces
  466.         $buffer preg_replace('/\s\s+/'' '$buffer);
  467.         return $buffer;
  468.     }
  469. }