1313 */
1414
1515use Contao \ArticleModel ;
16+ use Contao \CalendarModel ;
1617use Contao \ContentModel ;
18+ use Contao \Controller ;
1719use Contao \DataContainer ;
20+ use Contao \Environment ;
21+ use Contao \FrontendTemplate ;
1822use Contao \Input ;
23+ use Contao \Model ;
24+ use Contao \Module ;
25+ use Contao \ModuleNews ;
26+ use Contao \NewsArchiveModel ;
1927use Contao \StringUtil ;
28+ use Contao \System ;
29+ use Contao \Template ;
2030
2131class 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}
0 commit comments