/
scheduler.module
961 lines (882 loc) · 35 KB
/
scheduler.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
<?php
/**
* @file
* Scheduler publishes and unpublishes nodes on dates specified by the user.
*/
// The full set of date and time letters allowed in the scheduler date format.
define('SCHEDULER_DATE_LETTERS', 'djmnFMyY');
define('SCHEDULER_TIME_LETTERS', 'hHgGisaA');
define('SCHEDULER_DATE_FORMAT', 'Y-m-d H:m:s');
/**
* Implements hook_autoload_info().
*/
function scheduler_autoload_info() {
return array(
'SchedulerHandlerFieldSchedulerCountdown' => 'includes/scheduler_handler_field_scheduler_countdown.inc',
);
}
/**
* Implements hook_config_info().
*/
function scheduler_config_info() {
$prefixes['scheduler.settings'] = array(
'label' => t('Scheduler settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*/
function scheduler_permission() {
return array(
'administer scheduler' => array(
'title' => t('Administer scheduler'),
'description' => t('Configure scheduler date formats, pop-up calendar, default times, lightweight cron'),
),
'schedule publishing of nodes' => array(
'title' => t('Schedule content publication'),
'description' => t('Allows users to set a start and end time for content publication'),
),
'view scheduled content' => array(
'title' => t('View scheduled content list'),
'description' => t('Allows users to see all content which is scheduled.'),
),
);
}
/**
* Implements hook_menu().
*/
function scheduler_menu() {
$items = array();
$items['scheduler/cron'] = array(
'title' => 'Lightweight cron handler',
'description' => 'Run the lightweight cron process',
'page callback' => '_scheduler_run_cron',
'access callback' => '_scheduler_cron_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
'file' => 'scheduler.cron.inc',
);
// Redirect the legacy URL 'scheduler/timecheck' to the new admin tab
// 'admin/config/content/scheduler/timecheck'.
$items['scheduler/timecheck'] = array(
'page callback' => 'backdrop_goto',
'page arguments' => array('admin/config/content/scheduler/timecheck'),
'access arguments' => array('administer scheduler'),
'type' => MENU_CALLBACK,
);
$items['admin/config/content/scheduler'] = array(
'title' => 'Scheduler',
'description' => "Configure settings for scheduled publishing and unpublishing, run the lightweight cron and check your servers clock time.",
'page callback' => 'backdrop_get_form',
'page arguments' => array('scheduler_admin'),
'access arguments' => array('administer scheduler'),
'type' => MENU_NORMAL_ITEM,
'file' => 'scheduler.admin.inc',
);
$items['admin/config/content/scheduler/default'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 5,
);
$items['admin/config/content/scheduler/cron'] = array(
'title' => 'Lightweight Cron',
'description' => 'A lightweight cron handler to allow more frequent runs of Schedulers internal cron system.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('_scheduler_lightweight_cron'),
'access arguments' => array('administer scheduler'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'scheduler.admin.inc',
);
$items['admin/config/content/scheduler/timecheck'] = array(
'title' => 'Time Check',
'description' => 'Allows site admin to check their servers internal clock',
'page callback' => '_scheduler_timecheck',
'access arguments' => array('administer scheduler'),
'type' => MENU_LOCAL_TASK,
'weight' => 15,
'file' => 'scheduler.admin.inc',
);
$items['admin/content/scheduler'] = array(
'title' => 'Scheduled',
'page callback' => 'scheduler_list',
'page arguments' => array(NULL, NULL),
'access callback' => 'scheduler_list_access_callback',
'access arguments' => array(NULL),
'description' => 'Display a list of scheduled nodes',
'type' => MENU_LOCAL_TASK,
'file' => 'scheduler.admin.inc',
);
// Reuse the above definition in the scheduler admin page.
$items['admin/config/content/scheduler/list'] = array(
'title' => 'Scheduled Content',
'weight' => 20,
) + $items['admin/content/scheduler'];
// Menu callback for confirmation before manually deleting obsolete rows from
// the Scheduler table. Cannot use normal node delete link because these rows
// no longer exist in the {node} table.
$items['admin/content/scheduler/delete/%'] = array(
'title' => 'Delete Scheduled Data',
'description' => 'Delete obsolete rows from Scheduler table',
'page callback' => 'backdrop_get_form',
'page arguments' => array('_scheduler_delete_row_confirm', 4),
'access arguments' => array('administer scheduler'),
'type' => MENU_CALLBACK,
'file' => 'scheduler.admin.inc',
);
$items['user/%/scheduler'] = array(
'title' => 'Scheduled',
'page callback' => 'scheduler_list',
// This will pass the uid of the user account being viewed.
'page arguments' => array('user_only', 1),
'access callback' => 'scheduler_list_access_callback',
'access arguments' => array(1),
'description' => 'Display a list of scheduled nodes',
'type' => MENU_LOCAL_TASK,
'file' => 'scheduler.admin.inc',
);
return $items;
}
/**
* Return the users access to the scheduler list page.
*
* Separate function required because of the two access values to be checked.
*
* @param int $uid
* The user ID of the user of which the scheduled nodes will be listed. Omit
* this when listing the nodes of all users.
*/
function scheduler_list_access_callback($uid = NULL) {
global $user;
// All Scheduler users can see their own scheduled content via their user
// page. In addition, if they have 'view scheduled content' permission they
// will be able to see all scheduled content by all authors.
return user_access('view scheduled content') || ($uid == $user->uid && user_access('schedule publishing of nodes'));
}
/**
* Returns whether we use the date_popup for date/time selection.
*
* @return bool
* TRUE if we are using date_popup. FALSE otherwise.
*/
function _scheduler_use_date_popup() {
return (module_exists('date') && config_get('scheduler.settings', 'scheduler_field_type') == 'date_popup');
}
/**
* Implements hook_form_node_type_form_alter().
*/
function scheduler_form_node_type_form_alter(&$form, $form_state) {
// Load the real code only when needed.
module_load_include('inc', 'scheduler', 'scheduler.admin');
_scheduler_form_node_type_form_alter($form, $form_state);
}
/**
* Implements hook_form_alter().
*/
function scheduler_form_alter(&$form, $form_state) {
// Load the real code only when needed. First check if this a node form and
// that the user has permission to use Scheduler.
if (!empty($form['#attributes']['class']) && in_array('node-form', $form['#attributes']['class']) && user_access('schedule publishing of nodes')) {
// Check if scheduling has been enabled for this node type.
$publishing_enabled = config_get('node.type.' . $form['type']['#value'], 'scheduler_publish_enable') == 1;
$unpublishing_enabled = config_get('node.type.' . $form['type']['#value'], 'scheduler_unpublish_enable') == 1;
if ($publishing_enabled || $unpublishing_enabled) {
module_load_include('inc', 'scheduler', 'scheduler.edit');
_scheduler_form_alter($form, $form_state);
}
}
}
/**
* Checks whether a scheduled action on a node is allowed.
*
* This provides a way for other modules to prevent scheduled publishing or
* unpublishing, by implementing hook_scheduler_allow_publishing() or
* hook_scheduler_allow_unpublishing().
*
* @param object $node
* The node object on which the action is to be performed.
* @param string $action
* The action that needs to be checked. Can be 'publish' or 'unpublish'.
*
* @return bool
* TRUE if the action is allowed, FALSE if not.
*
* @see hook_scheduler_allow_publishing()
* @see hook_scheduler_allow_unpublishing()
*/
function _scheduler_allow($node, $action) {
// Default to TRUE.
$result = TRUE;
// Check that other modules allow the action.
$hook = 'scheduler_allow_' . $action . 'ing';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$result &= $function($node);
}
return $result;
}
/**
* Converts a time string from the user's timezone into a Unix timestamp.
*
* This expects the time string to be in the date format configured by the user.
*
* @param string $str
* A string containing a time in the user configured date format.
*
* @return int
* The time in Unix timestamp representation (UTC). NULL if the given time
* string is empty (NULL, FALSE, an empty string or only containing
* whitespace). FALSE if the given time string is malformed.
*/
function _scheduler_strtotime($str) {
$config = config('scheduler.settings');
if ($str && trim($str) != "") {
$date_format = $config->get('scheduler_date_format');
$date_only_format = $config->get('scheduler_date_only_format');
if (_scheduler_use_date_popup()) {
// Date Popup currently returns the value into the $node using its default
// format DATE_FORMAT_DATETIME but reduced to the same granularity as the
// requested format. Until date issue http://drupal.org/node/1855810 is
// resolved we can use the date_popup functions date_format_order() and
// date_limit_format() to derive the format of the returned string value.
$granularity = date_format_order($date_format);
$date_format = date_limit_format(DATE_FORMAT_DATETIME, $granularity);
$granularity = array('day', 'month', 'year');
$date_only_format = date_limit_format(DATE_FORMAT_DATETIME, $granularity);
}
$str = trim(preg_replace('/\s+/', ' ', $str));
$time = _scheduler_strptime($str, $date_format);
// If the time failed using the full $date_format or no time entry is
// allowed, but date-only with a default time is enabled, check if the input
// matches the "date only" date format.
$time_only_format = $config->get('scheduler_time_only_format');
if ((!$time || $time_only_format == '') && $config->get('scheduler_allow_date_only')) {
if ($time = _scheduler_strptime($str, $date_only_format)) {
// A time has been calculated, but also check that there was only a
// date entered and no extra mal-formed time elements.
if ($str == date($date_only_format, $time)) {
// Parse the default time string into elements, then add the total
// offset in seconds to the timestamp.
$default_time = date_parse($config->get('scheduler_default_time'));
$time_offset = ($default_time['hour'] * 3600) + ($default_time['minute'] * 60) + $default_time['second'];
$time += $time_offset;
}
else {
// The date was not the only text entered, so reject it.
$time = FALSE;
}
}
}
}
else {
// $str is empty.
$time = NULL;
}
return $time;
}
/**
* Parse a time/date as UTC time.
*
* The php function strptime() has a limited life, due to it returning varying
* results on different operating systems. It is not supported on Windows
* platforms at all. The replacement function date_parse_from_format() is not
* as flexible as strptime(), for example it forces two-digit minutes and
* seconds. _scheduler_strptime() gives us more control over the entities that
* are parsed and how the matching is achieved.
*
* @param string $date
* The string to parse.
* @param string $format
* The date format used in $date. For details on the date format options, see
* the PHP date() function.
*
* @return int
* The parsed time converted to a UTC timestamp using mktime().
*/
function _scheduler_strptime($date, $format) {
// Build a regex pattern for each element allowed in the date and time format.
$date_entities_and_replacements = array(
// Date elements, one for each character in SCHEDULER_DATE_LETTERS.
// Inline comments on each row are useful here, so 'ignore' for standards.
// @codingStandardsIgnoreStart
'd' => '(\d{2})', // Day of the month with leading zero.
'j' => '(\d{1,2})', // Day of the month without leading zero.
'm' => '(\d{2})', // Month number with leading zero.
'n' => '(\d{1,2})', // Month number without leading zero.
'M' => '(\w{3})', // Three-letter month abbreviation.
'F' => '(\w{3,9})', // Full month name, from 3 to 9 letters.
'y' => '(\d{2})', // Two-digit year.
'Y' => '(\d{4})', // Four-digit year.
// Time elements, one for each character in SCHEDULER_TIME_LETTERS.
'h' => '(\d{2})', // Hours in 12-hour format with leading zero.
'H' => '(\d{2})', // Hours in 24-hour format with leading zero.
'g' => '(\d{1,2})', // Hours in 12-hour format without leading zero.
'G' => '(\d{1,2})', // Hours in 24-hour format without leading zero.
'i' => '(\d{2})', // Minutes with leading zero.
's' => '(\d{2})', // Seconds with leading zero.
'a' => '([ap]m)', // Lower case meridian.
'A' => '([AP]M)', // Upper case meridian.
// @codingStandardsIgnoreEnd
);
$date_entities = array_keys($date_entities_and_replacements);
$date_regex_replacements = array_values($date_entities_and_replacements);
$custom_pattern = str_replace($date_entities, $date_regex_replacements, $format);
if (!preg_match("#$custom_pattern#", $date, $value_matches)) {
return FALSE;
}
if (!preg_match_all('/(\w)/', $format, $entity_matches)) {
return FALSE;
}
$results = array(
'day' => 0,
'month' => 0,
'year' => 0,
'hour' => 0,
'minute' => 0,
'second' => 0,
'meridiem' => NULL,
);
$index = 1;
foreach ($entity_matches[1] as $entity) {
$value = intval($value_matches[$index]);
switch ($entity) {
case 'd':
case 'j':
$results['day'] = $value;
break;
case 'm':
case 'n':
$results['month'] = $value;
break;
case 'M':
case 'F':
// Derive a time value from the matched month name text.
$temp_time = strtotime($value_matches[$index]);
if (empty($temp_time)) {
// If the text is not a valid month name or abbreviation then fail.
return FALSE;
}
// Derive the month number from the month name.
$results['month'] = date('n', $temp_time);
break;
case 'y':
case 'Y':
$results['year'] = $value;
break;
case 'H':
case 'h':
case 'g':
case 'G':
$results['hour'] = $value;
break;
case 'i':
$results['minute'] = $value;
break;
case 's':
$results['second'] = $value;
break;
case 'a':
case 'A':
$results['meridiem'] = $value_matches[$index];
break;
}
$index++;
}
if ($results['meridiem'] !== NULL) {
if ((strncasecmp($results['meridiem'], "pm", 2) == 0) && ($results['hour'] < 12)) {
$results['hour'] += 12;
}
if ((strncasecmp($results['meridiem'], "am", 2) == 0) && ($results['hour'] == 12)) {
$results['hour'] -= 12;
}
}
$time = mktime($results['hour'], $results['minute'], $results['second'], $results['month'], $results['day'], $results['year']);
return $time;
}
/**
* Implements hook_node_load().
*/
function scheduler_node_load($nodes, $types) {
$nids = array_keys($nodes);
$result = db_query('SELECT * FROM {scheduler} WHERE nid IN (:nids)', array(':nids' => $nids));
foreach ($result as $record) {
$nid = $record->nid;
$nodes[$nid]->publish_on = $record->publish_on;
$nodes[$nid]->unpublish_on = $record->unpublish_on;
$row = array();
// @todo This seems unneeded and is confusing. It is not certain that this
// node is either published or unpublished, probably it isn't or the
// 'publish_on' property wouldn't be set in the first place. Remove this
// for the D8 version.
$date_format = config_get('system.date', 'formats.medium.pattern');
$row['published'] = $record->publish_on ? date($date_format, $record->publish_on) : NULL;
$row['unpublished'] = $record->unpublish_on ? date($date_format, $record->unpublish_on) : NULL;
$nodes[$nid]->scheduler = $row;
}
}
/**
* Implements hook_node_view().
*/
function scheduler_node_view($node, $view_mode, $langcode) {
// If the node is going to be unpublished, then add this information to the
// header for search engines. Only do this when the current page is the
// full-page view of the node.
// @see https://googleblog.blogspot.be/2007/07/robots-exclusion-protocol-now-with-even.html
if ($view_mode == 'full' && !empty($node->unpublish_on) && node_is_page($node) && arg(2) != 'edit') {
backdrop_add_http_header('X-Robots-Tag', 'unavailable_after: ' . date(DATE_RFC850, $node->unpublish_on));
}
}
/**
* Implements hook_node_validate().
*/
function scheduler_node_validate($node, $form, &$form_state) {
$config = config('scheduler.settings');
$nodetype_config = config('node.type.' . $node->type);
// Skip all validation when deleting the node. To check if the delete button
// was clicked search for 'node_form_delete_submit' in the triggering_element
// #submit array. This is better than checking for the text 'Delete'.
// Use !== FALSE because the key returned will be 0.
// @see https://www.drupal.org/node/2723929
if (!empty($form_state['triggering_element']['#submit']) && array_search('node_form_delete_submit', $form_state['triggering_element']['#submit']) !== FALSE) {
if (form_get_errors()) {
// If there are already errors (from date_popup) remove them to allow
// deletion to proceed.
form_clear_error();
unset($_SESSION['messages']['error']);
};
return;
}
// Adjust the entered times for timezone consideration. Note, we must check
// to see if the value is numeric. If it is, assume we have already done the
// strtotime conversion. This prevents us running strtotime on a value we have
// already converted. This is needed because Drupal 6 removed 'submit' and
// added 'presave' and all this happens at different times. If the value is
// passed as an array this means we are using the Date Popup functionality.
$date_format = $config->get('scheduler_date_format');
$args = array('%time' => format_date(REQUEST_TIME, 'custom', $date_format));
if (!empty($node->publish_on) && !is_numeric($node->publish_on)) {
if (!is_array($node->publish_on)) {
$publishtime = _scheduler_strtotime($node->publish_on);
if ($publishtime === FALSE) {
form_set_error('publish_on', t("The 'publish on' value does not match the expected format of %time", $args));
}
elseif ($publishtime && config_get('node.type.' . $node->type, 'scheduler_publish_past_date') == 'error' && $publishtime < REQUEST_TIME) {
form_set_error('publish_on', t("The 'publish on' date must be in the future"));
}
}
elseif (!empty($node->publish_on['date'])) {
$publishtime = $node->publish_on['date'];
$publishtime .= (!empty($node->publish_on['time'])) ? ' ' . $node->publish_on['time'] : '';
}
}
if (!empty($node->unpublish_on) && !is_numeric($node->unpublish_on)) {
if (!is_array($node->unpublish_on)) {
$unpublishtime = _scheduler_strtotime($node->unpublish_on);
if ($unpublishtime === FALSE) {
form_set_error('unpublish_on', t("The 'unpublish on' value does not match the expected format of %time", $args));
}
elseif ($unpublishtime && $unpublishtime < REQUEST_TIME) {
form_set_error('unpublish_on', t("The 'unpublish on' date must be in the future"));
}
}
elseif (!empty($node->unpublish_on['date'])) {
$unpublishtime = $node->unpublish_on['date'];
$unpublishtime .= (!empty($node->unpublish_on['time'])) ? ' ' . $node->unpublish_on['time'] : '';
}
}
if (isset($publishtime) && isset($unpublishtime) && $unpublishtime < $publishtime) {
form_set_error('unpublish_on', t("The 'unpublish on' date must be later than the 'publish on' date."));
}
// The unpublish-on 'required' form attribute may not be set, but in some
// cases a value must still be entered.
if ($nodetype_config->get('scheduler_unpublish_enable')
&& $nodetype_config->get('scheduler_unpublish_required')
&& empty($node->unpublish_on)) {
// ... when also setting a publish-on date.
if (!empty($node->publish_on)) {
form_set_error('unpublish_on', t("If you set a 'publish-on' date then you must also set an 'unpublish-on' date."));
}
// ... or when publishing by directly ticking the published checkbox.
if ($form_state['values']['status']) {
form_set_error('unpublish_on', t("To publish this node you must also set an 'unpublish-on' date."));
}
}
}
/**
* Implements hook_node_presave().
*/
function scheduler_node_presave($node) {
$nodetype_config = config('node.type.' . $node->type);
foreach (array('publish_on', 'unpublish_on') as $key) {
if (empty($node->$key)) {
// Make sure publish_on and unpublish_on are not empty strings.
$node->$key = 0;
}
elseif (is_array($node->$key)) {
// Convert date popup array to unix timestamp.
$node->$key = _scheduler_strtotime($node->$key['date'] . ' ' . $node->$key['time']);
}
elseif (!is_numeric($node->$key)) {
// Convert to unix timestamp, but ensure any failure is converted to zero.
$node->$key = _scheduler_strtotime($node->$key) + 0;
}
}
if ($node->publish_on > 0) {
// Check that other modules allow the action on this node.
$publication_allowed = _scheduler_allow($node, 'publish');
// Publish the node immediately if the publication date is in the past.
$publish_immediately = $nodetype_config->get('scheduler_publish_past_date') == 'publish';
if ($publication_allowed && $publish_immediately && $node->publish_on <= REQUEST_TIME) {
// If required, set the created date to match published date.
if ($nodetype_config->get('scheduler_publish_touch') == 1) {
$node->created = $node->publish_on;
}
$node->publish_on = 0;
$node->status = 1;
// Allow modules to react to immediate publishing.
_scheduler_scheduler_api($node, 'publish_immediately');
}
else {
// Ensure the node is unpublished as it will be published by cron later.
$node->status = 0;
// Only inform the user that the node is scheduled if publication has not
// been prevented by other modules. Those modules have to display a
// message themselves explaining why publication is denied.
if ($publication_allowed) {
$date_format = config_get('scheduler.settings', 'scheduler_date_format');
backdrop_set_message(t('This post is unpublished and will be published @publish_time.', array(
'@publish_time' => format_date($node->publish_on, 'custom', $date_format),
)), 'status', FALSE);
}
}
}
}
/**
* Implements hook_node_insert().
*/
function scheduler_node_insert($node) {
// Only insert into database if we need to (un)publish this node at some date.
if (!empty($node->publish_on) || !empty($node->unpublish_on)) {
db_insert('scheduler')->fields(array(
'nid' => $node->nid,
'publish_on' => $node->publish_on,
'unpublish_on' => $node->unpublish_on,
))->execute();
// Invoke the events to indicate that a new node has been scheduled.
if (module_exists('rules')) {
if (!empty($node->publish_on)) {
rules_invoke_event('scheduler_new_node_is_scheduled_for_publishing_event', $node, $node->publish_on, $node->unpublish_on);
}
if (!empty($node->unpublish_on)) {
rules_invoke_event('scheduler_new_node_is_scheduled_for_unpublishing_event', $node, $node->publish_on, $node->unpublish_on);
}
}
}
}
/**
* Implements hook_node_update().
*/
function scheduler_node_update($node) {
// Only update database if we need to (un)publish this node at some date,
// otherwise the user probably cleared out the (un)publish dates so we should
// remove the record.
if (!empty($node->publish_on) || !empty($node->unpublish_on)) {
db_merge('scheduler')->key(array('nid' => $node->nid))->fields(array(
'publish_on' => $node->publish_on,
'unpublish_on' => $node->unpublish_on,
))->execute();
// Invoke the events to indicate that an existing node has been scheduled.
if (module_exists('rules')) {
if (!empty($node->publish_on)) {
rules_invoke_event('scheduler_existing_node_is_scheduled_for_publishing_event', $node, $node->publish_on, $node->unpublish_on);
}
if (!empty($node->unpublish_on)) {
rules_invoke_event('scheduler_existing_node_is_scheduled_for_unpublishing_event', $node, $node->publish_on, $node->unpublish_on);
}
}
}
else {
scheduler_node_delete($node);
}
}
/**
* Implements hook_node_delete().
*/
function scheduler_node_delete($node) {
db_delete('scheduler')->condition('nid', $node->nid)->execute();
}
/**
* Implements hook_node_type_delete().
*/
function scheduler_node_type_delete($info) {
$variables = array();
$variables[] = "scheduler_publish_enable_" . $info->type;
$variables[] = "scheduler_publish_touch_" . $info->type;
$variables[] = "scheduler_publish_required_" . $info->type;
$variables[] = "scheduler_publish_revision_" . $info->type;
$variables[] = "scheduler_publish_past_date_" . $info->type;
$variables[] = "scheduler_unpublish_enable_" . $info->type;
$variables[] = "scheduler_unpublish_required_" . $info->type;
$variables[] = "scheduler_unpublish_revision_" . $info->type;
foreach ($variables as $variable) {
variable_del($variable);
}
}
/**
* Implements hook_cron().
*/
function scheduler_cron() {
$config = config('scheduler.settings');
// Load the cron functions file.
module_load_include('inc', 'scheduler', 'scheduler.cron');
// During cron runs we do not want i18n_sync to make any changes to the
// translation nodes, as this affects processing later in the same cron job.
// Hence we save the i18n_sync state here, turn it off for the duration of
// Scheduler cron processing, then restore the setting afterwards.
// @todo Replace this workaround with a hook implementation when issue
// #2136557 lands.
// @see https://drupal.org/node/1182450
// @see https://drupal.org/node/2136557
if (module_exists('i18n_sync')) {
$i18n_sync_saved_state = i18n_sync();
i18n_sync(FALSE);
}
// Use backdrop_static so that any function can find out if we are running
// Scheduler cron. Set the default value to FALSE, then turn on the flag.
// @see scheduler_cron_is_running()
$scheduler_cron = &backdrop_static(__FUNCTION__, FALSE);
$scheduler_cron = TRUE;
// If the option is enabled clear caches if a node was published or
// unpublished, just like it would have been done if system_cron ran or the
// nodes were edited through the node edit form.
$nodes_published = _scheduler_publish();
$nodes_unpublished = _scheduler_unpublish();
if ($config->get('scheduler_cache_clear_all') && ($nodes_published || $nodes_unpublished)) {
// Clear the page and block caches.
cache_clear_all();
watchdog('scheduler', 'Page and block caches cleared.', array(), WATCHDOG_NOTICE, l(t('settings'), 'admin/config/content/scheduler'));
}
// Reset the static scheduler_cron flag.
backdrop_static_reset(__FUNCTION__);
// Restore the i18n_sync state.
module_exists('i18n_sync') ? i18n_sync($i18n_sync_saved_state) : NULL;
}
/**
* Return whether Scheduler cron is running.
*
* This function can be called from any Scheduler function, from any contrib
* module or from custom PHP in a view or rule.
*
* @return bool
* TRUE if scheduler_cron is currently running. FALSE if not.
*/
function scheduler_cron_is_running() {
return backdrop_static('scheduler_cron');
}
/**
* Access callback for the lightweight cron url /scheduler/cron/key.
*
* @param string $cron_key
* The cron key that was passed as a URL argument.
*
* @return bool
* TRUE if the cron url key is correct. FALSE otherwise.
*/
function _scheduler_cron_access($cron_key) {
$valid_cron_key = config_get('scheduler.settings', 'scheduler_lightweight_access_key');
return $valid_cron_key == $cron_key;
}
/**
* Scheduler API to perform actions when nodes are (un)published.
*
* This allows other modules to implement hook_scheduler_api($node, $action).
*
* @param object $node
* The node object.
* @param string $action
* The action being performed, either 'pre_publish', 'publish',
* 'publish_immediately', 'pre_unpublish' or 'unpublish'.
*/
function _scheduler_scheduler_api($node, $action) {
foreach (module_implements('scheduler_api') as $module) {
$function = $module . '_scheduler_api';
$function($node, $action);
}
}
/**
* Implements hook_theme().
*/
function scheduler_theme() {
return array(
'scheduler_timecheck' => array(
'arguments' => array('now' => NULL),
),
);
}
/**
* Implements hook_views_api().
*/
function scheduler_views_api() {
$info['api'] = 2;
return $info;
}
/**
* Implements hook_field_extra_fields().
*/
function scheduler_field_extra_fields() {
$fields = array();
foreach (node_type_get_types() as $type) {
$config = config('scheduler.settings');
$publishing_enabled = $config->get('scheduler_publish_enable_' . $type->type);
$unpublishing_enabled = $config->get('scheduler_unpublish_enable_' . $type->type);
$use_vertical_tabs = $config->get('scheduler_use_vertical_tabs_' . $type->type);
if (($publishing_enabled || $unpublishing_enabled) && !$use_vertical_tabs) {
$fields['node'][$type->type]['form']['scheduler_settings'] = array(
'label' => t('Scheduler'),
'description' => t('Fieldset containing scheduling settings'),
'weight' => 0,
);
}
}
return $fields;
}
/**
* Prepares variables for node templates.
*
* Makes the publish_on and unpublish_on data available as theme variables.
*
* @see template_preprocess_node()
*/
function scheduler_preprocess_node(&$variables) {
$node = $variables['node'];
$date_format = config_get('scheduler.settings', 'scheduler_date_format');
if (!empty($node->publish_on) && $node->publish_on && is_numeric($node->publish_on)) {
$variables['publish_on'] = format_date($node->publish_on, 'custom', $date_format);
}
if (!empty($node->unpublish_on) && $node->unpublish_on && is_numeric($node->unpublish_on)) {
$variables['unpublish_on'] = format_date($node->unpublish_on, 'custom', $date_format);
}
}
/**
* Implements hook_feeds_processor_targets_alter().
*
* This function exposes publish_on and unpublish_on as mappable targets to the
* Feeds module.
*/
function scheduler_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
$config = config('scheduler.settings');
// Scheduler module only works on nodes.
if ($entity_type == 'node') {
$publishing_enabled = $config->get('scheduler_publish_enable_' . $bundle_name);
$unpublishing_enabled = $config->get('scheduler_unpublish_enable_' . $bundle_name);
if ($publishing_enabled) {
$targets['publish_on'] = array(
'name' => t('Scheduler: publish on'),
'description' => t('The date when the Scheduler module will publish the node.'),
'callback' => 'scheduler_feeds_set_target',
);
}
if ($unpublishing_enabled) {
$targets['unpublish_on'] = array(
'name' => t('Scheduler: unpublish on'),
'description' => t('The date when the Scheduler module will unpublish the node.'),
'callback' => 'scheduler_feeds_set_target',
);
}
}
}
/**
* Mapping callback for the Feeds module.
*/
function scheduler_feeds_set_target($source, $entity, $target, $value, $mapping) {
// We expect a string or integer, but can accomodate an array, by taking the
// first item. Use trim() so that a string of blanks is reduced to empty.
$value = is_array($value) ? trim(reset($value)) : trim($value);
// Convert input from parser to timestamp form. If $value is empty or blank
// then strtotime() must not be used, otherwise it returns the current time.
if (!empty($value) && !is_numeric($value)) {
if (!$timestamp = strtotime($value)) {
// Throw an exception if the date format was not recognized.
throw new FeedsValidationException("Value $value for {$mapping['source']} could not be converted to a valid Scheduler $target date.");
}
}
else {
$timestamp = $value;
}
// If the timestamp is valid then use it to set the target field in the node.
if (is_numeric($timestamp) && $timestamp > 0) {
$entity->$target = $timestamp;
}
}
/**
* Implements hook_ctools_plugin_directory().
*/
function scheduler_ctools_plugin_directory($owner, $plugin_type) {
// Declare a form pane (panels content type) for use in ctools and page
// manager. This allows the Scheduler fieldset to be placed in a panel.
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/**
* Implements hook_i18n_sync_options().
*/
function scheduler_i18n_sync_options($entity_type, $bundle_name) {
// Keep the scheduler dates synchronised between separate nodes which have
// been defined as translations of each other.
if ($entity_type == 'node') {
$config = config('node.type.' . $bundle_name);
$options = array();
// $bundle_name holds the content_type.
if ($config->get('scheduler_publish_enable')) {
$options['publish_on'] = array(
'title' => t('Publish on'),
'description' => t('Scheduler Publish date and time'),
);
}
if ($config->get('scheduler_unpublish_enable')) {
$options['unpublish_on'] = array(
'title' => t('Unpublish on'),
'description' => t('Scheduler Unpublish date and time'),
);
}
return $options;
}
}
/**
* Implements hook_field_attach_prepare_translation_alter().
*/
function scheduler_field_attach_prepare_translation_alter($entity, $context) {
// Prefill the node translation form with values from the translation source
// node.
$source_entity = $context['source_entity'];
if (isset($source_entity->publish_on)) {
$entity->publish_on = $source_entity->publish_on;
}
if (isset($source_entity->unpublish_on)) {
$entity->unpublish_on = $source_entity->unpublish_on;
}
}
/**
* Implements hook_date_popup_pre_validate_alter().
*/
function scheduler_date_popup_pre_validate_alter($element, $form_state, &$input) {
$config = config('scheduler.settings');
// Provide a default time if this is enabled and the time field is empty.
if ($config->get('scheduler_allow_date_only') && $element['#array_parents'][0] == 'scheduler_settings' && !empty($input['date']) && empty($input['time'])) {
// Get the default time as a timestamp number.
$default_time = strtotime($config->get('scheduler_default_time'));
// Set the time in the required format just as if the user had typed it.
$input['time'] = format_date($default_time, 'custom', $config->get('scheduler_time_only_format'));
}
}
/**
* Internal function to add js 'theme' setting and return the js filename.
*/
function _scheduler_get_vertical_tabs_js() {
global $theme;
drupal_add_js(array('scheduler_vertical_tabs' => array('theme' => $theme)), array('type' => 'setting'));
return drupal_get_path('module', 'scheduler') . "/scheduler_vertical_tabs.js";
}