node.pages.inc 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
// $Id$

/**
 * @file
 * Page callbacks for adding, editing, deleting, and revisions management for content.
 */


/**
 * Menu callback; presents the node editing form, or redirects to delete confirmation.
 */
function node_page_edit($node) {
14 15
  $type_name = node_get_types('name', $node);
  drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $type_name, '@title' => $node->title)), PASS_THROUGH);
16
  return drupal_get_form($node->type . '_node_form', $node);
17 18 19 20
}

function node_add_page() {
  $item = menu_get_item();
21
  $content = system_admin_menu_block($item);
22 23 24 25 26
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
    $item = array_shift($content);
    drupal_goto($item['href']);
  }
27 28 29
  return theme('node_add_list', $content);
}

30 31 32 33 34
/**
 * Display the list of available node types for node creation.
 *
 * @ingroup themeable
 */
35 36 37 38 39 40
function theme_node_add_list($content) {
  $output = '';

  if ($content) {
    $output = '<dl class="node-type-list">';
    foreach ($content as $item) {
41
      $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
42
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    }
    $output .= '</dl>';
  }
  return $output;
}


/**
 * Present a node submission form or a set of links to such forms.
 */
function node_add($type) {
  global $user;

  $types = node_get_types();
  $type = isset($type) ? str_replace('-', '_', $type) : NULL;
  // If a node type has been specified, validate its existence.
  if (isset($types[$type]) && node_access('create', $type)) {
    // Initialize settings:
61
    $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => '');
62

63
    drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
64
    $output = drupal_get_form($type . '_node_form', $node);
65 66 67 68 69 70
  }

  return $output;
}

function node_form_validate($form, &$form_state) {
71 72 73 74 75 76 77
  $node = $form_state['values'];
  node_validate($node, $form);

  // Field validation. Requires access to $form_state, so this cannot be
  // done in node_validate() as it currently exists.
  $node = (object)$node;
  field_attach_form_validate('node', $node, $form, $form_state);
78 79 80
}

function node_object_prepare(&$node) {
81
  // Set up default values, if required.
82
  $node_options = variable_get('node_options_' . $node->type, array('status', 'promote'));
83 84 85 86 87 88 89
  // If this is a new node, fill in the default values.
  if (!isset($node->nid)) {
    foreach (array('status', 'promote', 'sticky') as $key) {
      $node->$key = in_array($key, $node_options);
    }
    global $user;
    $node->uid = $user->uid;
90
    $node->created = REQUEST_TIME;
91 92 93
  }
  else {
    $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
94 95
    // Remove the log message from the original node object.
    $node->log = NULL;
96 97 98 99 100
  }
  // Always use the default revision setting.
  $node->revision = in_array('revision', $node_options);

  node_invoke($node, 'prepare');
101
  node_invoke_node($node, 'prepare');
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

/**
 * Generate the node add/edit form array.
 */
function node_form(&$form_state, $node) {
  global $user;

  if (isset($form_state['node'])) {
    $node = $form_state['node'] + (array)$node;
  }
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
  }
  $node = (object)$node;
  foreach (array('body', 'title', 'format') as $key) {
    if (!isset($node->$key)) {
      $node->$key = NULL;
    }
  }
122 123 124
  if (!isset($form_state['node_preview'])) {
    node_object_prepare($node);
  }
125 126 127
  else {
    $node->build_mode = NODE_BUILD_PREVIEW;
  }
128

129
  // Set the id and identify this as a node edit form.
130
  $form['#id'] = 'node-form';
131
  $form['#node_edit_form'] = TRUE;
132

133 134
  // Basic node information.
  // These elements are just values so they are not even sent to the client.
135
  foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
136
    $form[$key] = array(
137
      '#type' => 'value',
138 139
      '#value' => isset($node->$key) ? $node->$key : NULL,
    );
140 141 142
  }

  // Changed must be sent to the client, for later overwrite error checking.
