Skip to content

Commit f8039c6

Browse files
committed
Toggle individual fields
Added option to enable or disable individual fields in the settings. (solves issue #3) Settings divided over several sub-pages.
1 parent 1550981 commit f8039c6

File tree

9 files changed

+203
-58
lines changed

9 files changed

+203
-58
lines changed

src/Plugin.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use craft\base\Field;
77
use craft\events\DefineFieldHtmlEvent;
88
use craft\events\DefineHtmlEvent;
9+
use craft\events\ModelEvent;
910
use craft\events\RegisterUrlRulesEvent;
1011
use craft\helpers\StringHelper;
1112
use craft\helpers\UrlHelper;
@@ -108,13 +109,42 @@ protected function settingsHtml(): ?string
108109
*/
109110
private function attachFunctions(): void
110111
{
112+
/**
113+
* Attach button to selected fields.
114+
*/
111115
Event::on(
112116
Field::class,
113117
Field::EVENT_DEFINE_INPUT_HTML,
114118
static function (DefineFieldHtmlEvent $event) {
115-
$event->html .= Craft::$app->view->renderTemplate('chatgpt-integration/form.twig', ['input' => $event->html, 'hash' => StringHelper::UUID() ] );
119+
/** @var SettingsModel $settings */
120+
$settings = Plugin::getInstance()->getSettings();
121+
122+
if (array_key_exists($event->sender->id, $settings->enabledFields) && $settings->enabledFields[$event->sender->id]){
123+
$event->html .= Craft::$app->view->renderTemplate('chatgpt-integration/form.twig', [ 'event' => $event, 'hash' => StringHelper::UUID()] );
124+
}
116125
}
117126
);
127+
128+
/**
129+
* Warn user in case there are no selected fields.
130+
*/
131+
Event::on(
132+
Plugin::class,
133+
Plugin::EVENT_AFTER_SAVE_SETTINGS,
134+
function (Event $event) {
135+
136+
/** @var SettingsModel $settings */
137+
$settings = Plugin::getInstance()->getSettings();
138+
139+
if (!in_array(true, $settings->enabledFields, false)){
140+
Craft::$app->getSession()->setError('ChatGPT-Integrations currently has no fields to attach to!');
141+
}
142+
}
143+
);
144+
145+
/**
146+
* Load twig with js code.
147+
*/
118148
Event::on(
119149
Entry::class,
120150
Entry::EVENT_DEFINE_SIDEBAR_HTML,
@@ -138,7 +168,9 @@ static function (RegisterUrlRulesEvent $event) {
138168
$event->rules['chatgpt-integration/prompts/edit/<id:\d+>'] = 'chatgpt-integration/prompt/edit-prompt';
139169
$event->rules['chatgpt-integration/prompts/delete/<id:\d+>'] = 'chatgpt-integration/prompt/delete-prompt';
140170

141-
$event->rules['chatgpt-integration/settings/general'] = 'chatgpt-integration/settings/settings';
171+
$event->rules['chatgpt-integration/settings/general'] = 'chatgpt-integration/settings/general';
172+
$event->rules['chatgpt-integration/settings/api'] = 'chatgpt-integration/settings/api';
173+
$event->rules['chatgpt-integration/settings/fields'] = 'chatgpt-integration/settings/fields';
142174
}
143175
);
144176

src/controllers/SettingsController.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@ class SettingsController extends \craft\web\Controller
1010
/**
1111
* @return Response
1212
*/
13-
public function actionSettings(): Response
13+
public function actionGeneral(): Response
1414
{
1515
$settings = Plugin::getInstance()->getSettings();
1616

1717
return $this->renderTemplate('chatgpt-integration/settings/general', [
1818
'settings' => $settings,
1919
]);
2020
}
21+
22+
/**
23+
* @return Response
24+
*/
25+
public function actionApi(): Response
26+
{
27+
$settings = Plugin::getInstance()->getSettings();
28+
29+
return $this->renderTemplate('chatgpt-integration/settings/api', [
30+
'settings' => $settings,
31+
]);
32+
}
33+
34+
/**
35+
* @return Response
36+
*/
37+
public function actionFields(): Response
38+
{
39+
$settings = Plugin::getInstance()->getSettings();
40+
41+
return $this->renderTemplate('chatgpt-integration/settings/fields', [
42+
'settings' => $settings,
43+
]);
44+
}
2145
}

src/models/SettingsModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class SettingsModel extends Model
3737
*/
3838
public bool $showTranslationPrompts = true;
3939

40+
/**
41+
* @var array
42+
*/
43+
public array $enabledFields = [];
44+
4045
/**
4146
* @return string
4247
*/

src/templates/_layouts/settings.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
{% set selectedSubnavItem = 'settings' %}
99

1010
{% set navItems = {
11-
'Settings-heading': { heading: "Settings"|t('chatgpt-integration') },
11+
'settings-heading': { heading: "Settings"|t('chatgpt-integration') },
1212
'general': { title: "General"|t('chatgpt-integration') },
13+
'fields': { title: "Fields"|t('chatgpt-integration') },
14+
'api': { title: "API"|t('chatgpt-integration') },
1315
} %}
1416

17+
{% set activeTab = craft.app.request.getSegment(3) ?? 'general' %}
18+
1519
{% block sidebar %}
1620
<nav>
1721
<ul>
@@ -22,7 +26,7 @@
2226
</li>
2327
{% else %}
2428
<li>
25-
<a href="{{ url('chatgpt-integration/settings/'~ key) }}" class="sel">{{ element.title }}</a>
29+
<a href="{{ url('chatgpt-integration/settings/'~ key) }}" {{ activeTab == key ? 'class="sel"' }}>{{ element.title }}</a>
2630
</li>
2731
{% endif %}
2832
{% endfor %}

src/templates/form.twig

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,42 @@
3434
{% endif %}
3535

3636
{% if craft.app.getPlugins.getPlugin('chatgpt-integration').settings.getAccessToken|length %}
37-
{% if (" type=\"text\"" in input or "textarea" in input) and "neo-input" not in input and "matrix-field" not in input %}
38-
{% set prompts = craft.chatgptIntegration.getPrompts(true) %}
39-
<div class="chatgpt-button">
40-
<button {{ attr(actionBtnAttributes) }}>ChatGPT</button>
41-
<div id="1" class="menu menu--disclosure chatgpt-menu">
42-
<ul class="padded">
43-
{% for item in prompts %}
44-
<li><a href="javascript:void(0)" class="doAi" type="button" role="button" data-hash="{{ hash }}" data-prompt="{{ currentLang }} {{ item.promptTemplate }}: " aria-label="{{ item.label }}">{{ item.label }}</a></li>
45-
{% else %}
46-
{% if displayTranslationPrompts == false %}
47-
<li style="padding: 1em 0"><p>{{ 'No prompts found'|t('chatgpt-integration') }}</p></li>
48-
{% endif %}
49-
{% endfor %}
50-
</ul>
51-
{% if displayTranslationPrompts %}
52-
{% if prompts|length %}
53-
<hr>
54-
{% endif %}
55-
<ul class="padded">
56-
{% if craft.app.sites.getAllSites()|length > 1 %}
57-
{% for site in craft.app.sites.getAllSites () %}
58-
<li><a href="#" class="doAi" data-hash="{{ hash }}" data-prompt="Translate to {{ site.language }}: " type="button" role="button" aria-label="{{ 'Translate to {language}'|t('chatgpt-integration', params = {
59-
language: site.language
60-
} ) }}">{{ 'Translate to {language}'|t('chatgpt-integration', params = {
61-
language: site.language
62-
} ) }}</a></li>
63-
{% endfor %}
37+
38+
{% set prompts = craft.chatgptIntegration.getPrompts(true) %}
39+
<div class="chatgpt-button">
40+
<button {{ attr(actionBtnAttributes) }}>ChatGPT</button>
41+
<div id="1" class="menu menu--disclosure chatgpt-menu">
42+
<ul class="padded">
43+
{% for item in prompts %}
44+
<li><a href="javascript:void(0)" class="doAi" type="button" role="button" data-hash="{{ hash }}" data-prompt="{{ currentLang }} {{ item.promptTemplate }}: " aria-label="{{ item.label }}">{{ item.label }}</a></li>
45+
{% else %}
46+
{% if displayTranslationPrompts == false %}
47+
<li style="padding: 1em 0"><p>{{ 'No prompts found'|t('chatgpt-integration') }}</p></li>
6448
{% endif %}
65-
</ul>
66-
{#<hr>
67-
<ul class="padded">
68-
<li>
69-
<a href="#" class="open-modal">{{ 'Senden'|t('chatgpt-integration') }}</a>
70-
</li>
71-
</ul>#}
49+
{% endfor %}
50+
</ul>
51+
{% if displayTranslationPrompts %}
52+
{% if prompts|length %}
53+
<hr>
7254
{% endif %}
73-
</div>
55+
<ul class="padded">
56+
{% if craft.app.sites.getAllSites()|length > 1 %}
57+
{% for site in craft.app.sites.getAllSites () %}
58+
<li><a href="#" class="doAi" data-hash="{{ hash }}" data-prompt="Translate to {{ site.language }}: " type="button" role="button" aria-label="{{ 'Translate to {language}'|t('chatgpt-integration', params = {
59+
language: site.language
60+
} ) }}">{{ 'Translate to {language}'|t('chatgpt-integration', params = {
61+
language: site.language
62+
} ) }}</a></li>
63+
{% endfor %}
64+
{% endif %}
65+
</ul>
66+
{#<hr>
67+
<ul class="padded">
68+
<li>
69+
<a href="#" class="open-modal">{{ 'Senden'|t('chatgpt-integration') }}</a>
70+
</li>
71+
</ul>#}
72+
{% endif %}
7473
</div>
75-
{% endif %}
74+
</div>
7675
{% endif %}

src/templates/settings/api.twig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{% extends 'chatgpt-integration/_layouts/settings' %}
2+
3+
{% import '_includes/forms' as forms %}
4+
5+
{% set title = 'Settings' %}
6+
{% set selectedSubnavItem = 'settings' %}
7+
8+
{% set fullPageForm = true %}
9+
10+
{% block content %}
11+
<input type="hidden" name="action" value="plugins/save-plugin-settings">
12+
<input type="hidden" name="pluginHandle" value="chatgpt-integration">
13+
{% namespace 'settings' %}
14+
{{ forms.autosuggestField({
15+
label: 'API: Access Token'|t('chatgpt-integration'),
16+
name: 'accessToken',
17+
value: settings.accessToken,
18+
instructions: 'Access token for the ChatGPT API'|t('chatgpt-integration'),
19+
suggestEnvVars: true,
20+
}) }}
21+
22+
{{ forms.textField({
23+
label: 'API: Max Tokens'|t('chatgpt-integration'),
24+
name: 'maxTokens',
25+
value: settings.maxTokens,
26+
autofocus: true,
27+
instructions: 'Maximum amount of tokens for chatgpt\'s response (<a href="https://platform.openai.com/docs/introduction/tokens">https://platform.openai.com/docs/introduction/tokens</a>)'|t('chatgpt-integration'),
28+
first: true,
29+
type: 'number',
30+
}) }}
31+
32+
{% endnamespace %}
33+
{% endblock %}
34+

src/templates/settings/fields.twig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% extends 'chatgpt-integration/_layouts/settings' %}
2+
3+
{% import '_includes/forms' as forms %}
4+
5+
{% set title = 'Settings' %}
6+
{% set selectedSubnavItem = 'settings' %}
7+
8+
{% set fullPageForm = true %}
9+
10+
{% block content %}
11+
<input type="hidden" name="action" value="plugins/save-plugin-settings">
12+
<input type="hidden" name="pluginHandle" value="chatgpt-integration">
13+
{% namespace 'settings' %}
14+
15+
{% set fields = craft.app.fields.getAllFields %}
16+
17+
{% if fields|length %}
18+
<div class="tableview tablepane">
19+
<table class="data fullwidth">
20+
<thead>
21+
<tr>
22+
<th scope="col">{{ 'Label' |t('chatgpt-integration') }}</th>
23+
<th scope="col">{{ 'Handle' |t('chatgpt-integration') }}</th>
24+
<th scope="col">{{ 'Field type' |t('chatgpt-integration') }}</th>
25+
<th scope="col">{{ 'Group' |t('chatgpt-integration') }}</th>
26+
<th class="thin">{{ 'Enabled' |t('chatgpt-integration') }}</th>
27+
28+
</tr>
29+
</thead>
30+
{% for field in fields %}
31+
<tbody>
32+
<tr class="s-{{ field.id }}">
33+
<td class="label-column">{{ field.name }}</td>
34+
<td class="handle-column">{{ field.handle }}</td>
35+
<td class="group-column">{{ craft.chatgptIntegration.getClass(field) }}</td>
36+
<td class="group-column">{{ field.group }}</td>
37+
<td class="enabled-column">
38+
{{ forms.lightswitch({
39+
name: 'enabledFields['~field.id~'].key',
40+
on: settings.enabledFields[field.id]
41+
}) }}
42+
</td>
43+
</tr>
44+
</tbody>
45+
{% endfor %}
46+
</table>
47+
</div>
48+
{% else %}
49+
<div>
50+
<p>{{ 'No fields exist yet.'|t('chatgpt-integration') }}</p>
51+
</div>
52+
{% endif %}
53+
54+
{% endnamespace %}
55+
{% endblock %}
56+

