vendor/fritzmg/contao-sharebuttons/system/modules/sharebuttons/classes/ShareButtons.php line 89

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * simple extension to provide a share buttons module
  6.  * 
  7.  * @copyright inspiredminds 2015-2019
  8.  * @package   sharebuttons
  9.  * @link      http://www.inspiredminds.at
  10.  * @author    Fritz Michael Gschwantner <fmg@inspiredminds.at>
  11.  * @license   LGPL-3.0-or-later
  12.  */
  13. use Contao\ArticleModel;
  14. use Contao\CalendarModel;
  15. use Contao\ContentModel;
  16. use Contao\Controller;
  17. use Contao\DataContainer;
  18. use Contao\Environment;
  19. use Contao\FrontendTemplate;
  20. use Contao\Input;
  21. use Contao\Model;
  22. use Contao\Module;
  23. use Contao\ModuleNews;
  24. use Contao\NewsArchiveModel;
  25. use Contao\StringUtil;
  26. use Contao\System;
  27. use Contao\Template;
  28. class ShareButtons
  29. {
  30.     const DEFAULT_THEME '';
  31.     const DEFAULT_TEMPLATE 'sharebuttons_default';
  32.     public static function createShareButtons($networks$theme self::DEFAULT_THEME$template self::DEFAULT_TEMPLATE$url null$title null$description null$image null$articleId null)
  33.     {
  34.         // access to page
  35.         global $objPage;
  36.         if (!$objPage) {
  37.             return '';
  38.         }
  39.         // try to deserialize
  40.         if( is_string$networks ) )
  41.             $networks StringUtil::deserialize$networks );
  42.         $networks array_intersect($networksarray_keys($GLOBALS['sharebuttons']['networks']));
  43.         // if there are no networks, don't do anything
  44.         if( !is_array$networks ) || count$networks ) == )
  45.             return '';
  46.         // process theme
  47.         if( $theme == 'sharebuttons_none' || $theme == 'none' || !in_array$themearray_keys$GLOBALS['sharebuttons']['themes'] ) ) )
  48.             $theme '';
  49.         // force theme to fontawesome if fontawesome template is used
  50.         if( stripos$template'fontawesome' ) !== false && $theme !== '' && $theme !== 'text' )
  51.             $theme 'fontawesome';
  52.         // force template to fontawesome if fontawesome theme is used
  53.         elseif( $theme == 'fontawesome' )
  54.             $template 'sharebuttons_fontawesome';
  55.         // check for empty template
  56.         if( !$template )
  57.             $template self::DEFAULT_TEMPLATE;
  58.         // create share buttons template
  59.         $objButtonsTemplate = new FrontendTemplate$template );
  60.         // assign enabled networks to template
  61.         $objButtonsTemplate->networks $networks;
  62.         // determine the share image (e.g. for pinterest)
  63.         if( !$image && isset( $GLOBALS['SOCIAL_IMAGES'] ) && is_array$GLOBALS['SOCIAL_IMAGES'] ) && count$GLOBALS['SOCIAL_IMAGES'] ) > )
  64.             $image Environment::get('base') . $GLOBALS['SOCIAL_IMAGES'][0];
  65.         // process url
  66.         if( $url && stripos$url'http' ) !== )
  67.             $url Environment::get('base') . $url;
  68.         // assign url, title, theme, image, description to template
  69.         $objButtonsTemplate->url         rawurlencode$url ?: Environment::get('base') . Environment::get('request') );
  70.         $objButtonsTemplate->title       rawurlencodestrip_tags$title ?: ( $objPage->pageTitle ?: $objPage->title ) ) );
  71.         $objButtonsTemplate->theme       $theme;
  72.         $objButtonsTemplate->image       rawurlencode$image );
  73.         $objButtonsTemplate->description rawurlencodestrip_tags$description ?: $objPage->description ) );
  74.         // add translations to template
  75.         $translations $GLOBALS['TL_LANG']['sharebuttons'];
  76.         $translations['mail_subject'] = rawurlencode$translations['mail_subject'] );
  77.         $objButtonsTemplate->lang $translations;
  78.         // add PDF link
  79.         if (\in_array('pdf'$networks)) {
  80.             $objArticle $articleId ArticleModel::findById($articleId) : ArticleModel::findPublishedByPidAndColumn($objPage->id'main');
  81.             if (null !== $objArticle) {
  82.                 $request Environment::get('indexFreeRequest');
  83.                 $objButtonsTemplate->pdfLink $request . ((strpos($request'?') !== false) ? '&amp;' '?') . 'pdf=' $objArticle->id;
  84.             }
  85.         }
  86.         // insert CSS if necessary
  87.         if( $theme )
  88.         {
  89.             $GLOBALS['TL_CSS'][] ='system/modules/sharebuttons/assets/base.css||static';
  90.             $css_theme $GLOBALS['sharebuttons']['themes'][ $theme ][1];
  91.             if (defined('TL_ROOT')) {
  92.                 $projectDir constant('TL_ROOT');
  93.             } else {
  94.                 $projectDir System::getContainer()->getParameter('kernel.project_dir');
  95.             }
  96.             if( is_file$projectDir '/' $css_theme ) )
  97.                 $GLOBALS['TL_CSS'][] = $css_theme.'||static';
  98.         }
  99.         // insert javascript
  100.         $GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/sharebuttons/assets/scripts.js|async';
  101.         // return parsed template
  102.         return $objButtonsTemplate->parse();
  103.     }
  104.     public static function createInsertTag($networks$theme ''$template ''$articleId null)
  105.     {
  106.         // try to deserialize
  107.         if (is_string($networks)) {
  108.             $networks StringUtil::deserialize($networkstrue);
  109.         }
  110.         $networks array_intersect($networksarray_keys($GLOBALS['sharebuttons']['networks']));
  111.         // check for networks
  112.         if (!\is_array($networks) || count($networks) === 0) {
  113.             return '';
  114.         }
  115.         $elements = [];
  116.         if ($theme) {
  117.             $elements[] = $theme;
  118.         }
  119.         if ($template) {
  120.             $elements[] = $template;
  121.         }
  122.         $elements[] = implode(':'$networks);
  123.         if ($articleId) {
  124.             $elements[] = $articleId;
  125.         }
  126.         // build insert tag
  127.         $strInsertTag '{{sharebuttons::' implode('::'$elements) . '}}';
  128.         // return insert tag
  129.         return $strInsertTag;
  130.     }
  131.     /**
  132.      * parseArticles hook for news
  133.      *
  134.      * @param \Template $objTemplate
  135.      * @param array $arrData
  136.      * @param \Module $objModule
  137.      * 
  138.      * @return void
  139.      */
  140.     public function parseArticles(Template $objTemplate$arrDataModule $objModule)
  141.     {
  142.         // check for news module
  143.         if(!$objModule instanceof ModuleNews)
  144.         {
  145.             return;
  146.         }
  147.         // get the news archive
  148.         $objArchive NewsArchiveModel::findById($arrData['pid']);
  149.         // get the networks for the archive
  150.         $arrNetworks StringUtil::deserialize($objArchive->sharebuttons_networkstrue); 
  151.         // prepare sharebuttons string
  152.         $strSharebuttons '';
  153.         // check if there are any networks
  154.         if (count($arrNetworks) > 0)
  155.         {
  156.             // set data
  157.             $theme       $objArchive->sharebuttons_theme;
  158.             $template    $objArchive->sharebuttons_template;
  159.             $url         $objTemplate->link;
  160.             $title       $arrData['headline'];
  161.             $description $arrData['teaser'];
  162.             $image       = ($objTemplate->addImage && $objTemplate->singleSRC) ? Environment::get('base') . $objTemplate->singleSRC null;
  163.             // create the share buttons
  164.             $strSharebuttons self::createShareButtons($arrNetworks$theme$template$url$title$description$image);
  165.         }
  166.         // set sharebuttons string
  167.         $objTemplate->sharebuttons $strSharebuttons;
  168.     }
  169.     /**
  170.      * parseTemplate hook for articles and events
  171.      *
  172.      * @param \Template $objTempalte
  173.      */
  174.     public function parseTemplateTemplate $objTemplate )
  175.     {
  176.         // check for mod_article template
  177.         if( stripos$objTemplate->getName(), 'mod_article' ) !== false )
  178.         {
  179.             // prepare sharebuttons string
  180.             $strSharebuttons '';
  181.             // get the networks
  182.             $arrNetworks StringUtil::deserialize$objTemplate->sharebuttons_networks );
  183.             // check if there are any networks
  184.             if( $arrNetworks )
  185.             {
  186.                 // set data
  187.                 $networks    $arrNetworks;
  188.                 $theme       $objTemplate->sharebuttons_theme;
  189.                 $template    $objTemplate->sharebuttons_template;
  190.                 $url         $objTemplate->href;
  191.                 $title       $objTemplate->title;
  192.                 $description $objTemplate->teaser;
  193.                 // create the share buttons
  194.                 $strSharebuttons self::createShareButtons($networks$theme$template$url$title$descriptionnull$objTemplate->id);                
  195.             }
  196.             // set sharebuttons variable
  197.             $objTemplate->sharebuttons $strSharebuttons;
  198.         }
  199.         // check for event template
  200.         elseif( stripos$objTemplate->getName(), 'event_' ) === )
  201.         {
  202.             // prepare sharebuttons string
  203.             $strSharebuttons '';
  204.             // get the calendar
  205.             if( ( $objCalendar CalendarModel::findById$objTemplate->pid ) ) !== null )
  206.             {
  207.                 // get the networks
  208.                 $arrNetworks StringUtil::deserialize$objCalendar->sharebuttons_networks );
  209.                 // check if there are any networks
  210.                 if( $arrNetworks )
  211.                 {
  212.                     // set data
  213.                     $networks    $arrNetworks;
  214.                     $theme       $objCalendar->sharebuttons_theme;
  215.                     $template    $objCalendar->sharebuttons_template;
  216.                     $url         $objTemplate->href;
  217.                     $title       $objTemplate->title;
  218.                     $description $objTemplate->teaser;
  219.                     $image       $objTemplate->singleSRC;
  220.                     // create the share buttons
  221.                     $strSharebuttons self::createShareButtons$networks$theme$template$url$title$description$image );                
  222.                 }
  223.             }
  224.             // set sharebuttons variable
  225.             $objTemplate->sharebuttons $strSharebuttons;            
  226.         }
  227.     }
  228.     /**
  229.      * replaceInsertTag hook
  230.      *
  231.      * @param string $strTag
  232.      * @param bool $blnCache
  233.      *
  234.      * @return string
  235.      */
  236.     public function replaceInsertTag$strTag$blnCache false )
  237.     {
  238.         // split tag
  239.         $arrTag explode'::'$strTag );
  240.         // check for share button tag
  241.         if( $arrTag[0] !== 'sharebuttons' )
  242.             return false;
  243.         else
  244.             array_shift$arrTag );
  245.         // determine theme, networks and template
  246.         $networks = array();
  247.         $theme self::DEFAULT_THEME;
  248.         $template self::DEFAULT_TEMPLATE;
  249.         $articleId null;
  250.         // go through each parameter
  251.         while (count($arrTag) > 0) {
  252.             // get the parameter
  253.             $strParam array_shift($arrTag);
  254.             // check for theme
  255.             if (in_array($strParamarray_keys($GLOBALS['sharebuttons']['themes']))) {
  256.                 $theme $strParam;
  257.                 continue;
  258.             }
  259.             // check for template
  260.             if (strpos($strParam'sharebuttons_') !== false) {
  261.                 $template $strParam;
  262.                 continue;
  263.             }
  264.             // check for article id
  265.             if (is_numeric($strParam)) {
  266.                 $articleId = (int) $strParam;
  267.             }
  268.             // check for networks
  269.             if (count($networks) == 0) {
  270.                 $networks array_intersect(explode(':'$strParam ), array_keys($GLOBALS['sharebuttons']['networks']));
  271.             }
  272.         }
  273.         // create sharebuttons
  274.         return self::createShareButtons($networks$theme$templatenullnullnullnull$articleId);
  275.     }
  276.     /**
  277.      * This dynamically allows PDF printing by checking whether a "pdf" share 
  278.      * network is enabled for the article itself or one of its content elements.
  279.      * 
  280.      * @param Model $objElement
  281.      * @param bool $blnReturn
  282.      *
  283.      * @return bool
  284.      */
  285.     public function isVisibleElement(Model $objElementbool $blnReturn): bool
  286.     {
  287.         if (!$objElement instanceof ArticleModel) {
  288.             return $blnReturn;
  289.         }
  290.         $articleId Input::get('pdf');
  291.         if (null === $articleId || (int) $objElement->id !== (int) $articleId) {
  292.             return $blnReturn;
  293.         }
  294.         $printable $objElement->printable;
  295.         if ((int) $printable === 1) {
  296.             return $blnReturn;
  297.         }
  298.         $options StringUtil::deserialize($printabletrue);
  299.         if (\in_array('pdf'$options)) {
  300.             return $blnReturn;
  301.         }
  302.         $articleNetworks StringUtil::deserialize($objElement->sharebuttons_networkstrue);
  303.         if (\in_array('pdf'$articleNetworks)) {
  304.             $options[] = 'pdf';
  305.             $objElement->printable serialize($options);
  306.             return $blnReturn;
  307.         }
  308.         $elements ContentModel::findPublishedByPidAndTable((int) $objElement->id$objElement->getTable());
  309.         if (null === $elements) {
  310.             return $blnReturn;
  311.         }
  312.         foreach ($elements as $element) {
  313.             if ('sharebuttons' !== $element->type) {
  314.                 continue;
  315.             }
  316.             $elementNetworks StringUtil::deserialize($element->sharebuttons_networkstrue);
  317.             if (\in_array('pdf'$elementNetworks)) {
  318.                 $options[] = 'pdf';
  319.                 $objElement->printable serialize($options);
  320.                 return $blnReturn;
  321.             }
  322.         }
  323.         return $blnReturn;
  324.     }
  325.     /**
  326.      * DCA functions
  327.      */
  328.     public function getNetworks(DataContainer $dc)
  329.     {
  330.         $networks $GLOBALS['sharebuttons']['networks'];
  331.         // Only allow PDF button in article settings and as a content element
  332.         if (!\in_array($dc->table, ['tl_article''tl_content'], true)) {
  333.             unset($networks['pdf']);
  334.         }
  335.         foreach ($networks as $network => &$label) {
  336.             if (!empty($GLOBALS['TL_LANG']['sharebuttons']['networks'][$network])) {
  337.                 $label $GLOBALS['TL_LANG']['sharebuttons']['networks'][$network];
  338.             }
  339.         }
  340.         return $networks;
  341.     }
  342.     public function getButtonThemes()
  343.     {
  344.         $themes = array( '' => $GLOBALS['TL_LANG']['sharebuttons']['no_theme'] );
  345.         foreach( $GLOBALS['sharebuttons']['themes'] as $k => $v )
  346.             $themes[$k] = $v[0];
  347.         return $themes;
  348.     }
  349.     public function getSharebuttonsTemplates()
  350.     {
  351.         return Controller::getTemplateGroup('sharebuttons_');
  352.     }
  353.     public static function isBackendRequest(): bool
  354.     {
  355.         if (method_exists(System::class, 'getContainer')) {
  356.             $container System::getContainer();
  357.             $request $container->get('request_stack')->getCurrentRequest();
  358.             
  359.             return $request && $container->get('contao.routing.scope_matcher')->isBackendRequest($request);
  360.         }
  361.         if (defined('TL_MODE')) {
  362.             return 'BE' === constant('TL_MODE');
  363.         }
  364.         return false;
  365.     }
  366. }