143
  $form['changed'] = array(
144
    '#type' => 'hidden',
145 146
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
  );
147 148 149 150 151 152 153 154 155 156
  // Get the node-specific bits.
  if ($extra = node_invoke($node, 'form', $form_state)) {
    $form = array_merge_recursive($form, $extra);
  }
  if (!isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }

  $form['#node'] = $node;

157 158 159 160
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
  );

161 162
  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
163
  if (!empty($node->revision) || user_access('administer nodes')) {
164 165 166 167 168 169
    $form['revision_information'] = array(
      '#type' => 'fieldset',
      '#title' => t('Revision information'),
      '#collapsible' => TRUE,
      // Collapsed by default when "Create new revision" is unchecked
      '#collapsed' => !$node->revision,
170 171
      '#group' => 'additional_settings',
      '#attached_js' => array(drupal_get_path('module', 'node') . '/node.js'),
172 173 174
      '#weight' => 20,
    );
    $form['revision_information']['revision'] = array(
175
      '#access' => user_access('administer nodes'),
176 177 178 179 180 181
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $node->revision,
    );
    $form['revision_information']['log'] = array(
      '#type' => 'textarea',
182
      '#title' => t('Revision log message'),
183
      '#rows' => 4,
184
      '#default_value' => !empty($node->log) ? $node->log : '',
185
      '#description' => t('Provide an explanation of the changes you are making. This will help other authors understand your motivations.'),
186 187 188 189 190 191 192 193 194 195
    );
  }

  // Node author information for administrators
  $form['author'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Authoring information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
196 197
    '#group' => 'additional_settings',
    '#attached_js' => array(drupal_get_path('module', 'node') . '/node.js'),
198
    '#weight' => 90,
199
  );
200
  $form['author']['name'] = array(
201 202 203 204
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
205
    '#default_value' => !empty($node->name) ? $node->name : '',
206
    '#weight' => -1,
207 208 209
    '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  );
  $form['author']['date'] = array(
210 211 212
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
213 214
    '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'Y-m-d H:i:s O'))),
  );
215

216
  if (isset($node->date)) {
217 218 219 220 221 222 223 224 225 226
    $form['author']['date']['#default_value'] = $node->date;
  }

  // Node options for administrators
  $form['options'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
227 228
    '#group' => 'additional_settings',
    '#attached_js' => array(drupal_get_path('module', 'node') . '/node.js'),
229
    '#weight' => 95,
230
  );
231
  $form['options']['status'] = array(
232 233
    '#type' => 'checkbox',
    '#title' => t('Published'),
234 235 236
    '#default_value' => $node->status,
  );
  $form['options']['promote'] = array(
237 238
    '#type' => 'checkbox',
    '#title' => t('Promoted to front page'),
239 240 241
    '#default_value' => $node->promote,
  );
  $form['options']['sticky'] = array(
242 243
    '#type' => 'checkbox',
    '#title' => t('Sticky at top of lists'),
244 245
    '#default_value' => $node->sticky,
  );
246 247 248

  // These values are used when the user has no administrator access.
  foreach (array('uid', 'created') as $key) {
249
    $form[$key] = array(
250
      '#type' => 'value',
251 252
      '#value' => $node->$key,
    );
253 254 255 256
  }

  // Add the buttons.
  $form['buttons'] = array();
257
  $form['buttons']['#weight'] = 100;
258
  $form['buttons']['submit'] = array(
259
    '#type' => 'submit',
260
    '#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
261
    '#value' => t('Save'),
262
    '#weight' => 5,
263
    '#submit' => array('node_form_submit'),
264
  );
265
  $form['buttons']['preview'] = array(
266
    '#type' => 'submit',
267
    '#value' => t('Preview'),
268
    '#weight' => 10,
269
    '#submit' => array('node_form_build_preview'),
270 271 272 273 274 275 276 277 278 279
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
    $form['buttons']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array('node_form_delete_submit'),
    );
  }
  $form['#validate'][] = 'node_form_validate';
