Skip to content

Commit

Permalink
Add re-save function
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Mar 1, 2019
1 parent aeb493d commit e8d22ff
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 16 deletions.
71 changes: 71 additions & 0 deletions src/controllers/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,75 @@ public function actionSettings()
{
return $this->renderTemplate('super-table/plugin-settings', [
'settings' => true,
'checking' => true,
'resaving' => true,
]);
}

public function actionResaveFields()
{
// This might take a while
App::maxPowerCaptain();

// Backup!
Craft::$app->getDb()->backup();

ob_start();

$db = Craft::$app->getDb();
$fieldsService = Craft::$app->getFields();
$superTableService = SuperTable::$plugin->getService();
$matrixService = Craft::$app->getMatrix();
$projectConfig = Craft::$app->getProjectConfig();

$superTableBlockTypes = (new Query())
->select(['*'])
->from(['{{%supertableblocktypes}}'])
->all();

foreach ($superTableBlockTypes as $superTableBlockType) {
$missingFields = false;

$superTableField = $fieldsService->getFieldById($superTableBlockType['fieldId']);
$fieldLayout = $fieldsService->getLayoutById($superTableBlockType['fieldLayoutId']);

// Find what the columns should be according to the block type fields
if ($fieldLayout && $superTableField && get_class($superTableField) == SuperTableField::class) {
foreach ($fieldLayout->getFields() as $field) {
if (get_class($field) == MissingField::class) {
$missingFields = true;
break;
}
}
} else {
echo " > Super Table field #{$superTableField->id} skipped as it isn't a Super Table field ...\n";

continue;
}

if ($missingFields) {
echo " > Super Table field #{$superTableField->id} skipped as it contains missing fields ...\n";

continue;
}

// Re-save it
$fieldsService->saveField($superTableField);

echo " > Super Table field #{$superTableField->id} re-saved ...\n";
}

$output = ob_get_contents();

ob_end_clean();

$output = nl2br($output);

$output .= '<br>Field re-saving complete.';

return $this->renderTemplate('super-table/plugin-settings', [
'resaving' => true,
'resaveOutput' => $output,
]);
}

Expand All @@ -54,6 +123,7 @@ public function actionFixContentTables()
$output .= '<br>Fixes complete.';

return $this->renderTemplate('super-table/plugin-settings', [
'checking' => true,
'fixed' => true,
'output' => $output,
]);
Expand Down Expand Up @@ -271,6 +341,7 @@ public function actionCheckContentTables()
}

return $this->renderTemplate('super-table/plugin-settings', [
'checking' => true,
'checkErrors' => $errors,
'output' => $output,
]);
Expand Down
58 changes: 42 additions & 16 deletions src/templates/plugin-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,60 @@

{% block content %}

<h2>{{ 'Check Content Tables' | t('isuper-table') }}</h2>
{% if checking is defined %}

<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>
<h2>{{ 'Check Content Tables' | t('super-table') }}</h2>

<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>
<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>

{% if output is defined %}
<hr>
<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>

{% if output is defined %}
<hr>

<code>{{ output | raw }}</code>

<code>{{ output | raw }}</code>
<hr>
{% endif %}

{% if settings is defined %}
<a class="btn submit" href="{{ actionUrl('super-table/plugin/check-content-tables') }}">{{ 'Check Content Tables' | t('super-table') }}</a>
{% endif %}

{% if checkErrors is defined %}
{% if checkErrors %}
<a class="btn submit" href="{{ actionUrl('super-table/plugin/fix-content-tables') }}">{{ 'Fix Content Tables' | t('super-table') }}</a>
{% else %}
<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
{% endif %}
{% endif %}

{% if fixed is defined %}
<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
{% endif %}

<hr>
{% endif %}

{% if settings is defined %}
<a class="btn submit" href="{{ actionUrl('super-table/plugin/check-content-tables') }}">{{ 'Check Content Tables' | t('super-table') }}</a>
{% endif %}

{% if checkErrors is defined %}
{% if checkErrors %}
<a class="btn submit" href="{{ actionUrl('super-table/plugin/fix-content-tables') }}">{{ 'Fix Content Tables' | t('super-table') }}</a>
{% else %}
{% if resaving is defined %}

<h2>{{ 'Re-save Super Table Fields' | t('super-table') }}</h2>

<p>{{ 'Re-saving all your Super Table fields at once can help to fix content table issues. If any Super Table fields contain missing field types (as in, the plugin for the field is missing), it will be skipped. This will also include any nested Super Tables in Matrix fields.' | t('super-table') | md }}</p>

{% if resaveOutput is defined %}
<hr>

<code>{{ resaveOutput | raw }}</code>

<hr>

<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
{% else %}
<a class="btn submit" href="{{ actionUrl('super-table/plugin/resave-fields') }}">{{ 'Re-save Fields' | t('super-table') }}</a>
{% endif %}
{% endif %}

{% if fixed is defined %}
<a class="btn submit" href="{{ cpUrl('super-table/settings') }}">{{ 'Done' | t('super-table') }}</a>
{% endif %}

{% endblock %}

0 comments on commit e8d22ff

Please sign in to comment.