Skip to content

Commit

Permalink
refactored the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
msenkpiel committed Apr 12, 2017
1 parent 69c7dd9 commit 707999c
Show file tree
Hide file tree
Showing 9 changed files with 994 additions and 0 deletions.
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CookieNotice Pimcore Plugin
================================================

Developer info: [Pimcore at Basilicom](http://basilicom.de/en/pimcore)

## Code Example / Method of Operation

After installing the plugin, configuration options are exposed via the
configure button in the Extension Manager.

## Installation

Add "basilicom-pimcore/cookie-notice" as a requirement to the composer.json
in the toplevel directory of your Pimcore installation. Then enable and install
the plugin in Pimcore Extension Manager (under Extras > Extensions)

Example:

{
"require": {
"basilicom-pimcore-plugin/protected-admin": ">=1.0.0"
}
}

## Contributors

* Marcel Eichhorn marcel.eichhorn@basilicom.de
* Marco Senkpiel marco.senkpiel@basilicom.de

## License

* GNU General Public License version 3 (GPLv3)

24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "basilicom-pimcore-plugin/cookie-notice",
"type": "pimcore-plugin",
"license": "GPLv3",
"description": "A Pimcore plugin that adds an editable cookie notice to your project.",
"keywords": ["pimcore", "plugin", "cookie", "notice"],
"homepage": "http://basilicom.de/en/pimcore",
"authors": [
{
"name": "Basilicom Team",
"email": "info@basilicom.de",
"homepage": "http://basilicom.de/en/pimcore",
"role": "Developer"
}
],
"require": {
"pimcore/installer-plugin": ">=1"
},
"autoload": {
"psr-4": {
"CookieNotice\\": "lib"
}
}
}
43 changes: 43 additions & 0 deletions controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php


class CookieNotice_IndexController extends \Website\Controller\Action
{
public function indexAction()
{

}

public function jsAction()
{
$this->disableViewAutoRender();
$this->getResponse()->setHeader("Content-Type", "application/javascript");

$text = $this->view->translate('cookie-notice-plugin_text');
$label = $this->view->translate('cookie-notice-plugin_button-label');
$lifetime = \Pimcore\Config::getWebsiteConfig()->get('cookieNoticeLifetime', 30);

if($text == '' || $text == 'cookie-notice-plugin_text'){
$text = 'Diese Internetseite verwendet Cookies, um die Nutzererfahrung zu verbessern und den Benutzern bestimmte Dienste und Funktionen bereitzustellen.';
}

if($label == '' || $label == 'cookie-notice-plugin_button-label') {
$label = 'Akzeptieren';
}

$js = file_get_contents(PIMCORE_PLUGINS_PATH . \CookieNotice\Plugin::COOKIE_NOTICE_CONFIG_JS) . PHP_EOL;
$js .= 'new CookieNotice("' . $text . '", "' . $label . '", "' . $lifetime . '");';

echo $js;
}

public function cssAction()
{
$this->disableViewAutoRender();
$this->getResponse()->setHeader("Content-Type", "text/css");

$css = file_get_contents(PIMCORE_WEBSITE_PATH . \CookieNotice\Plugin::COOKIE_NOTICE_CONFIG_CUSTOM_CSS);

echo $css;
}
}
69 changes: 69 additions & 0 deletions lib/CookieNotice/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace CookieNotice;

use Pimcore\API\Plugin as PluginLib;
use Pimcore\Db;

class Plugin extends PluginLib\AbstractPlugin implements PluginLib\PluginInterface
{
const COOKIE_NOTICE_CONFIG_DEFAULT_CSS = '/CookieNotice/static/css/cookienotice.sample.css';
const COOKIE_NOTICE_CONFIG_CUSTOM_CSS = '/var/config/cookienotice.css';
const COOKIE_NOTICE_CONFIG_JS = '/CookieNotice/static/js/cookienotice.js';
const COOKIE_NOTICE_CONFIG_LIFETIME = 'cookieNoticeLifetime';
const COOKIE_NOTICE_CONFIG_LIFETIME_DEFAULT = '30';
const DB_TABLE_WEBSITE_SETTINGS = 'website_settings';

public function init()
{
parent::init();

\Pimcore::getEventManager()->attach("frontend.controller.postInit", function ($event) {
$event->getTarget()->view->headLink()->appendStylesheet('/plugin/CookieNotice/index/css');
$event->getTarget()->view->headScript()->appendFile('/plugin/CookieNotice/index/js');
});
}

public function handleDocument($event)
{
}

public static function install()
{
$database = Db::get();

$database->insert(self::DB_TABLE_WEBSITE_SETTINGS, [
'name' => self::COOKIE_NOTICE_CONFIG_LIFETIME,
'type' => 'text',
'data' => self::COOKIE_NOTICE_CONFIG_LIFETIME_DEFAULT
]);

if (!file_exists(self::getCustomConfigCss())) {
$defaultContent = file_get_contents(self::getDefaultConfigCss());
file_put_contents(self::getCustomConfigCss(), $defaultContent);
}
}

public static function uninstall()
{
if (file_exists(self::getCustomConfigCss())) {
unlink(self::getCustomConfigCss());
}
}

public static function isInstalled()
{
return file_exists(self::getCustomConfigCss());
}

private static function getCustomConfigCss()
{
return PIMCORE_WEBSITE_PATH . self::COOKIE_NOTICE_CONFIG_CUSTOM_CSS;
}

private static function getDefaultConfigCss()
{
return PIMCORE_PLUGINS_PATH . self::COOKIE_NOTICE_CONFIG_DEFAULT_CSS;
}

}
34 changes: 34 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<zend-config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
<plugin>

<!-- the plugin name [A-Za-z_] -->
<pluginName>CookieNotice</pluginName>

<!-- a short description -->
<pluginDescription>Displays a notification bar regarding the EU cookie law</pluginDescription>

<!-- meta data -->
<pluginVersion>1.0</pluginVersion>
<pluginRevision>1</pluginRevision>
<pluginBuildTimestamp>0</pluginBuildTimestamp>

<!-- put in the URL for the plugin configuration eg. /plugin/CookieNotice/admin/index -->
<pluginIframeSrc></pluginIframeSrc>

<!-- className of the plugin which extends Pimcore_API_Plugin_Abstract-->
<pluginClassName>\CookieNotice\Plugin</pluginClassName>

<!-- include paths relative to plugin-directory -->
<pluginIncludePaths>
<path>/CookieNotice/lib</path>
</pluginIncludePaths>

<!-- namespaces to register with autoloader-->
<pluginNamespaces>
<namespace>CookieNotice</namespace>
</pluginNamespaces>

<pluginXmlEditorFile>/website/var/config/cookienotice.css</pluginXmlEditorFile>
</plugin>
</zend-config>
24 changes: 24 additions & 0 deletions static/css/cookienotice.sample.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.cookie-notice {
position: fixed;
bottom: 0px;
left: 0px;
width: 100%;
background: #efefef;
padding: 10px;
z-index: 999; }
.cookie-notice__inner {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
max-width: 1180px;
margin: 0 auto; }
.cookie-notice__text {
color: #333333; }
.cookie-notice__button {
margin: 0 0 0 15px;
border: 2px solid #333333;
padding: 10px;
color: #333333;
background-color: #ffffff;
cursor: pointer; }
32 changes: 32 additions & 0 deletions static/css/cookienotice.sample.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.cookie-notice {
position: fixed;
bottom: 0px;
left: 0px;
width: 100%;
background: #efefef;
padding: 10px;
z-index: 999;

&__inner {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
max-width: 1180px;
margin: 0 auto;
}

&__text {
color: #333333;
}

&__button {
margin: 0 0 0 15px;
border: 2px solid #333333;
padding: 10px;
color: #333333;
background-color: #ffffff;
cursor: pointer;
}
}

60 changes: 60 additions & 0 deletions static/js/cookienotice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var CookieNotice = function(text, label, lifetime) {

var cookieName = 'cookie-notice-accepted',
$wrapper,
$wrapperInner,
$textContainer,
$button,

init = function() {
var splitted = document.cookie.split(';'),
cookieFound = false;

for (var i = 0; i < splitted.length; i++) {
if (splitted[i].trim() == cookieName + '=1') {
cookieFound = true;
break;
}
}
if (!cookieFound) {
createElements();
}
},

createElements = function() {
$wrapper = document.createElement('div');
$wrapper.setAttribute('class', 'cookie-notice');
$wrapper.setAttribute('id', 'cookieNotice');

$wrapperInner = document.createElement('div');
$wrapperInner.setAttribute('class', 'cookie-notice__inner');

$textContainer = document.createElement('div');
$textContainer.setAttribute('class', 'cookie-notice__text');
$textContainer.innerHTML = text;

$button = document.createElement('div');
$button.setAttribute('class', 'cookie-notice__button');
$button.innerHTML = label;
$button.addEventListener('click', onButtonClick);

$wrapper.append($wrapperInner);
$wrapperInner.append($textContainer);
$wrapperInner.append($button);

document.body.append($wrapper);
},

onButtonClick = function(e) {
var days = (lifetime && parseInt(lifetime) > 0) ? parseInt(lifetime) : 30,
date = new Date();

date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = cookieName + "=1; expires=" + date.toUTCString();
$wrapper.parentNode.removeChild($wrapper);
};

window.onload = function() {
init();
};
};

0 comments on commit 707999c

Please sign in to comment.