src/templates/settings/general.twig

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,6 @@
3636
instructions: 'Provide default translation prompts based on the configured languages of your setup.'
3737
}) }}
3838

39-
{{ forms.autosuggestField({
40-
label: 'API: Access Token'|t('chatgpt-integration'),
41-
name: 'accessToken',
42-
value: settings.accessToken,
43-
instructions: 'Access token for the ChatGPT API'|t('chatgpt-integration'),
44-
suggestEnvVars: true,
45-
}) }}
46-
47-
{{ forms.textField({
48-
label: 'API: Max Tokens'|t('chatgpt-integration'),
49-
name: 'maxTokens',
50-
value: settings.maxTokens,
51-
autofocus: true,
52-
instructions: 'Maximum amount of tokens for chatgpt\'s response (<a href="https://platform.openai.com/docs/introduction/tokens">https://platform.openai.com/docs/introduction/tokens</a>)'|t('chatgpt-integration'),
53-
first: true,
54-
type: 'number',
55-
}) }}
5639
{% endnamespace %}
5740
{% endblock %}
5841

src/variables/ChatgptIntegrationVariable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public function getPrompts(bool $enabled = false): array
2121
{
2222
return Plugin::getInstance()->promptService->getPrompts($enabled);
2323
}
24+
25+
/**
26+
* @throws \ReflectionException
27+
*/
28+
public function getClass($object)
29+
{
30+
return (new \ReflectionClass($object))->getName();
31+
}
2432
}

0 commit comments

Comments
 (0)