You are here

node_recur.module in Node recur 7.2

Same filename and directory in other branches
  1. 7 node_recur.module

File

node_recur.module
View source
<?php

/**
 * Implements hook_menu().
 */
function node_recur_menu() {
  $items = array();
  $items['node/%node/recur'] = array(
    'title' => 'Repeat',
    'title callback' => 'node_recur_menu_title_callback',
    'title arguments' => array(
      1,
    ),
    'description' => 'Set recurring rules on this node',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_recur_node_recur_form',
      1,
    ),
    'access callback' => 'node_recur_node_recur_form_access',
    'access arguments' => array(
      1,
    ),
    'file' => 'node_recur.pages.inc',
    'type' => MENU_LOCAL_ACTION,
    'weight' => 1,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function node_recur_permission() {
  return array(
    'recur own nodes' => array(
      'title' => t('Recur own nodes'),
      'description' => t('Can recur nodes that the user is an author of.'),
    ),
    'recur all nodes' => array(
      'title' => t('Recur all nodes'),
      'description' => t('Can recur all published nodes.'),
    ),
  );
}

/**
 * Implements hook_form_node_type_form_alter().
 */
function node_recur_form_node_type_form_alter(&$form, &$form_state) {
  $type = $form['#node_type']->type;

  // Determine the available date fields on this node type
  $fields = array();
  $instances = field_info_instances();
  if (isset($instances['node'][$type])) {
    foreach ($instances['node'][$type] as $name => $field) {
      if ($field['widget']['module'] == 'date') {
        $fields[$name] = $field['label'] . ' (' . $name . ')';
      }
    }
  }
  $form['node_recur'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node recurring'),
    '#group' => 'additional_settings',
  );
  if (!empty($fields)) {
    $form['node_recur']['node_recur_enabled_node_type'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable recurring for this node type'),
      '#default_value' => node_recur_recurring_enabled($type) ? 1 : 0,
      '#description' => t('If checked, users with permission can create recurring copies of these nodes.'),
    );
    $form['node_recur']['node_recur_date_field_node_type'] = array(
      '#type' => 'select',
      '#title' => t('Date field'),
      '#options' => $fields,
      '#default_value' => node_recur_get_date_field_name($type),
      '#description' => t('Select the date field that will be used to base the recurrences on.'),
    );
  }
  else {
    $form['node_recur']['node_recur_null'] = array(
      '#markup' => t('There are no date fields available for this node type.'),
    );
  }
}

/**
 * Access handler for the node recur form
 */
function node_recur_node_recur_form_access($node) {
  global $user;
  $access = FALSE;

  // See if recurring is enabled for this node and we have a valid date field
  if (node_recur_recurring_enabled($node->type) && node_recur_get_date_field_name($node->type)) {

    // Check permissions
    $recur_access = user_access('recur all nodes') || user_access('recur own nodes') && $node->uid == $user->uid;
    if ($recur_access && node_access('create', $node->type)) {

      // Granted
      $access = TRUE;
    }

    // Allow modules to alter this
    drupal_alter('node_recur_access', $access, $node);
  }
  return $access;
}

/**
 * Determine if recurring is enabled for a given node type
 *
 * @param $type
 *   The node type
 * @return
 *   TRUE if recurring is enabled, otherwise FALSE
 */
function node_recur_recurring_enabled($type) {
  return variable_get("node_recur_enabled_node_type_{$type}", FALSE);
}

/**
 * Determine a node type's recurring date field name
 *
 * @param $type
 *   A node type
 * @return
 *   The node's date field name, otherwise NULL
 */
function node_recur_get_date_field_name($type) {
  if ($field_name = variable_get("node_recur_date_field_node_type_{$type}", FALSE)) {

    // Check that the field still exists
    if (field_info_field($field_name)) {
      return $field_name;
    }
  }
  return NULL;
}

/**
 * Title callback for the recur form menu item
 */
function node_recur_menu_title_callback($node) {
  return t('Repeat this !type', array(
    '!type' => strtolower(node_type_get_name($node)),
  ));
}

/**
 * Start the batch to generate recurring nodes
 */
function node_recur_node_batch_start($node, $dates) {

  // Generate the batch operations
  $operations = array();
  if (!empty($dates)) {
    foreach ($dates as $date) {
      $operations[] = array(
        'node_recur_node_batch_create_node',
        array(
          $node,
          $date,
        ),
      );
    }

    // Generate the batch
    $batch = array(
      'title' => t('Generating items'),
      'operations' => $operations,
      'finished' => 'node_recur_node_batch_finished',
      'file' => drupal_get_path('module', 'node_recur') . '/node_recur.batch.inc',
    );
    batch_set($batch);
    batch_process("node/{$node->nid}");
  }
}

/**
 * Create the recurring nodes
 */
function node_recur_node_batch_create_node($node, $date, &$context) {
  $field_name = node_recur_get_date_field_name($node->type);
  $view = field_view_value('node', $node, $field_name, $date);

  // Set the progress message
  $context['message'] = t('Date: !date', array(
    '!date' => $view['#markup'],
  ));

  // Clone the node
  $clone = node_load($node->nid, $node->vid, TRUE);

  // Inject the new dates
  unset($clone->{$field_name}[LANGUAGE_NONE]);
  $clone->{$field_name}[LANGUAGE_NONE][] = $date;

  // Store the start dates
  $context['results'][] = $date;

  // Save the new node
  if (module_exists('clone')) {
    $context = [
      'method' => 'node_recur',
      'prefix_title' => FALSE,
      'original_node' => $node,
    ];
    clone_action_clone($clone, $context);
  }
  else {
    $clone->clone_from_original_nid = $node->nid;
    unset($clone->nid);
    unset($clone->created);
    unset($clone->changed);
    unset($clone->vid);
    node_save($clone);
  }
}

/**
 * Batch ending
 */
function node_recur_node_batch_finished($success, $results, $operations) {
  if ($success) {
    $count = count($results);
    drupal_set_message(t('%count %items have been generated.', array(
      '%count' => $count,
      '%items' => format_plural($count, 'item', 'items'),
    )));
  }
  else {
    drupal_set_message(t('An error occurred. Please try again.'), 'error');
  }
}

Functions

Namesort descending Description
node_recur_form_node_type_form_alter Implements hook_form_node_type_form_alter().
node_recur_get_date_field_name Determine a node type's recurring date field name
node_recur_menu Implements hook_menu().
node_recur_menu_title_callback Title callback for the recur form menu item
node_recur_node_batch_create_node Create the recurring nodes
node_recur_node_batch_finished Batch ending
node_recur_node_batch_start Start the batch to generate recurring nodes
node_recur_node_recur_form_access Access handler for the node recur form
node_recur_permission Implements hook_permission().
node_recur_recurring_enabled Determine if recurring is enabled for a given node type