This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Support for Singletons #31
Open
magnumrocha
wants to merge
8
commits into
pauloamgomes:master
Choose a base branch
from
magnumrocha:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
360f7e2
add support for Singletons
magnumrocha 06fa01a
code improvements
magnumrocha 2576520
implemented schedule for singletons
magnumrocha ea75e3e
improve documentation
magnumrocha 3f5ccb5
code improvement
magnumrocha a3cb1d0
code improvement
magnumrocha 2f49dd4
code improvement
magnumrocha fc31f50
code improvement
magnumrocha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
<?php | ||
|
||
namespace Moderation\Controller; | ||
|
||
|
@@ -15,7 +15,7 @@ class RestApi extends Controller { | |
]; | ||
|
||
/** | ||
* Run schedulling. | ||
* Run scheduling. | ||
*/ | ||
public function list() { | ||
$range = $this->param('range', 10); | ||
|
@@ -45,38 +45,55 @@ public function list() { | |
} | ||
|
||
/** | ||
* Run schedulling. | ||
* Run scheduling. | ||
*/ | ||
public function run() { | ||
$results = $this->list(); | ||
$processed = []; | ||
$collections = []; | ||
$data = []; | ||
$entries = []; | ||
foreach ($results['active'] as $data) { | ||
$type = $data['schedule']['type']; | ||
if (!isset($collections[$data['_collection']])) { | ||
$collection = $this->app->module('collections')->collection($data['_collection']); | ||
$collections[$data['_collection']] = $collection; | ||
foreach ($results['active'] as $result) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably could we decouple better singletons from collections functionality, and reduce the size of the run function |
||
$type = $result['schedule']['type']; | ||
if (isset($result['_collection'])) { | ||
if (isset($data[$result['_collection']])) { | ||
$item = $data[$result['_collection']]; | ||
} | ||
else { | ||
$item = $this->app->module('collections')->collection($result['_collection']); | ||
$data[$result['_collection']] = $item; | ||
} | ||
$entry = (array) $this->app->storage->findOne("collections/{$item['_id']}", ['_id' => $result['_oid']]); | ||
} | ||
else { | ||
$collection = $collections[$data['_collection']]; | ||
if (isset($data[$result['_singleton']])) { | ||
$item = $data[$result['_singleton']]; | ||
} | ||
else { | ||
$item = $this->app->module('singletons')->singleton($result['_singleton']); | ||
$data[$result['_singleton']] = $item; | ||
} | ||
$entry = (array) $this->app->storage->findOne('singletons', ['filter' => ['_id' => $result['_oid']]); | ||
} | ||
|
||
$entry = (array) $this->app->storage->findOne("collections/{$collection['_id']}", ['_id' => $data['_oid']]); | ||
$field = $data['_field']; | ||
if ($data['_lang']) { | ||
$field .= "_{$data['_lang']}"; | ||
} | ||
if (isset($entry[$field]) && isset($this->moderation[$type])) { | ||
$old_status = $entry[$field]; | ||
$entry[$field] = $this->moderation[$type]; | ||
$this->app->module('collections')->save($data['_collection'], $entry, ['revision' => TRUE]); | ||
$this->app->storage->remove('moderation/schedule', ['_id' => $data['_id']]); | ||
if (isset($result['_collection'])) { | ||
$this->app->module('collections')->save($result['_collection'], $entry, ['revision' => TRUE]); | ||
} | ||
else { | ||
$this->app->module('singletons')->saveData($result['_singleton'], $entry, ['revision' => TRUE]); | ||
} | ||
$this->app->storage->remove('moderation/schedule', ['_id' => $result['_id']]); | ||
$processed[] = [ | ||
'_id' => $entry['_id'], | ||
'old_status' => $old_status, | ||
'new_status' => $entry[$field], | ||
'schedule_data' => $data, | ||
'schedule_data' => $result, | ||
]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Implements moderation related actions on cockpit collections. | ||
* Implements moderation related actions on cockpit collections and singletons. | ||
*/ | ||
|
||
/** | ||
|
@@ -34,7 +34,7 @@ | |
$options['fields'][$field_name] = 1; | ||
} | ||
|
||
$app->trigger("moderation.find.before", [$name, &$options]); | ||
$app->trigger("moderation.collections.find.before", [$name, &$options]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a breaking change, would be possible to maintain the |
||
}); | ||
|
||
/** | ||
|
@@ -50,7 +50,7 @@ | |
} | ||
|
||
// Get the moderation field. | ||
$field = $app->module('moderation')->getModerationField($name); | ||
$field = $app->module('moderation')->getModerationField('collection', $name); | ||
|
||
if (!$field) { | ||
return; | ||
|
@@ -79,10 +79,10 @@ | |
$moderation_field .= "_{$lang}"; | ||
} | ||
// However, this has not been filtered: | ||
$published = $app->module('moderation')->getLastPublished($entry['_id'], $moderation_field, $revisions); | ||
$published = $app->module('moderation')->getLastPublished($moderation_field, $revisions); | ||
|
||
if ($published) { | ||
$published = $app->module('moderation')->removeLangSuffix($name, $published, $lang, $ignoreDefaultFallback); | ||
$published = $app->module('moderation')->removeLangSuffix('collection', $name, $published, $lang, $ignoreDefaultFallback); | ||
$published = array_merge($entry, array_intersect_key($published, $entry)); | ||
$published = [$published]; | ||
$populated = cockpit_populate_collection($published, $populate); | ||
|
@@ -97,3 +97,93 @@ | |
// Rebuild array indices. | ||
$entries = array_values($entries); | ||
}); | ||
|
||
/** | ||
* Exclude unpublished singletons. | ||
*/ | ||
$app->on('singleton.getData.before', function ($singleton, &$options) use ($app) { | ||
|
||
// Exclude on unpublished state. | ||
foreach ($singleton['fields'] as $field) { | ||
if ($field['type'] === 'moderation') { | ||
$field_name = $field_name_lang = $field['name']; | ||
if ($field['localize'] && $lang = $app->param("lang", false)) { | ||
$field_name_lang .= "_{$lang}"; | ||
} | ||
|
||
$options['filter']['$and'][] = [$field_name_lang => ['$exists' => TRUE]]; | ||
$options['filter']['$and'][] = [$field_name_lang => ['$ne' => 'Unpublished']]; | ||
break; | ||
} | ||
} | ||
|
||
if (!isset($field_name)) { | ||
return; | ||
} | ||
|
||
// If we are using filter/fields we need to use original field name. | ||
if (!empty($options['fields'])) { | ||
$options['fields'][$field_name] = 1; | ||
} | ||
|
||
$app->trigger("moderation.singleton.getData.before", [$singleton, &$options]); | ||
}); | ||
|
||
/** | ||
* Iterate over the singleton. | ||
* | ||
* For the draft ones check if we have a previous published revision. | ||
*/ | ||
$app->on('singleton.getData.after', function ($singleton, &$data) use ($app) { | ||
$token = $app->param('previewToken', FALSE); | ||
// If we have a valid previewToken don't need to go further. | ||
if ($token && $app->module('moderation')->validateToken($token)) { | ||
return; | ||
} | ||
|
||
// Get the moderation field. | ||
$field = $app->module('moderation')->getModerationField('singleton', $singleton['name']); | ||
|
||
if (!$field) { | ||
return; | ||
} | ||
|
||
$lang = $app->param('lang', FALSE); | ||
$ignoreDefaultFallback = $app->param('ignoreDefaultFallback', FALSE); | ||
if ($ignoreDefaultFallback = $this->param('ignoreDefaultFallback', false)) { | ||
$ignoreDefaultFallback = \in_array($ignoreDefaultFallback, ['1', '0']) ? \boolval($ignoreDefaultFallback) : $ignoreDefaultFallback; | ||
} | ||
$moderation_field = $field['name']; | ||
$localize = $field['localize'] ?? FALSE; | ||
$populate = $app->param('populate', 1); | ||
|
||
if (!isset($data[$moderation_field])) { | ||
return; | ||
} | ||
|
||
// If Draft ensure we retrieve the latest published revision. | ||
// Please note that the singleton being checked has already been thru lang filtering. | ||
if ($data[$moderation_field] == 'Draft') { | ||
$revisions = $app->helper('revisions')->getList($singleton['_id']); | ||
|
||
if ($lang && $localize) { | ||
$moderation_field .= "_{$lang}"; | ||
} | ||
// However, this has not been filtered: | ||
$published = $app->module('moderation')->getLastPublished($moderation_field, $revisions); | ||
|
||
if ($published) { | ||
$published = $app->module('moderation')->removeLangSuffix('singleton', $singleton, $published, $lang, $ignoreDefaultFallback); | ||
$published = array_merge($data, array_intersect_key($published, $data)); | ||
$published = [$published]; | ||
$populated = cockpit_populate_collection($published, $populate); | ||
$data = $published[0] ? $published[0] : new stdClass(); | ||
} | ||
else { | ||
unset($data); | ||
} | ||
} | ||
else if ($data[$moderation_field] == 'Unpublished') { | ||
$data = new stdClass(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space?