From dc07300655e65abffa2034542883b8547de2bacb Mon Sep 17 00:00:00 2001 From: Fredrik Karlsson Date: Fri, 18 Oct 2019 20:28:09 +0200 Subject: [PATCH] Add support for localized moderation field --- actions.php | 19 +++++++++--- bootstrap.php | 16 ++++++++++ views/partials/entry-aside.php | 53 ++++++++++++++++++++++++++-------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/actions.php b/actions.php index 65416f8..2561acc 100644 --- a/actions.php +++ b/actions.php @@ -15,8 +15,12 @@ foreach ($collection['fields'] as $field) { if ($field['type'] === 'moderation') { $field_name = $field['name']; - $options['filter']['$and'][] = ["{$field['name']}" => ['$exists' => TRUE]]; - $options['filter']['$and'][] = ["{$field['name']}" => ['$ne' => 'Unpublished']]; + if ($field['localize'] && $lang = $app->param("lang", false)) { + $field_name .= "_$lang"; + } + + $options['filter']['$and'][] = [$field_name => ['$exists' => TRUE]]; + $options['filter']['$and'][] = [$field_name => ['$ne' => 'Unpublished']]; break; } } @@ -56,10 +60,18 @@ } // If Draft ensure we retrieve the latest published revision. + // Please note that the entry being checked has already been thru lang filtering. if ($entry[$moderation_field] == 'Draft') { $revisions = $app->helper('revisions')->getList($entry['_id']); + + if ($lang = $app->param('lang', false)) { + $moderation_field .= "_$lang"; + } + // However, this has not been filtered: $published = $app->module('moderation')->getLastPublished($entry['_id'], $moderation_field, $revisions); + if ($published) { + $published = $app->module('moderation')->removeLangSuffix($name, $published, $lang); $published = array_merge($entry, array_intersect_key($published, $entry)); $published = [$published]; $populated = cockpit_populate_collection($published, 1); @@ -73,5 +85,4 @@ } // Rebuild array indices. $entries = array_values($entries); -}); - +}); \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php index e5dc175..f552fca 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -58,6 +58,22 @@ return ['success' => $this->app->module('cockpit')->saveApiKeys($keys)]; }, + 'removeLangSuffix' => function($name, $entry, $lang) { + if ($lang) { + $collection = $this->app->module('collections')->collection($name); + foreach ($collection['fields'] as $field) { + if($field['localize']) { + $fieldName = $field['name']; + $suffixedFieldName = $fieldName."_$lang"; + $entry[$fieldName] = $entry[$suffixedFieldName]; + if (isset($entry["{$suffixedFieldName}_slug"])) { + $entry["{$fieldName}_slug"] = $entry["{$suffixedFieldName}_slug"]; + } + } + } + } + return $entry; + }, ]); // Incldude admin. diff --git a/views/partials/entry-aside.php b/views/partials/entry-aside.php index 113d948..add1114 100644 --- a/views/partials/entry-aside.php +++ b/views/partials/entry-aside.php @@ -1,14 +1,14 @@
- {originalModeration !== entry[moderation_field] ? App.i18n.get("Change to:") : App.i18n.get("Save as:")} @lang("{entry[moderation_field]}") + {originalModeration[lang] !== entry[moderation_field] ? App.i18n.get("Change to:") : App.i18n.get("Save as:")} @lang("{entry[moderation_field]}")