Skip to content

Commit

Permalink
Add initial version of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andersju committed May 25, 2021
1 parent 79151e3 commit 09aa281
Show file tree
Hide file tree
Showing 9 changed files with 808 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# cookienotice-ojs-plugin

A minimal, vanilla JS (ES6+) cookie notification plugin for [OJS](https://github.com/pkp/ojs)
for use on [Publicera](https://publicera.kb.se), an OJS portal hosted by the National Library of Sweden.

Installation
------------
* Download and extract the latest release from the [releases section](https://github.com/Kungbib/cookienotice-ojs-plugin/releases).

* Place the `cookieNotice` directory in your OJS installation's `plugins/generic` directory, or use "Upload A New Plugin" from within the OJS admin interface, if you don't have direct access to the server.

* Make sure the plugin is enabled under Administration -> Site Settings -> Plugins (under "Generic Plugins").

Note that this is a site-wide plugin. It can only be activated/deactivated on site level.

It simply loads a tiny bit of JS that checks whether the cookie `cookieNotice` has been set. If not,
it injects some HTML with cookie information and a button. If the button is clicked, the cookie is set
and the cookie information disappears.

If you find that the cookie notice is only shown on the main site and not on individual journals, `cd`
to the OJS root directory and run:

php lib/pkp/tools/installPluginVersion.php plugins/generic/cookieNotice/version.xm

Creating a new release
----------------------
Bump the plugin version in `cookieNotice/version.xml`.

Merge develop into master.

In the root directory (cookienotice-ojs-plugin), create a tar file with the latest code:
```
tar czf cookieNotice.tar.gz --directory=$(pwd) cookieNotice/
```
Create a new GitHub release, tag the new version (`v.<M>.<m>.<p>`) and attach `cookieNotice.tar.gz`.
47 changes: 47 additions & 0 deletions cookieNotice/CookieNoticePlugin.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
import('lib.pkp.classes.plugins.GenericPlugin');

class CookieNoticePlugin extends GenericPlugin {
public function register($category, $path, $mainContextId = NULL) {
$success = parent::register($category, $path);

if ($success && $this->getEnabled()) {
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addStyleSheet(
'cookieNoticeStyles',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/cookieNotice.css',
['context' => ['backend', 'frontend']]
);
$templateMgr->addJavaScript(
'cookieNoticeScript',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/cookieNotice.js',
['context' => ['backend', 'frontend']]
);
}

return $success;
}

public function getDisplayName() {
return __('plugins.generic.cookieNotice.displayName');
}

public function getDescription() {
return __('plugins.generic.cookieNotice.description');
}

public function isSitePlugin() {
return true;
}

// Can only be activated at site level.
function getCanEnable() {
return !((bool) Application::get()->getRequest()->getContext());
}

// Can only be deactivated at site level.
function getCanDisable() {
return !((bool) Application::get()->getRequest()->getContext());
}
}
7 changes: 7 additions & 0 deletions cookieNotice/css/cookieNotice.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#cookie-notice button {
text-transform: none;
}

.cookie-notice-hidden {
display: none;
}
4 changes: 4 additions & 0 deletions cookieNotice/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

require_once('CookieNoticePlugin.inc.php');
return new CookieNoticePlugin();
20 changes: 20 additions & 0 deletions cookieNotice/js/cookieNotice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let cookieInfoUrl = 'https://publicera.kb.se/kakor';
let cookieDurationDays = 365;

document.addEventListener('DOMContentLoaded', function() {
if (!document.cookie.split(';').some((item) => item.trim().startsWith('cookieNotice='))) {
const confirmationHtml = `
<div id="cookie-notice" class="alert alert-primary" role="alert">
<div class="container">
Genom att använda Publicera godkänner du de kakor (cookies) som finns på webbplatsen. <a href="${cookieInfoUrl}">Läs mer om hur vi använder kakor</a>.
<button id="cookie-notice-confirmation" type="button" class="btn btn-secondary btn-light btn-sm">OK, jag förstår</button>
</div>
</div>
`;
document.body.insertAdjacentHTML('afterbegin', confirmationHtml);
document.getElementById('cookie-notice-confirmation').addEventListener('click', function() {
document.cookie = `cookieNotice=1; max-age=${60*60*24*cookieDurationDays}`;
document.getElementById('cookie-notice').classList.add('cookie-notice-hidden');
});
}
});
5 changes: 5 additions & 0 deletions cookieNotice/locale/en_US/locale.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
msgid "plugins.generic.cookieNotice.displayName"
msgstr "Cookie Notice Plugin"

msgid "plugins.generic.cookieNotice.description"
msgstr "Turns on a site-wide cookie notice."
5 changes: 5 additions & 0 deletions cookieNotice/locale/sv_SE/locale.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
msgid "plugins.generic.cookieNotice.displayName"
msgstr "Cookie-notifieringsplugin"

msgid "plugins.generic.cookieNotice.description"
msgstr "Informerar besökare om att cookies används."
11 changes: 11 additions & 0 deletions cookieNotice/version.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE version SYSTEM "../../../lib/pkp/dtd/pluginVersion.dtd">
<version>
<application>cookieNotice</application>
<type>plugins.generic</type>
<release>1.0.0</release>
<date>2021-05-25</date>
<lazy-load>1</lazy-load>
<sitewide>1</sitewide>
<class>CookieNoticePlugin</class>
</version>

0 comments on commit 09aa281

Please sign in to comment.