scheduler.module in Scheduler 7
Same filename and directory in other branches
Scheduler publishes and unpublishes nodes on dates specified by the user.
File
scheduler.moduleView source
<?php
/**
* @file
* Scheduler publishes and unpublishes nodes on dates specified by the user.
*/
// The default format to use if no custom format has been configured.
define('SCHEDULER_DATE_FORMAT', 'Y-m-d H:i:s');
// The default date and time formats to use when only a date has been entered.
// These should match the date and time parts of SCHEDULER_DATE_FORMAT above.
define('SCHEDULER_DATE_ONLY_FORMAT', 'Y-m-d');
define('SCHEDULER_TIME_ONLY_FORMAT', 'H:i:s');
// The default time that will be used, until Admin sets a different value.
define('SCHEDULER_DEFAULT_TIME', '00:00:00');
// The full set of date and time letters allowed in the scheduler date format.
define('SCHEDULER_DATE_LETTERS', 'djmnFMyY');
define('SCHEDULER_TIME_LETTERS', 'hHgGisaA');
/**
* 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' => 'drupal_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' => 'drupal_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' => 'drupal_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' => 'drupal_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');
}
/**
* Implements hook_help().
*/
function scheduler_help($section) {
$output = '';
switch ($section) {
case 'admin/config/content/scheduler':
$output = '<p>' . t('Some Scheduler options are set for each different content type, and are accessed via the <a href="@link">admin content type</a> list.', array(
'@link' => url('admin/structure/types'),
)) . '</br>';
$output .= t('The options and settings below are common to all content types.') . '</p>';
break;
case 'admin/config/content/scheduler/cron':
$base_url = $GLOBALS['base_url'];
$access_key = variable_get('scheduler_lightweight_access_key', '');
$cron_url = $base_url . '/scheduler/cron' . ($access_key ? '/' . $access_key : '');
$output = '<p>' . t("When you have set up Drupal's standard crontab job cron.php then Scheduler will be executed during each cron run. However, if you would like finer granularity to scheduler, but don't want to run Drupal's cron more often then you can use the lightweight cron handler provided by Scheduler. This is an independent cron job which only runs the scheduler process and does not execute any cron tasks defined by Drupal core or any other modules.") . '</p>';
$output .= '<p>' . t("Scheduler's cron is at /scheduler/cron and a sample crontab entry to run scheduler every minute might look like:") . '</p>';
$output .= '<code>* * * * * wget -q -O /dev/null "' . $cron_url . '"</code>';
$output .= '<p>' . t('or') . '</p>';
$output .= '<code>* * * * * curl -s -o /dev/null "' . $cron_url . '"</code><br/><br/>';
break;
case 'admin/help#scheduler':
// This is shown at the top of admin/help/scheduler.
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Scheduler module provides the functionality for automatic publishing and unpublishing of nodes at specified future dates.') . '</p>';
$output .= '<p>' . t('You can read more in the <a href="@readme">readme.txt</a> file.', array(
'@readme' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'scheduler') . '/README.txt',
)) . '</p>';
break;
default:
}
return $output;
}
/**
* 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_popup') && variable_get('scheduler_field_type', 'date_popup') == '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['#node_edit_form']) && user_access('schedule publishing of nodes')) {
// Check if scheduling has been enabled for this node type.
$publishing_enabled = variable_get('scheduler_publish_enable_' . $form['type']['#value'], 0) == 1;
$unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $form['type']['#value'], 0) == 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) {
if ($str && trim($str) != "") {
$date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
$date_only_format = variable_get('scheduler_date_only_format', 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 = variable_get('scheduler_time_only_format', SCHEDULER_TIME_ONLY_FORMAT);
if ((!$time || $time_only_format == '') && variable_get('scheduler_allow_date_only', FALSE)) {
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(variable_get('scheduler_default_time', 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)',
);
$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 (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.
$row['published'] = $record->publish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->publish_on) : NULL;
$row['unpublished'] = $record->unpublish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->unpublish_on) : NULL;
// Add duplicates of the scheduling properties on $node->scheduler for
// backwards compatibility with the D5 and D6 versions of Scheduler. Please
// do not rely on these properties in new code, access them directly on
// $node->publish_on and $node->unpublish_on instead.
// @todo Remove this for the D8 version.
$row['publish_on'] = $record->publish_on;
$row['unpublish_on'] = $record->unpublish_on;
$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') {
drupal_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) {
// 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 module and a
// validation error has occurred. In this case we should skip validation as
// it is being handled by Date Popup.
$date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
$args = array(
'%time' => format_date(REQUEST_TIME, 'custom', $date_format),
);
if (!empty($node->publish_on) && !is_numeric($node->publish_on) && !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 && variable_get('scheduler_publish_past_date_' . $node->type, 'error') == 'error' && $publishtime < REQUEST_TIME) {
form_set_error('publish_on', t("The 'publish on' date must be in the future"));
}
}
if (!empty($node->unpublish_on) && !is_numeric($node->unpublish_on) && !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"));
}
}
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 (variable_get('scheduler_unpublish_enable_' . $node->type) && variable_get('scheduler_unpublish_required_' . $node->type) && 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) {
foreach (array(
'publish_on',
'unpublish_on',
) as $key) {
if (empty($node->{$key}) || is_array($node->{$key})) {
// Make sure publish_on and unpublish_on are not empty strings.
$node->{$key} = 0;
}
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 = variable_get('scheduler_publish_past_date_' . $node->type, 'error') == 'publish';
if ($publication_allowed && $publish_immediately && $node->publish_on <= REQUEST_TIME) {
// If required, set the created date to match published date.
if (variable_get('scheduler_publish_touch_' . $node->type, 0) == 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 = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
drupal_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() {
// 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 drupal_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 =& drupal_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 (variable_get('scheduler_cache_clear_all', 0) && ($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.
drupal_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 drupal_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 = variable_get('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) {
$publishing_enabled = variable_get('scheduler_publish_enable_' . $type->type, 0);
$unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $type->type, 0);
$use_vertical_tabs = variable_get('scheduler_use_vertical_tabs_' . $type->type, 1);
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 = variable_get('scheduler_date_format', 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) {
// Scheduler module only works on nodes.
if ($entity_type == 'node') {
$publishing_enabled = variable_get('scheduler_publish_enable_' . $bundle_name, 0);
$unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $bundle_name, 0);
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') {
$options = array();
// $bundle_name holds the content_type.
if (variable_get('scheduler_publish_enable_' . $bundle_name, 0)) {
$options['publish_on'] = array(
'title' => t('Publish on'),
'description' => t('Scheduler Publish date and time'),
);
}
if (variable_get('scheduler_unpublish_enable_' . $bundle_name, 0)) {
$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) {
// Provide a default time if this is enabled and the time field is empty.
if (variable_get('scheduler_allow_date_only', FALSE) && $element['#array_parents'][0] == 'scheduler_settings' && !empty($input['date']) && empty($input['time'])) {
// Get the default time as a timestamp number.
$default_time = strtotime(variable_get('scheduler_default_time', 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', variable_get('scheduler_time_only_format', 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";
}