Skip to content

Commit ab23470

Browse files
committed
Fix the JSON Decode issue and added settings
1 parent 00305d8 commit ab23470

File tree

7 files changed

+65
-74
lines changed

7 files changed

+65
-74
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mblode/sprout-forms-tables",
33
"description": "Tables Field for Sprout Forms",
44
"type": "craft-plugin",
5-
"version": "1.0.3",
5+
"version": "1.0.4",
66
"keywords": [
77
"craft",
88
"cms",
@@ -32,7 +32,7 @@
3232
"extra": {
3333
"name": "Tables Field for Sprout Forms",
3434
"handle": "sprout-forms-tables",
35-
"hasCpSettings": false,
35+
"hasCpSettings": true,
3636
"hasCpSection": false,
3737
"changelogUrl": "https://raw.githubusercontent.com/mblode/sprout-forms-tables/master/CHANGELOG.md",
3838
"class": "mblode\\sproutformstables\\SproutFormsTables"

src/SproutFormsTables.php

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Sprout Forms Tables plugin for Craft CMS 3.x
3+
* Tables Field for Sprout Forms for Craft CMS 3.x
44
*
55
* Custom Tables field for Sprout Forms plugin
66
*
@@ -10,9 +10,11 @@
1010

1111
namespace mblode\sproutformstables;
1212

13+
use mblode\sproutformstables\integrations\sproutforms\fields\Table;
14+
use mblode\sproutformstables\models\Settings;
15+
1316
use barrelstrength\sproutforms\services\Fields;
1417
use barrelstrength\sproutforms\events\RegisterFieldsEvent;
15-
use mblode\sproutformstables\integrations\sproutforms\fields\Table;
1618

1719
use Craft;
1820
use craft\base\Plugin;
@@ -24,19 +26,8 @@
2426
use yii\base\InvalidConfigException;
2527

2628
/**
27-
* Craft plugins are very much like little applications in and of themselves. We’ve made
28-
* it as simple as we can, but the training wheels are off. A little prior knowledge is
29-
* going to be required to write a plugin.
30-
*
31-
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
32-
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
33-
*
34-
* https://craftcms.com/docs/plugins/introduction
35-
*
36-
* @author Matthew Blode
37-
* @package SproutFormsTables
38-
* @since 1.0.0
39-
*
29+
* @property Settings $settings
30+
* @method Settings getSettings()
4031
*/
4132
class SproutFormsTables extends Plugin
4233
{
@@ -85,30 +76,12 @@ public function __construct($id, $parent = null, array $config = [])
8576
];
8677
}
8778

88-
// Base template directory
89-
// Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
90-
// if (is_dir($baseDir = $this->getBasePath().DIRECTORY_SEPARATOR.'templates')) {
91-
// $e->roots[$this->id] = $baseDir;
92-
// }
93-
// });
94-
9579
// Set this as the global instance of this module class
9680
static::setInstance($this);
9781

9882
parent::__construct($id, $parent, $config);
9983
}
10084

101-
/**
102-
* Set our $plugin static property to this class so that it can be accessed via
103-
* SproutFormsTables::$plugin
104-
*
105-
* Called after the plugin class is instantiated; do any one-time initialization
106-
* here such as hooks and events.
107-
*
108-
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
109-
* you do not need to load it in your init() method.
110-
*
111-
*/
11285
public function init()
11386
{
11487
parent::init();
@@ -131,24 +104,6 @@ function (PluginEvent $event) {
131104
}
132105
);
133106