280 281 282 283
  $form['#theme'] = array($node->type . '_node_form', 'node_form');

  $form['#builder_function'] = 'node_form_submit_build_node';
  field_attach_form('node', $node, $form, $form_state);
284

285 286 287 288 289 290 291 292 293 294 295 296
  return $form;
}

/**
 * Return a node body field, with format and teaser.
 */
function node_body_field(&$node, $label, $word_count) {

  // Check if we need to restore the teaser at the beginning of the body.
  $include = !isset($node->teaser) || ($node->teaser == substr($node->body, 0, strlen($node->teaser)));

  $form = array(
297
    '#after_build' => array('node_teaser_js', 'node_teaser_include_verify'));
298

299
  $form['#prefix'] = '<div class="body-field-wrapper clearfix">';
300 301
  $form['#suffix'] = '</div>';

302 303 304 305 306
  $form['teaser_js'] = array(
    '#type' => 'textarea',
    '#rows' => 10,
    '#teaser' => 'edit-body',
    '#teaser_checkbox' => 'edit-teaser-include',
307 308
    '#disabled' => TRUE,
  );
309 310 311 312 313 314 315 316 317 318 319 320 321 322

  $form['teaser_include'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show summary in full view'),
    '#default_value' => $include,
    '#prefix' => '<div class="teaser-checkbox">',
    '#suffix' => '</div>',
  );

  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($label),
    '#default_value' => $include ? $node->body : ($node->teaser . $node->body),
    '#rows' => 20,
323
    '#required' => ($word_count > 0),
324
    '#text_format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT,
325
  );
326 327 328 329 330

  return $form;
}

/**
331
 * Button submit function: handle the 'Delete' button on the node form.
332 333 334 335 336 337 338 339
 */
function node_form_delete_submit($form, &$form_state) {
  $destination = '';
  if (isset($_REQUEST['destination'])) {
    $destination = drupal_get_destination();
    unset($_REQUEST['destination']);
  }
  $node = $form['#node'];
340
  $form_state['redirect'] = array('node/' . $node->nid . '/delete', $destination);
341 342 343 344 345 346 347 348
}


function node_form_build_preview($form, &$form_state) {
  $node = node_form_submit_build_node($form, $form_state);
  $form_state['node_preview'] = node_preview($node);
}

349 350 351 352 353
/**
 * Present a node submission form.
 *
 * @ingroup themeable
 */
354 355 356 357
function theme_node_form($form) {
  $output = "\n<div class=\"node-form\">\n";

  $output .= "  <div class=\"standard\">\n";
358
  $output .= drupal_render_children($form);
359 360 361 362 363 364 365 366 367 368 369 370
  $output .= "  </div>\n";

  $output .= "</div>\n";

  return $output;
}

/**
 * Generate a node preview.
 */
function node_preview($node) {
  if (node_access('create', $node) || node_access('update', $node)) {
371
    // Load the user's name when needed.
372 373 374
    if (isset($node->name)) {
      // The use of isset() is mandatory in the context of user IDs, because
      // user ID 0 denotes the anonymous user.
375
      if ($user = user_load_by_name($node->name)) {
376 377 378 379 380 381 382
        $node->uid = $user->uid;
        $node->picture = $user->picture;
      }
      else {
        $node->uid = 0; // anonymous user
      }
    }
383
    elseif ($node->uid) {
384
      $user = user_load($node->uid);
385 386 387 388
      $node->name = $user->name;
      $node->picture = $user->picture;
    }

389
    $node->changed = REQUEST_TIME;
390 391 392 393 394

    // Extract a teaser, if it hasn't been set (e.g. by a module-provided
    // 'teaser' form item).
    if (!isset($node->teaser)) {
      $node->teaser = empty($node->body) ? '' : node_teaser($node->body, $node->format);
395 396 397 398
      // Chop off the teaser from the body if needed.
      if (!$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
        $node->body = substr($node->body, strlen($node->teaser));
      }
399 400
    }

401
    // Display a preview of the node.
402 403
    // Previewing alters $node so it needs to be cloned.
    if (!form_get_errors()) {
404
      $cloned_node = clone $node;
405 406 407
      $cloned_node->build_mode = NODE_BUILD_PREVIEW;
      $output = theme('node_preview', $cloned_node);
    }
408
    drupal_set_title(t('Preview'), PASS_THROUGH);
409 410 411 412 413 414 415 416 417 418

    return $output;
  }
}

