Skip to content

Commit 056e36a

Browse files
committed
Contao 5 compatibility
1 parent 50e6881 commit 056e36a

File tree

7 files changed

+84
-28
lines changed

7 files changed

+84
-28
lines changed

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
}
2727
],
2828
"require":{
29-
"php": "^7.1 || ^8.0",
30-
"contao/core-bundle": "^4.4",
29+
"php": ">=7.1",
30+
"contao/core-bundle": "^4.4 || ^5.0",
3131
"contao-community-alliance/composer-plugin": "^2.4 || ^3.0"
3232
},
3333
"replace": {
@@ -39,5 +39,12 @@
3939
"system/modules/sharebuttons": "system/modules/sharebuttons"
4040
}
4141
}
42+
},
43+
"autoload": {
44+
"classmap": [
45+
"system/modules/sharebuttons/classes/",
46+
"system/modules/sharebuttons/elements/",
47+
"system/modules/sharebuttons/modules/"
48+
]
4249
}
4350
}

system/modules/sharebuttons/classes/ShareButtons.php

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@
1313
*/
1414

1515
use Contao\ArticleModel;
16+
use Contao\CalendarModel;
1617
use Contao\ContentModel;
18+
use Contao\Controller;
1719
use Contao\DataContainer;
20+
use Contao\Environment;
21+
use Contao\FrontendTemplate;
1822
use Contao\Input;
23+
use Contao\Model;
24+
use Contao\Module;
25+
use Contao\ModuleNews;
26+
use Contao\NewsArchiveModel;
1927
use Contao\StringUtil;
28+
use Contao\System;
29+
use Contao\Template;
2030

