Skip to content

Commit

Permalink
Merge pull request #1 from clash82/initial
Browse files Browse the repository at this point in the history
EZP-24150: Implement privacy cookie law in demo bundle
  • Loading branch information
andrerom committed Apr 8, 2015
2 parents 7741e56 + 18a53b1 commit 02679d9
Show file tree
Hide file tree
Showing 15 changed files with 535 additions and 1 deletion.
33 changes: 33 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration;

class Configuration extends SiteAccessConfiguration
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('ez_privacy_cookie');
$systemNode = $this->generateScopeBaseNode($rootNode);

$systemNode
->scalarNode('cookie_name')
->defaultValue('privacyCookieAccepted')
->info('name to be used to store cookie status')
->isRequired()
->end()
->integerNode('days')
->defaultValue('365')
->info('how many days banner should be hidden when user accepts policy?')
->isRequired()
->end();

return $treeBuilder;
}
}
24 changes: 24 additions & 0 deletions DependencyInjection/EzSystemsPrivacyCookieExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class EzSystemsPrivacyCookieExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('default_settings.yml');
}
}
12 changes: 12 additions & 0 deletions EzSystemsPrivacyCookieBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class EzSystemsPrivacyCookieBundle extends Bundle
{
}
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
# EzSystemsPrivacyCookieBundle
# Privacy Cookie Bundle

This bundle adds privacy cookie banner into Symfony 2 application (eZ Publish / eZ Platform).

## Requirements

- any Symfony v2.x application
- BootstrapJS v3.x

## Installation
This package is available via composer, so the instructions below are similar to how you install any other open source Symfony Bundle.

Run the following command in a terminal, from your Symfony installation root (pick most recent release):
```
php composer.phar require ezsystems/privacy-cookie-bundle
```

Enable the bundle in `app/AppKernel.php` (for regular Symfony application) or `ezpublish\EzPublishKernel.php` (for eZ Publish / eZ Platform):
```
$bundles = array(
// existing bundles
new EzSystems\PrivacyCookieBundle\EzSystemsPrivacyCookieBundle()
);
```

Add external assets to your bundle:

- CSS:
```
bundles/ezsystemsprivacycookie/css/privacycookie.css
```

- JS:
```
bundles/ezsystemsprivacycookie/js/privacycookie.js
```

You should also add BootstrapJS libraries into your project.

If you are installing bundle via `composer require` you must also copy assets to your project `web` directory. You can do this by calling Symfony built-in command from the project root directory:

```
php app/console assets:install
```

If you are using eZ Publish / eZ Platform:

```
php ezpublish/console assets:install
```

## Usage

Place following helper `{{ show_privacy_cookie_banner(%privacy_policy_url%) }}` somewhere in your footer template before body ending tag. Replace `%privacy_policy_url%` with your policy page address.

Note that %privacy_policy_url% is not required, in this case no policy link will be shown.

Optional parameters can be set as a second argument in an array format:

Parameter | Default value | Description
------------- | ---------------------------------------------- | -----------
cookieName | privacyCookieAccepted | Sets your own status cookie name
days | 365 | How many days privacy banner should be hidden when user accepts policy?
bannerCaption | Cookies help us create a good experience (...) | Sets your own banner message caption

Example usage:

```
{{ show_privacy_cookie_banner('http://ez.no/Privacy-policy') }}
```

or

```
{{ show_privacy_cookie_banner(path('ez_urlalias', {contentId: 94}), {
cookieName: 'myCookie',
days: 7,
bannerCaption: 'Nice to see you here'}) }}
```
6 changes: 6 additions & 0 deletions Resources/config/default_settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
# how many days banner should be hidden when user accepts policy?
ez_privacy_cookie.days: 365

# name to be used to store cookie status
ez_privacy_cookie.cookie_name: "privacyCookieAccepted"
12 changes: 12 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
ez_privacy_cookie_twig_extension: EzSystems\PrivacyCookieBundle\Twig\PrivacyCookieTwigExtension

services:
ez_privacy_cookie.twig.extension:
class: %ez_privacy_cookie_twig_extension%
arguments:
- @service_container
- %ez_privacy_cookie.cookie_name%
- %ez_privacy_cookie.days%
tags:
- { name: twig.extension }
115 changes: 115 additions & 0 deletions Resources/public/css/privacycookie.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
.privacy-cookie-banner {
background: #006A8D;
overflow: hidden;
-webkit-transition: all .5s linear;
transition: all .5s linear;
max-height: 0;
opacity: 0;
bottom: 0;
left: 0;
right: 0;
position: fixed;
z-index: 1024;
}

.privacy-cookie-banner--slide-down {
-webkit-transform: translateY(100%);
-ms-transform: translateY(100%);
transform: translateY(100%);
opacity: 0;
max-height: 0;
}

