Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from Pervanovo/localize-support
Browse files Browse the repository at this point in the history
Add support for localized moderation field
  • Loading branch information
pauloamgomes authored Oct 18, 2019
2 parents 5f4fe6a + dc07300 commit 4004847
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
19 changes: 15 additions & 4 deletions actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -73,5 +85,4 @@
}
// Rebuild array indices.
$entries = array_values($entries);
});

});
16 changes: 16 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 41 additions & 12 deletions views/partials/entry-aside.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div class="uk-margin moderation-status" if="{field && field.length && moderation_field}">
<div class="uk-width-1-1 uk-form-select uk-moderation-element uk-moderation-{ entry[moderation_field] }">
<label class="uk-text">
<i if="{originalModeration == 'Unpublished'}" class="icon-Unpublished uk-icon-circle-o"></i>
<i if="{originalModeration == 'Draft'}" class="icon-Draft uk-icon-pencil"></i>
<i if="{originalModeration == 'Published'}" class="icon-Published uk-icon-circle"></i>
<strong>@lang('Status:')</strong> {originalModeration}
<i if="{originalModeration[lang] == 'Unpublished'}" class="icon-Unpublished uk-icon-circle-o"></i>
<i if="{originalModeration[lang] == 'Draft'}" class="icon-Draft uk-icon-pencil"></i>
<i if="{originalModeration[lang] == 'Published'}" class="icon-Published uk-icon-circle"></i>
<strong>@lang('Status:')</strong> {originalModeration[lang]}
</label>
<div class="uk-margin-small-top">
<span class="uk-badge uk-badge-outline">
{originalModeration !== entry[moderation_field] ? App.i18n.get("Change to:") : App.i18n.get("Save as:")} <strong>@lang("{entry[moderation_field]}")</strong>
{originalModeration[lang] !== entry[moderation_field] ? App.i18n.get("Change to:") : App.i18n.get("Save as:")} <strong>@lang("{entry[moderation_field]}")</strong>
</span>
</div>
<select bind="entry.{moderation_field}">
Expand All @@ -22,20 +22,28 @@
<script>
var $this = this;
$this.moderation_field = 'status';
$this.originalModeration = '';
$this.originalModeration = {'': 'Draft'};
$this.canPublish = {{ $app->module("cockpit")->hasaccess('moderation', ['manage', 'publish']) ? 1 : 0 }};
$this.canUnpublish = {{ $app->module("cockpit")->hasaccess('moderation', ['manage', 'unpublish']) ? 1 : 0 }};
$this.lang = $this.lang || "";
$this.localize = false;
var oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
if (/^.*(\/collections\/save_entry\/\w+)$/.test(url)) {
this.addEventListener('load', function() {
var entry = JSON.parse(this.responseText);
$this.originalModeration = entry[$this.moderation_field];
var entry = JSON.parse(this.responseText);
$this.originalModeration[''] = entry[$this.moderation_field_name];
if ($this.localize) {
for (var l of $this.languages) {
$this.originalModeration[l.code] = entry[$this.moderation_field_name + "_" + l.code];
}
}
});
}
return oldXHROpen.apply(this, arguments);
}
};
this.on('mount', function() {
$this.field = this.collection.fields.filter(function(definition) {
Expand All @@ -46,10 +54,22 @@
return;
}
$this.moderation_field = $this.field[0].name;
$this.localize = $this.field[0].localize || false;
$this.moderation_field_name = $this.field[0].name;
$this.moderation_field = moderation_field();
$this.originalModeration = $this.entry[$this.moderation_field] || 'Draft';
$this.entry[$this.moderation_field] = 'Draft';
$this.originalModeration[''] = $this.entry[$this.moderation_field_name] || 'Draft';
if ($this.entry[$this.moderation_field_name] !== 'Unpublished') {
$this.entry[$this.moderation_field_name] = 'Draft';
}
if ($this.localize) {
for (var l of $this.languages) {
$this.originalModeration[l.code] = $this.entry[$this.moderation_field_name + "_" + l.code] || 'Draft';
if ($this.entry[$this.moderation_field_name + "_" + l.code] !== 'Unpublished') {
$this.entry[$this.moderation_field_name + "_" + l.code] = 'Draft';
}
}
}
window.setTimeout(function() {
sidebar = document.querySelector('.uk-width-medium-1-4.uk-flex-order-first');
Expand All @@ -59,5 +79,14 @@
$this.update();
});
this.on("update", function() {
$this.moderation_field = moderation_field();
});
function moderation_field() {
return $this.localize && $this.lang
? $this.moderation_field_name + "_" + $this.lang
: $this.moderation_field_name;
}
</script>

0 comments on commit 4004847

Please sign in to comment.