Skip to content
Code-Schnipsel Gruppen Projekte
Bestätigt Commit 12a7b2ce erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

feat: snippet status / active / deactive

Übergeordneter 4c7d6ae3
No related branches found
No related tags found
2 Merge Requests!10Next,!9Dev
<?php
use QUI\HtmlSnippets\Snippets;
QUI::$Ajax->registerFunction(
'package_quiqqer_html-snippets_ajax_backend_activateSnippet',
function ($projectName, $snippetName) {
$Project = QUI::getProject($projectName);
$Snippet = Snippets::get($Project, $snippetName);
$Snippet->activate();
},
['projectName', 'snippetName'],
'Permission::checkAdminUser'
);
<?php
use QUI\HtmlSnippets\Snippets;
QUI::$Ajax->registerFunction(
'package_quiqqer_html-snippets_ajax_backend_deactivateSnippet',
function ($projectName, $snippetName) {
$Project = QUI::getProject($projectName);
$Snippet = Snippets::get($Project, $snippetName);
$Snippet->deactivate();
},
['projectName', 'snippetName'],
'Permission::checkAdminUser'
);
......@@ -11,11 +11,12 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
'qui/controls/Control',
'qui/controls/loader/Loader',
'package/quiqqer/html-snippets/bin/backend/Utils',
'qui/controls/buttons/Switch',
'controls/grid/Grid',
'Ajax',
'Locale'
], function(QUI, QUIControl, QUILoader, SnippetUtils, Grid, QUIAjax, QUILocale) {
], function(QUI, QUIControl, QUILoader, SnippetUtils, QUISwitch, Grid, QUIAjax, QUILocale) {
'use strict';
const lg = 'quiqqer/html-snippets';
......@@ -30,7 +31,8 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
'edit',
'remove',
'refresh',
'$onImport'
'$onImport',
'$onStatusChange'
],
initialize: function(options) {
......@@ -70,6 +72,11 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
const columnModel = [
{
header: QUILocale.get(lg, 'grid.status'),
dataIndex: 'status',
dataType: 'QUI',
width: 80
}, {
header: QUILocale.get(lg, 'grid.name'),
dataIndex: 'name',
dataType: 'string',
......@@ -146,6 +153,15 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
this.Loader.show();
QUIAjax.get('package_quiqqer_html-snippets_ajax_backend_getSnippets', (result) => {
for (let i = 0, len = result.length; i < len; i++) {
result[i].status = new QUISwitch({
status: result[i].active,
events: {
change: this.$onStatusChange
}
});
}
this.$Grid.setData({
data: result
});
......@@ -246,6 +262,30 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
}
}).open();
});
},
$onStatusChange: function(Switch) {
const status = Switch.getStatus();
const Row = Switch.getElm().getParent('.tr');
const rowData = this.$Grid.getDataByRow(Row.get('data-row'));
const snippetName = rowData.name;
const project = rowData.project;
this.Loader.show();
if (status) {
SnippetUtils.activateSnippet(snippetName, project).then(() => {
this.refresh();
}).catch(() => {
this.refresh();
});
} else {
SnippetUtils.deactivateSnippet(snippetName, project).then(() => {
this.refresh();
}).catch(() => {
this.refresh();
});
}
}
});
});
......@@ -15,9 +15,30 @@ define('package/quiqqer/html-snippets/bin/backend/Utils', function() {
});
},
getGDPRList: function() {
}
activateSnippet: function(snippetName, project) {
return new Promise(function(resolve, reject) {
require(['Ajax'], function(QUIAjax) {
QUIAjax.post('package_quiqqer_html-snippets_ajax_backend_activateSnippet', resolve, {
'package': 'quiqqer/html-snippets',
projectName: project,
snippetName: snippetName,
onError: reject
});
});
});
},
deactivateSnippet: function(snippetName, project) {
return new Promise(function(resolve, reject) {
require(['Ajax'], function(QUIAjax) {
QUIAjax.post('package_quiqqer_html-snippets_ajax_backend_deactivateSnippet', resolve, {
'package': 'quiqqer/html-snippets',
projectName: project,
snippetName: snippetName,
onError: reject
});
});
});
}
};
});
.quiqqer-html-snippet-input {
background: #f6f8fb;
padding: 10px;
}
.quiqqer-html-snippet-input textarea {
border: 1px solid #DEDEDE !important;
min-height: 200px;
position: relative;
width: 100%;
}
.quiqqer-html-snippet-input select {
margin-bottom: 10px;
.quiqqer-html-snippet-input > label {
float: left;
margin: 10px 0 5px;
width: 100% !important;
}
.quiqqer-html-snippet-input > label > span:first-child {
display: inline-block;
font-weight: bold;
padding-bottom: 5px;
padding-left: 3px;
width: 100%;
}
.quiqqer-html-snippet-input [name="gdpr"] {
background: #fff;
width: 100%;
}
\ No newline at end of file
}
.quiqqer-html-snippet-input-title {
border-bottom: 1px solid #0420451a;
width: 100%;
display: inline-block;
line-height: 2rem;
padding-bottom: 1rem;
position: relative;
}
.quiqqer-html-snippet-input-title > span {
font-size: 1.5rem;
}
.quiqqer-html-snippet-input-status {
position: absolute;
right: 3px;
top: 3px;
}
.quiqqer-html-snippet-input-status > span {
font-size: 12px;
margin-right: 10px;
}
......@@ -7,13 +7,14 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
'qui/QUI',
'qui/controls/Control',
'qui/controls/loader/Loader',
'qui/controls/buttons/Switch',
'package/quiqqer/html-snippets/bin/backend/Utils',
'Ajax',
'Locale',
'css!package/quiqqer/html-snippets/bin/backend/controls/SnippetInput.css'
], function(QUI, QUIControl, QUILoader, SnippetUtils, QUIAjax, QUILocale) {
], function(QUI, QUIControl, QUILoader, QUISwitch, SnippetUtils, QUIAjax, QUILocale) {
'use strict';
return new Class({
......@@ -24,6 +25,7 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
Binds: [
'$onImport',
'$onChange',
'$onStatusChange',
'$load'
],
......@@ -35,6 +37,7 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
this.$loaded = false;
this.$data = null;
this.$Status = null;
this.$Project = null;
this.$GDPR = null;
......@@ -55,15 +58,26 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
this.setAttribute('snippetName', this.$Textarea.get('data-qui-options-snippet'));
this.setAttribute('snippetEvent', this.$Textarea.get('data-qui-options-event'));
const TextAreaLabel = new Element('label').wraps(this.$Textarea);
new Element('span', {
html: 'Code'
}).inject(TextAreaLabel, 'top');
this.$Elm = new Element('div', {
'class': 'field-container-field quiqqer-html-snippet-input'
}).wraps(this.$Textarea);
}).wraps(TextAreaLabel);
this.Loader.inject(this.$Elm);
this.Loader.show();
SnippetUtils.isGDPRInstalled().then((isInstalled) => {
if (isInstalled) {
const GDPRLabel = new Element('label', {
html: '<span>GDPR Kategorie</span>'
}).inject(this.$Elm, 'top');
this.$GDPR = new Element('select', {
name: 'gdpr',
html: '' +
......@@ -83,19 +97,51 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
events: {
change: this.$onChange
}
}).inject(this.$Elm, 'top');
}).inject(GDPRLabel);
if (this.$data) {
this.$GDPR.value = this.$data.gdpr;
}
}
const Title = new Element('div', {
'class': 'quiqqer-html-snippet-input-title',
html: '<span>HTML-Snippet</span>'
}).inject(this.$Elm, 'top');
this.$StatusContainer = new Element('div', {
'class': 'quiqqer-html-snippet-input-status'
}).inject(Title);
new Element('span', {
html: 'HTML-Snippet ist aktiviert',
styles: {
'float': 'left'
}
}).inject(this.$StatusContainer);
this.$Status = new QUISwitch({
events: {
onChange: this.$onStatusChange
}
}).inject(this.$StatusContainer);
return this.$load();
}).then(() => {
this.$Textarea.addEvents({
change: this.$onChange,
blur: this.$onChange
});
if (this.$data && this.$data.active) {
this.$Status.setSilentOn();
}
if (this.$data && !this.$data.active) {
this.$Status.setSilentOff();
}
});
},
......@@ -141,6 +187,14 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
this.$GDPR.value = snippetData.gdpr;
}
if (this.$Status) {
if (this.$data.active) {
this.$Status.setSilentOn();
} else {
this.$Status.setSilentOff();
}
}
this.Loader.hide();
resolve();
}, {
......@@ -171,6 +225,32 @@ define('package/quiqqer/html-snippets/bin/backend/controls/SnippetInput', [
}
});
});
},
$onStatusChange: function() {
const status = this.$Status.getStatus();
const project = this.$Project.getName();
const snippetName = this.getAttribute('snippetName');
this.Loader.show();
if (status) {
SnippetUtils.activateSnippet(snippetName, project).then(() => {
this.Loader.hide();
}).catch((err) => {
QUI.getMessageHandler().then((MH) => {
MH.addError(err.getMessage());
});
});
} else {
SnippetUtils.deactivateSnippet(snippetName, project).then(() => {
this.Loader.hide();
}).catch((err) => {
QUI.getMessageHandler().then((MH) => {
MH.addError(err.getMessage());
});
});
}
}
});
});
......@@ -2,6 +2,7 @@
<database>
<global>
<table name="html_snippets">
<field type="INT(1) DEFAULT 0">active</field>
<field type="VARCHAR(255)">name</field>
<field type="VARCHAR(64)">project</field>
<field type="VARCHAR(255)">event</field>
......@@ -10,6 +11,7 @@
<primary>name,project</primary>
<index>event</index>
<index>active</index>
<comment>This table contains the html snippets for the different projects.</comment>
</table>
</global>
......
......@@ -34,6 +34,10 @@
<de><![CDATA[GDPR Kategorie]]></de>
<en><![CDATA[GDPR Category]]></en>
</locale>
<locale name="grid.status">
<de><![CDATA[Status]]></de>
<en><![CDATA[Status]]></en>
</locale>
</groups>
<groups name="quiqqer/html-snippets" datatype="js">
......
......@@ -4,6 +4,7 @@ namespace QUI\HtmlSnippets;
use QUI;
use QUI\Interfaces\Users\User;
use QUI\Permissions\Exception;
use QUI\Projects\Project;
class Snippet
......@@ -12,6 +13,7 @@ class Snippet
protected Project $Project;
protected string $snippet;
protected string $event;
protected bool $active = false;
protected string $gdprCategory = '';
/**
......@@ -27,6 +29,7 @@ class Snippet
$this->snippet = $data['snippet'];
$this->event = $data['event'];
$this->gdprCategory = !empty($data['gdpr']) ? $data['gdpr'] : '';
$this->active = !empty($data['active']);
}
//region get
......@@ -75,10 +78,16 @@ class Snippet
'event' => $this->event,
'Project' => $this->Project->getName(),
'snippet' => $this->getSnippet(),
'gdpr' => $this->gdprCategory
'gdpr' => $this->gdprCategory,
'active' => $this->active ? 1 : 0
];
}
public function isActive(): bool
{
return $this->active;
}
//endregion
//region set
......@@ -110,6 +119,26 @@ class Snippet
$this->gdprCategory = $gdprCategory;
}
/**
* @throws Exception
* @throws QUI\Database\Exception
*/
public function activate(User $User = null): void
{
$this->active = true;
$this->save($User);
}
/**
* @throws Exception
* @throws QUI\Database\Exception
*/
public function deactivate(User $User = null): void
{
$this->active = false;
$this->save($User);
}
/**
* Saves the snippet to the database.
*
......@@ -129,6 +158,7 @@ class Snippet
QUI::getDatabase()->update(
Snippets::table(),
[
'active' => $this->active ? 1 : 0,
'event' => $this->event,
'snippet' => $this->snippet,
'gdpr' => $this->gdprCategory
......
......@@ -56,6 +56,10 @@ class SnippetTemplateEvent
}
foreach ($this->snippets as $snippet) {
if (empty($snippet['active'])) {
continue;
}
$gdprIsInstalled = QUI::getPackageManager()->isInstalled('quiqqer/gdpr');
if (empty($snippet['gdpr']) || !$gdprIsInstalled) {
......
0% oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren