Skip to content

Instantly share code, notes, and snippets.

@IngoWinter
Last active September 2, 2020 05:37
Show Gist options
  • Save IngoWinter/31df14685b45ad8980aadaec1e757363 to your computer and use it in GitHub Desktop.
Save IngoWinter/31df14685b45ad8980aadaec1e757363 to your computer and use it in GitHub Desktop.
cookie gedöns modul

input.php/output.php: modul ein- und ausgabe, um html/scripte bei consent nachzuladen

module.js: lädt html/scripte des moduls bei consent ohne reload nach

<?php
$mform = new mform();
$cookies = [];
$qry = 'SELECT uid,service_name FROM '.rex::getTable('iwcc_cookie').' WHERE clang_id = '.rex_clang::getCurrentId();
foreach (rex_sql::factory()->getArray($qry) as $v) {
if ($v['uid'] == 'iwcc') continue;
$cookies[$v['uid']] = $v['service_name'];
}
$mform->addSelectField(1);
$mform->setOptions($cookies);
$mform->setSize(1);
$mform->setLabel('Dienst');
$mform->addTextAreaField(2, ['label' => 'HTML/JS das bei Consent geladen wird']);
$mform->addCheckboxField(5, [1 => 'Seitenreload nötig']);
$mform->addTextAreaField(3, ['label' => 'Platzhaltertext']);
$mform->addMediaField(1, ['label' => 'Platzhalterbild']);
echo $mform->show();
jQuery(function ($) {
'use strict';
$('.iwcc-module').each(function () {
var $this = $(this),
uid = $this.data('uid');
$(document).on('iwcc-saved', function (e) {
var consents = JSON.parse(e.originalEvent.detail);
consents.forEach(function (v) {
if (v === uid) {
$this.html(window.atob($this.data('payload')));
}
});
});
});
});
<?php
$serviceName = '';
$cookieUid = 'REX_VALUE[1]';
$needsReload = (bool)'REX_VALUE[5]' ? '-reload' : '';
$consented = false;
$placeholderImage = '';
$placeholderText = '';
$iwcc = new iwcc_frontend();
$iwcc->setDomain($_SERVER['HTTP_HOST']);
// "globale" platzhalter aus dem addon setzen
if (isset($iwcc->cookies[$cookieUid])) {
$placeholderImage = $iwcc->cookies[$cookieUid]['placeholder_image'];
$placeholderText = $iwcc->cookies[$cookieUid]['placeholder_text'];
}
if (isset($_COOKIE['iwcc'])) {
$cookieData = json_decode($_COOKIE['iwcc'], true);
foreach ($cookieData['consents'] as $consent) {
if ($cookieUid == $consent) {
$consented = true;
break;
}
}
}
?>
<?php if (rex::isFrontend()): ?>
<?php if ($consented): ?>
<div class="iwcc-module" data-uid="<?= $cookieUid ?>">
REX_VALUE[2 output=html]
</div>
<?php else: ?>
<div class="iwcc-module" data-payload="<?= base64_encode('REX_VALUE[2 output=html]') ?>" data-uid="<?= $cookieUid ?>">
<div class="iwcc-module__placeholder">
<div class="iwcc-module__placeholder-image">
<img src="/media/<?= ('REX_MEDIA[1]' ? 'REX_MEDIA[1]' : $placeholderImage) ?>" alt="">
</div>
<div class="iwcc-module__placeholder-text">
<div class="iwcc-module__placeholder-text-background">
<?= nl2br('REX_VALUE[3 output=html]' ? 'REX_VALUE[3 output=html]' : $placeholderText) ?>
<div class="iwcc-show-box<?= $needsReload ?>"><b>Datenschutz-Einstellungen anpassen</b></div>
</div>
</div>
</div>
</div>
<?php endif ?>
<?php else: ?>
<label><?= $serviceName ?></label>
<textarea readonly disabled style="width:100%;" id="iwcc-module-textarea-REX_SLICE_ID">REX_VALUE[2 output=html]</textarea>
<?php endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment