<?php
/**
 * EU-Cookies - VERITAS DATA GmbH
 * 
 * @package Module mod-eu-cookies for Joomla! 3.9
 * @version $Id: default.php v 1.20 2019-08-01 10:26:33 VD $
 * @author VERITAS DATA GmbH
 * @copyright (C) 2010-2016 VERITAS DATA GmbH
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 * 
 */
defined('_JEXEC') or die('Restricted access');

//JHtml::_('behavior.framework');
JHtml::_('jquery.framework');
JHtml::_('bootstrap.framework');

# Get the document object
$document = JFactory::getDocument();

# Load CSS
$document->addStyleSheet(JURI_MODULE_ASSETS . 'bootstrap/css/bootstrap.css');
$document->addStyleSheet(JURI_MODULE_ASSETS_CSS . 'eucookies.css' . '?' . ($settings->basics->cookie_debug ? time() : $settings->basics->version));

# Load JavaScript
//$document->addScript(JURI_MODULE_ASSETS . 'bootstrap/js/bootstrap.min.js');
$document->addScript(JURI_MODULE_ASSETS_JS . 'eucookies.js' . '?' . ($settings->basics->cookie_debug ? time() : $settings->basics->version));
$document->setMetaData('X-UA-Compatible', 'ie=edge', true);

# Add custom scripts
$document->addScriptDeclaration('
    
/* EU-Cookies Settings */
EUCookie.Version = "' . $settings->basics->version . ' ' . $settings->basics->language_code . '";
EUCookie.Name = "' . $settings->basics->cookie_ref . '";
EUCookie.Lifetime = ' . $settings->basics->lifetime . ';
EUCookie.DefaultValue = Boolean(' . json_encode($settings->basics->cookie_consent) . ');
EUCookie.Debug = Boolean(' . json_encode($settings->basics->cookie_debug) . ');

EUCookie.loadCookie();
if(EUCookie.Consent.time !== 0) {
    EUCookie.Consent.list.forEach((item, index) => {
        if(item != "undefined") {
            EUCookie.addConsent(index, item);
        }
    });
}

jQuery(document).ready(function () {
    EUCookie.render(' . $settings->basics->landing_page . ');
});
');

foreach ($settings->cookies as $cookie_setting) :
    $document->addScriptDeclaration('
        if(EUCookie.Consent.time === 0) {
            /* EU-Cookies add consent from settings */
            consent = {
                "id":' . intval($cookie_setting->id) . ',
                "name":"' . $cookie_setting->name . '",
                "type":"' . $cookie_setting->type . '",
                "agree":' . intval($cookie_setting->agreement) . '
            }
            EUCookie.addConsent(' . intval($cookie_setting->id) . ', consent);
        }
    ');
endforeach;

if ($settings->use_ga && !is_null($settings->ga_js_snippet)) :
    # Add ga/analytics/gtag js code
    $document->addCustomTag($settings->ga_js_snippet);
    $document->addScriptDeclaration('
            jQuery(document).ready(function(){
                EUCookie.GA.Property = "' . (property_exists($settings, 'gaProperty') ? $settings->gaProperty : '') . '";
                EUCookie.GA.DisableString = "ga-disable-" + EUCookie.GA.Property;
                if(typeof EUCookie.Consent.list[2] != "undefined") {
                    if (EUCookie.Consent.list[2].agree === 1) {
                        EUCookie.GA.optIn();
                    } else {
                        EUCookie.GA.optOut();
                    }
                }
            });
        ');

endif;

if ($settings->use_gtm && !is_null($settings->gtm_js_snippet)) :
    # Add gtm js code
    $document->addCustomTag($settings->gtm_js_snippet);
    $document->addScriptDeclaration('
            jQuery(document).ready(function(){
                EUCookie.GTM.ContainerId = "' . (property_exists($settings, 'gtmContainerId') ? $settings->gtmContainerId : '') . '";
                if(typeof EUCookie.Consent.list[2] != "undefined") {
                    if (EUCookie.Consent.list[2].agree === 1) {
                        EUCookie.GTM.optIn();
                    } else {
                        EUCookie.GTM.optOut();
                    }
                }
            });
        ');
    $document->addScriptDeclaration('
        jQuery(document).ready(function(){
            jQuery("body").prepend("<noscript>' . $settings->gtm_ns_snippet . '</noscript>");
        });
    ');
endif;

if ($settings->use_matomo && !is_null($settings->matomo_js_snippet)) :
    # Add matomo/piwik js code
    $document->addCustomTag($settings->matomo_js_snippet);
    # Add matomo/piwik consent
    $document->addCustomTag('
            <!-- Add matomo/piwik consent --> 
            <script>
            EUCookie.Matomo.SiteId = "' . (property_exists($settings, 'matomoSiteId') ? $settings->matomoSiteId : '') . '";
            EUCookie.Matomo.Url = "' . (property_exists($settings, 'matomoUrl') ? $settings->matomoUrl : '') . '";

            if(typeof EUCookie.Consent.list[2] != "undefined") {
                if (EUCookie.Consent.list[2].agree) {
                    EUCookie.Matomo.optIn();
                } else {
                    EUCookie.Matomo.optOut();
                }
            }
            </script>
            <!-- End matomo/piwik consent -->
        ');
endif;

if ($settings->use_fb && !is_null($settings->fb_js_snippet)) :
    # Add fb js code
    $document->addCustomTag($settings->fb_js_snippet);
    # Add fb consent
    $document->addCustomTag('
            <!-- Add Facebook Pixel consent --> 
            <script>
                EUCookie.FB.ID = "' . (property_exists($settings, 'fbId') ? $settings->fbId : '') . '";
                if(typeof EUCookie.Consent.list[2] != "undefined") {
                    if (EUCookie.Consent.list[2].agree === 1) {
                        EUCookie.FB.optIn();
                    } else {
                        EUCookie.FB.optOut();
                    }
                }
            </script>
            <!-- End Facebook Pixel consent -->
        ');
    $document->addScriptDeclaration('
        jQuery(document).ready(function(){
            jQuery("body").prepend("<noscript>' . $settings->fb_ns_snippet . '</noscript>");
        });
    ');
endif;

$css_classes = $settings->css['classes'];
$css_styles = $settings->css['styles'];
$buttons = $settings->buttons;
$captions = $settings->captions;

$document_style = "
    #mod-eu-cookies {
        background: {$css_styles->bgcolor};
    }
    #mod-eu-cookies *, #mod-eu-cookies p, #mod-eu-cookies span, #mod-eu-cookies h6 {
        font-size: {$css_styles->font_size}px;
        font-family: {$css_styles->font_family};
        color: {$css_styles->font_color};
    }
    #mod-eu-cookies #overview-caption * {
        font-size: {$css_styles->captions['overview']->font_size}px;
        font-family: {$css_styles->captions['overview']->font_family};
        color: {$css_styles->captions['overview']->font_color};
    }
    #mod-eu-cookies #details-caption * {
        font-size: {$css_styles->captions['details']->font_size}px;
        font-family: {$css_styles->captions['details']->font_family};
        color: {$css_styles->captions['details']->font_color};
    }
    #mod-eu-cookies #eu_cookie_decline {
        color: {$css_styles->buttons['close']->font_color};
    }
    #mod-eu-cookies #eu-cookies-accept button,
    #mod-eu-cookies #eu-cookies-accept-all button {
        border-color: {$css_styles->buttons['accept']->bordercolor} !important;
        background-color: {$css_styles->buttons['accept']->bgcolor} !important;
        font-size: {$css_styles->buttons['accept']->font_size}px;
        font-family: {$css_styles->buttons['accept']->font_family};
        color: {$css_styles->buttons['accept']->font_color};
    }
    #mod-eu-cookies #eu-cookies-settings button {
        border-color: {$css_styles->buttons['settings']->bordercolor} !important;
        background-color: {$css_styles->buttons['settings']->bgcolor} !important;
        font-size: {$css_styles->buttons['settings']->font_size}px;
        font-family: {$css_styles->buttons['settings']->font_family};
        color: {$css_styles->buttons['settings']->font_color};
    }
    #mod-eu-cookies #eu-cookies-more-info button {
        border-color: {$css_styles->buttons['more']->bordercolor} !important;
        background-color: {$css_styles->buttons['more']->bgcolor} !important;
        font-size: {$css_styles->buttons['more']->font_size}px;
        font-family: {$css_styles->buttons['more']->font_family};
        color: {$css_styles->buttons['more']->font_color};
    }
    #mod-eu-cookies #eu-cookies-save button {
        border-color: {$css_styles->buttons['save']->bordercolor} !important;
        background-color: {$css_styles->buttons['save']->bgcolor} !important;
        font-size: {$css_styles->buttons['save']->font_size}px;
        font-family: {$css_styles->buttons['save']->font_family};
        color: {$css_styles->buttons['save']->font_color};
    }
    #mod-eu-cookies #eu-cookies-accept button:hover,
    #mod-eu-cookies #eu-cookies-accept-all button:hover {
        color: {$css_styles->buttons['accept']->font_color} !important;
    }
    #mod-eu-cookies #eu-cookies-settings button:hover {
        color: {$css_styles->buttons['settings']->font_color} !important;
    }
    #mod-eu-cookies #eu-cookies-more-info button:hover {
        color: {$css_styles->buttons['more']->font_color} !important;
    }
    #mod-eu-cookies #eu-cookies-save button:hover {
        color: {$css_styles->buttons['save']->font_color} !important;
    }
    #mod-eu-cookies-icon {
        {$css_styles->icons['icon']->position}: 0;
    }
