Code coverage for /20080809/modules/node/node.pages.inc

Line #Times calledCode
1
<?php
2
// $Id: node.pages.inc,v 1.31 2008/07/10 11:12:02 dries Exp $
3
4
/**
5
 * @file
6
 * Page callbacks for adding, editing, deleting, and revisions management
for content.
7
 */
8
9
10
/**
11
 * Menu callback; presents the node editing form, or redirects to delete
confirmation.
12
 */
13272
function node_page_edit($node) {
1464
  drupal_set_title($node->title);
1564
  return drupal_get_form($node->type . '_node_form', $node);
160
}
17
18272
function node_add_page() {
190
  $item = menu_get_item();
200
  $content = system_admin_menu_block($item);
210
  return theme('node_add_list', $content);
220
}
23
24
/**
25
 * Display the list of available node types for node creation.
26
 *
27
 * @ingroup themeable
28
 */
29272
function theme_node_add_list($content) {
300
  $output = '';
31
320
  if ($content) {
330
    $output = '<dl class="node-type-list">';
340
    foreach ($content as $item) {
350
      $output .= '<dt>' . l($item['title'], $item['href'],
$item['localized_options']) . '</dt>';
360
      $output .= '<dd>' . filter_xss_admin($item['description']) .
'</dd>';
370
    }
380
    $output .= '</dl>';
390
  }
400
  return $output;
410
}
42
43
44
/**
45
 * Present a node submission form or a set of links to such forms.
46
 */
47272
function node_add($type) {
4886
  global $user;
49
5086
  $types = node_get_types();
5186
  $type = isset($type) ? str_replace('-', '_', $type) : NULL;
52
  // If a node type has been specified, validate its existence.
5386
  if (isset($types[$type]) && node_access('create', $type)) {
54
    // Initialize settings:
5586
    $node = array('uid' => $user->uid, 'name' => (isset($user->name) ?
$user->name : ''), 'type' => $type, 'language' => '');
56
5786
    drupal_set_title(t('Create @name', array('@name' =>
$types[$type]->name)));
5886
    $output = drupal_get_form($type . '_node_form', $node);
5949
  }
60
6149
  return $output;
620
}
63
64272
function node_form_validate($form, &$form_state) {
6573
  node_validate($form_state['values'], $form);
6673
}
67
68272
function node_object_prepare(&$node) {
69
  // Set up default values, if required.
7081
  $node_options = variable_get('node_options_' . $node->type,
array('status', 'promote'));
71
  // If this is a new node, fill in the default values.
7281
  if (!isset($node->nid)) {
7345
    foreach (array('status', 'promote', 'sticky') as $key) {
7445
      $node->$key = in_array($key, $node_options);
7545
    }
7645
    global $user;
7745
    $node->uid = $user->uid;
7845
    $node->created = time();
7945
  }
80
  else {
8136
    $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
82
  }
83
  // Always use the default revision setting.
8481
  $node->revision = in_array('revision', $node_options);
85
8681
  node_invoke($node, 'prepare');
8781
  node_invoke_nodeapi($node, 'prepare');
8881
}
89
90
/**
91
 * Generate the node add/edit form array.
92
 */
93272
function node_form(&$form_state, $node) {
9483
  global $user;
95
9683
  if (isset($form_state['node'])) {
976
    $node = $form_state['node'] + (array)$node;
986
  }
9983
  if (isset($form_state['node_preview'])) {
1002
    $form['#prefix'] = $form_state['node_preview'];
1012
  }
10283
  $node = (object)$node;
10383
  foreach (array('body', 'title', 'format') as $key) {
10483
    if (!isset($node->$key)) {
10544
      $node->$key = NULL;
10644
    }
10783
  }
10883
  if (!isset($form_state['node_preview'])) {
10981
    node_object_prepare($node);
11081
  }
111
  else {
1122
    $node->build_mode = NODE_BUILD_PREVIEW;
113
  }
114
115
  // Set the id of the top-level form tag
11683
  $form['#id'] = 'node-form';
117
118
  // Basic node information.
119
  // These elements are just values so they are not even sent to the
client.
12083
  foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as
$key) {
12183
    $form[$key] = array(
12283
      '#type' => 'value',
12383
      '#value' => isset($node->$key) ? $node->$key : NULL,
124
    );
12583
  }