2131
class ShareButtons
2232
{
@@ -30,7 +40,7 @@ public static function createShareButtons($networks, $theme = self::DEFAULT_THEM
3040

3141
// try to deserialize
3242
if( is_string( $networks ) )
33-
$networks = deserialize( $networks );
43+
$networks = StringUtil::deserialize( $networks );
3444

3545
$networks = array_intersect($networks, array_keys($GLOBALS['sharebuttons']['networks']));
3646

@@ -54,21 +64,21 @@ public static function createShareButtons($networks, $theme = self::DEFAULT_THEM
5464
$template = self::DEFAULT_TEMPLATE;
5565

5666
// create share buttons template
57-
$objButtonsTemplate = new \FrontendTemplate( $template );
67+
$objButtonsTemplate = new FrontendTemplate( $template );
5868

5969
// assign enabled networks to template
6070
$objButtonsTemplate->networks = $networks;
6171

6272
// determine the share image (e.g. for pinterest)
6373
if( !$image && isset( $GLOBALS['SOCIAL_IMAGES'] ) && is_array( $GLOBALS['SOCIAL_IMAGES'] ) && count( $GLOBALS['SOCIAL_IMAGES'] ) > 0 )
64-
$image = \Environment::get('base') . $GLOBALS['SOCIAL_IMAGES'][0];
74+
$image = Environment::get('base') . $GLOBALS['SOCIAL_IMAGES'][0];
6575

6676
// process url
6777
if( $url && stripos( $url, 'http' ) !== 0 )
68-
$url = \Environment::get('base') . $url;
78+
$url = Environment::get('base') . $url;
6979

7080
// assign url, title, theme, image, description to template
71-
$objButtonsTemplate->url = rawurlencode( $url ?: \Environment::get('base') . \Environment::get('request') );
81+
$objButtonsTemplate->url = rawurlencode( $url ?: Environment::get('base') . Environment::get('request') );
7282
$objButtonsTemplate->title = rawurlencode( strip_tags( $title ?: ( $objPage->pageTitle ?: $objPage->title ) ) );
7383
$objButtonsTemplate->theme = $theme;
7484
$objButtonsTemplate->image = rawurlencode( $image );
@@ -81,7 +91,7 @@ public static function createShareButtons($networks, $theme = self::DEFAULT_THEM
8191

8292
// add PDF link
8393
if (\in_array('pdf', $networks)) {
84-
$objArticle = $articleId ? \ArticleModel::findById($articleId) : \ArticleModel::findPublishedByPidAndColumn($objPage->id, 'main');
94+
$objArticle = $articleId ? ArticleModel::findById($articleId) : ArticleModel::findPublishedByPidAndColumn($objPage->id, 'main');
8595

8696
if (null !== $objArticle) {
8797
$request = Environment::get('indexFreeRequest');
@@ -94,7 +104,14 @@ public static function createShareButtons($networks, $theme = self::DEFAULT_THEM
94104
{
95105
$GLOBALS['TL_CSS'][] ='system/modules/sharebuttons/assets/base.css||static';
96106
$css_theme = $GLOBALS['sharebuttons']['themes'][ $theme ][1];
97-
if( is_file( TL_ROOT . '/' . $css_theme ) )
107+
108+
if (defined('TL_ROOT')) {
109+
$projectDir = constant('TL_ROOT');
110+
} else {
111+
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
112+
}
113+
114+
if( is_file( $projectDir . '/' . $css_theme ) )
98115
$GLOBALS['TL_CSS'][] = $css_theme.'||static';
99116
}
100117

@@ -109,7 +126,7 @@ public static function createInsertTag($networks, $theme = '', $template = '', $
109126
{
110127
// try to deserialize
111128
if (is_string($networks)) {
112-
$networks = deserialize($networks, true);
129+
$networks = StringUtil::deserialize($networks, true);
113130
}
114131

115132
$networks = array_intersect($networks, array_keys($GLOBALS['sharebuttons']['networks']));
@@ -152,19 +169,19 @@ public static function createInsertTag($networks, $theme = '', $template = '', $
152169
*
153170
* @return void
154171
*/
155-
public function parseArticles(\Template $objTemplate, $arrData, \Module $objModule)
172+
public function parseArticles(Template $objTemplate, $arrData, Module $objModule)
156173
{
157174
// check for news module
158-
if(!$objModule instanceof \ModuleNews)
175+
if(!$objModule instanceof ModuleNews)
159176
{
160177
return;
161178
}
162179

163180
// get the news archive
164-
$objArchive = \NewsArchiveModel::findById($arrData['pid']);
181+
$objArchive = NewsArchiveModel::findById($arrData['pid']);
165182

166183
// get the networks for the archive
167-
$arrNetworks = deserialize($objArchive->sharebuttons_networks, true);
184+
$arrNetworks = StringUtil::deserialize($objArchive->sharebuttons_networks, true);
168185

169186
// prepare sharebuttons string
170187
$strSharebuttons = '';
@@ -178,7 +195,7 @@ public function parseArticles(\Template $objTemplate, $arrData, \Module $objModu
178195
$url = $objTemplate->link;
179196
$title = $arrData['headline'];
180197
$description = $arrData['teaser'];
181-
$image = ($objTemplate->addImage && $objTemplate->singleSRC) ? \Environment::get('base') . $objTemplate->singleSRC : null;
198+
$image = ($objTemplate->addImage && $objTemplate->singleSRC) ? Environment::get('base') . $objTemplate->singleSRC : null;
182199

183200
// create the share buttons
184201
$strSharebuttons = self::createShareButtons($arrNetworks, $theme, $template, $url, $title, $description, $image);
@@ -194,7 +211,7 @@ public function parseArticles(\Template $objTemplate, $arrData, \Module $objModu
194211
*
195212
* @param \Template $objTempalte
196213
*/
197-
public function parseTemplate( \Template $objTemplate )
214+
public function parseTemplate( Template $objTemplate )
198215
{
199216
// check for mod_article template
200217
if( stripos( $objTemplate->getName(), 'mod_article' ) !== false )
@@ -203,7 +220,7 @@ public function parseTemplate( \Template $objTemplate )
203220
$strSharebuttons = '';
204221

205222
// get the networks
206-
$arrNetworks = deserialize( $objTemplate->sharebuttons_networks );
223+
$arrNetworks = StringUtil::deserialize( $objTemplate->sharebuttons_networks );
207224

208225
// check if there are any networks
209226
if( $arrNetworks )
@@ -230,10 +247,10 @@ public function parseTemplate( \Template $objTemplate )
230247
$strSharebuttons = '';
231248

232249
// get the calendar
233-
if( ( $objCalendar = \CalendarModel::findById( $objTemplate->pid ) ) !== null )
250+
if( ( $objCalendar = CalendarModel::findById( $objTemplate->pid ) ) !== null )
234251
{
235252
// get the networks
236-
$arrNetworks = deserialize( $objCalendar->sharebuttons_networks );
253+
$arrNetworks = StringUtil::deserialize( $objCalendar->sharebuttons_networks );
237254

238255
// check if there are any networks
239256
if( $arrNetworks )
@@ -326,7 +343,7 @@ public function replaceInsertTag( $strTag, $blnCache = false )
326343
*
327344
* @return bool
328345
*/
329-
public function isVisibleElement(\Model $objElement, bool $blnReturn): bool
346+
public function isVisibleElement(Model $objElement, bool $blnReturn): bool
330347
{
331348
if (!$objElement instanceof ArticleModel) {
332349
return $blnReturn;
@@ -415,6 +432,22 @@ public function getButtonThemes()
415432

416433
public function getSharebuttonsTemplates()
417434
{
418-
return \Controller::getTemplateGroup('sharebuttons_');
435+
return Controller::getTemplateGroup('sharebuttons_');
436+
}
437+
438+
public static function isBackendRequest(): bool
439+
{
440+
if (method_exists(System::class, 'getContainer')) {
441+
$container = System::getContainer();
442+
$request = $container->get('request_stack')->getCurrentRequest();
443+
444+
return $request && $container->get('contao.routing.scope_matcher')->isBackendRequest($request);
445+
}
446+
447+
if (defined('TL_MODE')) {
448+
return 'BE' === constant('TL_MODE');
449+
}
450+
451+
return false;
419452
}
420453
}

system/modules/sharebuttons/config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
/**
3838
* CSS
3939
*/
40-
if( TL_MODE == 'BE' )
40+
if (ShareButtons::isBackendRequest()) {
4141
$GLOBALS['TL_CSS'][] = 'system/modules/sharebuttons/assets/backend.css';
42-
42+
}
4343

4444
/**
4545
* Custom settings

system/modules/sharebuttons/dca/tl_calendar.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
* @license LGPL-3.0-or-later
1313
*/
1414

15+
use Contao\CalendarBundle\ContaoCalendarBundle;
1516

16-
if (\in_array('calendar', \ModuleLoader::getActive())) {
17+
if (class_exists(Contao\ModuleLoader::class)) {
18+
$add = \in_array('calendar', Contao\ModuleLoader::getActive());
19+
} else {
20+
$add = class_exists(ContaoCalendarBundle::class);
21+
}
22+
23+
if ($add) {
1724
/**
1825
* Add palettes to tl_calendar
1926
*/

system/modules/sharebuttons/dca/tl_news_archive.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
* @author Fritz Michael Gschwantner <fmg@inspiredminds.at>
1212
* @license LGPL-3.0-or-later
1313
*/
14+
15+
use Contao\NewsBundle\ContaoNewsBundle;
1416

17+
if (class_exists(Contao\ModuleLoader::class)) {
18+
$add = \in_array('news', Contao\ModuleLoader::getActive());
19+
} else {
20+
$add = class_exists(ContaoNewsBundle::class);
21+
}
1522

16-
if (\in_array('news', \ModuleLoader::getActive())) {
23+
if ($add) {
1724
/**
1825
* Add palettes to tl_news_archive
1926
*/

system/modules/sharebuttons/elements/ContentShareButtons.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
* @license LGPL-3.0-or-later
1313
*/
1414

15+
use Contao\ContentElement;
1516

16-
class ContentShareButtons extends \ContentElement
17+
class ContentShareButtons extends ContentElement
1718
{
1819
/**
1920
* Template
@@ -27,7 +28,7 @@ class ContentShareButtons extends \ContentElement
2728
protected function compile()
2829
{
2930
// show share buttons in backend
30-
if( TL_MODE == 'BE' )
31+
if(ShareButtons::isBackendRequest())
3132
$this->Template->sharebuttons = ShareButtons::createShareButtons($this->sharebuttons_networks,
3233
$this->sharebuttons_theme,
3334
$this->sharebuttons_template,

system/modules/sharebuttons/modules/ModuleShareButtons.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
* @license LGPL-3.0-or-later
1313
*/
1414

15+
use Contao\Module;
1516

16-
class ModuleShareButtons extends \Module
17+
class ModuleShareButtons extends Module
1718
{
1819
/**
1920
* Template

0 commit comments

Comments
 (0)