state_flow_schedule.module in State Machine 7.2
Same filename and directory in other branches
Module file for state_flow_schedule.
File
modules/state_flow_schedule/state_flow_schedule.moduleView source
<?php
/**
* @file
* Module file for state_flow_schedule.
*/
/**
* Implements hook_permission().
*/
function state_flow_schedule_permission() {
return array(
'schedule content workflow' => array(
'title' => t('Schedule content workflow.'),
'description' => t('Schedule content to be published.'),
),
);
}
/**
* Implements hook_views_api().
*/
function state_flow_schedule_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'state_flow_schedule') . '/includes/views',
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @param array $form
* @param array $form_state
* @param string $form_id
*/
function state_flow_schedule_form_state_flow_events_revision_alter(&$form, &$form_state, $form_id) {
$event_name = $form['event']['#value'];
if (isset($event_name) && empty($form['event_comment']['#default_value']) && empty($form['event_comment']['#value'])) {
$scheduled_events = array(
'schedule',
);
//allow other modules to determine events that can be scheduled
drupal_alter('state_flow_schedule_events', $scheduled_events);
if (in_array($event_name, $scheduled_events)) {
$form['selected-date'] = array(
'#type' => 'date_popup',
'#title' => 'Select a date and time',
'#date_year_range' => '-0:+3',
'#default_value' => time(),
'#date_label_position' => 'within',
'#date_format' => 'm/d/Y h:i a',
'#weight' => 2,
'#required' => TRUE,
);
$form['submit']['#weight'] = 3;
$form['cancel']['#weight'] = 4;
$form['event-name'] = array(
'#type' => 'hidden',
'#default_value' => $event_name,
);
$current_node = $form['node_revision']['#value'];
$form['nid'] = array(
'#type' => 'hidden',
'#default_value' => $current_node->nid,
);
$form['vid'] = array(
'#type' => 'hidden',
'#default_value' => $current_node->vid,
);
$form['#validate'][] = 'state_flow_scheduled_content_validate';
$form['#submit'][] = 'state_flow_schedule_content';
$form['event_comment']['#default_value'] = ucfirst($event_name);
}
}
}
/**
* Helper to determine user_access by event and permission
*
* @param string $event
* @param string $permission
* @return bool
*/
function state_flow_schedule_guard_permission($event, $permission) {
// If the user has the global permission, then return TRUE
if (user_access($permission)) {
return TRUE;
}
// Otherwise, return FALSE
return FALSE;
}
/**
* Validate callback when scheduling content to be published
*
* @param array $form
* @param array $form_state
*/
function state_flow_scheduled_content_validate($form, &$form_state) {
$event = $form_state['values']['event'];
$selected_date = $form_state['values']['selected-date'];
$current_time = time();
if (strtotime($selected_date) < $current_time && $event == 'schedule') {
form_set_error('selected-date', 'You must select a date in the future.');
}
}
/**
* Submit callback when scheduling content to be published
*
* @param array $form
* @param array $form_state
*/
function state_flow_schedule_content($form, &$form_state) {
global $user;
$values = $form_state['values'];
$selected_date = strtotime($values['selected-date']);
$event_name = $values['event-name'];
$nid = $values['nid'];
$vid = $values['vid'];
state_flow_schedule_schedule($nid, $vid, $selected_date);
//set message
$node = node_load($nid);
$message = t('This content has been scheduled to be published on !date', array(
'!date' => $values['selected-date'],
));
$state_flow = state_flow_load_state_machine($node);
$state_flow
->write_history($user->uid, $message);
}
/**
* Add nodes to be published to schedule table
*
* @param int $nid
* @param int $vid
* @param int $selected_date
*/
function state_flow_schedule_schedule($nid, $vid, $selected_date) {
$data = new stdClass();
$data->nid = $nid;
$data->vid = $vid;
$data->date = $selected_date;
drupal_write_record('state_flow_schedule', $data);
}
/**
* Implements hook_cron().
*/
function state_flow_schedule_cron() {
$results = state_flow_schedule_get_scheduled_nodes(NULL, NULL, time());
$queue = DrupalQueue::get('state_flow_schedule');
foreach ($results as $result) {
$queue
->createItem($result);
}
}
/*
* Implements hook_cron_queue_info().
*/
function state_flow_schedule_cron_queue_info() {
$queues['state_flow_schedule'] = array(
'worker callback' => 'state_flow_schedule_process_item',
'time' => 60,
);
return $queues;
}
/**
* Process scheduled nodes to fire publish event
*
* @param object $data
*/
function state_flow_schedule_process_item($data) {
global $user;
$user = user_load(1);
// Check to make sure nid and vid are valid.
$nid = isset($data->nid) ? $data->nid : 0;
$vid = isset($data->vid) ? $data->vid : 0;
if ($nid && $vid) {
if ($node = node_load($nid, $vid)) {
drupal_set_message('Attempting to publish nid:' . $node->nid . ', vid:' . $node->vid);
$state_flow = state_flow_load_state_machine($node, TRUE);
$state_flow
->fire_event('publish', $user->uid, 'Published on cron.');
}
}
}
/**
* Return all nodes scheduled to be published
*
* @param int $nid
* @param int $vid
* @param int $date
* @return object
*/
function state_flow_schedule_get_scheduled_nodes($nid = NULL, $vid = NULL, $date = NULL) {
if (isset($nid) && isset($vid)) {
$result = db_query('
SELECT *
FROM state_flow_schedule
WHERE nid = :nid
AND vid = :vid', array(
':nid' => $nid,
':vid' => $vid,
))
->fetchAll();
}
elseif (isset($date)) {
$result = db_query('
SELECT *
FROM state_flow_schedule
WHERE date <= :date', array(
':date' => $date,
))
->fetchAll();
}
else {
$result = db_query('
SELECT *
FROM state_flow_schedule')
->fetchAll();
}
return $result;
}
/**
* Implements hook_node_delete()
*/
function state_flow_schedule_node_delete($node) {
db_delete('state_flow_schedule')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_node_revision_delete
*/
function state_flow_schedule_node_revision_delete($node) {
db_delete('state_flow_schedule')
->condition('nid', $node->nid)
->condition('vid', $node->vid)
->execute();
}
Functions
Name![]() |
Description |
---|---|
state_flow_scheduled_content_validate | Validate callback when scheduling content to be published |
state_flow_schedule_content | Submit callback when scheduling content to be published |
state_flow_schedule_cron | Implements hook_cron(). |
state_flow_schedule_cron_queue_info | |
state_flow_schedule_form_state_flow_events_revision_alter | Implements hook_form_FORM_ID_alter(). |
state_flow_schedule_get_scheduled_nodes | Return all nodes scheduled to be published |
state_flow_schedule_guard_permission | Helper to determine user_access by event and permission |
state_flow_schedule_node_delete | Implements hook_node_delete() |
state_flow_schedule_node_revision_delete | Implements hook_node_revision_delete |
state_flow_schedule_permission | Implements hook_permission(). |
state_flow_schedule_process_item | Process scheduled nodes to fire publish event |
state_flow_schedule_schedule | Add nodes to be published to schedule table |
state_flow_schedule_views_api | Implements hook_views_api(). |