Skip to content

Commit

Permalink
Add option to purge only deleted messages (#565)
Browse files Browse the repository at this point in the history
* Add option to purge deleted messages
* Add "scan errors" column to messages table
  • Loading branch information
multiwebinc authored Apr 6, 2020
1 parent 4a3d601 commit 64d76ce
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 6 deletions.
11 changes: 9 additions & 2 deletions assets/js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
this.toHeader = null

/*
* Template for the "found" header (title)
*/
this.foundHeader = null

/*
* The table widget element
*/
Expand Down Expand Up @@ -76,14 +81,16 @@
$toolbar.prepend(Mustache.render(this.tableToolbar.html()))
}

this.setTitleContents = function(fromEl, toEl) {
this.setTitleContents = function(fromEl, toEl, foundEl) {
if (fromEl) this.fromHeader = $(fromEl)
if (toEl) this.toHeader = $(toEl)
if (foundEl) this.foundHeader = $(foundEl)
if (!this.tableElement) return

var $headers = $('table.headers th', this.tableElement)
$headers.eq(0).html(this.fromHeader.html())
$headers.eq(1).html(Mustache.render(this.toHeader.html(), { hideTranslated: this.hideTranslated } ))
$headers.eq(2).html(this.foundHeader.html())
}

this.setTableElement = function(el) {
Expand Down Expand Up @@ -155,4 +162,4 @@

$.translateMessages = new TranslateMessages;

}(window.jQuery);
}(window.jQuery);
4 changes: 4 additions & 0 deletions classes/ThemeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public static function scan()
*/
public function scanForMessages()
{
// Set all messages initially as being not found. The scanner later
// sets the entries it finds as found.
Message::query()->update(['found' => false]);

$this->scanThemeConfigForMessages();
$this->scanThemeTemplatesForMessages();
$this->scanMailTemplatesForMessages();
Expand Down
7 changes: 6 additions & 1 deletion controllers/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function onScanMessages()
}

ThemeScanner::scan();

if (post('purge_deleted_messages', false)) {
Message::where('found', 0)->delete();
}

Flash::success(Lang::get('rainlab.translate::lang.messages.scan_messages_success'));

Expand Down Expand Up @@ -198,7 +202,8 @@ protected function processTableData($messages, $from, $to)
'id' => $message->id,
'code' => $message->code,
'from' => $message->forLocale($fromCode),
'to' => $toContent
'to' => $toContent,
'found' => $message->found ? '' : Lang::get('rainlab.translate::lang.messages.not_found'),
];
}

Expand Down
16 changes: 15 additions & 1 deletion controllers/messages/_scan_messages_form.htm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h4 class="modal-title"><?= e(trans('rainlab.translate::lang.messages.scan_messa
<p>
<?= e(trans('rainlab.translate::lang.messages.scan_messages_process')) ?>
<?= e(trans('rainlab.translate::lang.messages.scan_messages_process_limitations')) ?>

</p>

<div class="form-preview">
Expand All @@ -29,6 +29,20 @@ <h4 class="modal-title"><?= e(trans('rainlab.translate::lang.messages.scan_messa
<?= e(trans('rainlab.translate::lang.messages.scan_messages_purge_help')) ?>
</p>
</div>

<div class="checkbox custom-checkbox">
<input
type="checkbox"
name="purge_deleted_messages"
value="1"
id="purgeDeletedMessages">
<label for="purgeDeletedMessages">
<?= e(trans('rainlab.translate::lang.messages.scan_messages_purge_deleted_label')) ?>
</label>
<p class="help-block">
<?= e(trans('rainlab.translate::lang.messages.scan_messages_purge_deleted_help')) ?>
</p>
</div>
</div>
</div>

Expand Down
5 changes: 5 additions & 0 deletions controllers/messages/_table_headers.htm
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@
<?php endif ?>
</ul>
</div>

<!-- Found Header -->
<script type="text/template" id="<?= $this->getId('foundTitle') ?>">
<?= e(trans('rainlab.translate::lang.messages.found')) ?> <i class="icon-info-circle" style="cursor: help" title="<?= e(trans('rainlab.translate::lang.messages.found_help')) ?>"></i>
</script>
3 changes: 3 additions & 0 deletions controllers/messages/config_table.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ columns:
title: From
to:
title: To
found:
title: Scan errors
width: 150px
2 changes: 1 addition & 1 deletion controllers/messages/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$(document).render(function(){
$.translateMessages.setTableElement('#<?= $table->getId() ?>')
$.translateMessages.setTitleContents('#<?= $this->getId('fromTitle') ?>', '#<?= $this->getId('toTitle') ?>')
$.translateMessages.setTitleContents('#<?= $this->getId('fromTitle') ?>', '#<?= $this->getId('toTitle') ?>', '#<?= $this->getId('foundTitle') ?>')
$.translateMessages.setToolbarContents('#<?= $this->getId('tableToolbar') ?>')
})

Expand Down
5 changes: 5 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@
'scan_messages_purge_label' => 'Purge all messages first',
'scan_messages_purge_help' => 'If checked, this will delete all messages, including their translations, before performing the scan.',
'scan_messages_purge_confirm' => 'Are you sure you want to delete all messages? This cannot be undone!',
'scan_messages_purge_deleted_label' => 'Purge missing messages after scan',
'scan_messages_purge_deleted_help' => 'If checked, after the scan is done, any messages the scanner did not find, including their translations, will be deleted. This cannot be undone!',
'hint_translate' => 'Here you can translate messages used on the front-end, the fields will save automatically.',
'hide_translated' => 'Hide translated',
'export_messages_link' => 'Export Messages',
'import_messages_link' => 'Import Messages',
'not_found' => 'Not found',
'found_help' => 'Whether any errors occurred during scanning.',
'found_title' => 'Scan errors',
],
];
10 changes: 9 additions & 1 deletion models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public static function importMessageCodes($messages, $locale = null)
$locale = static::DEFAULT_LOCALE;
}

$existingIds = [];

foreach ($messages as $code => $message) {
// Ignore empties
if (!strlen(trim($message))) {
Expand All @@ -181,16 +183,22 @@ public static function importMessageCodes($messages, $locale = null)

$messageData = $item->exists || $item->message_data ? $item->message_data : [];

// Do not overwrite existing translations
// Do not overwrite existing translations.
if (isset($messageData[$locale])) {
$existingIds[] = $item->id;
continue;
}

$messageData[$locale] = $message;

$item->message_data = $messageData;
$item->found = true;

$item->save();
}

// Set all messages found by the scanner as found
self::whereIn('id', $existingIds)->update(['found' => true]);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions updates/update_messages_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace RainLab\Translate\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class UpdateMessagesTable extends Migration
{
public function up()
{
Schema::table('rainlab_translate_messages', function($table)
{
$table->boolean('found')->default(1);
});
}

public function down()
{
Schema::table('rainlab_translate_messages', function($table)
{
$table->dropColumn('found');
});
}
}
4 changes: 4 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@
- migrate_morphed_indexes.php
1.6.8: Add support for transOrderBy; Add translatoin support for ThemeData; Update russian localization.
1.6.9: Clear Static Page menu cache after saving the model; CSS fix for Text/Textarea input fields language selector.
1.6.10:
- Add option to purge deleted messages when scanning messages
- Add Scan error column on Messages page
- update_messages_table.php

0 comments on commit 64d76ce

Please sign in to comment.