Skip to content

Commit

Permalink
v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBeckers committed Aug 21, 2020
1 parent b265a17 commit 2c5b3f4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 30 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Modal position setting.

### Fixed
- Fixed namespace.
- Fixed namespace.

## 1.3.1 - 2020-08-21
### Added
- Ordering to consent group [#12](https://github.com/dutchheight/craft-cookie-boss/issues/12).

### Fixed
- Added validation [#11](https://github.com/dutchheight/craft-cookie-boss/issues/11).
- Changed consent group description to text columt [#9](https://github.com/dutchheight/craft-cookie-boss/issues/9).
- Spelling mistake [#10](https://github.com/dutchheight/craft-cookie-boss/issues/10).
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dutchheight/craft-cookie-boss",
"description": "Allow your visitors to set their cookie preference.",
"type": "craft-plugin",
"version": "1.3.0",
"version": "1.3.1",
"keywords": [
"craft",
"cms",
Expand All @@ -24,7 +24,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1"
"craftcms/cms": "^3.1.0"
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 39 additions & 17 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use craft\helpers\UrlHelper;
use craft\web\Controller;
use dutchheight\cookieboss\CookieBoss;
use Exception;
use yii\web\NotFoundHttpException;
use yii\web\Response;


/**
* @author Dutch Height
* @package CookieBoss
* @since 1.0.0
* @package CookieBoss
* @author Dutch Height <[email protected]>
* @since 1.0.0
*/
class SettingsController extends Controller
{
Expand Down Expand Up @@ -43,7 +44,6 @@ public function actionPluginSettings($settings = null): Response
$variables['cookies'] = CookieBoss::getInstance()->cookieDescriptions->getAll();

$variables['settings'] = $settings;
// Craft::dump($settings);
$variables['tabs'] = [
'general' => [
'label' => Craft::t('app', 'General'),
Expand Down Expand Up @@ -84,33 +84,49 @@ public function actionSaveGeneral()
}

$forceReconsent = (bool)Craft::$app->getRequest()->getBodyParam('reset-consent');
return $this->saveSettings($forceReconsent);
return $this->_saveSettings($forceReconsent);
}

private function saveSettings($forceReconsent = false) {
$plugin = Craft::$app->getPlugins()->getPlugin('cookie-boss');
$cookies = Craft::$app->getRequest()->getRequiredBodyParam('cookies');
$cookies = empty($cookies) ? [] : $cookies;
$update = CookieBoss::getInstance()->cookieDescriptions->updateAll($cookies);
/**
* Save plugin settings
*
* @param boolean $forceReconsent Show the plugin new consenses?
*
* @return void
*/
private function _saveSettings($forceReconsent = false)
{
$plugin = Craft::$app->getPlugins()->getPlugin('cookie-boss');
$cookies = Craft::$app->getRequest()->getRequiredBodyParam('cookies');
$cookies = empty($cookies) ? [] : $cookies;
$update = CookieBoss::getInstance()->cookieDescriptions->updateAll($cookies);

if (is_array($update)) {
$this->error($plugin);
$this->_error();
return null;
}

$consentGroups = Craft::$app->getRequest()->getRequiredBodyParam('consentGroups');
$consentGroups = empty($consentGroups) ? [] : $consentGroups;
$update = CookieBoss::getInstance()->consentGroups->updateAll($consentGroups);
try {
$update = CookieBoss::getInstance()
->consentGroups
->updateAll($consentGroups);
} catch(Exception $e) {
$this->_error();
return $this->redirectToPostedUrl();
}

if (is_array($update)) {
$this->error($plugin);
$this->_error();
return null;
}

$settings['enabled'] = (Craft::$app->getRequest()->getRequiredBodyParam('enabled') == '1');
$settings['presentGroups'] = (Craft::$app->getRequest()->getRequiredBodyParam('presentGroups') == '1');
$settings['forceAccept'] = (Craft::$app->getRequest()->getRequiredBodyParam('forceAccept') == '1');

$settings['cookieTime'] = Craft::$app->getRequest()->getRequiredBodyParam('cookieTime') * 86400;
$settings['cookieTime'] = (Craft::$app->getRequest()->getRequiredBodyParam('cookieTime') || 1) * 86400;
$settings['title'] = Craft::$app->getRequest()->getRequiredBodyParam('title');
$settings['message'] = Craft::$app->getRequest()->getRequiredBodyParam('message');
$settings['messageSettings'] = Craft::$app->getRequest()->getRequiredBodyParam('messageSettings');
Expand All @@ -126,19 +142,25 @@ private function saveSettings($forceReconsent = false) {

$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings);
if (!$success) {
$this->error($plugin);
$this->_error();
return null;
}

Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.'));
return $this->redirectToPostedUrl();
}

private function error($plugin) {
/**
* Show Craft CMS error
*
* @return void
*/
private function _error()
{
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings."));
// Send the plugin back to the template
Craft::$app->getUrlManager()->setRouteParams([
'plugin' => $plugin,
'plugin' => Craft::$app->getPlugins()->getPlugin('cookie-boss'),
]);
}
}
2 changes: 1 addition & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected function insertDefaultData()
'consentGroupId' => 1,
'name' => 'Cookie message',
'key' => 'craft-cookie-boss',
'desc' => 'This info isn\'t shared with third partys. And will be removed after7 days.',
'desc' => 'This info isn\'t shared with third parties and will be removed after 7 days.',
'purpose' => 'We use this to save your cookie preferences and if you have seen the cookie modal.',
'enabled' => 1
], true);
Expand Down
32 changes: 32 additions & 0 deletions src/migrations/m200821_140849_add_consent_group_order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace dutchheight\cookieboss\migrations;

use Craft;
use craft\db\Migration;

/**
* m200821_140849_add_consent_group_order migration.
*/
class m200821_140849_add_consent_group_order extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
if (!$this->db->columnExists('{{%cookieboss_consentgroup}}', 'order')) {
$this->addColumn('{{%cookieboss_consentgroup}}', 'order', $this->integer()->null()->after('id'));
}
}

/**
* @inheritdoc
*/
public function safeDown()
{
if ($this->db->columnExists('{{%cookieboss_consentgroup}}', 'order')) {
$this->dropColumn('{{%cookieboss_consentgroup}}', 'order');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace dutchheight\cookieboss\migrations;

use Craft;
use craft\db\Migration;

/**
* m200821_142415_edit_cookieboss_consentgroup_description migration.
*/
class m200821_142415_edit_cookieboss_consentgroup_description extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->alterColumn('{{%cookieboss_consentgroup}}', 'desc', $this->text()->notNull()->defaultValue(''));
}

/**
* @inheritdoc
*/
public function safeDown()
{
$this->alterColumn('{{%cookieboss_consentgroup}}', 'desc', $this->string(255)->notNull()->defaultValue(''));
}
}
35 changes: 29 additions & 6 deletions src/services/ConsentGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
use craft\helpers\StringHelper;

use dutchheight\cookieboss\records\ConsentGroup;
use yii\base\Exception;

class ConsentGroupService extends Component {

const CONFIG_KEY = 'plugins.cookie-boss.consentGroups';
const TABLE = '{{%cookieboss_consentgroup}}';

public function getAll() {
return ConsentGroup::find()->orderBy('id')->all();
return ConsentGroup::find()->orderBy('order')->all();
}

public function getAllByHandle($handle) {
Expand All @@ -39,27 +40,41 @@ public function getAllAsSelectOptions() {
return $returner;
}

public function getAllEnabled() {
return ConsentGroup::find()->where(['enabled' => 1])->orderBy('id')->all();
public function getAllEnabled()
{
return ConsentGroup::find()->where(['enabled' => 1])->orderBy('order')->all();
}

public function deleteAll() {
public function deleteAll()
{
return ConsentGroup::deleteAll();
}

public function selectId($el) {
public function selectId($el)
{
if ($el['id']) {
return (int)$el['id'];
}
}

public function updateAll(Array $groups) {
/**
* Update all consent groups
*
* @param Array $groups consent group array
*
* @return void
*
* @throws Exception validation exception
*/
public function updateAll(Array $groups)
{
// All id's that are left over should be deleted
$currentIds = array_map(
'self::selectId',
ConsentGroup::find()->select(['id'])->asArray()->all()
);

$order = 1;
foreach ($groups as $group) {
// Check if cg is new
if ($group['id']) {
Expand All @@ -80,15 +95,21 @@ public function updateAll(Array $groups) {
}

// Update props
$cg->order = $order;
$cg->enabled = (!empty($group["enabled"]) ? $group["enabled"] : 0);
$cg->defaultValue = (!empty($group["defaultValue"]) ? $group["defaultValue"] : 0);
$cg->required = (!empty($group["required"]) ? $group["required"] : 0);
$cg->handle = $group["handle"];
$cg->name = $group["name"];
$cg->desc = $group["desc"];

if (!ConsentGroup::validateMultiple([$cg])) {
throw new Exception("Validation exception");
}

// Save to config
$this->saveConsentGroup($cg);
$order ++;
}

if (count($currentIds) > 0) {
Expand All @@ -109,6 +130,7 @@ private function saveConsentGroup(ConsentGroup $consentGroup) {

// Save it to the project config
Craft::$app->projectConfig->set(self::CONFIG_KEY . "." .$consentGroup->uid, [
'order' => $consentGroup->order,
'enabled' => $consentGroup->enabled,
'defaultValue' => $consentGroup->defaultValue,
'required' => $consentGroup->required,
Expand Down Expand Up @@ -148,6 +170,7 @@ public function handleChangedConsentGroup(ConfigEvent $event)
} else {
Craft::$app->db->createCommand()->update(self::TABLE, [
'uid' => $uid,
'order' => $event->newValue['order'],
'enabled' => $event->newValue['enabled'],
'defaultValue' => $event->newValue['defaultValue'],
'required' => $event->newValue['required'],
Expand Down
7 changes: 4 additions & 3 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,22 @@
},
name: {
id: 'name',
heading: 'Name' |t('app'),
heading: 'Name' |t('app') ~ "*",
type: 'singleline',
info: 'The name of this consent group.' |t('cookie-boss'),
},
handle: {
heading: 'Handle' |t('app'),
heading: 'Handle' |t('app') ~ "*",
type: 'singleline',
class: 'code',
autocorrect: false,
autocapitalize: false,
info: 'Handle to use in twig.' |t('cookie-boss'),
},
desc: {
heading: 'Description' |t('app'),
heading: 'Description' |t('app') ~ "*",
type: 'text',
required: true,
info: 'The description of this group. This description is visable for your visitors.' |t('cookie-boss'),
},
defaultValue: {
Expand Down

0 comments on commit 2c5b3f4

Please sign in to comment.