/**
 * Display a node preview for display during node creation and editing.
 *
 * @param $node
 *   The node object which is being previewed.
419 420
 *
 * @ingroup themeable
421 422 423
 */
function theme_node_preview($node) {
  $output = '<div class="preview">';
424

425 426 427 428 429 430 431 432 433 434 435 436 437
  $preview_trimmed_version = FALSE;
  // Do we need to preview trimmed version of post as well as full version?
  if (isset($node->teaser) && isset($node->body)) {
    $teaser = trim($node->teaser);
    $body = trim(str_replace('<!--break-->', '', $node->body));

    // Preview trimmed version if teaser and body will appear different;
    // also (edge case) if both teaser and body have been specified by the user
    // and are actually the same.
    if ($teaser != $body || ($body && strpos($node->body, '<!--break-->') === 0)) {
      $preview_trimmed_version = TRUE;
    }
  }
438

439
  if ($preview_trimmed_version) {
440
    drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication.<span class="no-js"> You can insert the delimiter "&lt;!--break--&gt;" (without the quotes) to fine-tune where your post gets split.</span>'));
441
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
442
    $output .= drupal_render(node_build(clone $node, TRUE));
443
    $output .= '<h3>' . t('Preview full version') . '</h3>';
444
    $output .= drupal_render(node_build($node, FALSE));
445 446
  }
  else {
447
    $output .= drupal_render(node_build($node, FALSE));
448 449 450 451 452 453 454 455 456 457 458 459
  }
  $output .= "</div>\n";

  return $output;
}

function node_form_submit($form, &$form_state) {
  global $user;

  $node = node_form_submit_build_node($form, $form_state);
  $insert = empty($node->nid);
  node_save($node);
460
  $node_link = l(t('view'), 'node/' . $node->nid);
461
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
462
  $t_args = array('@type' => node_get_types('name', $node), '%title' => $node->title);
463 464 465

  if ($insert) {
    watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
466
    drupal_set_message(t('@type %title has been created.', $t_args));
467 468 469
  }
  else {
    watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
470
    drupal_set_message(t('@type %title has been updated.', $t_args));
471 472 473
  }
  if ($node->nid) {
    unset($form_state['rebuild']);
474
    $form_state['nid'] = $node->nid;
475
    $form_state['redirect'] = 'node/' . $node->nid;
476 477 478 479
  }
  else {
    // In the unlikely case something went wrong on save, the node will be
    // rebuilt and node form redisplayed the same way as in preview.
480
    drupal_set_message(t('The post could not be saved.'), 'error');
481 482 483 484 485 486 487 488 489 490
  }
}

/**
 * Build a node by processing submitted form values and prepare for a form rebuild.
 */
function node_form_submit_build_node($form, &$form_state) {
  // Unset any button-level handlers, execute all the form-level submit
  // functions to process the form values into an updated node.
  unset($form_state['submit_handlers']);
491 492 493 494
  form_execute_handlers('submit', $form, $form_state);
  $node = node_submit($form_state['values']);

  field_attach_submit('node', $node, $form, $form_state);
495

496 497 498 499 500 501 502 503 504
  $form_state['node'] = (array)$node;
  $form_state['rebuild'] = TRUE;
  return $node;
}

/**
 * Menu callback -- ask for confirmation of node deletion
 */