";

$document->addStyleDeclaration($document_style);

$msie_style = '<!--[if lt IE 9]>' . "\n";
$msie_style .= '<style type="text/css">' . "\n";
$msie_style .= "background-color: {$css_styles->bgcolor} ;" . "\n";
$msie_style .= "opacity: {$css_styles->opacity};" . "\n";
$msie_style .= '</style>' . "\n";
$msie_style .= '<![endif]-->' . "\n";

$document->addCustomTag($msie_style);
?>

<div id="mod-eu-cookies" class="<?php echo $css_classes->position; ?>" style="display: none">
    <div class="eu-cookies-wrapper">
        <div id="eu-cookies-box">
            <div id="eu-cookies-privacy-content">
                <?php if (boolval($buttons['close']->show)) : ?>
                    <a href="javascript:void(0)" id="eu-cookies-deny" title="<?php echo $buttons['close']->mouseover_text; ?>" >
                        <button id="eu_cookie_decline" class="xclose">
                            &times;
                        </button>
                    </a>
                <?php endif; ?>

                <div class="user-content">
                    <?php if ($captions['overview']->show) : ?>
                        <div class="full-width">
                            <div id="overview-caption">
                                <<?php echo $captions['overview']->htmltag; ?>>
                                <?php echo $captions['overview']->text; ?>
                                </<?php echo $captions['overview']->htmltag; ?>>
                            </div>
                        </div>
                    <?php endif; ?>

                    <!-- EU-Cookies Message -->
                    <div class="full-width scroll-150">
                        <?php echo $settings->basics->message ?>
                    </div>
                </div>

                <!-- EU-Cookies Accept Button -->
                <div class="cookie-accept">
                    <?php if (boolval($buttons['accept']->show)) : ?>
                        <a href="javascript:void(0)" id="<?php echo $buttons['accept']->behavior == 0 ? "eu-cookies-accept" : "eu-cookies-accept-all"; ?>" class="overview-btn <?php echo $css_classes->accept; ?>">
                            <<?php echo $buttons['accept']->htmltag; ?>>
                            <?php echo $buttons['accept']->text; ?>
                            </<?php echo $buttons['accept']->htmltag; ?>>
                        </a>
                    <?php endif; ?>
                </div>

                <!-- EU-Cookies Settings Button -->
                <div class="cookie-settings">
                    <?php if (boolval($buttons['settings']->show)) : ?>
                        <a href="javascript:void(0)" id="eu-cookies-settings" class="overview-btn <?php echo $css_classes->settings; ?>">
                            <<?php echo $buttons['settings']->htmltag; ?>>
                            <?php echo $buttons['settings']->text; ?>
                            </<?php echo $buttons['settings']->htmltag; ?>>
                        </a>
                    <?php endif; ?>
                </div>

                <div class="cookie-more-information">
                    <!-- EU-Cookies More Button -->
                    <?php if (boolval($buttons['more']->show)) : ?>
                        <a href="<?php echo $buttons['more']->url ?>" target="<?php echo $buttons['more']->target; ?>" id="eu-cookies-more-info" class="overview-btn <?php echo $css_classes->more; ?>">
                            <<?php echo $buttons['more']->htmltag; ?>>
                            <?php echo $buttons['more']->text; ?>
                            </<?php echo $buttons['more']->htmltag; ?>>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div id="eu-cookies-details">
            <div id="eu-cookies-privacy-content">
                <?php if ($captions['details']->show) : ?>
                    <div class="full-width">
                        <div id="details-caption">
                            <<?php echo $captions['details']->htmltag; ?>>
                            <?php echo $captions['details']->text; ?>
                            </<?php echo $captions['details']->htmltag; ?>>
                        </div>
                    </div>
                <?php endif; ?>

                <?php foreach ($settings->cookies as $cookie) : ?>
                    <div id="cookie-type-<?php echo $cookie->id; ?>" class="checkbox <?php echo $cookie->readonly ? "disabled" : ""; ?>">
                        <div class="row">
                            <div class="col-xs-8">
                                <<?php echo $cookie->tag; ?>>
                                <?php echo $cookie->name; ?>
                                </<?php echo $cookie->tag; ?>>
                            </div>
                            <div class="col-xs-4 content-right">
                                <input type="checkbox"
                                       id="cookie-checkbox-<?php echo $cookie->id; ?>"
                                       name="cookie-type-<?php echo $cookie->id; ?>"
                                       value="1"
                                       <?php echo $cookie->readonly ? "disabled" : ""; ?>
                                       data-toggle="toggle" data-style="slow" data-onstyle="success" data-offstyle="danger"
                                       data-on="<?php echo JText::_('MOD_EU_COOKIES_ON'); ?>"
                                       data-off="<?php echo JText::_('MOD_EU_COOKIES_OFF'); ?>"
                                       <?php echo $cookie->checkbox_checked ? "checked" : ""; ?>
                                       onchange="EUCookie.changeConsent(<?php echo intval($cookie->id); ?>, Number(this.checked));">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <p class="cookie-explain-content">
                                    <?php echo $cookie->text; ?>
                                </p>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>

                <!-- EU-Cookies Save Button -->
                <div class="cookie-save">
                    <a href="javascript:void(0)" id="eu-cookies-save" class="<?php echo $css_classes->save; ?>">
                        <<?php echo $buttons['save']->htmltag; ?>>
                        <?php echo $buttons['save']->text; ?>
                        </<?php echo $buttons['save']->htmltag; ?>>
                    </a>
                </div>

                <div class="site-links">
                    <?php if ($buttons['privacy']->show && !empty($buttons['privacy']->url)) : ?>
                        <!-- EU-Cookies Link Datenschutz -->
                        <a href="<?php echo $buttons['privacy']->url ?>" target="<?php echo $buttons['privacy']->target ?>" class="link-datenschutz">
                            <?php echo $buttons['privacy']->text ?>
                        </a>
                    <?php endif; ?>
                    <?php if ($buttons['impressum']->show && !empty($buttons['impressum']->url)) : ?>
                        <!-- EU-Cookies Link Impressum -->
                        <a href="<?php echo $buttons['impressum']->url ?>" target="<?php echo $buttons['impressum']->target ?>" class="link-impressum">
                            <?php echo $buttons['impressum']->text ?>
                        </a>
                    <?php endif; ?>
                    <?php if ($buttons['aboutcookies']->show && !empty($buttons['aboutcookies']->url)) : ?>
                        <!-- EU-Cookies Link About Cookies -->
                        <a href="<?php echo $buttons['aboutcookies']->url ?>" target="<?php echo $buttons['aboutcookies']->target ?>" class="link-aboutcookies">
                            <?php echo $buttons['aboutcookies']->text ?>
                        </a>
                    <?php endif; ?>
                </div>


            </div>
        </div>

        <div id="back-link" class="content-right">
            <span>
                <?php echo JText::_('BACK'); ?>
            </span>
        </div>
        <div id="close-link-hidden" class="content-right">
            <span>
                <?php echo JText::_('JCANCEL'); ?>
            </span>
        </div>
    </div>
</div>
<?php if ($settings->basics->overlay_show) : ?>
    <div id="mod-eu-cookies-overlay"></div>
<?php endif; ?>

<?php if ($settings->basics->icon->show) : ?>
    <div id="mod-eu-cookies-icon" style="display: none">
        <a href="#" onclick="EUCookie.showSettings()" title="<?php echo $settings->basics->icon->text; ?>">
            <img src="<?php echo $settings->basics->icon->src; ?>" class="icon" />
        </a>
    </div>
<?php endif; ?>