-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_approval.module
More file actions
executable file
·615 lines (546 loc) · 23.2 KB
/
node_approval.module
File metadata and controls
executable file
·615 lines (546 loc) · 23.2 KB
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
<?php
/**
* @file
* A module for creating main module entity.
*/
// Loading the Constant Defined File
//module_load_include('inc', 'node_approval', 'node_approval.constant');
/**
* Implements hook_help().
* This function return the help info regarding main module
* @param <type> $path
* @param <type> $arg
* @return <type>
*/
function node_approval_help($path, $arg)
{
if ($path == 'admin/help#node_approval') {
return t('A node approval for vcss setup!');
}
}
/**
* Implements hook_permission().
*/
function node_approval_permission()
{
return array(
APPROVE_REAL_LIFE_STORIES_CONTENT => array(
'title' => t('Approve real life stories content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
/* APPROVE_VIDEO_LIBRARY => array(
'title' => t('Approve video library content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),*/
APPROVE_RESOURCES => array(
'title' => t('Approve resources content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_FAQ => array(
'title' => t('Approve faq content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_BLOG => array(
'title' => t('Approve blog content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_EVENTS => array(
'title' => t('Approve events content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_CAREERS => array(
'title' => t('Approve careers content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_LINKS => array(
'title' => t('Approve links content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
/* APPROVE_LOCATIONS => array(
'title' => t('Approve locations content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),*/
APPROVE_GALLERY => array(
'title' => t('Approve gallery content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
APPROVE_MISCELLANEOUS => array(
'title' => t('Approve miscellaneous content'),
'description' => t('Check to approve this content'),
'restrict access' => TRUE,
),
);
}
function check_node_approval_status(&$node, $nid)
{
if($node->type=='sitelocations'||$node->type=='videos'||$node->type=='banner'){
//Escape sitelocations and videos from node approval
}else {
$result = db_select('node_approval', 'na')
->fields('na', array('user_ids', 'node_id'))
->condition('node_id', $nid)
->execute()
->fetchObject();
if ($result) {
$userids = unserialize($result->user_ids);
$publish_flag = false;
//For each user id check if they have approved the node content by cross referencing their ids with ones on the user_has_approved table.
foreach ($userids as $userid) {
$approved = db_select('user_has_approved', 'uha')
->fields('uha', array('status'))
->condition('user_id', $userid)
->condition('node_id', $nid)
->execute()
->fetchObject();
if ($approved) {
if ($approved->status == 1) {
$publish_flag = true;//Collect approvals from every user assigned to review the content.
} else {
$publish_flag = false;//If any 1 of the user has not set their approved status the content wont be published.
break;
}
}
}
//Change the status field of the node from {node} table for implementing and concluding this approval mechanism
if ($publish_flag == true) {
//Set status to 1 if true.
$node->status = 1;
db_update('node_revision')
->fields(
array(
'status' => '1',
)
)
->condition('nid', $node->nid)
->execute();
return 1;
} else {
//Set status to 0 if false.
$node->status = 0;
db_update('node_revision')
->fields(
array(
'status' => '0',
)
)
->condition('nid', $node->nid)
->execute();
return 0;
}
}
}
}
/**
* Implements hook_node_insert().
*/
function node_approval_node_insert($node)
{
global $user;
// Don't publish content by default before approval
if ($node->is_new) {
//Escape node types from needing approval.
if($node->type=='sitelocations'||$node->type=='videos'||$node->type=='banner') {
$node->status = "1";
}else{
$node->status = "0";
//Send mail to respective persons with permission to approve the content
//Role having permission to approve content of this node type
$node_type = '';
switch ($node->type) {
case 'reallifestories':
$roles = user_roles(TRUE, APPROVE_REAL_LIFE_STORIES_CONTENT);
$node_type = "real life stories";
break;
/* case 'videos':
$roles = user_roles(TRUE, APPROVE_VIDEO_LIBRARY);
$node_type = "videos";
break;*/
case 'resources':
$roles = user_roles(TRUE, APPROVE_RESOURCES);
$node_type = "resources";
break;
case 'faq':
$roles = user_roles(TRUE, APPROVE_FAQ);
$node_type = "faq";
break;
case 'blog':
$roles = user_roles(TRUE, APPROVE_BLOG);
$node_type = "blog";
break;
case 'events':
$roles = user_roles(TRUE, APPROVE_EVENTS);
$node_type = "events";
break;
case 'careers':
$roles = user_roles(TRUE, APPROVE_CAREERS);
$node_type = "careers";
break;
case 'links':
$roles = user_roles(TRUE, APPROVE_LINKS);
$node_type = "links";
break;
/* case 'sitelocations':
$roles = user_roles(TRUE, APPROVE_LOCATIONS);
$node_type = "site locations";
break;*/
case 'gallery':
$roles = user_roles(TRUE, APPROVE_GALLERY);
$node_type = "gallery";
break;
case 'miscellaneous':
$roles = user_roles(TRUE, APPROVE_MISCELLANEOUS);
$node_type = "miscellaneous";
break;
}
//For each roles get the users who have this role
$users = array();
//Get roles as an array
foreach ($roles as $key => $roleU) {
if ($roleU == 'administrator') {
} else {
$role = user_role_load_by_name($roleU);
$result = db_select('users_roles', 'ur')
->fields('ur', array('uid'))
->condition('rid', $role->rid)
->execute()->fetchAll();
//Get users with the current loop role
foreach ($result as $user_info) {
$user_loaded = user_load($user_info->uid);
if (array_key_exists(1, $user_loaded->roles)) {
//Excluding admin
} else {
$users[] = $user_info->uid;
}
}
//Send email requesting content approval
if (!empty($users)) {
$responsible_users = user_load_multiple($users);
foreach ($responsible_users as $rusers) {
if ($rusers->uid == $user->uid) {
//Logic to not send email to current user who entered the content.
} else {
$module = 'node_approval';
$key = 'node_approval_request';
// Specify 'to' and 'from' addresses.
$to = $rusers->mail;
$from = variable_get('site_mail', $rusers->mail);
// "params" loads in additional context for email content completion in
// hook_mail(). In this case, we want to pass in the values the user entered
// into the form, which include the message body in $form_values['message'].
$params['message'] = "New content for ".$node_type." has been created and waiting to be approved.";
$params['subject'] = "Content approval request for ".$node_type." content.";
// The language of the e-mail. This will one of three values:
// - user_preferred_language(): Used for sending mail to a particular website
// user, so that the mail appears in their preferred language.
// - global $language: Used when sending a mail back to the user currently
// viewing the site. This will send it in the language they're currently
// using.
// - language_default(): Used when sending mail to a pre-existing, 'neutral'
// address, such as the system e-mail address, or when you're unsure of the
// language preferences of the intended recipient.
//
// Since in our case, we are sending a message to a random e-mail address that
// is not necessarily tied to a user account, we will use the site's default
// language.
$language = language_default();
// Whether or not to automatically send the mail when drupal_mail() is
// called. This defaults to TRUE, and is normally what you want unless you
// need to do additional processing before drupal_mail_send() is called.
$send = TRUE;
// Send the mail, and check for success. Note that this does not guarantee
// message delivery; only that there were no PHP-related issues encountered
// while sending.
$mailresult = drupal_mail($module, $key, $to, $language, $params, $from, $send);
if ($mailresult['result'] == TRUE) {
}
}
}//Users loop ends here
$users = array();//Empty users array on loop end to prevent sending mail repeatedly to the responsible user previously set
}
}
}//Role loop ends here
}
}
create_approval_entries($node);//create fields in node_approval and user_has_approved table
check_node_approval_status($node,$node->nid);//Set field to unpublished by default.
$node->is_new = false;//treat the node as an old entry as at this point a record for this node has already been created in the database
node_save($node);//Save the node status if any changes have occured from check_node_approval_status function.
}
function node_approval_node_presave($node){
global $user;
if($node->type=='sitelocations'||$node->type=='videos'||$node->type=='banner'){
//Escape videos and locations from requiring approval.
}else {
if (isset($node->nid) && !empty($node->nid)):
if (isset($node->individual_approval_status)) {
$res = db_update('user_has_approved')
->fields(
array(
'status' => $node->individual_approval_status,
)
)
->condition('node_id', $node->nid)
->condition('user_id', $user->uid)
->execute();
//Check whether all responsible users have approved the content or not and change the published status.
$node->status = check_node_approval_status($node, $node->nid);
unset($node->individual_approval_status);
}
endif;//Only run for new nodes
}
}
/**
* Implements hook_node_update().
*/
function node_approval_node_update($node)
{
//echo "Node before update:";var_dump($node);
//global $user;
/* if(isset($node->nid)&&!empty($node->nid)):
if (isset($node->individual_approval_status)) {
$res = db_update('user_has_approved')
->fields(
array(
'status' => $node->individual_approval_status,
)
)
->condition('node_id', $node->nid)
->condition('user_id', $user->uid)
->execute();
//Check whether all responsible users have approved the content or not and change the published status.
$node->status = check_node_approval_status($node, $node->nid);
unset($node->individual_approval_status);
}
endif;//Only run for new nodes*/
}
/**
* Implements hook_node_delete().
*/
function node_approval_node_delete($node)
{
db_delete('node_approval')
->condition('node_id', $node->nid)
->execute();
db_delete('user_has_approved')
->condition('node_id', $node->nid)
->execute();
}
function create_approval_entries($node)
{
global $user;
//Send mail when the node is first created
if ($node->is_new) {
switch ($node->type) {
case 'reallifestories':
$roles = user_roles(TRUE, APPROVE_REAL_LIFE_STORIES_CONTENT);
break;
/* case 'videos':
$roles = user_roles(TRUE, APPROVE_VIDEO_LIBRARY);
break;*/
case 'resources':
$roles = user_roles(TRUE, APPROVE_RESOURCES);
break;
case 'faq':
$roles = user_roles(TRUE, APPROVE_FAQ);
break;
case 'blog':
$roles = user_roles(TRUE, APPROVE_BLOG);
break;
case 'events':
$roles = user_roles(TRUE, APPROVE_EVENTS);
break;
case 'careers':
$roles = user_roles(TRUE, APPROVE_CAREERS);
break;
case 'links':
$roles = user_roles(TRUE, APPROVE_LINKS);
break;
/* case 'sitelocations':
$roles = user_roles(TRUE, APPROVE_LOCATIONS);
break;*/
case 'gallery':
$roles = user_roles(TRUE, APPROVE_GALLERY);
break;
case 'miscellaneous':
$roles = user_roles(TRUE, APPROVE_MISCELLANEOUS);
break;
}
//For each roles get the users who have this role
$users = array();
foreach ($roles as $key => $roleU) {
if ($roleU == 'administrator') {
} else {
$role = user_role_load_by_name($roleU);
$result = db_select('users_roles', 'ur')
->fields('ur', array('uid'))
->condition('rid', $role->rid)
->execute()->fetchAll();
foreach ($result as $record) {
$user_loaded = user_load($record->uid);
if (array_key_exists(1, $user_loaded->roles)) {
//Excluding admin
} else {
if ($record->uid == $user->uid) {
//Avoid creating an entry of user id in user has approved table.
} else {
$users[] = $record->uid;//This is to enter user ids apart from current user into users array
db_insert('user_has_approved')
->fields(
array(
'user_id' => $record->uid,
'status' => 0,
'node_id' => $node->nid,
'bundle' => $node->type,
)
)->execute();
}
}
}
}
}
//For each node only one entry is needed in node_approval table.
$serialized_uids = serialize($users);
$insert = db_insert('node_approval')
->fields(
array(
'user_ids' => $serialized_uids,
'published' => 0,
'node_id' => $node->nid,
)
)->execute();
drupal_get_messages($insert);
}
}
function node_approval_mail($key, &$message, $params)
{
global $user;
$options = array(
'langcode' => $message['language']->language,
);
switch ($key) {
// Send a simple message from the contact form.
case 'node_approval_request':
$message['subject'] = t('E-mail sent from @site-name', array('@site-name' => variable_get('site_name', 'Drupal')), $options) . " " . $params['subject'];
// Note that the message body is an array, not a string.
$message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
// Because this is just user-entered text, we do not need to translate it.
// Since user-entered text may have unintentional HTML entities in it like
// '<' or '>', we need to make sure these entities are properly escaped,
// as the body will later be transformed from HTML to text, meaning
// that a normal use of '<' will result in truncation of the message.
$message['body'][] = check_plain($params['message']);
break;
}
}
/**
* Implements hook_form_alter().
*/
function node_approval_form_alter(&$form, &$form_state, $form_id)
{
global $user;
//Check if this record is new or not
if (!isset($form_state['node']->nid) || isset($form_state['node']->is_new)) {
} else {
//Check user id for its permission to display the check box.
$allowCheckboxVisible = false;
switch ($form['type']['#value']) {
case 'reallifestories':
$allowCheckboxVisible = user_access(APPROVE_REAL_LIFE_STORIES_CONTENT);
break;
/* case 'videos':
$allowCheckboxVisible = user_access(APPROVE_VIDEO_LIBRARY);
break;*/
case 'resources':
$allowCheckboxVisible = user_access(APPROVE_RESOURCES);
break;
case 'faq':
$allowCheckboxVisible = user_access(APPROVE_FAQ);
break;
case 'blog':
$allowCheckboxVisible = user_access(APPROVE_BLOG);
break;
case 'events':
$allowCheckboxVisible = user_access(APPROVE_EVENTS);
break;
case 'careers':
$allowCheckboxVisible = user_access(APPROVE_CAREERS);
break;
case 'links':
$allowCheckboxVisible = user_access(APPROVE_LINKS);
break;
/* case 'sitelocations':
$allowCheckboxVisible = user_access(APPROVE_LOCATIONS);
break;*/
case 'gallery':
$allowCheckboxVisible = user_access(APPROVE_GALLERY);
break;
case 'miscellaneous':
$allowCheckboxVisible = user_access(APPROVE_MISCELLANEOUS);
break;
}
//Make the approved checkbox visible to only those users who have be allocated to approve the content
//i.e. cross reference uid's from the user_has_approved table to make the checkbox available here
$current_userid = $user->uid;
$result = db_select('user_has_approved', 'uha')
->fields('uha', array('user_id'))
->condition('node_id',$form_state['node']->nid)
->condition('user_id', $current_userid)
->execute()
->fetchAll();
if ($result):
if ($allowCheckboxVisible) {
$form['individual_approval_status'] = array(
'#type' => 'checkbox',
'#title' => t('Approve Content'),
'#default_value' => get_approval_status($user->uid, $form_state['node']->nid),
'#description' => t("Check the above box to approve content."),
'#weight' => -10,
);
}
endif;
}
//Hide the published status button from view.
$form['options']['status'] = array(
'#type' => 'value',
'#value' => 1
);
return $form;
}
/*
Function to check if approved by the particular user or not
*/
function get_approval_status($uid, $nid)
{
$flag = db_select('user_has_approved', 'uha')
->fields('uha', array('status'))
->condition('user_id', $uid)
->condition('node_id', $nid)
->execute()
->fetchObject();
/* $qryToGetStatus = "Select uhr.status from user_has_approved as uhr where user_id=" . $uid . " and node_id=" . $nid;
$flag = db_query($qryToGetStatus);*/
if (!$flag) {
// problem with query
} elseif ($flag) {
// results returned
if ($flag->status == '1') {
return true;// Checked
} else {
return false;//Not Checked
}
} else {
// no results, but good query
return false;//Not created
//return array();//Indicates not approved or pending.//no entry made.
}
}