function node_delete_confirm(&$form_state, $node) {
505
  $form['nid'] = array(
506
    '#type' => 'value',
507 508
    '#value' => $node->nid,
  );
509 510 511

  return confirm_form($form,
    t('Are you sure you want to delete %title?', array('%title' => $node->title)),
512
    isset($_GET['destination']) ? $_GET['destination'] : 'node/' . $node->nid,
513
    t('This action cannot be undone.'),
514
    t('Delete'),
515 516
    t('Cancel')
  );
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
}

/**
 * Execute node deletion
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    node_delete($form_state['values']['nid']);
  }

  $form_state['redirect'] = '<front>';
}

/**
 * Generate an overview table of older revisions of a node.
 */
function node_revision_overview($node) {
534
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
535 536 537 538 539 540 541 542 543 544 545

  $header = array(t('Revision'), array('data' => t('Operations'), 'colspan' => 2));

  $revisions = node_revision_list($node);

  $rows = array();
  $revert_permission = FALSE;
  if ((user_access('revert revisions') || user_access('administer nodes')) && node_access('update', $node)) {
    $revert_permission = TRUE;
  }
  $delete_permission = FALSE;
546
  if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
547 548 549 550 551 552 553 554
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
    $operations = array();

    if ($revision->current_vid > 0) {
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid"), '!username' => theme('username', $revision)))
555
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
556 557 558 559 560
                     'class' => 'revision-current');
      $operations[] = array('data' => theme('placeholder', t('current revision')), 'class' => 'revision-current', 'colspan' => 2);
    }
    else {
      $row[] = t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => theme('username', $revision)))
561
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
562 563 564 565 566 567 568 569 570 571 572 573 574
      if ($revert_permission) {
        $operations[] = l(t('revert'), "node/$node->nid/revisions/$revision->vid/revert");
      }
      if ($delete_permission) {
        $operations[] = l(t('delete'), "node/$node->nid/revisions/$revision->vid/delete");
      }
    }
    $rows[] = array_merge($row, $operations);
  }

  return theme('table', $header, $rows);
}

575 576 577 578 579
/**
 * Ask for confirmation of the reversion to prevent against CSRF attacks.
 */
function node_revision_revert_confirm($form_state, $node_revision) {
  $form['#node_revision'] = $node_revision;
580
  return confirm_form($form, t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($node_revision->revision_timestamp))), 'node/' . $node_revision->nid . '/revisions', '', t('Revert'), t('Cancel'));
581 582 583 584 585 586 587 588 589 590 591 592 593
}

function node_revision_revert_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
  $node_revision->revision = 1;
  $node_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($node_revision->revision_timestamp)));
  if (module_exists('taxonomy')) {
    $node_revision->taxonomy = array_keys($node_revision->taxonomy);
  }

  node_save($node_revision);

  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
594
  drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_types('name', $node_revision), '%title' => $node_revision->title, '%revision-date' => format_date($node_revision->revision_timestamp))));
595
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
596
}
597

598 599
function node_revision_delete_confirm($form_state, $node_revision) {
  $form['#node_revision'] = $node_revision;
600
  return confirm_form($form, t('Are you sure you want to delete the revision from %revision-date?', array('%revision-date' => format_date($node_revision->revision_timestamp))), 'node/' . $node_revision->nid . '/revisions', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
601 602 603 604
}

function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
605
  db_query("DELETE FROM {node_revision} WHERE nid = %d AND vid = %d", $node_revision->nid, $node_revision->vid);
606
  node_invoke_node($node_revision, 'delete_revision');
607
  watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
608
  drupal_set_message(t('Revision from %revision-date of @type %title has been deleted.', array('%revision-date' => format_date($node_revision->revision_timestamp), '@type' => node_get_types('name', $node_revision), '%title' => $node_revision->title)));
609
  $form_state['redirect'] = 'node/' . $node_revision->nid;
610
  if (db_result(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = %d', $node_revision->nid)) > 1) {
611 612 613
    $form_state['redirect'] .= '/revisions';
  }
}