This repository has been archived by the owner on May 9, 2023. It is now read-only.
/
actions.php
99 lines (85 loc) · 3.12 KB
/
actions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* @file
* Implements moderation related actions on cockpit collections.
*/
/**
* Exclude unpublished entries.
*/
$app->on('collections.find.before', function ($name, &$options) use ($app) {
// Get the collection.
$collection = $this->module('collections')->collection($name);
// Exclude on unpublished state.
foreach ($collection['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.find.before", [$name, &$options]);
});
/**
* Iterate over the collection entries.
*
* For the draft ones check if we have a previous published revision.
*/
$app->on('collections.find.after', function ($name, &$entries) 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($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);
foreach ($entries as $idx => $entry) {
if (!isset($entry[$moderation_field])) {
continue;
}
// 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 && $localize) {
$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, $ignoreDefaultFallback);
$published = array_merge($entry, array_intersect_key($published, $entry));
$published = [$published];
$populated = cockpit_populate_collection($published, $populate);
$published = current($populated);
$entries[$idx] = $published;
}
else {
unset($entries[$idx]);
}
}
}
// Rebuild array indices.
$entries = array_values($entries);
});