aboutsummaryrefslogtreecommitdiff
path: root/node_import_truncate.module
blob: 058523673820cee98e54b41ce4b39b07bed38984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?
// $Id$

/**
 * Implementation of hook_menu();
 */
function node_import_truncate_menu() {
  $items['admin/content/node_import/truncate'] = array(
    'title'            => 'Node Import Truncate',
    'description'      => 'Mass deletion page.',
    'page callback'    => 'node_import_truncate_page',
    'access arguments' => array('administer nodes'),
    'type'             => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback.
 */
function node_import_truncate_page($taskid = null) {
  $output  = t('Choose whether to delete older content.');
  $output .= drupal_get_form('node_import_truncate_form', $taskid);
  return $output;
}

/**
 * Mass deletion form.
 */
function node_import_truncate_form(&$form_state, $taskid = null) {
  $form['taskid'] = array(
    '#type'  => 'value',
    '#value' => $taskid,
  );
  $form['mass_delete'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Pre-import mass deletion'),
    '#description'   => t('Delete old content before importing new data.'),
    '#default_value' => variable_get('node_import_truncate', '0'),
  );
  $form['submit'] = array(
    '#type'  => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Implementation of hook_validate();
 */
function node_import_truncate_form_validate($form, $form_state) {
  if (!isset($form_state['values']['mass_delete'])) {
    form_set_error('mass_delete', t('Please choose an option'));
  }
}

/**
 * Handle post-validation form-submission.
 */
function node_import_truncate_form_submit($form, &$form_state) {
  $mass_delete = $form_state['values']['mass_delete'];
  variable_set('node_import_truncate', $mass_delete);

  if (isset($form_state['values']['taskid']['#value'])) {
    $taskid   = $form_state['values']['taskid']['#value'];
    $redirect = 'admin/content/node_import/' . $taskid;
  } else {
    $redirect = null;
  }
    
  if ($mass_delete == 1) {

    $task  = node_import_load($taskid);
    $type  = ereg_replace('.*:', '', $task['type']);
    $batch = array(
        'title'      => t('Deleting old data.'),
        'operations' => array(
          array('node_import_truncate', array($type)),
          ),
        'finished'   => 'node_import_truncate_callback',
        );

    batch_set($batch);
    batch_process($redirect);
  } else {
    drupal_goto($redirect);
  }
}

/**
 * Implementation of hook_node_import_task();
 *
 * As the user may be adding a spreadsheet containing
 * old and new content, we need to check whether to remove
 * all previous content to prevent duplicate data in the
 * database.
 */
function node_import_truncate_node_import_task($task, $op) {
  if ($op == 'insert') {
    drupal_goto('admin/content/node_import/truncate/' . $task['taskid']);
  }
}

/**
 * Mass deletion of content.
 */
function node_import_truncate($type, &$context) {
  $type = 'WHERE type = "' . $type . '"';
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress']     = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max']          = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node} ' . $type));
  }
  $limit = 5;
  $result = db_query_range('SELECT nid FROM {node} ' . $type, $context['sandbox']['current_node'], 0, $limit);
  while ($row = db_fetch_array($result)) {
    $node = node_delete($row['nid'], NULL, TRUE);
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $node->nid;
    $context['results'][]               = $node->nid .' : '. $node->title;
    $context['message']                 = $node->title;
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

/**
 * Callback for node_import_truncate();
 */
function node_import_truncate_callback($success, $results, $operations) {
  if ($success) {
    if ($results > 0) {
      $message = format_plural(count($results), t('One old post processed.'), t('@count old posts processed.'));
    }
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
}