-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b265a17
commit 2c5b3f4
Showing
8 changed files
with
145 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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,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'); | ||
|
@@ -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'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/migrations/m200821_142415_edit_cookieboss_consentgroup_description.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters