You are here

function node_revision_delete_admin_settings_form in Node Revision Delete 7.3

Form constructor for Node Revision Delete administration form.

Parameters

array $form: The form element.

array $form_state: The form state.

Return value

array The form definition

Throws

\Exception

1 string reference to 'node_revision_delete_admin_settings_form'
node_revision_delete_menu in ./node_revision_delete.module
Implements hook_menu().

File

./node_revision_delete.admin.inc, line 22

Code

function node_revision_delete_admin_settings_form(array $form, array &$form_state) {

  // Table header.
  $header = array(
    t('Content type'),
    t('Machine name'),
    t('Minimum to keep'),
    t('Minimum age'),
    t('When to delete'),
    t('Candidate nodes'),
    t('Candidate revisions'),
    t('Operations'),
  );

  // Table rows.
  $rows = array();

  // Getting the config variables.
  $node_revision_delete_track = variable_get('node_revision_delete_track', array());

  // Looking for all the content types.
  $content_types = node_type_get_names();

  // Check if there are candidate revisions to delete.
  $exists_candidates = FALSE;

  // Return to the same page after save the content type.
  $destination = 'admin/config/content/node_revision_delete';
  foreach ($content_types as $content_type => $content_type_name) {

    // Operations dropbutton.
    $operations = array(
      'edit' => array(
        'title' => t('Edit'),
        'href' => 'admin/structure/types/manage/' . $content_type,
        'query' => array(
          'destination' => $destination,
        ),
      ),
    );
    if (isset($node_revision_delete_track[$content_type])) {

      // Minimum revisions to keep in the database.
      $minimum_revisions_to_keep = $node_revision_delete_track[$content_type]['minimum_revisions_to_keep'];

      // Minimum age to delete (is a number, 0 for none).
      $minimum_age_to_delete_number = $node_revision_delete_track[$content_type]['minimum_age_to_delete'];
      $minimum_age_to_delete = (bool) $minimum_age_to_delete_number ? _node_revision_delete_time_string('minimum_age_to_delete', $minimum_age_to_delete_number) : t('None');

      // When to delete time (is a number, 0 for always).
      $when_to_delete_number = $node_revision_delete_track[$content_type]['when_to_delete'];
      $when_to_delete = (bool) $when_to_delete_number ? _node_revision_delete_time_string('when_to_delete', $when_to_delete_number) : t('Always delete');

      // Number of candidates nodes to delete theirs revision.
      $candidates = _node_revision_delete_candidates($content_type, $minimum_revisions_to_keep, $minimum_age_to_delete_number, $when_to_delete_number);
      $candidate_nodes = count(array_unique(array_column($candidates, 'nid')));
      $candidate_revisions = count($candidates);

      // Formatting the numbers.
      $candidate_nodes = number_format($candidate_nodes, 0, '.', '.');
      $candidate_revisions = number_format($candidate_revisions, 0, '.', '.');

      // If we have candidates nodes then we will allow to run the batch job.
      if ($candidate_revisions && !$exists_candidates) {
        $exists_candidates = TRUE;
      }

      // Action to delete the configuration for the content type.
      $operations['untrack'] = array(
        'title' => t('Untrack'),
        'href' => '/admin/config/content/node_revision_delete/delete/' . $content_type,
        'query' => array(
          'destination' => $destination,
        ),
      );
    }
    else {
      $minimum_revisions_to_keep = t('Untracked');
      $candidate_nodes = t('Untracked');
      $candidate_revisions = t('Untracked');
      $when_to_delete = t('Untracked');
      $minimum_age_to_delete = t('Untracked');
    }

    // We have ctools?
    if (module_exists('ctools')) {

      // Hack to get consistent style with views ctools dropbutton.
      if (module_exists('views_ui')) {
        if (module_load_include('inc', 'views_ui', 'includes/admin')) {
          foreach (views_ui_get_admin_css() as $file => $options) {
            drupal_add_css($file, $options);
          }
        }
      }

      // Rendering the dropbutton.
      $links = array(
        'links' => $operations,
        'attributes' => array(
          'class' => array(
            'links',
          ),
        ),
      );
      $dropbutton = '<div class="admin-operations">' . theme('links__ctools_dropbutton', $links) . '</div>';
    }
    else {
      $operations_list = array();
      foreach ($operations as $key => $value) {
        $link = l($operations[$key]['title'], $operations[$key]['href'], array(
          'query' => $operations[$key]['query'],
        ));
        $operations_list[$key] = array(
          'data' => $link,
        );
      }
      $links = array(
        'items' => $operations_list,
        'attributes' => array(
          'class' => array(
            'links',
            'inline',
          ),
        ),
      );
      $dropbutton = '<div class="admin-operations">' . theme('item_list', $links) . '</div>';
    }

    // Setting the row values.
    $rows[] = array(
      $content_type_name,
      $content_type,
      $minimum_revisions_to_keep,
      $minimum_age_to_delete,
      $when_to_delete,
      $candidate_nodes,
      $candidate_revisions,
      array(
        'data' => '<div class="links">' . $dropbutton . '</div>',
        'class' => array(
          'links',
        ),
      ),
    );
  }

  // Table with current configuration.
  $form['current_configuration'] = array(
    '#theme' => 'table',
    '#caption' => t('Current configuration'),
    '#header' => $header,
    '#rows' => $rows,
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'node_revision_delete') . '/css/node_revision_delete.css',
      ),
    ),
  );

  // Configuration for node_revision_delete_cron variable.
  $form['node_revision_delete_cron'] = array(
    '#type' => 'textfield',
    '#title' => t('How many revisions do you want to delete per cron run?'),
    '#description' => t('Deleting node revisions is a database intensive task. Increase this value if you think that the server can handle more deletions per cron run.'),
    '#default_value' => variable_get('node_revision_delete_cron'),
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#size' => 10,
    '#maxlength' => 5,
  );

  // Available options for node_revision_delete_time variable.
  $options_node_revision_delete_time = _node_revision_delete_time_value();
  $form['node_revision_delete_time'] = array(
    '#type' => 'select',
    '#title' => t('How often should revision be deleted while cron runs?'),
    '#description' => t('Frequency of the scheduled mass revision deletion.'),
    '#options' => $options_node_revision_delete_time,
    '#default_value' => variable_get('node_revision_delete_time'),
  );

  // Time options.
  $allowed_time = array(
    'days' => t('Days'),
    'weeks' => t('Weeks'),
    'months' => t('Months'),
  );

  // Configuration for the node_revision_delete_minimum_age_to_delete_time
  // variable.
  $form['minimum_age_to_delete'] = array(
    '#type' => 'fieldset',
    '#title' => t('Minimum age of revision to delete configuration'),
  );
  $node_revision_delete_minimum_age_to_delete_time = variable_get('node_revision_delete_minimum_age_to_delete_time');
  $form['minimum_age_to_delete']['node_revision_delete_minimum_age_to_delete_time_max_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number allowed'),
    '#description' => t('The maximum number in the "Minimum age of revision to delete" configuration in each content type edit page. If you change this number and the new value is smaller than the value defined for a content type in the "Minimum age of revision to delete" setting, the "Minimum age of revision to delete" setting for that content type will take it.'),
    '#default_value' => $node_revision_delete_minimum_age_to_delete_time['max_number'],
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#size' => 8,
    '#maxlength' => 5,
  );
  $form['minimum_age_to_delete']['node_revision_delete_minimum_age_to_delete_time_time'] = array(
    '#type' => 'select',
    '#title' => t('The time value'),
    '#description' => t('The time value allowed in the "Minimum age of revision to delete" configuration in each content type edit page. If you change this value all the configured content types will take it.'),
    '#options' => $allowed_time,
    '#size' => 1,
    '#default_value' => $node_revision_delete_minimum_age_to_delete_time['time'],
  );

  // Configuration for the node_revision_delete_when_to_delete_time variable.
  $form['when_to_delete'] = array(
    '#type' => 'fieldset',
    '#title' => t('When to delete configuration'),
  );
  $node_revision_delete_when_to_delete_time = variable_get('node_revision_delete_when_to_delete_time');
  $form['when_to_delete']['node_revision_delete_when_to_delete_time_max_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number allowed'),
    '#description' => t('The maximum number allowed in the "When to delete" configuration in each content type edit page. If you change this number and the new value is smaller than the value defined for a content type in the "When to delete" setting, the "When to delete" setting for that content type will take it.'),
    '#default_value' => $node_revision_delete_when_to_delete_time['max_number'],
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#size' => 8,
    '#maxlength' => 5,
  );
  $form['when_to_delete']['node_revision_delete_when_to_delete_time_time'] = array(
    '#type' => 'select',
    '#title' => t('The time value'),
    '#description' => t('The time value allowed in the "When to delete" configuration in each content type edit page. If you change this value all the configured content types will take it.'),
    '#options' => $allowed_time,
    '#size' => 1,
    '#default_value' => $node_revision_delete_when_to_delete_time['time'],
  );

  // Providing the option to run now the batch job.
  if ($exists_candidates) {
    $disabled = FALSE;
    $description = t('This will start a batch job to delete old revisions for tracked content types.');
  }
  else {
    $disabled = TRUE;
    $description = t('There not exists candidates nodes with revisions to delete.');
  }
  $form['run_now'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete revisions now'),
    '#description' => $description,
    '#disabled' => $disabled,
  );
  $form['dry_run'] = array(
    '#type' => 'checkbox',
    '#title' => t('Dry run'),
    '#description' => t('Test run without deleting revisions but seeing the output results.'),
    '#states' => array(
      // Hide the dry run option when the run now checkbox is disabled.
      'visible' => array(
        ':input[name="run_now"]' => array(
          'checked' => TRUE,
        ),
      ),
      'unchecked' => array(
        ':input[name="run_now"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  // Adding the donation text.
  $form['#prefix'] = _node_revision_delete_get_donation_text();
  return $form;
}