diff options
author | Silvio <silvio@devlet.com.br> | 2010-10-08 16:15:19 -0300 |
---|---|---|
committer | Silvio <silvio@devlet.com.br> | 2010-10-08 16:15:19 -0300 |
commit | d005ff67b39bcfddd982a61a97311b969a287650 (patch) | |
tree | fefa5afcfb7d9f5756806e333c3ee336ec5e8fa5 | |
parent | 50d19224d34e2d1da0dae3093bcdd21f1d71aff2 (diff) | |
download | node_truncate-d005ff67b39bcfddd982a61a97311b969a287650.tar.gz node_truncate-d005ff67b39bcfddd982a61a97311b969a287650.tar.bz2 |
Forking module into 'node_truncate'
-rw-r--r-- | README.txt | 13 | ||||
-rw-r--r-- | node_import_truncate.module | 149 | ||||
-rw-r--r-- | node_truncate.info (renamed from node_import_truncate.info) | 1 | ||||
-rw-r--r-- | node_truncate.module | 127 |
4 files changed, 130 insertions, 160 deletions
@@ -1,11 +1,4 @@ -Node Import Truncate -==================== +Node Truncate +============= -This module adds a final step at the Node Import form so users -can choose whether to erase all nodes of a given content type -before importing new data into it. - -It might be useful in cases like users periodically importing -a CSV spreadsheet with new as well old content: this module can -erase all previous imported data to avoid duplicate content in -the database. +Use this module to delete all posts from a given content type. diff --git a/node_import_truncate.module b/node_import_truncate.module deleted file mode 100644 index 9a88fb1..0000000 --- a/node_import_truncate.module +++ /dev/null @@ -1,149 +0,0 @@ -<?php -// $Id$ - -/** - * @file - * Node Import Truncate module. - * - * This module adds a final step at the Node Import form so users - * can choose whether to erase all nodes of a given content type - * before importing new data into it. - */ - -/** - * 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'])) { - $taskid = $form_state['values']['taskid']; - $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); -} diff --git a/node_import_truncate.info b/node_truncate.info index fa3dc9e..ee647bd 100644 --- a/node_import_truncate.info +++ b/node_truncate.info @@ -3,5 +3,4 @@ name = Node Import Truncate description = Allow to truncate a content type before importing data. package = Development core = 6.x -dependencies[] = node_import version = "6.x-0.1" diff --git a/node_truncate.module b/node_truncate.module new file mode 100644 index 0000000..b5fe7e4 --- /dev/null +++ b/node_truncate.module @@ -0,0 +1,127 @@ +<?php +// $Id$ + +/** + * @file + * Node Truncate module. + * + * Use this module to delete all posts from a given content type. + */ + +/** + * Implementation of hook_menu(); + */ +function node_truncate_menu() { + $items['admin/content/node_truncate'] = array( + 'title' => 'Node Truncate', + 'description' => 'Mass deletion page.', + 'page callback' => 'node_truncate_page', + 'access arguments' => array('administer nodes'), + 'type' => MENU_NORMAL_ITEM, + ); + return $items; +} + +/** + * Menu callback. + */ +function node_truncate_page() { + $output = t('Choose a content type to truncate.'); + $output .= drupal_get_form('node_truncate_form'); + return $output; +} + +/** + * Mass deletion form. + */ +function node_truncate_form(&$form_state) { + $node_types = node_get_types(); + $node_codes = array(); + + foreach ($node_types as $item) { + $node_codes[$item->type] = $item->name; + } + + $form['node_types'] = array( + '#type' => 'checkboxes', + '#title' => t('Node types'), + '#options' => $node_codes, + '#default_value' => variable_get('node_truncate_nodetypes', array()), + '#description' => t('Select nodetypes which should be truncated.'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; +} + +/** + * Implementation of hook_validate(); + */ +function node_truncate_form_validate($form, $form_state) { + if (!isset($form_state['values']['node_types'])) { + form_set_error('mass_delete', t('Please choose at least one node type.')); + } +} + +/** + * Handle post-validation form-submission. + */ +function node_truncate_form_submit($form, &$form_state) { + $operations = array(); + + foreach(array_filter($form_state['values']['node_types']) as $type) { + $operations[] = array('node_truncate', array($type)); + } + + $batch = array( + 'title' => t('Deleting old data.'), + 'operations' => $operations, + 'finished' => 'node_truncate_callback', + ); + + batch_set($batch); + batch_process($redirect); +} + +/** + * Mass deletion of content. + */ +function node_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_truncate(); + */ +function node_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); +} |