126
127
  // Changed must be sent to the client, for later overwrite error
checking.
12883
  $form['changed'] = array(
12983
    '#type' => 'hidden',
13083
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
131
  );
132
  // Get the node-specific bits.
13383
  if ($extra = node_invoke($node, 'form', $form_state)) {
13483
    $form = array_merge_recursive($form, $extra);
13583
  }
13683
  if (!isset($form['title']['#weight'])) {
1370
    $form['title']['#weight'] = -5;
1380
  }
139
14083
  $form['#node'] = $node;
141
142
  // Add a log field if the "Create new revision" option is checked, or if
the
143
  // current user has the ability to check that option.
14483
  if (!empty($node->revision) || user_access('administer nodes')) {
1456
    $form['revision_information'] = array(
1466
      '#type' => 'fieldset',
1476
      '#title' => t('Revision information'),
1486
      '#collapsible' => TRUE,
149
      // Collapsed by default when "Create new revision" is unchecked
1506
      '#collapsed' => !$node->revision,
1516
      '#weight' => 20,
152
    );
1536
    $form['revision_information']['revision'] = array(
1546
      '#access' => user_access('administer nodes'),
1556
      '#type' => 'checkbox',
1566
      '#title' => t('Create new revision'),
1576
      '#default_value' => $node->revision,
158
    );
1596
    $form['revision_information']['log'] = array(
1606
      '#type' => 'textarea',
1616
      '#title' => t('Log message'),
1626
      '#rows' => 2,
1636
      '#description' => t('An explanation of the additions or updates being
made to help other authors understand your motivations.'),
164
    );
1656
  }
166
167
  // Node author information for administrators
16883
  $form['author'] = array(
16983
    '#type' => 'fieldset',
17083
    '#access' => user_access('administer nodes'),
17183
    '#title' => t('Authoring information'),
17283
    '#collapsible' => TRUE,
17383
    '#collapsed' => TRUE,
17483
    '#weight' => 20,
175
  );
17683
  $form['author']['name'] = array(
17783
    '#type' => 'textfield',
17883
    '#title' => t('Authored by'),
17983
    '#maxlength' => 60,
18083
    '#autocomplete_path' => 'user/autocomplete',
18183
    '#default_value' => $node->name ? $node->name : '',
18283
    '#weight' => -1,
18383
    '#description' => t('Leave blank for %anonymous.', array('%anonymous'
=> variable_get('anonymous', t('Anonymous')))),
184
  );
18583
  $form['author']['date'] = array(
18683
    '#type' => 'textfield',
18783
    '#title' => t('Authored on'),
18883
    '#maxlength' => 25,
18983
    '#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'))),
190
  );
191
19283
  if (isset($node->date)) {
19342
    $form['author']['date']['#default_value'] = $node->date;
19442
  }
195
196
  // Node options for administrators
19783
  $form['options'] = array(
19883
    '#type' => 'fieldset',
19983
    '#access' => user_access('administer nodes'),
20083
    '#title' => t('Publishing options'),
20183
    '#collapsible' => TRUE,
20283
    '#collapsed' => TRUE,
20383
    '#weight' => 25,
204
  );
20583
  $form['options']['status'] = array(
20683
    '#type' => 'checkbox',
20783
    '#title' => t('Published'),
20883
    '#default_value' => $node->status,
209
  );
21083
  $form['options']['promote'] = array(
21183
    '#type' => 'checkbox',
21283
    '#title' => t('Promoted to front page'),
21383
    '#default_value' => $node->promote,
214
  );
