Skip to content

Commit 62b3b5a

Browse files
committed
Add checker/fixer to plugin settings
1 parent 808ccca commit 62b3b5a

File tree

3 files changed

+121
-9
lines changed

3 files changed

+121
-9
lines changed

src/SuperTable.php

+20
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
use Craft;
1212
use craft\base\Plugin;
1313
use craft\events\RegisterComponentTypesEvent;
14+
use craft\events\RegisterUrlRulesEvent;
15+
use craft\helpers\UrlHelper;
1416
use craft\services\Elements;
1517
use craft\services\Fields;
18+
use craft\web\UrlManager;
1619
use craft\web\twig\variables\CraftVariable;
1720

1821
use NerdsAndCompany\Schematic\Schematic;
@@ -28,6 +31,7 @@ class SuperTable extends Plugin
2831
// =========================================================================
2932

3033
public $schemaVersion = '2.0.8';
34+
public $hasCpSettings = true;
3135

3236
// Traits
3337
// =========================================================================
@@ -46,16 +50,32 @@ public function init()
4650

4751
$this->_setPluginComponents();
4852
$this->_setLogging();
53+
$this->_registerCpRoutes();
4954
$this->_registerVariables();
5055
$this->_registerFieldTypes();
5156
$this->_registerElementTypes();
5257
$this->_registerIntegrations();
5358
$this->_registerConfigListeners();
5459
}
5560

61+
public function getSettingsResponse()
62+
{
63+
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('super-table/settings'));
64+
}
65+
66+
5667
// Private Methods
5768
// =========================================================================
5869

70+
private function _registerCpRoutes()
71+
{
72+
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
73+
$event->rules = array_merge($event->rules, [
74+
'super-table/settings' => 'super-table/plugin/settings',
75+
]);
76+
});
77+
}
78+
5979

6080
private function _registerVariables()
6181
{

src/controllers/PluginController.php

+63-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class PluginController extends Controller
2121
// Public Methods
2222
// =========================================================================
2323

24+
public function actionSettings()
25+
{
26+
return $this->renderTemplate('super-table/plugin-settings', [
27+
'settings' => true,
28+
]);
29+
}
30+
2431
public function actionFixContentTables()
2532
{
2633
// Backup!
@@ -36,11 +43,14 @@ public function actionFixContentTables()
3643

3744
ob_end_clean();
3845

39-
echo nl2br($output);
46+
$output = nl2br($output);
4047

41-
echo '<br>Fixes complete.';
48+
$output .= '<br>Fixes complete.';
4249

43-
exit;
50+
return $this->renderTemplate('super-table/plugin-settings', [
51+
'fixed' => true,
52+
'output' => $output,
53+
]);
4454
}
4555

4656
public function actionCheckContentTables()
@@ -154,18 +164,62 @@ public function actionCheckContentTables()
154164
}
155165
}
156166

167+
// Check each table for missing field columns in their content tables
168+
$superTableBlockTypes = (new Query())
169+
->select(['*'])
170+
->from(['{{%supertableblocktypes}}'])
171+
->all();
172+
173+
foreach ($superTableBlockTypes as $superTableBlockType) {
174+
$correctFieldColumns = [];
175+
$dbFieldColumns = [];
176+
177+
$fieldLayout = $fieldsService->getLayoutById($superTableBlockType['fieldLayoutId']);
178+
179+
// Find what the columns should be according to the block type fields
180+
if ($fieldLayout) {
181+
foreach ($fieldLayout->getFields() as $field) {
182+
if ($field::hasContentColumn()) {
183+
$correctFieldColumns[] = 'field_' . $field->handle;
184+
}
185+
}
186+
}
187+
188+
$field = $fieldsService->getFieldById($superTableBlockType['fieldId']);
189+
190+
if ($field) {
191+
$contentTable = $field->contentTable;
192+
193+
if ($contentTable) {
194+
$columns = Craft::$app->getDb()->getTableSchema($contentTable)->columns;
195+
196+
foreach ($columns as $key => $column) {
197+
if (strstr($key, 'field_')) {
198+
$dbFieldColumns[] = $key;
199+
}
200+
}
201+
202+
if ($correctFieldColumns != $dbFieldColumns) {
203+
$errors = true;
204+
echo " > ERROR: {$contentTable} has missing field columns ...\n";
205+
}
206+
}
207+
}
208+
}
209+
157210
$output = ob_get_contents();
158211
ob_end_clean();
159212

160-
echo nl2br($output);
213+
$output = nl2br($output);
161214

162-
if ($errors) {
163-
echo '<br>Fix the above errors by running the <a href="' . UrlHelper::actionUrl('super-table/plugin/fix-content-tables') . '">Super Table content table fixer.';
164-
} else {
165-
echo 'No errors found.';
215+
if (!$errors) {
216+
$output .= 'No errors found.';
166217
}
167218

168-
exit;
219+
return $this->renderTemplate('super-table/plugin-settings', [
220+
'checkErrors' => $errors,
221+
'output' => $output,
222+
]);
169223
}
170224

171225
}

src/templates/plugin-settings.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{% extends '_layouts/cp' %}
2+
{% import '_includes/forms' as forms %}
3+
4+
{% set title = 'Super Table' | t('super-table') %}
5+
6+
{% block content %}
7+
8+
<h2>{{ 'Check Content Tables' | t('isuper-table') }}</h2>
9+
10+
<p>{{ 'If you\'re updating from Craft 3.0.x, or are experiencing error messages with content tables, please use this tool to check for errors or inconsistencies.' | t('super-table') | md }}</p>
11+
12+
<p>{{ 'This tool will first check to see if there are any errors, then advise if you want to go ahead and fix them.' | t('super-table') | md }}</p>
13+
14+
{% if output is defined %}
15+
<hr>
16+
17+
<code>{{ output | raw }}</code>
18+
19+
<hr>
20+
{% endif %}
21+
22+
{% if settings is defined %}
23+
<a class="btn submit" href="{{ actionUrl('super-table/plugin/check-content-tables') }}">{{ 'Check Content Tables' | t('super-table') }}</a>
24+
{% endif %}
25+
26+
{% if checkErrors is defined %}
27+
{% if checkErrors %}
28+
<a class="btn submit" href="{{ actionUrl('super-table/plugin/fix-content-tables') }}">{{ 'Fix Content Tables' | t('super-table') }}</a>
29+
{% else %}
30+
<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
31+
{% endif %}
32+
{% endif %}
33+
34+
{% if fixed is defined %}
35+
<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
36+
{% endif %}
37+
38+
{% endblock %}

0 commit comments

Comments
 (0)