134-
/**
135-
* Logging in Craft involves using one of the following methods:
136-
*
137-
* Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use.
138-
* Craft::info(): record a message that conveys some useful information.
139-
* Craft::warning(): record a warning message that indicates something unexpected has happened.
140-
* Craft::error(): record a fatal error that should be investigated as soon as possible.
141-
*
142-
* Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log`
143-
*
144-
* It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets
145-
* the category to the method (prefixed with the fully qualified class name) where the constant appears.
146-
*
147-
* To enable the Yii debug toolbar, go to your user account in the AdminCP and check the
148-
* [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel
149-
*
150-
* http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
151-
*/
152107
Craft::info(
153108
Craft::t(
154109
'sprout-forms-tables',
@@ -162,4 +117,20 @@ function (PluginEvent $event) {
162117
// Protected Methods
163118
// =========================================================================
164119

120+
/**
121+
* @return Settings
122+
*/
123+
protected function createSettingsModel(): Settings
124+
{
125+
return new Settings();
126+
}
127+
/**
128+
* @return string The rendered settings HTML
129+
*/
130+
protected function settingsHtml(): string
131+
{
132+
return Craft::$app->view->renderTemplate('sprout-forms-tables/settings', [
133+
'settings' => $this->getSettings(),
134+
]);
135+
}
165136
}

src/integrations/sproutforms/fields/Table.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public function getSettingsHtml()
6666
*/
6767
public function getInputHtml($value, ElementInterface $element = null): string
6868
{
69-
$value = json_decode($value, true);
69+
$settings = Craft::$app->getPlugins()->getPlugin('sprout-forms-tables')->getSettings();
70+
71+
if (!$settings->decodeValue) {
72+
$value = json_decode($value, true);
73+
}
7074

7175
return Craft::$app->getView()->renderTemplate('sprout-forms-tables/_integrations/sproutforms/formtemplates/fields/table/cpinput',
7276
[
@@ -127,24 +131,14 @@ public function getTemplatesPath()
127131
return Craft::getAlias('@mblode/sproutformstables/templates/_integrations/sproutforms/formtemplates/fields/');
128132
}
129133

130-
131134
public function normalizeValue($value, ElementInterface $element = null)
132135
{
133136
// String value when retrieved from db
134137
if (is_string($value)) {
135-
$tableArray = json_decode($value, true);
136-
137-
$value = [];
138+
$settings = Craft::$app->getPlugins()->getPlugin('sprout-forms-tables')->getSettings();
138139

139-
foreach($tableArray as $item) {
140-
$row = "";
141-
foreach($item as $key => $val) {
142-
if (strlen($val) > 0) {
143-
$row .= $key . ': ' . $val . ', ';
144-
}
145-
}
146-
$row = substr($row, 0, -2);
147-
array_push($value, $row);
140+
if ($settings->decodeValue) {
141+
$value = json_decode($value, true);
148142
}
149143
}
150144

src/models/Settings.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace mblode\sproutformstables\models;
4+
5+
use craft\base\Model;
6+
7+
class Settings extends Model
8+
{
9+
// Public Properties
10+
// =========================================================================
11+
/**
12+
* JSON Decode the table value that is used on the notification email. (Only turn on with a custom notification email template).
13+
*
14+
* @var boolean
15+
*/
16+
public $decodeValue = true;
17+
}

src/templates/_integrations/sproutforms/formtemplates/fields/table/input.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
{% for col in cols %}
4141
<td>
4242
<input type="text"
43-
{%- if name %} name="{{ name }}[{{ row }}][{{ col.value }}]"{% endif %}
43+
{%- if name %} name="{{ name }}[row-{{ row }}][{{ col.value }}]"{% endif %}
4444
{%- if id %} id="{{ id }}"{% endif %}
4545
{%- if class %} class="{{ class }} sprout-forms-tables-input"{% endif %}
4646
{%- if required %} required aria-required="true"{% endif %}

src/templates/_integrations/sproutforms/formtemplates/fields/table/settings.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{% import "_includes/forms" as forms %}
22

33
{{ forms.editableTableField({
4-
label: "Table",
5-
instructions: 'Define the table contents.',
4+
label: "Table" | t,
5+
instructions: 'Define the table contents.' | t,
66
id: 'cols',
77
name: 'cols',
8-
addRowLabel: 'Add a row',
8+
addRowLabel: 'Add a row' | t,
99
value: field.cols,
1010
cols: {
1111
label: {
12-
heading: 'Table Column',
12+
heading: 'Table Column' | t,
1313
type: 'singleline',
1414
autopopulate: 'value',
1515
},
1616
value: {
17-
heading: 'Value',
17+
heading: 'Value' | t,
1818
type: 'singleline',
1919
class: 'code'
2020
},
@@ -23,7 +23,7 @@
2323
}) }}
2424

2525
{{ forms.textField({
26-
label: "Size",
26+
label: "Number of rows" | t,
2727
id: 'size',
2828
name: 'size',
2929
value: field.size,

src/templates/settings.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% import "_includes/forms" as forms %}
2+
3+
{{ forms.lightswitchField({
4+
id: 'decodeValue',
5+
name: 'decodeValue',
6+
label: 'JSON Decode Table Values' | t,
7+
on: settings.decodeValue,
8+
instructions: 'JSON Decode the table value that is used on the notification email. (Only turn on with a custom notification email template).' | t
9+
}) }}

0 commit comments

Comments
 (0)