21583
  $form['options']['sticky'] = array(
21683
    '#type' => 'checkbox',
21783
    '#title' => t('Sticky at top of lists'),
21883
    '#default_value' => $node->sticky,
219
  );
220
221
  // These values are used when the user has no administrator access.
22283
  foreach (array('uid', 'created') as $key) {
22383
    $form[$key] = array(
22483
      '#type' => 'value',
22583
      '#value' => $node->$key,
226
    );
22783
  }
228
229
  // Add the buttons.
23083
  $form['buttons'] = array();
23183
  $form['buttons']['submit'] = array(
23283
    '#type' => 'submit',
23383
    '#value' => t('Save'),
23483
    '#weight' => 5,
23583
    '#submit' => array('node_form_submit'),
236
  );
23783
  $form['buttons']['preview'] = array(
23883
    '#type' => 'submit',
23983
    '#value' => t('Preview'),
24083
    '#weight' => 10,
24183
    '#submit' => array('node_form_build_preview'),
242
  );
24383
  if (!empty($node->nid) && node_access('delete', $node)) {
24417
    $form['buttons']['delete'] = array(
24517
      '#type' => 'submit',
24617
      '#value' => t('Delete'),
24717
      '#weight' => 15,
24817
      '#submit' => array('node_form_delete_submit'),
249
    );
25017
  }
25183
  $form['#validate'][] = 'node_form_validate';
25283
  $form['#theme'] = array($node->type . '_node_form', 'node_form');
25383
  return $form;
2540
}
255
256
/**
257
 * Return a node body field, with format and teaser.
258
 */
259272
function node_body_field(&$node, $label, $word_count) {
260
261
  // Check if we need to restore the teaser at the beginning of the body.
26276
  $include = !isset($node->teaser) || ($node->teaser == substr($node->body,
0, strlen($node->teaser)));
263
264
  $form = array(
26576
    '#after_build' => array('node_teaser_js',
'node_teaser_include_verify'));
266
26776
  $form['#prefix'] = '<div class="body-field-wrapper">';
26876
  $form['#suffix'] = '</div>';
269
27076
  $form['teaser_js'] = array(
27176
    '#type' => 'textarea',
27276
    '#rows' => 10,
27376
    '#teaser' => 'edit-body',
27476
    '#teaser_checkbox' => 'edit-teaser-include',
27576
    '#disabled' => TRUE,
276
  );
277
27876
  $form['teaser_include'] = array(
27976
    '#type' => 'checkbox',
28076
    '#title' => t('Show summary in full view'),
28176
    '#default_value' => $include,
28276
    '#prefix' => '<div class="teaser-checkbox">',
28376
    '#suffix' => '</div>',
284
  );
285
28676
  $form['body'] = array(
28776
    '#type' => 'textarea',
28876
    '#title' => check_plain($label),
28976
    '#default_value' => $include ? $node->body : ($node->teaser .
$node->body),
29076
    '#rows' => 20,
29176
    '#required' => ($word_count > 0),
292
  );
293
29476
  $form['format'] = filter_form($node->format);
295
29676
  return $form;
2970
}
298
299
/**
300
 * Button sumit function: handle the 'Delete' button on the node form.
301
 */
302272
function node_form_delete_submit($form, &$form_state) {
3030
  $destination = '';
3040
  if (isset($_REQUEST['destination'])) {
3050
    $destination = drupal_get_destination();
3060
    unset($_REQUEST['destination']);
3070
  }
3080
  $node = $form['#node'];
3090
  $form_state['redirect'] = array('node/' . $node->nid . '/delete',
$destination);
3100
}
311
312
313272
function node_form_build_preview($form, &$form_state) {
3142
  $node = node_form_submit_build_node($form, $form_state);
3152
  $form_state['node_preview'] = node_preview($node);
3162
}
317
318
/**
319
 * Present a node submission form.
320
 *
321
 * @ingroup themeable
322
 */
323272
function theme_node_form($form) {
32486
  $output = "\n<div class=\"node-form\">\n";
325
326
  // Admin form fields and submit buttons must be rendered first, because
327
  // they need to go to the bottom of the form, and so should not be part
of
328
  // the catch-all call to drupal_render().
32986
  $admin = '';
33086
  if (isset($form['author'])) {
33186
    $admin .= "    <div class=\"authored\">\n";
33286
    $admin .= drupal_render($form['author']);
33386
    $admin .= "    </div>\n";
33486
  }
33586
  if (isset($form['options'])) {
33686
    $admin .= "    <div class=\"options\">\n";
33786
    $admin .= drupal_render($form['options']);
33886
    $admin .= "    </div>\n";
33986
  }
34086
  $buttons = drupal_render($form['buttons']);
341
342
  // Everything else gets rendered here, and is displayed before the admin
form
343
  // field and the submit buttons.
34486
  $output .= "  <div class=\"standard\">\n";
34586
  $output .= drupal_render($form);
34686
  $output .= "  </div>\n";
347
34886
  if (!empty($admin)) {
34986
    $output .= "  <div class=\"admin\">\n";
35086
    $output .= $admin;
35186
    $output .= "  </div>\n";
35286
  }
35386
  $output .= $buttons;
35486
  $output .= "</div>\n";
355
35686
  return $output;
3570
}
358
359
/**
360
 * Generate a node preview.
361
 */
362272
function node_preview($node) {
3632
  if (node_access('create', $node) || node_access('update', $node)) {
364
    // Load the user's name when needed.
3652
    if (isset($node->name)) {
366
      // The use of isset() is mandatory in the context of user IDs,
because
367
      // user ID 0 denotes the anonymous user.
3682
      if ($user = user_load(array('name' => $node->name))) {
3692
        $node->uid = $user->uid;
3702
        $node->picture = $user->picture;
3712
      }
372
      else {
3730
        $node->uid = 0; // anonymous user
374
      }
3752
    }
3760
    else if ($node->uid) {
3770
      $user = user_load(array('uid' => $node->uid));
3780
      $node->name = $user->name;
3790
      $node->picture = $user->picture;
3800
    }
381
3822
    $node->changed = time();
383
384
    // Extract a teaser, if it hasn't been set (e.g. by a module-provided
385
    // 'teaser' form item).
3862
    if (!isset($node->teaser)) {
3870
      $node->teaser = empty($node->body) ? '' : node_teaser($node->body,
$node->format);
388
      // Chop off the teaser from the body if needed.
3890
      if (!$node->teaser_include && $node->teaser == substr($node->body, 0,
strlen($node->teaser))) {
3900
        $node->body = substr($node->body, strlen($node->teaser));
3910
      }
3920
    }
393
394
    // Display a preview of the node.
395
    // Previewing alters $node so it needs to be cloned.
3962
    if (!form_get_errors()) {
3972
      $cloned_node = clone $node;
3982
      $cloned_node->build_mode = NODE_BUILD_PREVIEW;
3992
      $output = theme('node_preview', $cloned_node);
4002
    }
4012
    drupal_set_title(t('Preview'));
402
4032
    return $output;
4040
  }
4050
}
406
407
/**
408
 * Display a node preview for display during node creation and editing.
409
 *
410
 * @param $node
411
 *   The node object which is being previewed.
412
 *
413
 * @ingroup themeable
414
 */
415272
function theme_node_preview($node) {
4162
  $output = '<div class="preview">';
417
4182
  $preview_trimmed_version = FALSE;
419
  // Do we need to preview trimmed version of post as well as full
version?
4202
  if (isset($node->teaser) && isset($node->body)) {
4211
    $teaser = trim($node->teaser);
4221
    $body = trim(str_replace('<!--break-->', '', $node->body));
423
424
    // Preview trimmed version if teaser and body will appear different;
425
    // also (edge case) if both teaser and body have been specified by the
user
426
    // and are actually the same.
4271
    if ($teaser != $body || ($body && strpos($node->body, '<!--break-->')
=== 0)) {
4281
      $preview_trimmed_version = TRUE;
4291
    }
4301
  }
431
4322
  if ($preview_trimmed_version) {
4331
    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>'));
4341
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
4351
    $output .= node_view(clone $node, 1, FALSE, 0);
4361
    $output .= '<h3>' . t('Preview full version') . '</h3>';
4371
    $output .= node_view($node, 0, FALSE, 0);
4381
  }
439
  else {
4401
    $output .= node_view($node, 0, FALSE, 0);
441
  }
4422
  $output .= "</div>\n";
443
4442
  return $output;
4450
}
446
447272
function node_form_submit($form, &$form_state) {
44864
  global $user;
449
45064
  $node = node_form_submit_build_node($form, $form_state);
45164
  $insert = empty($node->nid);
45264
  node_save($node);
45364
  $node_link = l(t('view'), 'node/' . $node->nid);
45464
  $watchdog_args = array('@type' => $node->type, '%title' =>
$node->title);
45564
  $t_args = array('@type' => node_get_types('name', $node), '%title' =>
$node->title);
456
45764
  if ($insert) {
45837
    watchdog('content', '@type: added %title.', $watchdog_args,
WATCHDOG_NOTICE, $node_link);
45937
    drupal_set_message(t('@type %title has been created.', $t_args));
46037
  }
461
  else {
46227
    watchdog('content', '@type: updated %title.', $watchdog_args,
WATCHDOG_NOTICE, $node_link);
46327
    drupal_set_message(t('@type %title has been updated.', $t_args));
464
  }
46564
  if ($node->nid) {
46664
    unset($form_state['rebuild']);
46764
    $form_state['nid'] = $node->nid;
46864
    $form_state['redirect'] = 'node/' . $node->nid;
46964
  }
470
  else {
471
    // In the unlikely case something went wrong on save, the node will be
472
    // rebuilt and node form redisplayed the same way as in preview.
4730
    drupal_set_message(t('The post could not be saved.'), 'error');
474
  }
47564
}
476
477
/**
478
 * Build a node by processing submitted form values and prepare for a form
rebuild.
479
 */
480272
function node_form_submit_build_node($form, &$form_state) {
481
  // Unset any button-level handlers, execute all the form-level submit
482
  // functions to process the form values into an updated node.
48370
  unset($form_state['submit_handlers']);
48470
  form_execute_handlers('submit', $form, $form_state);
48570
  $node = node_submit($form_state['values']);
48670
  $form_state['node'] = (array)$node;
48770
  $form_state['rebuild'] = TRUE;
48870
  return $node;
4890
}
490
491
/**
492
 * Menu callback -- ask for confirmation of node deletion
493
 */
494272
function node_delete_confirm(&$form_state, $node) {
49522
  $form['nid'] = array(
49622
    '#type' => 'value',
49722
    '#value' => $node->nid,
498
  );
499
50022
  return confirm_form($form,
50122
    t('Are you sure you want to delete %title?', array('%title' =>
$node->title)),
50222
    isset($_GET['destination']) ? $_GET['destination'] : 'node/' .
$node->nid,
50322
    t('This action cannot be undone.'),
50422
    t('Delete'),
50522
    t('Cancel')
50622
  );
5070
}
508
509
/**
510
 * Execute node deletion
511
 */
512272
function node_delete_confirm_submit($form, &$form_state) {
51311
  if ($form_state['values']['confirm']) {
51411
    node_delete($form_state['values']['nid']);
51511
  }
516
51711
  $form_state['redirect'] = '<front>';
51811
}
519
520
/**
521
 * Generate an overview table of older revisions of a node.
522
 */
