Skip to content

Commit

Permalink
fix pdf printing (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Feb 26, 2021
1 parent fb9c110 commit 3dd4402
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
}
],
"require":{
"php": ">=7.1",
"contao/core-bundle": "^3.5 || ^4.2",
"php": "^7.1",
"contao/core-bundle": "^4.4",
"contao-community-alliance/composer-plugin": "^2.4 || ^3.0"
},
"replace": {
Expand Down
82 changes: 67 additions & 15 deletions system/modules/sharebuttons/classes/ShareButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
* @license LGPL-3.0-or-later
*/

use Contao\ArticleModel;
use Contao\ContentModel;
use Contao\DataContainer;
use Contao\Input;
use Contao\StringUtil;

class ShareButtons
{
Expand Down Expand Up @@ -79,9 +84,8 @@ public static function createShareButtons($networks, $theme = self::DEFAULT_THEM
$objArticle = $articleId ? \ArticleModel::findById($articleId) : \ArticleModel::findPublishedByPidAndColumn($objPage->id, 'main');

if (null !== $objArticle) {
$articleAlias = $objArticle->alias ?: $objArticle->id;
$articleHref = '/articles/' . (($objArticle->inColumn != 'main') ? $objArticle->inColumn . ':' : '') . $articleAlias;
$objButtonsTemplate->pdfLink = $objArticle->getRelated('pid')->getFrontendUrl($articleHref) . '?getpdf=' . $objArticle->id;
$request = Environment::get('indexFreeRequest');
$objButtonsTemplate->pdfLink = $request . ((strpos($request, '?') !== false) ? '&' : '?') . 'pdf=' . $objArticle->id;
}
}

Expand Down Expand Up @@ -314,7 +318,8 @@ public function replaceInsertTag( $strTag, $blnCache = false )


/**
* isVisibleElement hook
* This dynamically allows PDF printing by checking whether a "pdf" share
* network is enabled for the article itself or one of its content elements.
*
* @param Model $objElement
* @param bool $blnReturn
Expand All @@ -323,17 +328,55 @@ public function replaceInsertTag( $strTag, $blnCache = false )
*/
public function isVisibleElement(\Model $objElement, bool $blnReturn): bool
{
if ($objElement instanceof \ArticleModel && null !== ($articleId = \Input::get('getpdf')) && (int) $objElement->id === (int) $articleId) {
\Input::setGet('pdf', $articleId);
$printable = $objElement->printable;
if (!$objElement instanceof ArticleModel) {
return $blnReturn;
}

if ((int) $printable !== 1) {
$options = $printable ? \StringUtil::deserialize($printable) : [];
$articleId = Input::get('pdf');

if (!\in_array('pdf', $options)) {
$options[] = 'pdf';
$objElement->printable = serialize($options);
}
if (null === $articleId || (int) $objElement->id !== (int) $articleId) {
return $blnReturn;
}

$printable = $objElement->printable;

if ((int) $printable === 1) {
return $blnReturn;
}

$options = StringUtil::deserialize($printable, true);

if (\in_array('pdf', $options)) {
return $blnReturn;
}

$articleNetworks = StringUtil::deserialize($objElement->sharebuttons_networks, true);

if (\in_array('pdf', $articleNetworks)) {
$options[] = 'pdf';
$objElement->printable = serialize($options);

return $blnReturn;
}

$elements = ContentModel::findPublishedByPidAndTable((int) $objElement->id, $objElement->getTable());

if (null === $elements) {
return $blnReturn;
}

foreach ($elements as $element) {
if ('sharebuttons' !== $element->type) {
continue;
}

$elementNetworks = StringUtil::deserialize($element->sharebuttons_networks, true);

if (\in_array('pdf', $elementNetworks)) {
$options[] = 'pdf';
$objElement->printable = serialize($options);

return $blnReturn;
}
}

Expand All @@ -344,17 +387,26 @@ public function isVisibleElement(\Model $objElement, bool $blnReturn): bool
/**
* DCA functions
*/
public function getNetworks()
public function getNetworks(DataContainer $dc)
{
return $GLOBALS['sharebuttons']['networks'];
$networks = $GLOBALS['sharebuttons']['networks'];

// Only allow PDF button in article settings and as a content element
if (!\in_array($dc->table, ['tl_article', 'tl_content'], true)) {
unset($networks['pdf']);
}

return $networks;
}

public function getButtonThemes()
{
$themes = array( '' => $GLOBALS['TL_LANG']['sharebuttons']['no_theme'] );
foreach( $GLOBALS['sharebuttons']['themes'] as $k => $v )
$themes[$k] = $v[0];
return $themes;
}

public function getSharebuttonsTemplates()
{
return \Controller::getTemplateGroup('sharebuttons_');
Expand Down

0 comments on commit 3dd4402

Please sign in to comment.