Skip to content
Snippets Groups Projects
Commit 85116c0c authored by Croitor Alexandru's avatar Croitor Alexandru
Browse files

Added bulk convert support. Fixed conversion bug with discarded fields. Fixed some code.

parent 65e3f0ab
No related branches found
No related tags found
No related merge requests found
......@@ -27,13 +27,25 @@ function node_convert_menu($may_cache) {
if ($may_cache) {
}
else {
$items[] = array(
'path' => 'admin/content/convert_bulk',
'title' => t('Convert nodes'),
'description' => t('Convert selected nodes from one node type, to another.'),
'callback' => 'drupal_get_form',
'callback arguments' => array("node_convert_bulk"),
'access' => user_access('administer content types'),
'type' => MENU_NORMAL_ITEM,
);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$items[] = array('path' => 'node/'. arg(1) .'/convert', 'title' => t('Convert'),
'callback' => 'node_convert_form_page',
'callback arguments' => array(arg(1)),
'access' => user_access('administer content types'),
'weight' => 6,
'type' => MENU_LOCAL_TASK);
$items[] = array(
'path' => 'node/'. arg(1) .'/convert',
'title' => t('Convert'),
'callback' => 'node_convert_form_page',
'callback arguments' => array(arg(1)),
'access' => user_access('administer content types'),
'weight' => 6,
'type' => MENU_LOCAL_TASK,
);
}
}
return $items;
......@@ -81,7 +93,7 @@ function node_convert_conversion_form($nid, $form_values = NULL) {
if (count($source_fields) == 0) {
// In case there are no cck fields, just convert the node type
$form['no_fields_flag'] = array('#type' => 'hidden', '#value' => 'true');
$form['no_fields'] = array('#type' => 'markup', '#value' => t("There are no cck fields to convert."));
$form['no_fields'] = array('#type' => 'markup', '#value' => t("There are no cck fields to convert. Please press submit."));
}
else { // Otherwise
foreach ($source_fields as $field) {
......@@ -146,7 +158,6 @@ function node_convert_conversion_form_submit($form_id, $form_values) {
foreach ($form_values as $key => $value) {
if (preg_match("/source_field_[0-9]+?/", $key) == 1) $source_fields[] = $value; // Source fields
if (preg_match("/dest_field_[0-9]+?/", $key) == 1) $dest_fields[] = $value; // Destination fields
if (preg_match("/dest_field_type_[0-9]+?/", $key) == 1) $dest_fields_type[] = $value; // Destination field types
}
}
node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag);
......@@ -165,6 +176,156 @@ function theme_node_convert_conversion_form($form) {
return $output;
}
function node_convert_bulk($form_values = NULL) {
$form = array();
/* Setting the steps */
if (!isset($form_values['step'])) {
$op = "choose_source_dest_type";
}
elseif ($form_values['step'] == "choose_source_dest_type") {
$op = "choose_nodes";
}
elseif ($form_values['step'] == "choose_nodes") {
$op = "choose_fields";
}
$form['step'] = array(
'#type' => 'hidden',
'#value' => $op,
);
if ($op == "choose_source_dest_type") {
$types = array_keys(content_types()); // Get available content types
foreach ($types as $value) $options[$value] = $value;
$form['source_type'] = array(
'#type' => 'select',
'#title' => t("Source type"),
'#options' => $options,
);
$form['dest_type'] = array(
'#type' => 'select',
'#title' => t("Destination type"),
'#options' => $options,
);
}
elseif ($op == "choose_nodes") {
$info = array('source_type' => $form_values['source_type'], 'dest_type' => $form_values['dest_type']);
$result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE type = '%s'", $info['source_type']);
while ($node = db_fetch_object($result)) {
$nodes[$node->nid] = '';
$form['title'][$node->nid] = array('#value' => $node->title);
$form['type'][$node->nid] = array('#value' => check_plain(node_get_types('name', $node)));
$form['author'][$node->nid] = array('#value' => theme('username', $node));
$form['status'][$node->nid] = array('#value' => ($node->status ? t('published') : t('not published')));
}
$form['nodes'] = array('#type' => 'checkboxes', '#options' => $nodes);
$info = serialize($info);
$form['info'] = array('#type' => 'hidden', '#value' => $info);
}
elseif ($op == "choose_fields") {
$info = unserialize($form_values['info']);
$nodes = $form_values['nodes'];
$info['nodes'] = $nodes;
$source_fields = content_types($info['source_type']);
$source_fields = $source_fields['fields']; // Get the cck fields of the source type
if (count($source_fields) == 0) {
// In case there are no cck fields, just convert the node type
$no_fields = true;
$info['no_fields'] = $no_fields;
}
else foreach ($source_fields as $field) {
$i++;
$options = array();
$dest_fields = content_types($info['dest_type']);
$dest_fields = $dest_fields['fields']; // Get the destination type fields
$options['discard'] = 'discard';
// Populate only the destination type fields into the select that are of the same type (cck type and multiple property)
foreach ($dest_fields as $value) if ($field['type'] == $value['type'] && $field['multiple'] == $value['multiple']) {
$options[$value['field_name']] = $value['field_name'];
}
$info['fields']['source'][] = $field['field_name']; // Remember the source fields to be converted
// The select populated with possible destination cck fields for each source field
$form['dest_field_'. $i] = array('#type' => 'select', '#options' => $options, '#title' => $field['field_name'] ." ". t("should be inserted into"));
}
$info = serialize($info);
$form['info'] = array('#type' => 'hidden', '#value' => $info);
}
$form['#multistep'] = TRUE;
$form['#redirect'] = FALSE;
$form['submit'] = array('#type' => 'submit', '#value' => t("Submit"));
return $form;
}
function node_convert_bulk_validate($form_id, $form_values) {
if ($form_values['step'] == "choose_source_dest_type") {
if ($form_values['source_type'] == $form_values['dest_type']) {
form_set_error('source_type', t('Please select different node types.'));
form_set_error('dest_type', t('Please select different node types.'));
}
}
elseif ($form_values['step'] == "choose_nodes") {
$no_nodes = TRUE;
foreach ($form_values['nodes'] as $value) {
if ($value != 0) {
$no_nodes = FALSE;
break;
}
}
if ($no_nodes == TRUE) {
form_set_error('', t('Please select at least one node.'));
}
}
}
function node_convert_bulk_submit($form_id, $form_values) {
if ($form_values['step'] == "choose_fields") {
$info = unserialize($form_values['info']);
if ($info['no_fields'] == false) { // If there are cck fields that can to be converted
foreach ($form_values as $key => $value) {
if (preg_match("/dest_field_[0-9]+?/", $key) == 1) $info['fields']['destination'][] = $value; // Destination fields
}
}
foreach ($info['nodes'] as $nid) {
node_convert_node_convert($nid, $info['dest_type'], $info['fields']['source'], $info['fields']['destination'], $info['no_fields']);
}
drupal_set_message("Nodes converted succesufuly.");
drupal_goto("admin/content/node");
}
}
function theme_node_convert_bulk($form) {
if ($form['step']['#value'] == "choose_source_dest_type") {
$output = '<div>'. t("Choose the source type of the nodes that should be shown, and the destination type to which they will be converted.") .'</div>';
$output .= drupal_render($form);
}
elseif ($form['step']['#value'] == "choose_nodes") {
$header = array(theme('table_select_header_cell'), t("Title"), t("Type"), t("Author"), t("Status"));
foreach (element_children($form['title']) as $key) {
$row = array();
$row[] = drupal_render($form['nodes'][$key]);
$row[] = drupal_render($form['title'][$key]);
$row[] = drupal_render($form['type'][$key]);
$row[] = drupal_render($form['author'][$key]);
$row[] = drupal_render($form['status'][$key]);
$rows[] = $row;
}
$output = '<div>'. t("Choose the nodes that will be converted.") .'</div>';
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
}
elseif ($form['step']['#value'] == "choose_fields") {
$output = '<div>'. t("Choose the fields where the values should be stored.") .'</div>';
$info = unserialize($form['info']['#value']);
if ($info['no_fields'] == TRUE) $output .= '<div>'. t("There are no cck fields to convert. Please press submit.") .'</div>';
$output .= drupal_render($form);
}
return $output;
}
/**
* Converts a node to another content type.
*
......@@ -211,10 +372,10 @@ function node_convert_field_convert($nid, $source_field, $dest_field) {
$db_info_source = content_database_info($field_info_source); // Get DB specific source field information
// If the source field has a separate table, we will have to delete the node info in it
if (strpos($db_info_source['table'], "content_field_") !== false) $db_source_content_field = true; else $db_source_content_field = false;
if ($dest_fields[$key] == "discard") { // If the source field value should be discarded
if ($dest_field == "discard") { // If the source field value should be discarded
// Delete node info in the separate field table
if ($db_source_content_field == true) db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
continue;
return;
}
$field_info_dest = content_fields($dest_field); // Get destination field information
$db_info_dest = content_database_info($field_info_dest); // Get DB specific destination field information
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment