You are here

function scheduler_node_validate in Scheduler 7

Implements hook_node_validate().

File

./scheduler.module, line 495
Scheduler publishes and unpublishes nodes on dates specified by the user.

Code

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."));
    }
  }
}