forked from backdrop-contrib/nodequeue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodequeue.actions.inc
274 lines (245 loc) · 8.16 KB
/
nodequeue.actions.inc
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* @file
* Provides actions integration for nodequeues.
*/
/**
* Configuration form for Add to Nodequeues action.
*/
function nodequeue_add_action_form($context) {
// Default values for form.
if (!isset($context['qids'])) {
$context['qids'] = '';
}
$queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
foreach ($queues as $qid => $queue) {
$options[$qid] = $queue->title;
}
$form = array();
if (count($options)) {
// Add form components.
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queue"),
'#default_value' => $context['qids'],
'#multiple' => TRUE,
'#options' => $options,
'#required' => TRUE,
'#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
);
}
else {
backdrop_set_message(t('Please <a href="@url">create</a> a nodequeue first.', array('@url' => url('admin/structure/nodequeue'))));
}
return $form;
}
/**
* Submit handler for Add to Nodequeues action configuration.
*/
function nodequeue_add_action_submit($form, &$form_state) {
return array(
'qids' => $form_state['values']['qids'],
);
}
/**
* Action to add a node to a queue.
*/
function nodequeue_add_action($node, $context) {
$queues = nodequeue_load_queues($context['qids'], TRUE);
// Filter out queues by node type. We choose not to use nodequeue_get_qids() because it checks for access control which only matters if we administering a queue.
$eligible_queues = array();
foreach ($queues as $queue) {
if (in_array($node->type, $queue->types)) {
$eligible_queues[$queue->qid] = $queue;
}
}
if (!empty($eligible_queues)) {
// Remove the node from the eligible queues (if needed).
nodequeue_remove_action($node, array('qids' => array_keys($eligible_queues)));
// Use API to get the eligible subqueues
$eligible_subqueues = nodequeue_get_subqueues_by_node($eligible_queues, $node);
// Add node to each subqueue.
foreach ($eligible_subqueues as $subqueue) {
nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
}
}
}
/**
* Old-style action to add a node to a queue.
*/
function action_nodequeue_add($op, $edit = array(), $node) {
switch ($op) {
case 'metadata':
return array(
'description' => t('Add to Nodequeues'),
'type' => t('node'),
'batchable' => TRUE,
'configurable' => TRUE,
);
break;
case 'do':
$queues = nodequeue_load_queues($edit['qids'], TRUE);
// Filter out queues by node type. We choose not to use nodequeue_get_qids() because it checks for access control which only matters if we administering a queue.
$eligible_queues = array();
foreach ($queues as $queue) {
if (in_array($node->type, $queue->types)) {
$eligible_queues[$queue->qid] = $queue;
}
}
if (!empty($eligible_queues)) {
// Remove the node from the eligible queues (if needed).
action_nodequeue_remove('do', array('qids' => array_keys($eligible_queues)), $node);
// Use API to get the eligible subqueues
$eligible_subqueues = nodequeue_get_subqueues_by_node($eligible_queues, $node);
// Add node to each subqueue.
foreach ($eligible_subqueues as $subqueue) {
nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
}
}
break;
// return an HTML config form for the action
case 'form':
// default values for form
if (!isset($edit['qids'])) {
$edit['qids'] = '';
}
$queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
foreach ($queues as $qid => $queue) {
$options[$qid] = $queue->title;
}
$form = array();
if (count($options)) {
// add form components
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queue"),
'#default_value' => $edit['qids'],
'#multiple' => TRUE,
'#options' => $options,
'#required' => TRUE,
'#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
);
}
else {
backdrop_set_message(t('Please <a href="@url">create</a> a nodequeue first.', array('@url' => url('admin/structure/nodequeue'))));
}
return $form;
// validate the HTML form
// process the HTML form to store configuration
case 'submit':
$params = array(
'qids' => $edit['qids']
);
return $params;
break;
}
}
/**
* Configuration form for Remove from Nodequeues action.
*/
function nodequeue_remove_action_form($context) {
// Default values for form.
if (!isset($context['qids'])) {
$context['qids'] = array();
}
$queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
foreach ($queues as $qid => $queue) {
$options[$qid] = $queue->title;
}
// Add form components.
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queues"),
'#default_value' => $context['qids'],
'#multiple' => TRUE,
'#decription' => t('Specify the queues from which the node should be removed. If the queue is a smartqueue, the node shall be removed from all subqueues.'),
'#required' => TRUE,
'#options' => $options,
);
return $form;
}
/**
* Submit handler for Remove from Nodequeues action configuration.
*/
function nodequeue_remove_action_submit($form, &$form_state) {
return array(
'qids' => $form_state['values']['qids'],
);
}
function nodequeue_remove_action($node, $context) {
$qids = $context['qids'];
// If a node is being deleted, ensure it's also removed from any queues.
$args = $qids;
$result = db_select('nodequeue_nodes', 'n')
->fields('n')
->condition('nid', $node->nid)
->condition('qid', $args)
->execute()
->fetchAll();
foreach ($result as $obj) {
// This removes by nid, not position, because if we happen to have a
// node in a queue twice, the 2nd position would be wrong.
nodequeue_subqueue_remove_node($obj->sqid, $node->nid);
}
}
/**
* Old-style action to remove a node from a queue.
*/
function action_nodequeue_remove($op, $edit = array(), $node) {
switch ($op) {
case 'metadata':
return array(
'description' => t('Remove from Nodequeues'),
'type' => t('node'),
'batchable' => TRUE,
'configurable' => TRUE,
);
break;
case 'do':
$qids = $edit['qids'];
// If a node is being deleted, ensure it's also removed from any queues.
$args = $qids;
$result = db_select('nodequeue_nodes', 'n')
->fields('n')
->condition('nid', $node->nid)
->condition('qid', $args)
->execute()
->fetchAll();
foreach ($result as $obj) {
// This removes by nid, not position, because if we happen to have a
// node in a queue twice, the 2nd position would be wrong.
nodequeue_subqueue_remove_node($obj->sqid, $node->nid);
}
break;
// return an HTML config form for the action
case 'form':
// default values for form
if (!isset($edit['qids'])) {
$edit['qids'] = array();
}
$queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
foreach ($queues as $qid => $queue) {
$options[$qid] = $queue->title;
}
// add form components
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queues"),
'#default_value' => $edit['qids'],
'#multiple' => TRUE,
'#decription' => t('Specify the queues from which the node should be removed. If the queue is a smartqueue, the node shall be removed from all subqueues.'),
'#required' => TRUE,
'#options' => $options,
);
return $form;
break;
// validate the HTML form
// process the HTML form to store configuration
case 'submit':
$params = array(
'qids' => $edit['qids']
);
return $params;
break;
}
}