From 2c5b3f455b88ddb5cc02195ea51d9c8462797056 Mon Sep 17 00:00:00 2001 From: Kevin Beckers Date: Fri, 21 Aug 2020 16:28:44 +0200 Subject: [PATCH] v1.3.1 --- CHANGELOG.md | 11 +++- composer.json | 4 +- src/controllers/SettingsController.php | 56 +++++++++++++------ src/migrations/Install.php | 2 +- ...m200821_140849_add_consent_group_order.php | 32 +++++++++++ ...it_cookieboss_consentgroup_description.php | 28 ++++++++++ src/services/ConsentGroupService.php | 35 ++++++++++-- src/templates/settings.twig | 7 ++- 8 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 src/migrations/m200821_140849_add_consent_group_order.php create mode 100644 src/migrations/m200821_142415_edit_cookieboss_consentgroup_description.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 62416f8..451940f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,4 +73,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Modal position setting. ### Fixed -- Fixed namespace. \ No newline at end of file +- 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). \ No newline at end of file diff --git a/composer.json b/composer.json index ac9979e..6f796d4 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -24,7 +24,7 @@ } ], "require": { - "craftcms/cms": "^3.0.0-RC1" + "craftcms/cms": "^3.1.0" }, "autoload": { "psr-4": { diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index ee76c67..5a3d6b6 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -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 + * @since 1.0.0 */ class SettingsController extends Controller { @@ -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'), @@ -84,25 +84,41 @@ 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; } @@ -110,7 +126,7 @@ private function saveSettings($forceReconsent = false) { $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'); @@ -126,7 +142,7 @@ private function saveSettings($forceReconsent = false) { $success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings); if (!$success) { - $this->error($plugin); + $this->_error(); return null; } @@ -134,11 +150,17 @@ private function saveSettings($forceReconsent = false) { 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'), ]); } } \ No newline at end of file diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 37810c4..4557840 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -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); diff --git a/src/migrations/m200821_140849_add_consent_group_order.php b/src/migrations/m200821_140849_add_consent_group_order.php new file mode 100644 index 0000000..7982d75 --- /dev/null +++ b/src/migrations/m200821_140849_add_consent_group_order.php @@ -0,0 +1,32 @@ +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'); + } + } +} diff --git a/src/migrations/m200821_142415_edit_cookieboss_consentgroup_description.php b/src/migrations/m200821_142415_edit_cookieboss_consentgroup_description.php new file mode 100644 index 0000000..e3be081 --- /dev/null +++ b/src/migrations/m200821_142415_edit_cookieboss_consentgroup_description.php @@ -0,0 +1,28 @@ +alterColumn('{{%cookieboss_consentgroup}}', 'desc', $this->text()->notNull()->defaultValue('')); + } + + /** + * @inheritdoc + */ + public function safeDown() + { + $this->alterColumn('{{%cookieboss_consentgroup}}', 'desc', $this->string(255)->notNull()->defaultValue('')); + } +} diff --git a/src/services/ConsentGroupService.php b/src/services/ConsentGroupService.php index 261e616..342a64d 100644 --- a/src/services/ConsentGroupService.php +++ b/src/services/ConsentGroupService.php @@ -10,6 +10,7 @@ use craft\helpers\StringHelper; use dutchheight\cookieboss\records\ConsentGroup; +use yii\base\Exception; class ConsentGroupService extends Component { @@ -17,7 +18,7 @@ class ConsentGroupService extends Component { const TABLE = '{{%cookieboss_consentgroup}}'; public function getAll() { - return ConsentGroup::find()->orderBy('id')->all(); + return ConsentGroup::find()->orderBy('order')->all(); } public function getAllByHandle($handle) { @@ -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']) { @@ -80,6 +95,7 @@ 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); @@ -87,8 +103,13 @@ public function updateAll(Array $groups) { $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) { @@ -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, @@ -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'], diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 1bfc640..0276c0c 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -179,12 +179,12 @@ }, 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, @@ -192,8 +192,9 @@ 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: {