forked from pauloamgomes/CockpitCMS-Moderation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.php
189 lines (161 loc) · 6.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* @file
* Implements moderation related actions on cockpit collections and singletons.
*/
/**
* 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.collections.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('collection', $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($moderation_field, $revisions);
if ($published) {
$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);
$published = current($populated);
$entries[$idx] = $published;
}
else {
unset($entries[$idx]);
}
}
}
// 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();
}
});