You are here

function revisioning_scheduler_form_node_form_alter in Revisioning 7

Same name and namespace in other branches
  1. 8 revisioning_scheduler/revisioning_scheduler.module \revisioning_scheduler_form_node_form_alter()

Implements hook_form_BASEFORMID_alter().

This function adds a publication date & time field to the Edit form. It also loads a small javascript file, which controls visibility of the field in response to clicks on the revision moderation radio-buttons.

File

revisioning_scheduler/revisioning_scheduler.module, line 72
Allows revisions to be published at specified dates and times.

Code

function revisioning_scheduler_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['#node_edit_form']) && variable_get('revisioning_scheduler_on_edit_form', TRUE)) {
    $node = $form_state['node'];
    if (!user_access('administer nodes')) {

      // If the node is already published then scheduling a publication date is
      // inappropriate, unless the new revision goes into moderation.
      // The exception are users with 'administer nodes' permission, as they
      // can change the Published checkbox and moderation radio buttons, so we
      // have to deal with that client-side, see file revision-schedule.js
      if ($node->status == NODE_PUBLISHED) {
        return;
      }
      if (empty($node->revision_moderation) && !(empty($node->nid) && revisioning_content_is_moderated($node->type, $node))) {

        // No moderation specified and not a new node of a type subject to
        // moderation.
        return;
      }
      if (!revisioning_user_node_access('publish revisions', $node)) {
        return;
      }
    }

    // Don't offer the form if auto-publish is enabled for this node and user.
    if (revisioning_user_may_auto_publish($node)) {
      return;
    }
    $date_format = variable_get('revisioning_scheduler_date_format');
    if (empty($date_format)) {
      $date_format = REVISIONING_SCHEDULER_DEFAULT_DATE_FORMAT;
    }
    $description1 = t('Please use this format: %format, e.g %datetime. If you enter "now" this content will be published immediately.', array(
      '%format' => $date_format,
      '%datetime' => format_date(time(), 'custom', $date_format),
    ));
    $description2 = t('If you do not wish to schedule publication, leave the field blank.');
    if (isset($node->vid)) {
      $result = db_query('SELECT * FROM {revisioning_scheduler} WHERE revision_vid = :vid', array(
        ':vid' => $node->vid,
      ));
      $revision = $result
        ->fetchAssoc();
    }
    $scheduled_datetime = empty($revision) ? '' : format_date($revision['revision_date'], 'custom', $date_format);
    $form['revision_information']['publication_date'] = array(
      '#type' => 'textfield',
      '#size' => 25,
      '#maxlength' => 25,
      '#title' => t('Optionally schedule a date and time for publication'),
      '#description' => $description1 . ' ' . $description2,
      '#default_value' => $scheduled_datetime,
      '#weight' => 10,
      '#attributes' => array(
        'class' => array(
          'publication-date',
        ),
      ),
    );
    $form['#attached']['js'][] = drupal_get_path('module', 'revisioning_scheduler') . '/revision-schedule.js';
  }
}