| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: node_export.pages.inc,v 1.1.2.9 2009/09/08 10:14:12 danielb Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* The Node Export pages include. |
* The Node Export pages include. |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
/** |
/** |
| 10 |
* Menu callback to configure module settings. |
* Menu callback to configure module settings. |
| 11 |
*/ |
*/ |
| 12 |
function node_export_settings() { |
function node_export_settings() { |
| 13 |
|
|
| 14 |
$form['basic'] = array( |
$form['basic'] = array( |
| 15 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 16 |
'#title' => t('General settings'), |
'#title' => t('General settings'), |
| 17 |
); |
); |
| 18 |
$form['basic']['node_export_method'] = array( |
$form['basic']['node_export_method'] = array( |
| 19 |
'#type' => 'radios', |
'#type' => 'radios', |
| 20 |
'#title' => t('Method to use when importing a node'), |
'#title' => t('Method to use when importing a node'), |
| 21 |
'#options' => array('prepopulate' => t('Pre-populate the node form fields'), 'save-edit' => t('Save as a new node then edit')), |
'#options' => array('prepopulate' => t('Pre-populate the node form fields'), 'save-edit' => t('Save as a new node then edit')), |
| 22 |
'#default_value' => variable_get('node_export_method', 'prepopulate'), |
'#default_value' => variable_get('node_export_method', 'prepopulate'), |
| 23 |
); |
); |
| 24 |
|
|
| 25 |
$form['publishing'] = array( |
$form['publishing'] = array( |
| 26 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 27 |
'#title' => t('Reset values on import'), |
'#title' => t('Reset values on import'), |
| 28 |
); |
); |
| 29 |
$types = node_get_types('names'); |
$types = node_get_types('names'); |
| 30 |
|
|
| 31 |
foreach ($types as $type => $name) { |
foreach ($types as $type => $name) { |
| 32 |
$form['publishing'][$type] = array( |
$form['publishing'][$type] = array( |
| 33 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 34 |
'#title' => $name, |
'#title' => $name, |
| 35 |
'#description' => t('Reset these values when importing nodes of type @s.', array('@s' => $name)), |
'#description' => t('Reset these values when importing nodes of type @s.', array('@s' => $name)), |
| 36 |
); |
); |
| 37 |
$form['publishing'][$type]['node_export_reset_'. $type] = array( |
$form['publishing'][$type]['node_export_reset_'. $type] = array( |
| 38 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 39 |
'#title' => t('Publishing options (status, moderate, promote, sticky, and revision)'), |
'#title' => t('Publishing options (status, moderate, promote, sticky, and revision)'), |
| 40 |
'#default_value' => variable_get('node_export_reset_'. $type, FALSE), |
'#default_value' => variable_get('node_export_reset_'. $type, FALSE), |
| 41 |
); |
); |
| 42 |
$form['publishing'][$type]['node_export_reset_created_'. $type] = array( |
$form['publishing'][$type]['node_export_reset_created_'. $type] = array( |
| 43 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 44 |
'#title' => t('Created time (<em>Authored on</em> date)'), |
'#title' => t('Created time (<em>Authored on</em> date)'), |
| 45 |
'#default_value' => variable_get('node_export_reset_created_'. $type, TRUE), |
'#default_value' => variable_get('node_export_reset_created_'. $type, TRUE), |
| 46 |
); |
); |
| 47 |
$form['publishing'][$type]['node_export_reset_menu_'. $type] = array( |
$form['publishing'][$type]['node_export_reset_menu_'. $type] = array( |
| 48 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 49 |
'#title' => t('Menu link'), |
'#title' => t('Menu link'), |
| 50 |
'#default_value' => variable_get('node_export_reset_menu_'. $type, TRUE), |
'#default_value' => variable_get('node_export_reset_menu_'. $type, TRUE), |
| 51 |
); |
); |
| 52 |
$form['publishing'][$type]['node_export_reset_path_'. $type] = array( |
$form['publishing'][$type]['node_export_reset_path_'. $type] = array( |
| 53 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 54 |
'#title' => t('URL path'), |
'#title' => t('URL path'), |
| 55 |
'#default_value' => variable_get('node_export_reset_path_'. $type, TRUE), |
'#default_value' => variable_get('node_export_reset_path_'. $type, TRUE), |
| 56 |
); |
); |
| 57 |
$form['publishing'][$type]['node_export_reset_book_mlid_'. $type] = array( |
$form['publishing'][$type]['node_export_reset_book_mlid_'. $type] = array( |
| 58 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 59 |
'#title' => t('Book menu link'), |
'#title' => t('Book menu link'), |
| 60 |
'#default_value' => variable_get('node_export_reset_book_mlid_'. $type, TRUE), |
'#default_value' => variable_get('node_export_reset_book_mlid_'. $type, TRUE), |
| 61 |
); |
); |
| 62 |
} |
} |
| 63 |
// Need the variable default key to be something that's never a valid node type. |
// Need the variable default key to be something that's never a valid node type. |
| 64 |
$form['omit'] = array( |
$form['omit'] = array( |
| 65 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 66 |
'#title' => t('Content types that are not to be exported - omitted due to incompatibility'), |
'#title' => t('Content types that are not to be exported - omitted due to incompatibility'), |
| 67 |
); |
); |
| 68 |
$form['omit']['node_export_omitted'] = array( |
$form['omit']['node_export_omitted'] = array( |
| 69 |
'#type' => 'checkboxes', |
'#type' => 'checkboxes', |
| 70 |
'#title' => t('Omitted content types'), |
'#title' => t('Omitted content types'), |
| 71 |
'#default_value' => variable_get('node_export_omitted', array()), |
'#default_value' => variable_get('node_export_omitted', array()), |
| 72 |
'#options' => $types, |
'#options' => $types, |
| 73 |
'#description' => t('Select any node types which should <em>never</em> be exported. Typically you should will want to select here all node types for which importing fails (e.g. image nodes).'), |
'#description' => t('Select any node types which should <em>never</em> be exported. Typically you should will want to select here all node types for which importing fails (e.g. image nodes).'), |
| 74 |
); |
); |
| 75 |
|
|
| 76 |
return system_settings_form($form); |
return system_settings_form($form); |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
/** |
/** |
| 80 |
* exports a node - populate a node code form |
* exports a node - populate a node code form |
| 81 |
* set $return_code to TRUE to not return form but the code instead. |
* set $return_code to TRUE to not return form but the code instead. |
| 82 |
*/ |
*/ |
| 83 |
function node_export_node_export($original_node, $return_code = FALSE, $iteration = 0) { |
function node_export_node_export($original_node, $return_code = FALSE, $iteration = 0) { |
| 84 |
if (isset($original_node->nid)) { |
if (isset($original_node->nid)) { |
| 85 |
global $user; |
global $user; |
| 86 |
|
|
| 87 |
if (node_export_is_permitted($original_node->type)) { |
if (node_export_is_permitted($original_node->type)) { |
| 88 |
$node = drupal_clone($original_node); |
$node = drupal_clone($original_node); |
| 89 |
|
|
| 90 |
drupal_set_title(t('Export of !title', array('!title' => check_plain($node->title)))); |
drupal_set_title(t('Export of !title', array('!title' => check_plain($node->title)))); |
| 91 |
|
|
| 92 |
// Fix taxonomy array |
// Fix taxonomy array |
| 93 |
if (isset($node->taxonomy) && count($node->taxonomy)) { |
if (isset($node->taxonomy) && count($node->taxonomy)) { |
| 94 |
$vocabularies = taxonomy_get_vocabularies(); |
$vocabularies = taxonomy_get_vocabularies(); |
| 95 |
$new_taxonomy = array(); |
$new_taxonomy = array(); |
| 96 |
foreach ($node->taxonomy as $term) { |
foreach ($node->taxonomy as $term) { |
| 97 |
// Free-tagging vocabularies need a different format |
// Free-tagging vocabularies need a different format |
| 98 |
if ($vocabularies[$term->vid]->tags) { |
if ($vocabularies[$term->vid]->tags) { |
| 99 |
$new_taxonomy['tags'][$term->vid][] = $term->name; |
$new_taxonomy['tags'][$term->vid][] = $term->name; |
| 100 |
} |
} |
| 101 |
else { |
else { |
| 102 |
$new_taxonomy[$term->vid][$term->tid] = $term->tid; |
$new_taxonomy[$term->vid][$term->tid] = $term->tid; |
| 103 |
} |
} |
| 104 |
} |
} |
| 105 |
if (isset($new_taxonomy['tags']) && count($new_taxonomy['tags'])) { |
if (isset($new_taxonomy['tags']) && count($new_taxonomy['tags'])) { |
| 106 |
// Comma seperate the tags |
// Comma seperate the tags |
| 107 |
foreach ($new_taxonomy['tags'] as $vid => $tags) { |
foreach ($new_taxonomy['tags'] as $vid => $tags) { |
| 108 |
$new_taxonomy['tags'][$vid] = implode(', ', $tags); |
$new_taxonomy['tags'][$vid] = implode(', ', $tags); |
| 109 |
} |
} |
| 110 |
} |
} |
| 111 |
$node->taxonomy = $new_taxonomy; |
$node->taxonomy = $new_taxonomy; |
| 112 |
} |
} |
| 113 |
|
|
| 114 |
// Fix menu array |
// Fix menu array |
| 115 |
$node->menu = node_export_get_menu($original_node); |
$node->menu = node_export_get_menu($original_node); |
| 116 |
|
|
| 117 |
// Let other modules do special fixing up. |
// Let other modules do special fixing up. |
| 118 |
// The function signature is: hook_export_node_alter(&$node, $original_node, $method) |
// The function signature is: hook_export_node_alter(&$node, $original_node, $method) |
| 119 |
// Where $method is 'export' |
// Where $method is 'export' |
| 120 |
drupal_alter('node_export_node', $node, $original_node, 'export'); |
drupal_alter('node_export_node', $node, $original_node, 'export'); |
| 121 |
$node_code = node_export_node_encode($node, $iteration); |
$node_code = node_export_node_encode($node, $iteration); |
| 122 |
return ($return_code ? $node_code : drupal_get_form('node_export_form', $node_code)); |
return ($return_code ? $node_code : drupal_get_form('node_export_form', $node_code)); |
| 123 |
} |
} |
| 124 |
} |
} |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
/** |
/** |
| 128 |
* Export Form |
* Export Form |
| 129 |
*/ |
*/ |
| 130 |
function node_export_form($form_state, $code) { |
function node_export_form($form_state, $code) { |
| 131 |
$form = array(); |
$form = array(); |
| 132 |
$form['export'] = array( |
$form['export'] = array( |
| 133 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 134 |
'#title' => t('Node Code'), |
'#title' => t('Node Code'), |
| 135 |
'#default_value' => $code, |
'#default_value' => $code, |
| 136 |
'#rows' => 30, |
'#rows' => 30, |
| 137 |
'#description' => t('Copy this code and then on the site you want to import to, go to the Import Node link under Content Management, and paste it in there.'), |
'#description' => t('Copy this code and then on the site you want to import to, go to the Import Node link under Content Management, and paste it in there.'), |
| 138 |
'#attributes' => array( |
'#attributes' => array( |
| 139 |
'style' => 'width: 97%;', |
'style' => 'width: 97%;', |
| 140 |
), |
), |
| 141 |
); |
); |
| 142 |
return $form; |
return $form; |
| 143 |
} |
} |
| 144 |
|
|
| 145 |
/** |
/** |
| 146 |
* Import Node UI |
* Import Node UI |
| 147 |
*/ |
*/ |
| 148 |
function node_export_node_import() { |
function node_export_node_import() { |
| 149 |
global $user; |
global $user; |
| 150 |
$output; |
$output; |
| 151 |
if ($_POST['import_code']) { |
if ($_POST['import_code']) { |
| 152 |
$node_code = trim($_POST['import_code']); |
$node_code = trim($_POST['import_code']); |
| 153 |
$import = node_export_node_decode($node_code); |
$import = node_export_node_decode($node_code); |
| 154 |
$types_exist = node_export_import_types_check($import); |
$types_exist = node_export_import_types_check($import); |
| 155 |
if ($types_exist) { |
if ($types_exist) { |
| 156 |
if (is_array($import)) { |
if (is_array($import)) { |
| 157 |
foreach ($import as $new_node) { |
foreach ($import as $new_node) { |
| 158 |
$new_nid = node_export_node_save($new_node); |
$new_nid = node_export_node_save($new_node); |
| 159 |
drupal_set_message(t("Imported !node", array('!node' => l($new_node->title, 'node/'. $new_nid)))); |
drupal_set_message(t("Imported !node", array('!node' => l($new_node->title, 'node/'. $new_nid)))); |
| 160 |
} |
} |
| 161 |
drupal_goto('admin/content/node'); |
drupal_goto('admin/content/node'); |
| 162 |
} |
} |
| 163 |
else { |
else { |
| 164 |
return node_export_node_check($import); |
return node_export_node_check($import); |
| 165 |
} |
} |
| 166 |
} |
} |
| 167 |
} |
} |
| 168 |
return drupal_get_form('node_export_import_form', $form); |
return drupal_get_form('node_export_import_form', $form); |
| 169 |
} |
} |
| 170 |
|
|
| 171 |
/** |
/** |
| 172 |
* Check if all types in the import exist. Return TRUE/FALSE. |
* Check if all types in the import exist. Return TRUE/FALSE. |
| 173 |
*/ |
*/ |
| 174 |
function node_export_import_types_check($import) { |
function node_export_import_types_check($import) { |
| 175 |
$types_exist = TRUE; |
$types_exist = TRUE; |
| 176 |
if (is_array($import)) { |
if (is_array($import)) { |
| 177 |
foreach ($import as $new_node) { |
foreach ($import as $new_node) { |
| 178 |
if (node_get_types('name', $new_node) == FALSE) { |
if (node_get_types('name', $new_node) == FALSE) { |
| 179 |
$types_exist = $new_node->type; |
$types_exist = $new_node->type; |
| 180 |
} |
} |
| 181 |
} |
} |
| 182 |
} |
} |
| 183 |
else { |
else { |
| 184 |
$new_node = $import; |
$new_node = $import; |
| 185 |
if (node_get_types('name', $new_node) == FALSE) { |
if (node_get_types('name', $new_node) == FALSE) { |
| 186 |
$types_exist = $new_node->type; |
$types_exist = $new_node->type; |
| 187 |
} |
} |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
if ($types_exist !== TRUE) { |
if ($types_exist !== TRUE) { |
| 191 |
drupal_set_message(t('Error encountered during import. Node type %t unknown on this site. No nodes imported.', array('%t' => $types_exist)), 'error'); |
drupal_set_message(t('Error encountered during import. Node type %t unknown on this site. No nodes imported.', array('%t' => $types_exist)), 'error'); |
| 192 |
$types_exist = FALSE; |
$types_exist = FALSE; |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
return $types_exist; |
return $types_exist; |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
/** |
/** |
| 199 |
* Import Form |
* Import Form |
| 200 |
*/ |
*/ |
| 201 |
function node_export_import_form($form_state) { |
function node_export_import_form($form_state) { |
| 202 |
$form['#prefix'] = t('<p>You may import a node by copy-and-pasting the results of an exported node. </p>'); |
$form['#prefix'] = t('<p>You may import a node by copy-and-pasting the results of an exported node. </p>'); |
| 203 |
$method = variable_get('node_export_method', 'prepopulate'); |
$method = variable_get('node_export_method', 'prepopulate'); |
| 204 |
if ($method == 'prepopulate') { |
if ($method == 'prepopulate') { |
| 205 |
$form['#prefix'] .= t('<p>You will be given a prepopulated node add form which you must save to create the node.</p>'); |
$form['#prefix'] .= t('<p>You will be given a prepopulated node add form which you must save to create the node.</p>'); |
| 206 |
} |
} |
| 207 |
elseif ($method == 'save-edit') { |
elseif ($method == 'save-edit') { |
| 208 |
$form['#prefix'] .= t('<p>The node will be created and you will then be taken to the node edit page.</p>'); |
$form['#prefix'] .= t('<p>The node will be created and you will then be taken to the node edit page.</p>'); |
| 209 |
} |
} |
| 210 |
$form['#prefix'] .= t('<p>To change this behaviour, <a href="/admin/settings/node_export">configure the settings</a>.</p>'); |
$form['#prefix'] .= t('<p>To change this behaviour, <a href="/admin/settings/node_export">configure the settings</a>.</p>'); |
| 211 |
$form['import_code'] = array( |
$form['import_code'] = array( |
| 212 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 213 |
'#title' => t('Import Code'), |
'#title' => t('Import Code'), |
| 214 |
'#default_value' => '', |
'#default_value' => '', |
| 215 |
'#required' => TRUE, |
'#required' => TRUE, |
| 216 |
'#rows' => 30, |
'#rows' => 30, |
| 217 |
'#description' => t('Cut and paste the results of an <em>Export Node</em> here.'), |
'#description' => t('Cut and paste the results of an <em>Export Node</em> here.'), |
| 218 |
); |
); |
| 219 |
$form['#redirect'] = FALSE; |
$form['#redirect'] = FALSE; |
| 220 |
$form['submit'] = array( |
$form['submit'] = array( |
| 221 |
'#type' => 'submit', |
'#type' => 'submit', |
| 222 |
'#value' => t('Import'), |
'#value' => t('Import'), |
| 223 |
); |
); |
| 224 |
return $form; |
return $form; |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
/** |
/** |
| 228 |
* Menu callback: prompt the user to confirm the operation |
* Menu callback: prompt the user to confirm the operation |
| 229 |
*/ |
*/ |
| 230 |
function node_export_node_check($node) { |
function node_export_node_check($node) { |
| 231 |
|
|
| 232 |
$method = variable_get('node_export_method', 'prepopulate'); |
$method = variable_get('node_export_method', 'prepopulate'); |
| 233 |
|
|
| 234 |
switch ($method) { |
switch ($method) { |
| 235 |
case 'save-edit': |
case 'save-edit': |
| 236 |
$new_nid = node_export_node_save($node); |
$new_nid = node_export_node_save($node); |
| 237 |
drupal_goto('node/'. $new_nid .'/edit'); |
drupal_goto('node/'. $new_nid .'/edit'); |
| 238 |
break; |
break; |
| 239 |
case 'prepopulate': |
case 'prepopulate': |
| 240 |
default: |
default: |
| 241 |
include_once(drupal_get_path('module', 'node') .'/node.pages.inc'); |
include_once(drupal_get_path('module', 'node') .'/node.pages.inc'); |
| 242 |
return node_export_node_prepopulate($node); |
return node_export_node_prepopulate($node); |
| 243 |
break; |
break; |
| 244 |
} |
} |
| 245 |
} |
} |
| 246 |
|
|
| 247 |
/** |
/** |
| 248 |
* exports a node - prepopulate a node editing form |
* exports a node - prepopulate a node editing form |
| 249 |
*/ |
*/ |
| 250 |
function node_export_node_prepopulate($original_node) { |
function node_export_node_prepopulate($original_node) { |
| 251 |
if (node_export_is_permitted($original_node->type)) { |
if (node_export_is_permitted($original_node->type)) { |
| 252 |
$node = node_export_node_clone($original_node, "prepopulate"); |
$node = node_export_node_clone($original_node, "prepopulate"); |
| 253 |
return str_replace('admin/content/import', 'node/add/'. $node->type, drupal_get_form($node->type .'_node_form', $node)); |
return str_replace('admin/content/import', 'node/add/'. $node->type, drupal_get_form($node->type .'_node_form', $node)); |
| 254 |
} |
} |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
/** |
/** |
| 258 |
* exports a node by directly saving it. |
* exports a node by directly saving it. |
| 259 |
*/ |
*/ |
| 260 |
function node_export_node_save($original_node) { |
function node_export_node_save($original_node) { |
| 261 |
if (node_export_is_permitted($original_node->type)) { |
if (node_export_is_permitted($original_node->type)) { |
| 262 |
$node = node_export_node_clone($original_node, "save-edit"); |
$node = node_export_node_clone($original_node, "save-edit"); |
| 263 |
node_save($node); |
node_save($node); |
| 264 |
return $node->nid; |
return $node->nid; |
| 265 |
} |
} |
| 266 |
} |
} |
| 267 |
|
|
| 268 |
/** |
/** |
| 269 |
* Prepare a clone of the node during import. |
* Prepare a clone of the node during import. |
| 270 |
*/ |
*/ |
| 271 |
function node_export_node_clone($original_node, $mode) { |
function node_export_node_clone($original_node, $mode) { |
| 272 |
global $user; |
global $user; |
| 273 |
|
|
| 274 |
$node = drupal_clone($original_node); |
$node = drupal_clone($original_node); |
| 275 |
|
|
| 276 |
$node->nid = NULL; |
$node->nid = NULL; |
| 277 |
$node->vid = NULL; |
$node->vid = NULL; |
| 278 |
|
|
| 279 |
$node->name = $user->name; |
$node->name = $user->name; |
| 280 |
$node->uid = $user->uid; |
$node->uid = $user->uid; |
| 281 |
|
|
| 282 |
if (variable_get('node_export_reset_created_'. $node->type, TRUE)) { |
if (variable_get('node_export_reset_created_'. $node->type, TRUE)) { |
| 283 |
$node->created = NULL; |
$node->created = NULL; |
| 284 |
} |
} |
| 285 |
|
|
| 286 |
if (variable_get('node_export_reset_menu_'. $node->type, TRUE)) { |
if (variable_get('node_export_reset_menu_'. $node->type, TRUE)) { |
| 287 |
$node->menu = NULL; |
$node->menu = NULL; |
| 288 |
} |
} |
| 289 |
|
|
| 290 |
if (variable_get('node_export_reset_path_'. $node->type, TRUE)) { |
if (variable_get('node_export_reset_path_'. $node->type, TRUE)) { |
| 291 |
$node->path = NULL; |
$node->path = NULL; |
| 292 |
} |
} |
| 293 |
|
|
| 294 |
if (variable_get('node_export_reset_book_mlid_'. $node->type, TRUE) && isset($node->book['mlid'])) { |
if (variable_get('node_export_reset_book_mlid_'. $node->type, TRUE) && isset($node->book['mlid'])) { |
| 295 |
$node->book['mlid'] = NULL; |
$node->book['mlid'] = NULL; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
$node->files = array(); |
$node->files = array(); |
| 299 |
|
|
| 300 |
if (variable_get('node_export_reset_'. $node->type, FALSE)) { |
if (variable_get('node_export_reset_'. $node->type, FALSE)) { |
| 301 |
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote')); |
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote')); |
| 302 |
// Fill in the default values. |
// Fill in the default values. |
| 303 |
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) { |
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) { |
| 304 |
$node->$key = in_array($key, $node_options); |
$node->$key = in_array($key, $node_options); |
| 305 |
} |
} |
| 306 |
} |
} |
| 307 |
// Let other modules do special fixing up. |
// Let other modules do special fixing up. |
| 308 |
// The function signature is: hook_export_node_alter(&$node, $original_node, $method) |
// The function signature is: hook_export_node_alter(&$node, $original_node, $method) |
| 309 |
// Where $method is either 'prepopulate' or 'save-edit'. |
// Where $method is either 'prepopulate' or 'save-edit'. |
| 310 |
drupal_alter('node_export_node', $node, $original_node, $mode); |
drupal_alter('node_export_node', $node, $original_node, $mode); |
| 311 |
|
|
| 312 |
return $node; |
return $node; |
| 313 |
|
|
| 314 |
} |
} |
| 315 |
|
|
| 316 |
/** |
/** |
| 317 |
* Build node code string recursively |
* Build node code string recursively |
| 318 |
*/ |
*/ |
| 319 |
function node_export_node_encode($var, $iteration = 0) { |
function node_export_node_encode($var, $iteration = 0) { |
| 320 |
$tab = ''; |
$tab = ''; |
| 321 |
for ($i = 0; $i < $iteration; $i++) { |
for ($i = 0; $i < $iteration; $i++) { |
| 322 |
$tab = $tab ." "; |
$tab = $tab ." "; |
| 323 |
} |
} |
| 324 |
$iteration++; |
$iteration++; |
| 325 |
if (is_object($var)) { |
if (is_object($var)) { |
| 326 |
$var = (array)$var; |
$var = (array)$var; |
| 327 |
$var['#_export_node_encode_object'] = '1'; |
$var['#_export_node_encode_object'] = '1'; |
| 328 |
} |
} |
| 329 |
if (is_array($var)) { |
if (is_array($var)) { |
| 330 |
$empty = empty($var); |
$empty = empty($var); |
| 331 |
$code = "array(". ($empty ? '' : "\n"); |
$code = "array(". ($empty ? '' : "\n"); |
| 332 |
foreach ($var as $key => $value) { |
foreach ($var as $key => $value) { |
| 333 |
$out = $tab ." '". $key ."' => ". node_export_node_encode($value, $iteration) .",\n"; |
$out = $tab ." '". $key ."' => ". node_export_node_encode($value, $iteration) .",\n"; |
| 334 |
drupal_alter('node_export_node_encode', $out, $tab, $key, $value, $iteration); |
drupal_alter('node_export_node_encode', $out, $tab, $key, $value, $iteration); |
| 335 |
$code .= $out; |
$code .= $out; |
| 336 |
} |
} |
| 337 |
$code .= ($empty ? '' : $tab) .")"; |
$code .= ($empty ? '' : $tab) .")"; |
| 338 |
return $code; |
return $code; |
| 339 |
} |
} |
| 340 |
else { |
else { |
| 341 |
if (is_string($var)) { |
if (is_string($var)) { |
| 342 |
return "'". addslashes($var) ."'"; |
return "'". addslashes($var) ."'"; |
| 343 |
} |
} |
| 344 |
elseif (is_numeric($var)) { |
elseif (is_numeric($var)) { |
| 345 |
return $var; |
return $var; |
| 346 |
} |
} |
| 347 |
elseif (is_bool($var)) { |
elseif (is_bool($var)) { |
| 348 |
return ($var ? 'TRUE' : 'FALSE'); |
return ($var ? 'TRUE' : 'FALSE'); |
| 349 |
} |
} |
| 350 |
else { |
else { |
| 351 |
return 'NULL'; |
return 'NULL'; |
| 352 |
} |
} |
| 353 |
} |
} |
| 354 |
} |
} |
| 355 |
|
|
| 356 |
/** |
/** |
| 357 |
* Evaluate and return decoded string. |
* Evaluate and return decoded string. |
| 358 |
*/ |
*/ |
| 359 |
function node_export_node_decode($string) { |
function node_export_node_decode($string) { |
| 360 |
$array = eval('return '. $string .';'); |
$array = eval('return '. $string .';'); |
| 361 |
$return = node_export_node_decode_objects($array); |
$return = node_export_node_decode_objects($array); |
| 362 |
return $return; |
return $return; |
| 363 |
} |
} |
| 364 |
|
|
| 365 |
/** |
/** |
| 366 |
* Recursively convert arrays back to objects. |
* Recursively convert arrays back to objects. |
| 367 |
*/ |
*/ |
| 368 |
function node_export_node_decode_objects($array) { |
function node_export_node_decode_objects($array) { |
| 369 |
foreach ($array as $k => $v) { |
foreach ($array as $k => $v) { |
| 370 |
if (is_array($v)) { |
if (is_array($v)) { |
| 371 |
$array[$k] = node_export_node_decode_objects($v); |
$array[$k] = node_export_node_decode_objects($v); |
| 372 |
} |
} |
| 373 |
if (is_string($v)) { |
if (is_string($v)) { |
| 374 |
$array[$k] = stripslashes($v); |
$array[$k] = stripslashes($v); |
| 375 |
} |
} |
| 376 |
} |
} |
| 377 |
if ($array['#_export_node_encode_object']) { |
if ($array['#_export_node_encode_object']) { |
| 378 |
unset($array['#_export_node_encode_object']); |
unset($array['#_export_node_encode_object']); |
| 379 |
$array = (object)$array; |
$array = (object)$array; |
| 380 |
} |
} |
| 381 |
return $array; |
return $array; |
| 382 |
} |
} |
| 383 |
|
|
| 384 |
/** |
/** |
| 385 |
* Create a new menu entry with title, parent and weight exported from |
* Create a new menu entry with title, parent and weight exported from |
| 386 |
* another nodes menu. Returns NULL if the node has no menu title. |
* another nodes menu. Returns NULL if the node has no menu title. |
| 387 |
*/ |
*/ |
| 388 |
function node_export_get_menu($node) { |
function node_export_get_menu($node) { |
| 389 |
// This will fetch the existing menu item if the node had one. |
// This will fetch the existing menu item if the node had one. |
| 390 |
node_invoke_nodeapi($node, 'prepare'); |
node_invoke_nodeapi($node, 'prepare'); |
| 391 |
|
|
| 392 |
// Only keep the values we care about. |
// Only keep the values we care about. |
| 393 |
if (!empty($node->menu)) { |
if (!empty($node->menu)) { |
| 394 |
|
|
| 395 |
// Store a copy of the old menu |
// Store a copy of the old menu |
| 396 |
$old_menu = $node->menu; |
$old_menu = $node->menu; |
| 397 |
|
|
| 398 |
// Now fetch the defaults for a new menu entry. |
// Now fetch the defaults for a new menu entry. |
| 399 |
$node = NULL; |
$node = NULL; |
| 400 |
node_invoke_nodeapi($node, 'prepare'); |
node_invoke_nodeapi($node, 'prepare'); |
| 401 |
|
|
| 402 |
// Make a list of values to attempt to copy. |
// Make a list of values to attempt to copy. |
| 403 |
$menu_fields = array( |
$menu_fields = array( |
| 404 |
'link_title', 'plid', 'menu_name', 'weight', // These should import properly always. |
'link_title', 'plid', 'menu_name', 'weight', // These should import properly always. |
| 405 |
'hidden', 'expanded', 'has_children', // These will only import properly on 'Save as a new node then edit' imports. |
'hidden', 'expanded', 'has_children', // These will only import properly on 'Save as a new node then edit' imports. |
| 406 |
); |
); |
| 407 |
|
|
| 408 |
// Copy those fields from the old menu over the new menu defaults. |
// Copy those fields from the old menu over the new menu defaults. |
| 409 |
foreach ($menu_fields as $menu_field) { |
foreach ($menu_fields as $menu_field) { |
| 410 |
$node->menu[$menu_field] = $old_menu[$menu_field]; |
$node->menu[$menu_field] = $old_menu[$menu_field]; |
| 411 |
} |
} |
| 412 |
|
|
| 413 |
// Return the menu. |
// Return the menu. |
| 414 |
return $node->menu; |
return $node->menu; |
| 415 |
} |
} |
| 416 |
|
|
| 417 |
} |
} |