.privacy-cookie-banner--slide-up {
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
opacity: 1;
max-height: 145px;
}

.privacy-cookie-banner__message {
margin-top: 15px;
color: #fff;
}

.privacy-cookie-banner__btn {
margin-top: 10px;
background: #FF6400;
color: #fff;
border: none;
height: 30px;
}

.privacy-cookie-banner__btn:hover,
.privacy-cookie-banner__btn:focus {
background: #FF7A00;
color: #fff;
}

.privacy-cookie-banner__learn-more {
margin-top: 14px;
margin-left: 20px;
color: #fff;
display: block;
text-decoration: none;
}

.privacy-cookie-banner__learn-more:hover,
.privacy-cookie-banner__learn-more:focus {
color: #FF6400;
text-decoration: none;
}

.privacy-cookie-banner__close {
color: #fff;
margin-top: 21px;
display: block;
opacity: 1;
font-size: 10px;
text-shadow: none;
}

.privacy-cookie-banner__close:hover,
.privacy-cookie-banner__close:focus {
color: #FF6400;
text-decoration: none;
}

.privacy-cookie-banner__action {
height: 50px;
}

@font-face {
font-family: 'icomoon';
src:url('../fonts/icomoon.eot?-btxkoh');
src:url('../fonts/icomoon.eot?#iefix-btxkoh') format('embedded-opentype'),
url('../fonts/icomoon.woff?-btxkoh') format('woff'),
url('../fonts/icomoon.ttf?-btxkoh') format('truetype'),
url('../fonts/icomoon.svg?-btxkoh#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}

[class^="privacy-cookie-banner__icon-"],
[class*=" privacy-cookie-banner__icon-"] {
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;

/* better font rendering */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.privacy-cookie-banner__icon-link:before {
content: "\e969";
}

.privacy-cookie-banner__icon-close:before {
content: "\ea0f";
}
Binary file added Resources/public/fonts/icomoon.eot
Binary file not shown.
12 changes: 12 additions & 0 deletions Resources/public/fonts/icomoon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/public/fonts/icomoon.ttf
Binary file not shown.
Binary file added Resources/public/fonts/icomoon.woff
Binary file not shown.
95 changes: 95 additions & 0 deletions Resources/public/js/privacycookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/

(function (global, doc) {
var eZ = global.eZ = global.eZ || {};

/**
* Contains logic needed to display privacy banner.
*
* @class
* @param {Object} config
*/
eZ.PrivacyCookieBanner = function (config) {
this.cookieName = config.cookieName || 'EzPrivacyCookieStatus';
this.bannerElement = document.getElementById(config.bannerId || 'privacy-cookie-banner');
this.acceptElement = document.getElementById(config.acceptId || 'privacy-cookie-banner__privacy-accept');
this.closeElement = document.getElementById(config.closeId || 'privacy-cookie-banner__privacy-close');
this.days = config.days || 365;
};

/**
* Hide privacy banner.
*/
eZ.PrivacyCookieBanner.prototype.hideBanner = function () {
this.bannerElement.className = this.bannerElement.className.replace(/\b privacy-cookie-banner--slide-up\b/, '');
this.bannerElement.className = this.bannerElement.className + ' privacy-cookie-banner--slide-down';
};

/**
* Show privacy banner.
*/
eZ.PrivacyCookieBanner.prototype.showBanner = function () {
this.bannerElement.className = this.bannerElement.className.replace(/\b privacy-cookie-banner--slide-down\b/, '');
this.bannerElement.className = this.bannerElement.className + ' privacy-cookie-banner--slide-up';
};

/**
* Get cookie value by name.
*
* @param {string} name
* @returns {string}
*/
eZ.PrivacyCookieBanner.prototype.getCookie = function (name) {
var cookieName = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ')
c = c.substring(1);
if (c.indexOf(cookieName) === 0)
return c.substring(cookieName.length, c.length);
}
return '';
};

/**
* Set cookie value.
*
* @param {string} name
* @param {string} value
* @param {int} days
*/
eZ.PrivacyCookieBanner.prototype.setCookie = function (name, value, days) {
var d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = 'expires=' + d.toUTCString();
document.cookie = name + '=' + value + '; ' + expires;
};

/**
* Accept privacy policy (set cookie status) and hide banner.
*/
eZ.PrivacyCookieBanner.prototype.accept = function () {
this.setCookie(this.cookieName, '1', this.days);
this.hideBanner();
};

/**
* Display privacy banner depending on cookie status.
*/
eZ.PrivacyCookieBanner.prototype.show = function () {
if (this.getCookie(this.cookieName) !== '1') {
this.acceptElement.addEventListener('click', function () {
this.accept();
}.bind(this), false);
this.closeElement.addEventListener('click', function () {
this.hideBanner();
}.bind(this), false);
this.showBanner();
}
};

})(window, document);
Loading

0 comments on commit 02679d9

Please sign in to comment.