-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjournalstream.module
executable file
·702 lines (661 loc) · 23 KB
/
journalstream.module
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php
/**
* @file
* Code for the scf_library feature.
*/
include_once('journalstream.features.inc');
/** Constants **/
define('JS_TOPIC_QUEUE_TYPE','topic_queue'); //Node type of topic queue
define('JS_COLLECTION_TYPE','collection'); //Node type of topic queue
define('JS_COLLECTIONS_QUEUE_VIEW', 'journalstream_queue');
define('JS_NOT_REVIEWED','not_reviewed');
define('JS_ACCEPTED', 'accepted');
define('JS_REJECTED', 'rejected');
/**
* Implements hook_menu()
*/
function journalstream_menu() {
$items = array();
//Admin Page
$items['admin/settings/journalstream'] = array(
'title' => 'Journalstream Options',
'description' => 'Manage Journalstream options.',
'page callback' => 'drupal_get_form',
'page arguments' => array('journalstream_admin_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'journalstream.admin.inc',
);
//Lists the latest published journalstream
$items['journalstream'] = array(
'title' => 'Latest Journalstream',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'journalstream_latest_collection',
'access arguments' => array('access content'),
);
//Jouranlstream Moderation Tabs
$items['node/%node/not_reviewed'] = array(
'title' => 'Not Reviewed',
'type' => MENU_LOCAL_TASK,
'page callback' => 'journalstream_queue_view',
'page arguments' => array(1,2),
'access callback' => 'journalstream_local_task_loader',
'access arguments' => array(1),
'weight' => 5,
);
$items['node/%node/accepted'] = array(
'title' => 'Accepted',
'type' => MENU_LOCAL_TASK,
'page callback' => 'journalstream_queue_view',
'page arguments' => array(1,2),
'access callback' => 'journalstream_local_task_loader',
'access arguments' => array(1),
'weight' => 6,
);
$items['node/%node/rejected'] = array(
'title' => 'Rejected',
'type' => MENU_LOCAL_TASK,
'page callback' => 'journalstream_queue_view',
'page arguments' => array(1,2),
'access callback' => 'journalstream_local_task_loader',
'access arguments' => array(1),
'weight' => 7,
);
$items['node/%node/preview'] = array(
'title' => 'Preview',
'type' => MENU_LOCAL_TASK,
'page callback' => 'journalstream_queue_view',
'page arguments' => array(1,2),
'access callback' => 'journalstream_local_task_loader',
'access arguments' => array(1),
'weight' => 8,
);
return $items;
}
/**
* Implements hook_admin_paths().
*/
function journalstream_admin_paths() {
$paths = array(
'node/*/not_reviewed' => TRUE,
'node/*/accepted' => TRUE,
'node/*/rejected' => TRUE,
);
return $paths;
}
/** View Related Hooks **/
/**
* Renders the correct view based on path.
* @param $node
* The Topic Query Node
* @param $review_status
* The review status of the node
*/
function journalstream_queue_view($node, $review_status) {
$view = views_get_view(JS_COLLECTIONS_QUEUE_VIEW);
$output = '';
$statuses = array('not_reviewed','accepted','rejected','preview');
$view_output = array();
if(in_array($review_status,$statuses)) {
$view_output = $view->execute_display($review_status, array($node->nid));
}
// Add publish form to preview page
if($review_status == 'preview') {
$view_output['content'] .= drupal_render(drupal_get_form('journalstream_queue_publish_form',array('nid' => $node->nid)));
}
return (!empty($view_output['content'])) ? $view_output['content'] : '';
}
/**
* Hook for rendering the most recent published collection
*/
function journalstream_latest_collection() {
$nid = _journalstream_most_recent_collection_nid();
if($nid) {
menu_set_active_item('journalstream');
return menu_execute_active_handler('node/' . $nid, FALSE);
}
else {
drupal_set_title("There are currently no published collections.");
}
}
/**
* Implements hook_form
* Displays a button/form for publishing a queue
*/
function journalstream_queue_publish_form($form, &$form_state, $extra) {
$nid = (isset($extra['nid'])) ? $extra['nid'] : false;
if($nid) {
$form['publish'] = array(
'#type' => 'button',
'#value' => 'Publish',
);
$form['#action'] = url('node/add/collection/' . $nid);
return $form;
}
}
/**
* Add default node template to theme suggestion
**/
function journalstream_theme_registry_alter(&$theme_registry) {
$mod_path = drupal_get_path('module', 'journalstream') . '/templates';
$theme_registry_copy = $theme_registry; // munge on a copy
_theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'journalstream', $mod_path);
$theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
$hooks = array('node');
foreach ($hooks as $h) {
_journalstream_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);
}
// Add Theme hook for altering view
// Code from http://drupal.org/node/303586#comment-2552974
//$theme_registry['views_view_row_node__papers_of_the_week']['preprocess functions'][] = 'pow_preprocess_views_view_row_node__papers_of_the_week';
}
/**
* Helper function for re-ordering arrays (needed by theme_registry_alter)
**/
function _journalstream_insert_after_first_element(&$a, $element) {
if(is_array($a)) {
$first_element = array_shift($a);
array_unshift($a, $first_element, $element);
}
}
/**
* Access callback for ONLY displaying the journalstream local tasks
* if the node type is JS_TOPIC_QUEUE_TYPE
*/
function journalstream_local_task_loader($node) {
global $user;
//Check node access perms
$access_granted = node_access('update', $node);
if($node->type == JS_TOPIC_QUEUE_TYPE && $access_granted) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Implements hook_form_alter()
*/
function journalstream_form_alter(&$form, &$form_state, $form_id) {
//Hide elements from the topic queue node form
if($form_id == 'topic_queue_node_form') {
unset($form['field_not_reviewed_nodes']);
unset($form['field_accepted_nodes']);
unset($form['field_rejected_nodes']);
}
//Add Node Topic Queues to the collection Add Form
if($form_id == 'collection_node_form') {
$topic_node = null;
//The field structure is quite complex so we need to copy it
$paper_element_copy = (isset($form['field_papers'][LANGUAGE_NONE][0])) ? $form['field_papers'][LANGUAGE_NONE][0] : null;
if(arg(3)) {
$topic_node = node_load(arg(3));
if(!empty($topic_node->field_accepted_nodes[LANGUAGE_NONE])) {
foreach($topic_node->field_accepted_nodes[LANGUAGE_NONE] as $node) {
// On occasion deleted nodes will be present in accepted queue.
// Ensure they are actual nodes. (TODO: refactor to make more efficient)
if(node_load($node['nid'])) {
$elem = $paper_element_copy;
$elem['nid']['#default_value'] = $node['nid'];
$form['field_papers'][LANGUAGE_NONE][] = $elem;
}
}
}
//Prepopulate Topic Queue
$form['field_topic_queue'][LANGUAGE_NONE][0]['nid']['#default_value'] = $topic_node->nid;
}
}
//TODO: Create interface for selecting which node types have journalstream options
if($form_id == 'biblio_node_form') {
$node = menu_get_object();
$default_topic_queues = array();
if(isset($node->nid)) {
$default_topic_queues = journalstream_get_node_topic_queues($node->nid);
}
$form['journalstream_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Journalstream Options'),
'#description' => 'The options below will add this node to the selected JournalStream moderation queues.',
'#collapsible' => TRUE,
'#weight' => 35,
'#group' => 'additional_settings',
);
$form['journalstream_settings']['topic_queues'] = array(
'#type' => 'checkboxes',
'#title' => t('Topic Queue:'),
'#options' => _journalstream_get_topic_queues(),
'#default_value' => $default_topic_queues,
);
$form['journalstream_settings']['moderation_status'] = array(
'#type' => 'select',
'#title' => t('Moderation Status:'),
'#options' => _journalstream_get_moderation_options(),
);
}
}
/**
* Implements hook_form_node()
* Used for adding journalstream elements to certain node object types
*/
function journalstream_node_load($nodes, $types) {
$journalstream_types = variable_get('journalstream_content_types', _journalstream_get_default_types());
foreach($nodes as $node) {
if(!in_array($node->type,$journalstream_types)) {
continue;
}
$nid = $node->nid;
$journalstream = array();
$journalstream['topic_queues'] = journalstream_get_node_topic_queues($nid);
$journalstream['moderation_status'] = 'not_reviewed';
$nodes[$nid]->journalstream = $journalstream;
}
}
/**
* Implements hook_preprocess_node().
*
* This function injects a moderation form to the normal node teaser view
*/
function journalstream_preprocess_node(&$vars) {
if($vars['view_mode'] == 'teaser_moderation') {
$vars['theme_hook_suggestions'][] = 'node__teaser_moderation';
$node = node_load($vars['nid']);
$node_view = node_view($node, 'teaser');
$parent_node = menu_get_object();
$vars['content'] = array();
$vars['moderation_form']['#markup'] = '<div class="node-moderation-form-wrapper">';
$vars['moderation_form']['#markup'] .= drupal_render(drupal_get_form('journalstream_moderation_form', array('parent_node' => $parent_node, 'node' => $node)));
$vars['moderation_form']['#markup'] .= '</div>';
$vars['inline_moderation_form']['#markup'] = '<div class="node-inline-moderation-form-wrapper">';
$vars['inline_moderation_form']['#markup'] .= drupal_render(drupal_get_form('journalstream_inline_moderation_form', array('node' => $node)));
$vars['inline_moderation_form']['#markup'] .= '</div>';
$vars['content']['#markup'] = drupal_render($node_view);
}
}
/**
* Implements hook_node_insert()
*/
function journalstream_node_insert($node) {
//Remove items from topic queue once a collection has been inserted
if($node->type == 'collection') {
$topic_queue_node = (isset($node->field_topic_queue[LANGUAGE_NONE][0]['nid'])) ? node_load($node->field_topic_queue[LANGUAGE_NONE][0]['nid']) : false;
if($topic_queue_node) {
_journalstream_flush_queue($topic_queue_node->nid);
}
//Make sure all referenced nodes are published
if(!empty($node->field_papers[LANGUAGE_NONE])) {
foreach($node->field_papers[LANGUAGE_NONE] as $pnid) {
$pnode = node_load($pnid['nid']);
$pnode->status = 1;
node_save($pnode);
}
}
}
//Add inserted nodes to queue (if applicable)
$journalstream_types = variable_get('journalstream_content_types', _journalstream_get_default_types());
if(in_array($node->type, $journalstream_types) && isset($node->topic_queues) && !empty($node->topic_queues)) {
$moderation_status = (isset($node->moderation_status)) ? $node->moderation_status : 'not_reviewed';
foreach($node->topic_queues as $queue) {
journalstream_add_to_queue(array($node->nid),$queue, $moderation_status);
}
}
}
/**
* Implements hook_node_update()
*/
function journalstream_node_update($node) {
//Add inserted nodes to queue (if applicable)
$journalstream_types = variable_get('journalstream_content_types', _journalstream_get_default_types());
if(in_array($node->type, $journalstream_types)) {
$current_topic_queues = journalstream_get_node_topic_queues($node->nid);
$moderation_status = (isset($node->moderation_status)) ? $node->moderation_status : 'not_reviewed';
if(!empty($node->topic_queues)) {
//Remove this node from any topic queues it is not longer associated with
foreach($current_topic_queues as $current_topic_queue) {
if(!in_array($current_topic_queue,$node->topic_queues)) {
journalstream_remove_from_queue($node->nid,$current_topic_queue);
}
}
foreach($node->topic_queues as $queue) {
//We don't want to re-insert nodes into a topic queue
if(!in_array($queue, $current_topic_queues)) {
journalstream_add_to_queue(array($node->nid),$queue, $moderation_status);
}
}
}
}
}
/**
* Implements hook_entity_info_alter().
* Adds a moderation form view for a node teaser.
*/
function journalstream_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['teaser_moderation'] = array(
'label' => t('SCF Collection Moderation'),
'custom settings' => TRUE,
);
}
/**
* Implements hook_form()
* Renders the node moderation form
*/
function journalstream_moderation_form($form, &$form_state, $extra = null) {
$review_statuses = array('not_reviewed' => 'Not Reviewed', 'accepted' => 'Accepted', 'rejected' => 'Rejected');
$nid = isset($extra['node']->nid) ? $extra['node']->nid : '';
$pnid = isset($extra['parent_node']->nid) ? $extra['parent_node']->nid : '';
$default = _journalstream_get_node_queue($extra['parent_node'], $nid);
$form['moderation_form']['review_status'] = array(
'#title' => 'paper review status',
'#type' => 'radios',
'#options' => $review_statuses,
'#default_value' => $default,
'#ajax' => array(
'callback' => 'journalstream_moderation_form_callback',
),
);
$form['moderation_form']['editors_pick_flag'] = array(
'#markup' => flag_create_link('js_editors_pick', $nid),
);
$form['moderation_form']['classic_flag'] = array(
'#markup' => flag_create_link('js_classic_paper', $nid),
);
$form['parent_node'] = array('#type' => 'value', '#value' => $pnid);
$form['node'] = array('#type' => 'value', '#value' => $nid);
return $form;
}
/**
* Implements hook_form()
* Generates an (optional) inline moderation form so modules can add custom fields to journalstream moderation.
*/
function journalstream_inline_moderation_form($form, &$form_state, $extra = null) {
return $form;
}
/**
* Callback for handling moderation calls
*/
function journalstream_moderation_form_callback($form, $form_state) {
//Add the node to the correct queue
journalstream_add_to_queue(array($form_state['values']['node']), $form_state['values']['parent_node'], $form_state['values']['review_status']);
// Dom element containing node
$element = "#node-" . $form_state['values']['node'] . "-moderation-form";
$commands = array();
//Hide the calling node form
$commands[] = ajax_command_invoke($element, 'slideUp');
//$commands[] = ajax_command_remove($element);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Hook for getting a list of topic queue nodes
* @return array()
*/
function _journalstream_get_topic_queues() {
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', JS_TOPIC_QUEUE_TYPE)
->execute();
$nodes = array();
if(isset($result['node'])) {
foreach($result['node'] as $row) {
$node = node_load($row->nid);
if($node && isset($node->nid) && isset($node->title)) {
$nodes[$node->nid] = $node->title;
}
}
}
return $nodes;
}
/**
* Hook for returning list of queue moderation options
* @return array
*/
function _journalstream_get_moderation_options() {
return array(
'not_reviewed' => 'Not Reviewed',
'accepted' => 'Accepted',
'rejected' => 'Rejected',
);
}
/**
* Hook for returning default content types used by journalstream
* @return array()
*/
function _journalstream_get_default_types() {
return array('biblio','resources');
}
/**
* Helper function used for clearing the moderation queue
* Hook is fired after posting a collection
* @param $nid
* Node id of the moderation queue to flush.
*/
function _journalstream_flush_queue($nid) {
$node = node_load($nid);
// Delete rejected papers.
// @todo this might delete existing papers which already existed in the system
// prior to the biblio import. Biblio will not re-import them, it will skip
// the import and add the existing paper to the queue, which might end up
// rejected and then deleted. What should happen is that these exiting
// papers should be flagged during the import so as to not be deleted during
// the rejected queue flushing.
if (!empty($node->field_rejected_nodes[LANGUAGE_NONE])) {
foreach ($node->field_rejected_nodes[LANGUAGE_NONE] as $item) {
node_delete($item['nid']);
}
}
//Don't flush Not Reviewed (for now)
//$node->field_not_reviewed_nodes[LANGUAGE_NONE] = array();
$node->field_accepted_nodes[LANGUAGE_NONE] = array();
$node->field_rejected_nodes[LANGUAGE_NONE] = array();
node_save($node);
}
/**
* Helper function for determining which queue a node is in
* @param $parent_node
* Parent node object to search
* @param $nid
* Nid of node to search by
*/
function _journalstream_get_node_queue($parent_node, $nid) {
//Iterate through Not Reviewed nodes
if(!empty($parent_node->field_not_reviewed_nodes[LANGUAGE_NONE])) {
foreach($parent_node->field_not_reviewed_nodes[LANGUAGE_NONE] as $row) {
if($row['nid'] == $nid) {
return 'not_reviewed';
}
}
}
//Iterate Through Accepted Nodes
if(!empty($parent_node->field_accepted_nodes[LANGUAGE_NONE])) {
foreach($parent_node->field_accepted_nodes[LANGUAGE_NONE] as $row) {
if($row['nid'] == $nid) {
return 'accepted';
}
}
}
//Iterate Through Rejected Nodes
if(!empty($parent_node->field_rejected_nodes[LANGUAGE_NONE])) {
foreach($parent_node->field_rejected_nodes[LANGUAGE_NONE] as $row) {
if($row['nid'] == $nid) {
return 'rejected';
}
}
}
}
/** Database Related Hooks **/
/**
* Callback used for binding a referenced (biblio) node to a Topic Queue
*
* @param $nids
* An indexed array of Node IDs to append to queue.
* @param $queue_nid
* The Node ID of the topic queue.
* @param $queue
* The queue status to append to. Options are:
* JS_NOT_REVIEWED
* JS_ACCEPTED
* JS_REJECTED
*/
function journalstream_add_to_queue($nids, $queue_nid, $queue = JS_NOT_REVIEWED) {
$qnode = node_load($queue_nid);
if(!$qnode) {
return;
}
foreach($nids as $nid) {
// Remove node from queues
$field_names = array('field_accepted_nodes', 'field_not_reviewed_nodes', 'field_rejected_nodes');
foreach ($field_names as $field_name) {
if(!empty($qnode->{$field_name}[LANGUAGE_NONE])) {
$field_items = $qnode->{$field_name}[LANGUAGE_NONE];
foreach($field_items as $k => $v) {
if($v['nid'] == $nid) {
unset($field_items[$k]);
}
}
// Re-add the remaining items to the field array from scratch, in order
// to have a consistent order of deltas.
$qnode->{$field_name}[LANGUAGE_NONE] = array();
foreach($field_items as $k => $v) {
$qnode->{$field_name}[LANGUAGE_NONE][] = $v;
}
}
}
//Add Node to new queue
switch($queue) {
case JS_ACCEPTED:
$qnode->field_accepted_nodes[LANGUAGE_NONE][] = array('nid' => $nid);
break;
case JS_REJECTED:
$qnode->field_rejected_nodes[LANGUAGE_NONE][] = array('nid' => $nid);
break;
default:
$qnode->field_not_reviewed_nodes[LANGUAGE_NONE][] = array('nid' => $nid);
break;
}
}
$result = node_save($qnode);
return $result;
}
/**
* Hook for removing a node from a queue
*/
function journalstream_remove_from_queue($nid, $queue_nid) {
$qnode = node_load($queue_nid);
// Remove node from queues
if(!empty($qnode->field_accepted_nodes[LANGUAGE_NONE])) {
foreach($qnode->field_accepted_nodes[LANGUAGE_NONE] as $k => $v) {
if($v['nid'] == $nid) {
unset($qnode->field_accepted_nodes[LANGUAGE_NONE][$k]);
}
}
}
if(!empty($qnode->field_not_reviewed_nodes[LANGUAGE_NONE])) {
foreach($qnode->field_not_reviewed_nodes[LANGUAGE_NONE] as $k => $v) {
if($v['nid'] == $nid) {
unset($qnode->field_not_reviewed_nodes[LANGUAGE_NONE][$k]);
}
}
}
if(!empty($qnode->field_rejected_nodes[LANGUAGE_NONE])) {
foreach($qnode->field_rejected_nodes[LANGUAGE_NONE] as $k => $v) {
if($v['nid'] == $nid) {
unset($qnode->field_rejected_nodes[LANGUAGE_NONE][$k]);
}
}
}
$result = node_save($qnode);
return $result;
}
/**
* Returns an array of topic queue nids a node belongs too
* @param $nid
* @return array of nids
*/
function journalstream_get_node_topic_queues($nid) {
$nid = (int) $nid;
$query = new EntityFieldQuery;
$nodes = array();
$topic_queues = array();
$nodes[] = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'topic_queue')
->fieldCondition('field_not_reviewed_nodes','nid',$nid,'=')
->execute();
$nodes[] = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'topic_queue')
->fieldCondition('field_accepted_nodes','nid',$nid,'=')
->execute();
$nodes[] = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'topic_queue')
->fieldCondition('field_rejected_nodes','nid',$nid,'=')
->execute();
foreach($nodes as $node) {
if(!empty($node) && !empty($node['node'])) {
$n = array_keys($node['node']);
if(!empty($n[0])) {
$topic_queues[] = $n[0];
}
}
}
return $topic_queues;
}
/**
* Hook for retrieving the most recent Journalstream Collection
* @return $nid of most recent published collection node.
*/
function _journalstream_most_recent_collection_nid() {
$sql = "Select n.nid FROM {node} n WHERE n.type = :ntype AND n.status = 1 ORDER BY created DESC LIMIT 1";
$result = db_query($sql, array(':ntype' => JS_COLLECTION_TYPE));
foreach($result as $row) {
return $row->nid;
}
}
/**
* Function for locating and deleting orphaned Journalstream Topic Queue references
**/
function journalstream_cleanup() {
//$journalstream_nids =
$query = new EntityFieldQuery;
$changed = false;
$count = 0;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', JS_TOPIC_QUEUE_TYPE)
->execute();
foreach($result['node'] as $row) {
$queue_node = node_load($row->nid);
//Iterate through all the queues and remove any nodes not found
if(!empty($queue_node->field_accepted_nodes[LANGUAGE_NONE])) {
foreach($queue_node->field_accepted_nodes[LANGUAGE_NONE] as $key => $accepted_nid) {
if(!node_load($accepted_nid['nid'])) {
unset($queue_node->field_accepted_nodes[LANGUAGE_NONE][$key]);
$changed = true;
$count += 1;
}
}
}
if(!empty($queue_node->field_rejected_nodes[LANGUAGE_NONE])) {
foreach($queue_node->field_rejected_nodes[LANGUAGE_NONE] as $key => $rejected_nid) {
if(!node_load($rejected_nid['nid'])) {
unset($queue_node->field_rejected_nodes[LANGUAGE_NONE][$key]);
$changed = true;
$count += 1;
}
}
}
if(!empty($queue_node->field_not_reviewed_nodes[LANGUAGE_NONE])) {
foreach($queue_node->field_not_reviewed_nodes[LANGUAGE_NONE] as $key => $nr_nid) {
if(!node_load($nr_nid['nid'])) {
unset($queue_node->field_not_reviewed_nodes[LANGUAGE_NONE][$key]);
$changed = true;
$count += 1;
}
}
}
if($changed) {
node_save($queue_node);
}
return $count;
}
}