523272
function node_revision_overview($node) {
5243
  drupal_set_title(t('Revisions for %title', array('%title' =>
$node->title)));
525
5263
  $header = array(t('Revision'), array('data' => t('Operations'), 'colspan'
=> 2));
527
5283
  $revisions = node_revision_list($node);
529
5303
  $rows = array();
5313
  $revert_permission = FALSE;
5323
  if ((user_access('revert revisions') || user_access('administer nodes'))
&& node_access('update', $node)) {
5333
    $revert_permission = TRUE;
5343
  }
5353
  $delete_permission = FALSE;
5363
  if ((user_access('delete revisions') || user_access('administer nodes'))
&& node_access('delete', $node)) {
5373
    $delete_permission = TRUE;
5383
  }
5393
  foreach ($revisions as $revision) {
5403
    $row = array();
5413
    $operations = array();
542
5433
    if ($revision->current_vid > 0) {
5443
      $row[] = array('data' => t('!date by !username', array('!date' =>
l(format_date($revision->timestamp, 'small'), "node/$node->nid"),
'!username' => theme('username', $revision)))
5453
                               . (($revision->log != '') ? '<p
class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
5463
                     'class' => 'revision-current');
5473
      $operations[] = array('data' => theme('placeholder', t('current
revision')), 'class' => 'revision-current', 'colspan' => 2);
5483
    }
549
    else {
5503
      $row[] = t('!date by !username', array('!date' =>
l(format_date($revision->timestamp, 'small'),
"node/$node->nid/revisions/$revision->vid/view"), '!username' =>
theme('username', $revision)))
5513
               . (($revision->log != '') ? '<p class="revision-log">' .
filter_xss($revision->log) . '</p>' : '');
5523
      if ($revert_permission) {
5533
        $operations[] = l(t('revert'),
"node/$node->nid/revisions/$revision->vid/revert");
5543
      }
5553
      if ($delete_permission) {
5563
        $operations[] = l(t('delete'),
"node/$node->nid/revisions/$revision->vid/delete");
5573
      }
558
    }
5593
    $rows[] = array_merge($row, $operations);
5603
  }
561
5623
  return theme('table', $header, $rows);
5630
}
564
565
/**
566
 * Ask for confirmation of the reversion to prevent against CSRF attacks.
567
 */
568272
function node_revision_revert_confirm($form_state, $node_revision) {
5692
  $form['#node_revision'] = $node_revision;
5702
  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'));
5710
}
572
573272
function node_revision_revert_confirm_submit($form, &$form_state) {
5741
  $node_revision = $form['#node_revision'];
5751
  $node_revision->revision = 1;
5761
  $node_revision->log = t('Copy of the revision from %date.', array('%date'
=> format_date($node_revision->revision_timestamp)));
5771
  if (module_exists('taxonomy')) {
5781
    $node_revision->taxonomy = array_keys($node_revision->taxonomy);
5791
  }
580
5811
  node_save($node_revision);
582
5831
  watchdog('content', '@type: reverted %title revision %revision.',
array('@type' => $node_revision->type, '%title' => $node_revision->title,
'%revision' => $node_revision->vid));
5841
  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))));
5851
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
5861
}
587
588272
function node_revision_delete_confirm($form_state, $node_revision) {
5892
  $form['#node_revision'] = $node_revision;
5902
  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'));
5910
}
592
593272
function node_revision_delete_confirm_submit($form, &$form_state) {
5941
  $node_revision = $form['#node_revision'];
5951
  db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d",
$node_revision->nid, $node_revision->vid);
5961
  node_invoke_nodeapi($node_revision, 'delete revision');
5971
  watchdog('content', '@type: deleted %title revision %revision.',
array('@type' => $node_revision->type, '%title' => $node_revision->title,
'%revision' => $node_revision->vid));
5981
  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)));
5991
  $form_state['redirect'] = 'node/' . $node_revision->nid;
6001
  if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid
= %d', $node_revision->nid)) > 1) {
6011
    $form_state['redirect'] .= '/revisions';
6021
  }
6031
}
604272