-
Notifications
You must be signed in to change notification settings - Fork 5
/
content_moderation.module
760 lines (680 loc) · 26.4 KB
/
content_moderation.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
// $Id: content_moderation.module,v 1.12.2.17 2010/04/18 11:31:29 eugenmayer Exp $
# Copyright (c) 2010 Impressive.media
# Author: Eugen Mayer
require_once('content_moderation.workflow.inc');
require_once('content_moderation.trigger.inc');
require_once('content_moderation.rules.inc');
/*
* Implementation of hook_menu().
*/
function content_moderation_menu() {
$items = array();
$items["moderate/%node/%/change"] = array(
'title' => 'Change moderation state',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('content_moderation_change_state_form',2),
'access callback' => '_content_moderation_statechange_allowed',
'access arguments' => array(2)
);
$items["node/%node/moderation_history"] = array(
'title' => 'Moderation history',
'description' => 'Show the content moderation history.',
'type' => MENU_LOCAL_TASK,
'page callback' => '_content_moderation_history',
'page arguments' => array(1),
'access callback' => '_content_moderation_access',
'access arguments' => array('view history',1),
);
$items["node/%node/latest_revision"] = array(
'page callback' => '_content_moderation_show_latest_revision',
'page arguments' => array(1),
'access callback' => '_content_moderation_access',
'access arguments' => array('view revision',1),
);
// Module settings.
$items["admin/settings/content_moderation"] = array(
'title' => 'Content moderation',
'description' => 'Configure content moderation.',
'page callback' => 'drupal_get_form',
'page arguments' => array('content_moderation_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'content_moderation.admin.inc',
);
$items['admin/settings/content_moderation/general'] = array(
'title' => 'General',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
return $items;
}
/*
* Implementation of hook_menu_alter
*/
function content_moderation_menu_alter(&$items) {
// we need to get this wrapper in place, as we cant use the nodeapi "prepare" op
// as module like taxonomy, files and so forth would not fetch the correct versions
$items['node/%node/edit']['page callback'] = '_content_moderation_node_edit_page';
}
/*
* Implementing hook_block
*/
function content_moderation_block($op = "list", $delta = 0, $edit = array()){
switch($op) {
case 'list':
$blocks[0]['info'] = t('Content moderation info');
return $blocks;
break;
case 'view':
switch($delta) {
case 0:
$node = _content_moderation_get_current_viewed_node();
// No need to show the block if the viewd node is not moderated. Questionable.
if (!_content_moderation_moderate_node_type($node->type)) {
return;
}
$m = drupal_get_path('module', 'content_moderation');
drupal_add_css("$m/css/content_moderation_block.css");
$pending_revisions = _content_moderation_get_latest_revisions($node->nid,variable_get('content_moderation_pending_count', 5),NULL);
$revisions_list = theme('content_moderation_revisions_list',$pending_revisions);
// All kinds of actions we can do.
$live = _content_moderation_live_revision($node->nid);
if($live != NULL) {
$links['edit_live'] = "node/{$node->nid}/edit";
$links['compare'] = "node/{$node->nid}/revisions";
$links['live_view'] = 'node/'.$live->nid;
}
$links['edit_state'] = _content_moderation_change_state_link($node->vid,$node->nid);
$links['compare_with_live'] = "node/{$node->nid}/revisions/view/{$node->vid}/{$live->vid}";
// Get the state of the currently view revision
$state = ucfirst(_content_moderation_current_state($node->vid));
$block['subject'] = t("Revisions");
$block['content'] = theme('content_moderation_info_block',$node, $links, $state, $live,$revisions_list);
$block['cache'] = false;
return $block;
break;
}
break;
}
}
/*
* Implementation of hook_theme
*/
function content_moderation_theme() {
return array(
'content_moderation_info_block' => array(
'template' => 'content_moderation_info_block',
'arguments' => array('node' => NULL,'links' => NULL, 'state' => t('none'), 'live' => NULL,'revisions_list' => ''),
),
'content_moderation_node_history' => array(
'arguments' => array('history' => array()),
),
'content_moderation_revisions_list' => array(
'template' => 'content_moderation_revisions_list',
'arguments' => array('revisions' => NULL),
),
);
}
/**
* Implementation of hook_help().
*/
function content_moderation_help($path, $arg) {
switch ($path) {
case 'admin/help#content_moderation':
return '<p>'. t("Enables you to moderate the creation of nodes and there further editing. You can have a 'Live version' for all visitors and pending revisions which need to be approved and become the new 'Live Version' then.") .'</p>';
}
}
/*
* Implementation of hook_nodeapi
*/
function content_moderation_nodeapi(&$node, $op, $a3, $a4) {
global $user;
$type = $node->type;
if (!_content_moderation_moderate_node_type($type)) {
// Dont moderate this content type
return;
}
switch ($op) {
case 'prepare':
if (_content_moderation_moderate_node_type($node->type)) {
$live = _content_moderation_live_revision($node->nid);
// We only need to change the behavior if the current version is not the latest pending version
if($live != NULL && $node->vid != $live->vid) {
drupal_set_message(t('You are now editing the most recent pending revision (@revision) and not the live version!',array('@revision' => $node->vid)),'warning', false);
}
}
break;
case 'presave':
// As we cant use $node->old_vid, which always points to the revision
// on before the current, we need to save the live version ourself
$node->live = _content_moderation_live_revision($node->nid);
// We dont need to care about the title if we dont have a approved version yet.
// Without a approved version, the node revision shown to the use is changed everytime
// so in that case, content moderation behaves like the classic node edit workflow
if($node->live != NULL) {
$current_title = _content_moderation_get_current_title($node->nid);
// Lets backup the new title if they differ. We need this as we will set back the title here
// so modules like pathalias dont break when the node get saved ( path alias will follow the new title otherwise)
if($current_title != false && $current_title != $node->title) {
$node->new_title = $node->title;
$node->title = $current_title;
}
else {
$node->new_title = false;
}
}
break;
case 'update':
// If we have a current live version (that must not be the current vid entry in {node}!
// but rather what is in the history table), reset the vid the to live version
// so that finaly the version presented to the visitor does not change.
// Without a approved version, the node revision shown to the use is changed everytime
// so in that case, content moderation behaves like the classic node edit workflow
if($node->live != NULL) {
// Reset the title.
if($node->new_title !== false) {
db_query("UPDATE {node_revisions} SET title='%s' WHERE vid=%d",$node->new_title, $node->vid);
}
// Reset the revision
$live_version = _content_moderation_revision_infos($node->live->vid);
db_query("UPDATE {node} SET vid = %d, changed='%s', title='%s' WHERE nid = %d", $live_version->vid, $live_version->timestamp, $live_version->title, $node->nid);
drupal_set_message(t('Your content has been saved as a pending version'));
}
break;
case 'view':
// We are not showing the node, possible its just a view or something.
// Dont show all the messages below then.
if(arg(0) != 'node' || !is_numeric(arg(1)) || !(arg(2) == "") ) {
return;
}
$live = _content_moderation_live_revision($node->nid);
// If we are showing the node, but there is no live version yet, give a hard notice.
if($live == NULL) {
// If this node has no live version yet, notice the user that the content is not valid.
// Valid reasons are, that the node has been just created and not approved or, that all live
// revisions have been revoked
drupal_set_message ( t('The content of this document has not been approved and is therefor not valid yet!'), 'warning' );
}
// If we are showing a revision, show the author informations
if (($live != NULL && $node->vid != $live->vid) && user_access('view content moderation message created by')) {
// Get username for the revision rather than the original node.
$revision_author = user_load($node->revision_uid);
drupal_set_message(
t('The revision of this node has been created on @date by !author.',
array(
'@date'=> format_date($node->revision_timestamp, 'small'),
'!author'=> theme('username', $revision_author)
)
)
);
}
// If we are showing the live revision of the node and there are any pending version. tell the user about that
else if(user_access('view content moderation message pending')){
$pending_revisions = _content_moderation_get_latest_revisions($node->nid,0,NULL);
$count = count($pending_revisions);
if($count == 1)
drupal_set_message(t('This document has 1 pending revision',array('@count' => $count)));
else if($count > 1) {
drupal_set_message(t('This document has @count pending revisions',array('@count' => $count)));
}
}
break;
case 'delete':
// A node is delete, lets delete the history
db_query('DELETE FROM {content_moderation_node_history} WHERE nid=%d',$node->nid);
db_query('DELETE FROM {content_moderation_revision_state} WHERE nid=%d',$node->nid);
break;
}
}
function content_moderation_yaml_theme_layout($layout, $available) {
global $theme;
$node = _impressive_media_get_cur_node();
if (!_content_moderation_moderate_node_type($node->type)) {
return;
}
// TODO: make this configureable
return '2col_right_13';
}
/*
* Add a edit button if we are viewing a live-version or the current latest version
*/
function content_moderation_drupalwiki_menu_alter($tree) {
if($tree['meta']['name'] != 'drupalwiki_menu_toolbar') {
return $tree;
}
// Dont show the toolbar if we are editing
if( !(arg(0) == 'node' && arg(2) == 'revisions' && is_numeric(arg(3))) ) {
return $tree;
}
$nid = arg(1);
$vid = arg(3);
// We fetch the latest 2, as that way we can verify 2 things
$latest = _content_moderation_get_latest_revisions($nid,1);
if(
(is_array($latest) && count($latest) > 0 && $latest[0]->vid == $vid) ||
_content_moderation_is_not_approved_live($vid)
)
{
$editbtn['link'] = array(
'title' => t('Edit'),
'path' => 'node/%/edit',
'href' => "node/$nid/edit",
'icon' => icon_get_icon_path('edit','20'),
'has_childred' => 0
);
$editbtn['below'] = array();
array_unshift($tree,$editbtn);
}
return $tree;
}
/*
* Implementation of hook_perm.
*/
function content_moderation_perm() {
// TODO: cache this
// This user right is later used for granting the user the approval of pending version
// making those version "live".
$states = _content_moderation_states();
$types = _content_moderation_moderate_node_types();
$rights = array();
foreach($types as $type) {
foreach($states as $from_state) {
$to_states = _content_moderation_next_states($from_state,$type);
// values are translated
$to_states = array_keys($to_states);
foreach($to_states as $to_state) {
$rights[] = "content moderation $type state $from_state to $to_state";
}
}
$rights[] = "content moderation view $type moderation history";
}
$rights[] = 'view content moderation message created by';
$rights[] = 'view content moderation message pending';
return $rights;
}
/*
* Call this method in the form_submit or presave methods to let the updated node
* be the new live version.
*/
function content_moderation_reset_live_status($nid = NULL) {
if ($nid == NULL || !is_numeric($nid)) {
return;
}
db_query('DELETE from {content_moderation} WHERE nid = %d', $nid);
}
/*
* Implementation of hook_form_alter
*/
function content_moderation_form_node_type_form_alter(&$form, $form_state) {
$form[ 'workflow' ][ 'node_options' ][ '#options' ][ 'node_content_moderation' ] = t('Moderate revisions');
$form['#validate'][] = '_content_moderation_validate_node_type_form';
}
/*
* Implementation of form_alter.
* Forcing new reversion and publishing.
*/
function content_moderation_form_alter(&$form, $form_state, $form_id) {
if (!_content_moderation_moderate_node_type($form['#node']->type)) {
// Dont moderate this content type.
return;
}
if ($form['#id'] == 'node-form') {
// force new revision
$form['revision_information']['revision']['#prefix'] = t('As this content is in moderation, you cannot choose here. A new revision is always created.');
$form['revision_information']['revision']['#disabled'] = true;
$form['revision_information']['revision']['#default_value'] = true;
$descr = t('You cant choose here, as this content is under moderation. Publishing is forced.');
if(is_array($form['options']['status']) && variable_get('content_moderation_force_publish', 1)) {
$form['options']['status']['#disabled'] = true;
$form['options']['status']['#default_value'] = true;
$form['options']['status']['#prefix'] = $descr;
$form['options']['status']['#value'] = true;
}
if(module_exists('impressive_media_icombined_access')) {
$form['custom_options']['status']['#disabled'] = true;
$form['custom_options']['status']['#default_value'] = true;
$form['custom_options']['status']['#value'] = true;
$form['custom_options']['status']['#prefix'] = $descr;
}
// Add this so we get redirected to the new create revision.
// Skip this, if thats a new node, as this will happen automatically then
// Skip this also, if the current node has no approved version then, because content_moderation
// will behave like the normal node edit workflow then (and not restore the vid)
if($form['nid']['#value'] != NULL && _content_moderation_live_revision($form['nid']['#value']) != NULL){
$form['buttons']['submit']['#submit'][] = '_content_moderation_submit_node_form';
}
}
}
/*
* Implementation content_moderation_node_history
* Theming the output of content moderations history table
*/
function theme_content_moderation_node_history($history) {
$data = array();
foreach($history as $e) {
$user = user_load($e->uid);
$state_class = "state_{$e->state_name}";
// TODO: proper localized date format
$data[] = array (
format_date($e->stamp,'small'), // date
l($user->name,"user/{$user->uid}"), // Who changed that state?
"<span class='$state_class'>".ucfirst(t('!old',array('!old' => $e->old_state_name))).' -> '.ucfirst(t($e->state_name)).'</span>', // Transition
l($e->vid,"node/{$e->nid}/revisions/{$e->vid}/view"), // revision
check_plain($e->comment), // comment
);
}
$title = t('Moderation history');
$title = "<h2>$title</h2>";
return $title.theme('table',
array(
t('Date'),
t('Author'),
t('Transition'),
t('Revision'),
t('Comment'),
)
,$data);
}
/*
* Needed by the views API
*/
function content_moderation_views_api() {
return array('api' => 2.0);
}
/*
* This is out small helper to ensure, that always the newes vid gets edited.
* The normal node API would edit the vid which is currently in {node},
* But we want to edit the VID which is the most recent one.
*/
function _content_moderation_node_edit_page($node) {
if(variable_get('content_moderation_moderate_latest_only', 1) === 1) {
$moderated_revs = _content_moderation_has_moderated_revisions($node->nid);
// Do not allow state changing of revisions, which are not the latest
// TODO: Maybe we need a exception for the live state to unapprove it later?
if(count($moderated_revs) > 0) {
$none_state = _content_moderation_none_state();
drupal_set_message(t('You cannot edit the document, as the latest revision is under moderation. Set the revision back to the state "!state" to be able to edit.', array('!state' => $none_state)), 'warning');
drupal_goto("node/{$node->nid}");
return false;
}
}
if (_content_moderation_moderate_node_type($node->type)) {
$vid = _content_moderation_latest_revision($node->nid);
// Drupal uses the vid of the node table, not the real latest revision out of node_revisions
// so we need to determine if it differes. If yes, load the latst revision, as thats our last pending one.
if($node->vid != $vid) {
// We only need to change the behavior if the current version is not the latest pending version.
$node = node_load($node->nid,$vid);
}
}
return node_page_edit($node);
}
/*
* Fetch the revision data without using node_load to save some performance.
*/
function _content_moderation_revision_infos($vid) {
$result = db_query('SELECT * FROM {node_revisions} WHERE vid=%d',$vid);
return db_fetch_object($result);
}
/*
* Calculated the most recent vid of the current node
*/
function _content_moderation_latest_revision($nid) {
return db_result(db_query('SELECT MAX(vid) FROM {node_revisions} WHERE nid=%d',$nid));
}
/*
* Generating a history overview of a node.
*/
function _content_moderation_history($node) {
$m = drupal_get_path('module', 'content_moderation');
drupal_add_css("$m/css/content_moderation_history.css");
$history = _content_moderation_get_history(array('name' => 'nid', 'value' => $node->nid));
drupal_set_title(t('History'));
return theme('content_moderation_node_history',$history);
}
/*
* Returning the node object of the live version of the node
*/
function _content_moderation_live_revision($nid) {
$live_state = _content_moderation_live_state();
$vid = db_result(db_query("SELECT vid from {content_moderation_node_history} WHERE nid=%d AND state_name='%s' order by hid DESC limit 1", $nid,$live_state));
if($vid == false) {
return NULL;
}
return node_load($nid, $vid);
}
/*
* Returns the key which represents the live version.
* Should be later an interface to change the live state
*/
function _content_moderation_live_state() {
return 'live';
}
/*
* Returns the key which represents the neutral non moderated version.
* Should be later an interface to change the live state
*/
function _content_moderation_none_state() {
return 'none';
}
/*
* Determine if this content type is set to be moderated
*/
function _content_moderation_moderate_node_type($type) {
// Is this content even in moderatation?
$var = variable_get( "node_options_$type", array(0));
if( array_search('node_content_moderation',$var) ) {
return true;
}
//else
return false;
}
/*
* Determine if this content type is set to be moderated
*/
function _content_moderation_moderate_node_types() {
$types = node_get_types('types');
$result= array();
foreach($types as $type) {
// Is this content even in moderatation?
if( _content_moderation_moderate_node_type($type->type)) {
$result[] = $type->type;
}
}
return $result;
}
/*
* Getting nid by vid
*/
function _content_moderation_get_nid($vid) {
return db_result(db_query('select nid from {node_revisions} where vid=%d',$vid));
}
/*
* Return the proper path to the change-state action
*/
function _content_moderation_change_state_link($vid,$nid) {
return "moderate/$nid/$vid/change";
}
/*
* Checking, if the user has the proper rights to change the state of a node, so
* checking the transition and node type.
*/
function _content_moderation_state_allowed($user,$from_state,$to_state,$node_type) {
return user_access("content moderation $node_type state $from_state to $to_state");
}
/*
* Provides a hook where modules can register there access handler.
* Example:
*
* function mymodul_content_moderation_access($op,$node) {
* switch($op) {
* case 'view history':
* return node_acces($op,$node)
* break;
* }
* }
*/
function _content_moderation_access($op,$node) {
if(_content_moderation_moderate_node_type($node->type) === false) {
return false;
}
$modules = module_implements('content_moderation_access');
if(count($modules) > 0) {
$grants = module_invoke_all('content_moderation_access',$op,$node);
$result = true;
foreach($grants as $bool) {
$return = $return && $bool;
}
return $result;
}
//else the default
switch($op) {
case 'view history':
return _content_moderation_moderate_node_type($node->type) && user_access("content moderation view {$node->type} moderation history");
break;
case 'view revision':
return node_access('view', $node);
break;
}
}
/*
* Get a number of latest revisions of a node.
* Use 0 for limit if you want all the revisions
*/
function _content_moderation_get_latest_revisions($nid,$limit = 5,$vid = NULL, $exclude = array()) {
if($vid == NULL) {
$live = _content_moderation_live_revision($nid);
$vid = $live->vid;
}
if($limit != 0) {
$limit = " limit $limit";
}
else {
// limit is zero, so dont use one
$limit = '';
}
$filter = array();
foreach($exclude as $v) {
$filter[] = "nr.vid<>$v";
}
if(count($filter) > 0) {
$filter = join(' AND ',$filter);
$filter = ' AND '.$filter;
}
else {
$filter = '';
}
// TODO: important, most probably use db_rewrite_sql here
// if vid is still null, we actually dont have a live revision. So we need to count all revisions, even that one
// which is currently references in the node table.
if($vid == NULL) {
$result = db_query("select nr.*,cmrs.state from {node_revisions} as nr LEFT JOIN {content_moderation_revision_state} as cmrs on nr.vid=cmrs.vid where nr.nid=%d {$filter} order by nr.vid DESC $limit",$nid);
}
else {
$result = db_query("select nr.*,cmrs.state from {node_revisions} as nr LEFT JOIN {content_moderation_revision_state} as cmrs on nr.vid=cmrs.vid where nr.nid=%d AND nr.vid>%d {$filter} order by nr.vid DESC $limit",$nid,$vid);
}
$revs = array();
while ($rev = db_fetch_object($result)) {
$revs[] = $rev;
}
return $revs;
}
function _content_moderation_get_current_viewed_node() {
$router_item = menu_get_item($path);
// if its not a node view, return
if($router_item['map'][0] != 'node') {
return NULL;
}
$node = $router_item['map'][1];
// something wrong with the url
if(!is_object($node)) {
return NULL;
}
if(is_numeric($router_item['map'][2]) && $router_item['map'][3] == 'view') {
$vid = $router_item['map'][2];
$node = node_load($node->nid,$vid);
}
return $node;
}
/*
* Small helper for trigger and events
*/
function _content_moderation_node_transition_matrix() {
static $node_state_matrix = array();
// chaching
if(count($node_state_matrix) > 0) {
return $node_state_matrix;
}
$states = _content_moderation_states();
$types = _content_moderation_moderate_node_types();
foreach($types as $type) {
foreach($states as $from_state) {
$to_states = _content_moderation_next_states($from_state,$type);
// Values are translated.
$to_states = array_keys($to_states);
foreach($to_states as $to_state) {
$node_state_matrix["{$type}_{$from_state}_{$to_state}"]= t("State of node type @type changed from '@from_state' to '@to_state'", array('@type' => $type, '@from_state' => $from_state, '@to_state' => $to_state));
}
}
}
return $node_state_matrix;
}
/*
* Checks if the current version is the currently shown as live
* without beeing approved before (current vid in the {node} table
*/
function _content_moderation_is_not_approved_live($vid) {
$result = db_result(db_query('SELECT count(vid) FROM {node} where vid=%d limit 1', $vid));
if($result > 0) {
return true;
}
// else
return false;
}
function _content_moderation_get_current_title($nid) {
return db_result(db_query('SELECT title from {node} where nid=%d',$nid));
}
function _content_moderation_show_latest_revision($node) {
$revisions = _content_moderation_get_latest_revisions($node->nid,1);
$node = node_load($node->nid,$revisions[0]->vid);
menu_set_active_item("node/{$node->nid}/revisions/{$node->vid}/view");
return node_page_view($node);
}
function _content_moderation_has_moderated_revisions($nid) {
$none_state = _content_moderation_none_state();
$live_state = _content_moderation_live_state();
// Get all revisions which are neither in the none nor in the live state
$result = db_query("select nr.*,cmrs.state from {node_revisions} as nr LEFT JOIN {content_moderation_revision_state} as cmrs on nr.vid=cmrs.vid where nr.nid=%d AND cmrs.state!='%s' AND cmrs.state!='%s'",$nid,$none_state,$live_state);
if($result === false) {
return array();
}
$revs = array();
while ($rev = db_fetch_object($result)) {
$revs[] = $rev;
}
return $revs;
}
/*
* Helper to ensure, that Create new revision is always checked, when a node type is selected to be moderated
*/
function _content_moderation_validate_node_type_form($from,&$form_state) {
if($form_state['values']['node_options']['node_content_moderation'] === 'node_content_moderation' && $form_state['values']['node_options']['revision'] === 0) {
form_set_error('node_options',t('If you chose to moderate this content type, you must switch "Create new revision" in the workflow tab on'));
}
}
/*
* Redirects to the new created revisions
*/
function _content_moderation_submit_node_form($form, &$form_state){
$nid = $form['nid']['#value'];
$form_state['redirect'] = "node/$nid/latest_revision";
}
function _content_moderation_latest_live_in_history($nid) {
$result = _content_moderation_get_history(array('name' => 'nid','value' => $nid), 'live');
if(is_array($result) && count($result) > 0) {
return $result[0];
}
// else
return false;
}