Commit 1772b2ba authored by Croitor Alexandru's avatar Croitor Alexandru
Browse files

Added multiple values field support. Added many-many comments.

parent 234a50ef
......@@ -12,7 +12,7 @@ function node_convert_help($section='') {
switch ($section) {
case "admin/help#node_convert":
$output = '<p>'. t("Converts a node's type to another type, also transfers cck field values") .'</p>';
$output = '<p>'. t("Converts a node's type to another type, also transfers cck field values.") .'</p>';
break;
}
......@@ -66,34 +66,39 @@ function node_convert_convert_form($nid, $form_values = NULL) {
);
/* Form steps */
if ($op == "choose_destination_type") {
$form['current_type'] = array('#type' => 'markup', '#value' => $node->type);
$types = array_keys(content_types());
$form['current_type'] = array('#type' => 'markup', '#value' => $node->type); // Remember current node type
$types = array_keys(content_types()); // Get available content types
$key = array_search($form['current_type']['#value'], $types);
if ($key !== false) unset($types[$key]);
if ($key !== false) unset($types[$key]); // Delete the current content type from the list
foreach ($types as $value) $options[$value] = $value;
// Populate the select with possible content types
$form['destination_type'] = array('#type' => 'select', '#options' => $options, '#title' => t("To what content type should this node be converted"));
}
if ($op == "choose_destination_fields") {
$form['destination_type'] = array('#type' => 'hidden', '#value' => $form_values['destination_type']);
$form['destination_type'] = array('#type' => 'hidden', '#value' => $form_values['destination_type']); // Remember the destination type
$source_fields = content_types($node->type);
$source_fields = $source_fields['fields'];
$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
$form['no_fields_flag'] = array('#type' => 'hidden', '#value' => 'true');
$form['no_fields'] = array('#type' => 'markup', '#value' => t("There are no cck fields to convert."));
}
else {
else { // Otherwise
foreach ($source_fields as $field) {
$i++;
$options = array();
$dest_fields = content_types($form_values['destination_type']);
$dest_fields = $dest_fields['fields'];
$dest_fields = $dest_fields['fields']; // Get the destination type fields
$options['discard'] = 'discard';
foreach ($dest_fields as $value) if ($field['type'] == $value['type']) {
// 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'];
}
$form['source_field_'. $i] = array('#type' => 'hidden', '#value' => $field['field_name']);
$form['dest_field_type_'. $i] = array('#type' => 'hidden', '#value' => $field['type']);
$form['source_field_'. $i] = array('#type' => 'hidden', '#value' => $field['field_name']); // Remember the source fields to be converted
$form['dest_field_type_'. $i] = array('#type' => 'hidden', '#value' => $field['type']); // Remember the destination type fields
// 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"));
// Print the current value of the source field
if ($field['type'] == "image") {
$temp_value = $node->{$field['field_name']}[0]['title'] ." ; ". $node->{$field['field_name']}[0]['filepath'];
}
......@@ -105,8 +110,7 @@ function node_convert_convert_form($nid, $form_values = NULL) {
}
else $temp_value = $node->{$field['field_name']}[0]['value'];
if (empty($temp_value)) $temp_value = "NULL";
$form['current_field_value_' .$i] = array('#type' => 'markup', '#value' => '<div>'. t("Current value is:") ." <b>". $temp_value .'</b></div>') ;
$form['current_field_value_' .$i] = array('#type' => 'markup', '#value' => '<div>'. t("Current value is:") ." <b>". $temp_value .'</b></div>');
}
}
}
......@@ -121,30 +125,34 @@ function node_convert_convert_form($nid, $form_values = NULL) {
function node_convert_convert_form_submit($form_id, $form_values) {
if ($form_values['step'] == "choose_destination_fields") {
// Information needed in the convert process: nid, vid, source type, destination type
$dest_node_type = $form_values['destination_type'];
$nid = $form_values['nid'];
$node = node_load($nid);
$vid = $node->vid;
$source_node_type = $node->type;
if ($form_values['no_fields_flag'] == false) {
if ($form_values['no_fields_flag'] == false) { // If there are cck fields that can to be converted
foreach ($form_values as $key => $value) {
if (preg_match("/source_field_[0-9]+?/", $key) == 1) $source_fields[] = $value;
if (preg_match("/dest_field_[0-9]+?/", $key) == 1) $dest_fields[] = $value;
if (preg_match("/dest_field_type_[0-9]+?/", $key) == 1) $dest_fields_type[] = $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
}
}
db_query("UPDATE {node} SET type = '%s' WHERE nid = %d", $dest_node_type, $nid);
db_query("INSERT INTO {%s} (nid, vid) VALUES (%d, %d)", "content_type_". $dest_node_type, $nid, $vid);
if ($form_values['no_fields_flag'] == false) {
foreach ($source_fields as $key => $field) {
$db_info_source = content_database_info(content_fields($field));
db_query("UPDATE {node} SET type = '%s' WHERE nid = %d", $dest_node_type, $nid); // Change the node type in the DB
db_query("INSERT INTO {%s} (nid, vid) VALUES (%d, %d)", "content_type_". $dest_node_type, $nid, $vid); // Add the current node to the chosen content type
if ($form_values['no_fields_flag'] == false) { // If there are cck fields that can be converted
foreach ($source_fields as $key => $field) { // Conversion process for each field
$field_info_source = content_fields($field); // Get source field information
$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 ($dest_fields[$key] == "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;
}
$db_info_dest = content_database_info(content_fields($dest_fields[$key]));
$field_info_dest = content_fields($dest_fields[$key]); // Get destination field information
$db_info_dest = content_database_info($field_info_dest); // Get DB specific destination field information
if (strpos($db_info_dest['table'], "content_field_") !== false) $db_dest_content_field = true; else $db_dest_content_field = false;
$data = array();
$dest_column_names = array();
......@@ -152,11 +160,23 @@ function node_convert_convert_form_submit($form_id, $form_values) {
$column_placeholders = array();
$column_assignments = array();
foreach ($db_info_source['columns'] as $column => $attributes) {
$source_column_names[] = $attributes['column'];
$source_column_names[] = $attributes['column']; // Collect the DB columns names of the source field
}
$source_values = db_fetch_array(db_query("SELECT ". implode(", ", $source_column_names) ." FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid));
$source_values = array_values($source_values);
if ($field_info_source['multiple'] != 0) { // If the field has multiple values, we remember each value from the DB
$query = db_query("SELECT ". implode(", ", $source_column_names) .", delta FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid);
while ($db_row = db_fetch_array($query)) {
$source_values[] = array_merge(array_values($db_row), array($nid, $vid));
}
}
else { // Otherwise we just remember one row of info
$source_values = db_fetch_array(db_query("SELECT ". implode(", ", $source_column_names) ." FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid));
$source_values = array_values($source_values);
$source_values[] = $nid;
$source_values[] = $vid;
}
// After getting the source field values, we delete them in the DB
if ($db_source_content_field == true) db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
// Prepare the INSERT and UDPATE queries, for transfering the values in the destination fields
foreach ($db_info_dest['columns'] as $column => $attributes) {
$dest_column_names[] = $attributes['column'];
switch ($attributes['type']) {
......@@ -176,20 +196,23 @@ function node_convert_convert_form_submit($form_id, $form_values) {
$column_assignments[] = $attributes['column'] ." = '%s'";
}
}
if ($db_dest_content_field == true) {
$source_values[] = $nid;
$source_values[] = $vid;
$gg = db_query("INSERT INTO {". $db_info_dest['table'] ."} (". implode(", ", $dest_column_names) .", nid, vid) VALUES (". implode(', ', $column_placeholders) .", %d, %d)", $source_values);
if ($field_info_source['multiple'] != 0) { // If the field has multiple values, we put each value into the DB
foreach ($source_values as $values) {
$gg = db_query("INSERT INTO {". $db_info_dest['table'] ."} (". implode(", ", $dest_column_names) .", delta, nid, vid) VALUES (". implode(', ', $column_placeholders) .", %d, %d, %d)", $values);
}
}
else {
$source_values[] = $nid;
$source_values[] = $vid;
$gg = db_query("UPDATE {". $db_info_dest['table'] ."} SET " . implode(", ", $column_assignments) ." WHERE nid = %d AND vid = %d", $source_values);
if ($db_dest_content_field == true) { // If the field is re-used, we insert the data in the field's own table in the DB
$gg = db_query("INSERT INTO {". $db_info_dest['table'] ."} (". implode(", ", $dest_column_names) .", nid, vid) VALUES (". implode(', ', $column_placeholders) .", %d, %d)", $source_values);
}
else { // Otherwise we just update it in the content_type table
$gg = db_query("UPDATE {". $db_info_dest['table'] ."} SET " . implode(", ", $column_assignments) ." WHERE nid = %d AND vid = %d", $source_values);
}
}
}
}
db_query("DELETE FROM {%s} WHERE nid = %d", "content_type_". $source_node_type, $nid);
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:". $nid .":". $vid);
db_query("DELETE FROM {%s} WHERE nid = %d", "content_type_". $source_node_type, $nid); // We delete the source node_type info
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:". $nid .":". $vid); // We clear the cache
drupal_set_message("The current node type has been changed succesufuly");
drupal_goto("node/". $nid);
}
......@@ -198,6 +221,7 @@ function node_convert_convert_form_submit($form_id, $form_values) {
function theme_node_convert_convert_form($form) {
$output = '';
if (isset($form['current_type'])) $output .= '<div>'. t("The current node type is:") ." <b>". drupal_render($form['current_type']) .'</b></div>';
// If no fields were converted, we notify it to the user
if (isset($form['no_fields'])) $output .= '<div>'. drupal_render($form['no_fields']) .'</div>';
$output .= drupal_render($form);
return $output;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment