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

Line #Times calledCode
1
<?php
2
// $Id: node.pages.inc,v 1.45 2008/10/13 00:33:03 webchick 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
 */
13368
function node_page_edit($node) {
1465
  drupal_set_title($node->title);
1565
  return drupal_get_form($node->type . '_node_form', $node);
160
}
17
18368
function node_add_page() {
190
  $item = menu_get_item();
200
  $content = system_admin_menu_block($item);
21
  // Bypass the node/add listing if only one content type is available.
220
  if (count($content) == 1) {
230
    $item = array_shift($content);
240
    drupal_goto($item['href']);
250
  }
260
  return theme('node_add_list', $content);
270
}
28
29
/**
30
 * Display the list of available node types for node creation.
31
 *
32
 * @ingroup themeable
33
 */
34368
function theme_node_add_list($content) {
350
  $output = '';
36
370
  if ($content) {
380
    $output = '<dl class="node-type-list">';
390
    foreach ($content as $item) {
400
      $output .= '<dt>' . l($item['title'], $item['href'],
$item['localized_options']) . '</dt>';
410
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
420
    }
430
    $output .= '</dl>';
440
  }
450
  return $output;
460
}
47
48
49
/**
50
 * Present a node submission form or a set of links to such forms.
51
 */
52368
function node_add($type) {
5394
  global $user;
54
5594
  $types = node_get_types();
5694
  $type = isset($type) ? str_replace('-', '_', $type) : NULL;
57
  // If a node type has been specified, validate its existence.
5894
  if (isset($types[$type]) && node_access('create', $type)) {
59
    // Initialize settings:
6094
    $node = array('uid' => $user->uid, 'name' => (isset($user->name) ?
$user->name : ''), 'type' => $type, 'language' => '');
61
6294
    drupal_set_title(t('Create @name', array('@name' =>
$types[$type]->name)), PASS_THROUGH);
6394
    $output = drupal_get_form($type . '_node_form', $node);
6454
  }
65
6654
  return $output;
670
}
68
69368
function node_form_validate($form, &$form_state) {
7077
  node_validate($form_state['values'], $form);
7177
}
72
73368
function node_object_prepare(&$node) {
74
  // Set up default values, if required.
7586
  $node_options = variable_get('node_options_' . $node->type,
array('status', 'promote'));
76
  // If this is a new node, fill in the default values.
7786
  if (!isset($node->nid)) {
7849
    foreach (array('status', 'promote', 'sticky') as $key) {
7949
      $node->$key = in_array($key, $node_options);
8049
    }
8149
    global $user;
8249
    $node->uid = $user->uid;
8349
    $node->created = REQUEST_TIME;
8449
  }
85
  else {
8637
    $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
87
  }
88
  // Always use the default revision setting.
8986
  $node->revision = in_array('revision', $node_options);
90
9186
  node_invoke($node, 'prepare');
9286
  node_invoke_nodeapi($node, 'prepare');
9386
}
94
95
/**
96
 * Generate the node add/edit form array.
97
 */
98368
function node_form(&$form_state, $node) {
9989
  global $user;
100
10189
  if (isset($form_state['node'])) {
1027
    $node = $form_state['node'] + (array)$node;
1037
  }
10489
  if (isset($form_state['node_preview'])) {
1053
    $form['#prefix'] = $form_state['node_preview'];
1063
  }
10789
  $node = (object)$node;
10889
  foreach (array('body', 'title', 'format') as $key) {
10989
    if (!isset($node->$key)) {
11048
      $node->$key = NULL;
11148
    }
11289
  }
11389
  if (!isset($form_state['node_preview'])) {
11486
    node_object_prepare($node);
11586
  }
116
  else {
1173
    $node->build_mode = NODE_BUILD_PREVIEW;
118
  }
119
120
  // Set the id and identify this as a node edit form.
12189
  $form['#id'] = 'node-form';
12289
  $form['#node_edit_form'] = TRUE;
123
124
  // Basic node information.
125
  // These elements are just values so they are not even sent to the
client.
12689
  foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as
$key) {
12789
    $form[$key] = array(
12889
      '#type' => 'value',
12989
      '#value' => isset($node->$key) ? $node->$key : NULL,
130
    );
13189
  }
132
133
  // Changed must be sent to the client, for later overwrite error
checking.
13489
  $form['changed'] = array(
13589
    '#type' => 'hidden',
13689
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
137
  );
138
  // Get the node-specific bits.
13989
  if ($extra = node_invoke($node, 'form', $form_state)) {
14089
    $form = array_merge_recursive($form, $extra);
14189
  }
14289
  if (!isset($form['title']['#weight'])) {
1430
    $form['title']['#weight'] = -5;
1440
  }
145
14689
  $form['#node'] = $node;
147
148
  // Add a log field if the "Create new revision" option is checked, or if
the
149
  // current user has the ability to check that option.
15089
  if (!empty($node->revision) || user_access('administer nodes')) {
1516
    $form['revision_information'] = array(
1526
      '#type' => 'fieldset',
1536
      '#title' => t('Revision information'),
1546
      '#collapsible' => TRUE,
155
      // Collapsed by default when "Create new revision" is unchecked
1566
      '#collapsed' => !$node->revision,
1576
      '#weight' => 20,
158
    );
1596
    $form['revision_information']['revision'] = array(
1606
      '#access' => user_access('administer nodes'),
1616
      '#type' => 'checkbox',
1626
      '#title' => t('Create new revision'),
1636
      '#default_value' => $node->revision,
164
    );
1656
    $form['revision_information']['log'] = array(
1666
      '#type' => 'textarea',
1676
      '#title' => t('Log message'),
1686
      '#rows' => 2,
1696
      '#description' => t('An explanation of the additions or updates being
made to help other authors understand your motivations.'),
170
    );
1716
  }
172
173
  // Node author information for administrators
17489
  $form['author'] = array(
17589
    '#type' => 'fieldset',
17689
    '#access' => user_access('administer nodes'),
17789
    '#title' => t('Authoring information'),
17889
    '#collapsible' => TRUE,
17989
    '#collapsed' => TRUE,
18089
    '#weight' => 90,
181
  );
18289
  $form['author']['name'] = array(
18389
    '#type' => 'textfield',
18489
    '#title' => t('Authored by'),
18589
    '#maxlength' => 60,
18689
    '#autocomplete_path' => 'user/autocomplete',
18789
    '#default_value' => !empty($node->name) ? $node->name : '',
18889
    '#weight' => -1,
18989
    '#description' => t('Leave blank for %anonymous.', array('%anonymous'
=> variable_get('anonymous', t('Anonymous')))),
190
  );
19189
  $form['author']['date'] = array(
19289
    '#type' => 'textfield',
19389
    '#title' => t('Authored on'),
19489
    '#maxlength' => 25,
19589
    '#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'))),
196
  );
197
19889
  if (isset($node->date)) {
19944
    $form['author']['date']['#default_value'] = $node->date;
20044
  }
201
202
  // Node options for administrators
20389
  $form['options'] = array(
20489
    '#type' => 'fieldset',
20589
    '#access' => user_access('administer nodes'),
20689
    '#title' => t('Publishing options'),
20789
    '#collapsible' => TRUE,
20889
    '#collapsed' => TRUE,
20989
    '#weight' => 95,
210
  );
21189
  $form['options']['status'] = array(
21289
    '#type' => 'checkbox',
21389
    '#title' => t('Published'),
21489
    '#default_value' => $node->status,
215
  );
21689
  $form['options']['promote'] = array(
21789
    '#type' => 'checkbox',
21889
    '#title' => t('Promoted to front page'),
21989
    '#default_value' => $node->promote,
220
  );
22189
  $form['options']['sticky'] = array(
22289
    '#type' => 'checkbox',
22389
    '#title' => t('Sticky at top of lists'),
22489
    '#default_value' => $node->sticky,
225
  );
226
227
  // These values are used when the user has no administrator access.
22889
  foreach (array('uid', 'created') as $key) {
22989
    $form[$key] = array(
23089
      '#type' => 'value',
23189
      '#value' => $node->$key,
232
    );
23389
  }
234
235
  // Add the buttons.
23689
  $form['buttons'] = array();
23789
  $form['buttons']['#weight'] = 100;
23889
  $form['buttons']['submit'] = array(
23989
    '#type' => 'submit',
24089
    '#access' => !variable_get('node_preview', 0) || (!form_get_errors() &&
isset($form_state['node_preview'])),
24189
    '#value' => t('Save'),
24289
    '#weight' => 5,
24389
    '#submit' => array('node_form_submit'),
244
  );
24589
  $form['buttons']['preview'] = array(
24689
    '#type' => 'submit',
24789
    '#value' => t('Preview'),
24889
    '#weight' => 10,
24989
    '#submit' => array('node_form_build_preview'),
250
  );
25189
  if (!empty($node->nid) && node_access('delete', $node)) {
25218
    $form['buttons']['delete'] = array(
25318
      '#type' => 'submit',
25418
      '#value' => t('Delete'),
25518
      '#weight' => 15,
25618
      '#submit' => array('node_form_delete_submit'),
257
    );
25818
  }
25989
  $form['#validate'][] = 'node_form_validate';
26089
  $form['#theme'] = array($node->type . '_node_form', 'node_form');
26189
  return $form;
2620
}
263
264
/**
265
 * Return a node body field, with format and teaser.
266
 */
267368
function node_body_field(&$node, $label, $word_count) {
268
269
  // Check if we need to restore the teaser at the beginning of the body.
27082
  $include = !isset($node->teaser) || ($node->teaser == substr($node->body,
0, strlen($node->teaser)));
271
272
  $form = array(
27382
    '#after_build' => array('node_teaser_js',
'node_teaser_include_verify'));
274
27582
  $form['#prefix'] = '<div class="body-field-wrapper">';
27682
  $form['#suffix'] = '</div>';
277
27882
  $form['teaser_js'] = array(
27982
    '#type' => 'textarea',
28082
    '#rows' => 10,
28182
    '#teaser' => 'edit-body',
28282
    '#teaser_checkbox' => 'edit-teaser-include',
28382
    '#disabled' => TRUE,
284
  );
285
28682
  $form['teaser_include'] = array(
28782
    '#type' => 'checkbox',
28882
    '#title' => t('Show summary in full view'),
28982
    '#default_value' => $include,
29082
    '#prefix' => '<div class="teaser-checkbox">',
29182
    '#suffix' => '</div>',
292
  );
293
29482
  $form['body'] = array(
29582
    '#type' => 'textarea',
29682
    '#title' => check_plain($label),
29782
    '#default_value' => $include ? $node->body : ($node->teaser .
$node->body),
29882
    '#rows' => 20,
29982
    '#required' => ($word_count > 0),
30082
    '#input_format' => isset($node->format) ? $node->format :
FILTER_FORMAT_DEFAULT,
301
  );
302
30382
  return $form;
3040
}
305
306
/**
307
 * Button sumit function: handle the 'Delete' button on the node form.
308
 */
309368
function node_form_delete_submit($form, &$form_state) {
3100
  $destination = '';
3110
  if (isset($_REQUEST['destination'])) {
3120
    $destination = drupal_get_destination();
3130
    unset($_REQUEST['destination']);
3140
  }
3150
  $node = $form['#node'];
3160
  $form_state['redirect'] = array('node/' . $node->nid . '/delete',
$destination);
3170
}
318
319
320368
function node_form_build_preview($form, &$form_state) {
3213
  $node = node_form_submit_build_node($form, $form_state);
3223
  $form_state['node_preview'] = node_preview($node);
3233
}
324
325
/**
326
 * Present a node submission form.
327
 *
328
 * @ingroup themeable
329
 */
330368
function theme_node_form($form) {
33192
  $output = "\n<div class=\"node-form\">\n";
332
33392
  $output .= "  <div class=\"standard\">\n";
33492
  $output .= drupal_render($form);
33592
  $output .= "  </div>\n";
336
33792
  $output .= "</div>\n";
338
33992
  return $output;
3400
}
341
342
/**
343
 * Generate a node preview.
344
 */
345368
function node_preview($node) {
3463
  if (node_access('create', $node) || node_access('update', $node)) {
347
    // Load the user's name when needed.
3483
    if (isset($node->name)) {
349
      // The use of isset() is mandatory in the context of user IDs,
because
350
      // user ID 0 denotes the anonymous user.
3513
      if ($user = user_load(array('name' => $node->name))) {
3523
        $node->uid = $user->uid;
3533
        $node->picture = $user->picture;
3543
      }
355
      else {
3560
        $node->uid = 0; // anonymous user
357
      }
3583
    }
3590
    elseif ($node->uid) {
3600
      $user = user_load(array('uid' => $node->uid));
3610
      $node->name = $user->name;
3620
      $node->picture = $user->picture;
3630
    }
364
3653
    $node->changed = REQUEST_TIME;
366
367
    // Extract a teaser, if it hasn't been set (e.g. by a module-provided
368
    // 'teaser' form item).
3693
    if (!isset($node->teaser)) {
3700
      $node->teaser = empty($node->body) ? '' : node_teaser($node->body,
$node->format);
371
      // Chop off the teaser from the body if needed.
3720
      if (!$node->teaser_include && $node->teaser == substr($node->body, 0,
strlen($node->teaser))) {
3730
        $node->body = substr($node->body, strlen($node->teaser));
3740
      }
3750
    }
376
377
    // Display a preview of the node.
378
    // Previewing alters $node so it needs to be cloned.
3793
    if (!form_get_errors()) {
3803
      $cloned_node = clone $node;
3813
      $cloned_node->build_mode = NODE_BUILD_PREVIEW;
3823
      $output = theme('node_preview', $cloned_node);
3833
    }
3843
    drupal_set_title(t('Preview'), PASS_THROUGH);
385
3863
    return $output;
3870
  }
3880
}
389
390
/**
391
 * Display a node preview for display during node creation and editing.
392
 *
393
 * @param $node
394
 *   The node object which is being previewed.
395
 *
396
 * @ingroup themeable
397
 */
398368
function theme_node_preview($node) {
3993
  $output = '<div class="preview">';
400
4013
  $preview_trimmed_version = FALSE;
402
  // Do we need to preview trimmed version of post as well as full version?
4033
  if (isset($node->teaser) && isset($node->body)) {
4042
    $teaser = trim($node->teaser);
4052
    $body = trim(str_replace('<!--break-->', '', $node->body));
406
407
    // Preview trimmed version if teaser and body will appear different;
408
    // also (edge case) if both teaser and body have been specified by the
user
409
    // and are actually the same.
4102
    if ($teaser != $body || ($body && strpos($node->body, '<!--break-->')
=== 0)) {
4111
      $preview_trimmed_version = TRUE;
4121
    }
4132
  }
414
4153
  if ($preview_trimmed_version) {
4161
    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>'));
4171
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
4181
    $output .= node_view(clone $node, 1, FALSE, 0);
4191
    $output .= '<h3>' . t('Preview full version') . '</h3>';
4201
    $output .= node_view($node, 0, FALSE, 0);
4211
  }
422
  else {
4232
    $output .= node_view($node, 0, FALSE, 0);
424
  }
4253
  $output .= "</div>\n";
426
4273
  return $output;
4280
}
429
430368
function node_form_submit($form, &$form_state) {
43167
  global $user;
432
43367
  $node = node_form_submit_build_node($form, $form_state);
43467
  $insert = empty($node->nid);
43567
  node_save($node);
43667
  $node_link = l(t('view'), 'node/' . $node->nid);
43767
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
43867
  $t_args = array('@type' => node_get_types('name', $node), '%title' =>
$node->title);
439
44067
  if ($insert) {
44140
    watchdog('content', '@type: added %title.', $watchdog_args,
WATCHDOG_NOTICE, $node_link);
44240
    drupal_set_message(t('@type %title has been created.', $t_args));
44340
  }
444
  else {
44527
    watchdog('content', '@type: updated %title.', $watchdog_args,
WATCHDOG_NOTICE, $node_link);
44627
    drupal_set_message(t('@type %title has been updated.', $t_args));
447
  }
44867
  if ($node->nid) {
44967
    unset($form_state['rebuild']);
45067
    $form_state['nid'] = $node->nid;
45167
    $form_state['redirect'] = 'node/' . $node->nid;
45267
  }
453
  else {
454
    // In the unlikely case something went wrong on save, the node will be
455
    // rebuilt and node form redisplayed the same way as in preview.
4560
    drupal_set_message(t('The post could not be saved.'), 'error');
457
  }
45867
}
459
460
/**
461
 * Build a node by processing submitted form values and prepare for a form
rebuild.
462
 */
463368
function node_form_submit_build_node($form, &$form_state) {
464
  // Unset any button-level handlers, execute all the form-level submit
465
  // functions to process the form values into an updated node.
46674
  unset($form_state['submit_handlers']);
46774
  form_execute_handlers('submit', $form, $form_state);
46874
  $node = node_submit($form_state['values']);
46974
  $form_state['node'] = (array)$node;
47074
  $form_state['rebuild'] = TRUE;
47174
  return $node;
4720
}
473
474
/**
475
 * Menu callback -- ask for confirmation of node deletion
476
 */
477368
function node_delete_confirm(&$form_state, $node) {
47822
  $form['nid'] = array(
47922
    '#type' => 'value',
48022
    '#value' => $node->nid,
481
  );
482
48322
  return confirm_form($form,
48422
    t('Are you sure you want to delete %title?', array('%title' =>
$node->title)),
48522
    isset($_GET['destination']) ? $_GET['destination'] : 'node/' .
$node->nid,
48622
    t('This action cannot be undone.'),
48722
    t('Delete'),
48822
    t('Cancel')
48922
  );
4900
}
491
492
/**
493
 * Execute node deletion
494
 */
495368
function node_delete_confirm_submit($form, &$form_state) {
49611
  if ($form_state['values']['confirm']) {
49711
    node_delete($form_state['values']['nid']);
49811
  }
499
50011
  $form_state['redirect'] = '<front>';
50111
}
502
503
/**
504
 * Generate an overview table of older revisions of a node.
505
 */
506368
function node_revision_overview($node) {
5073
  drupal_set_title(t('Revisions for %title', array('%title' =>
$node->title)), PASS_THROUGH);
508
5093
  $header = array(t('Revision'), array('data' => t('Operations'), 'colspan'
=> 2));
510
5113
  $revisions = node_revision_list($node);
512
5133
  $rows = array();
5143
  $revert_permission = FALSE;
5153
  if ((user_access('revert revisions') || user_access('administer nodes'))
&& node_access('update', $node)) {
5163
    $revert_permission = TRUE;
5173
  }
5183
  $delete_permission = FALSE;
5193
  if ((user_access('delete revisions') || user_access('administer nodes'))
&& node_access('delete', $node)) {
5203
    $delete_permission = TRUE;
5213
  }
5223
  foreach ($revisions as $revision) {
5233
    $row = array();
5243
    $operations = array();
525
5263
    if ($revision->current_vid > 0) {
5273
      $row[] = array('data' => t('!date by !username', array('!date' =>
l(format_date($revision->timestamp, 'small'), "node/$node->nid"),
'!username' => theme('username', $revision)))
5283
                               . (($revision->log != '') ? '<p
class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
5293
                     'class' => 'revision-current');
5303
      $operations[] = array('data' => theme('placeholder', t('current
revision')), 'class' => 'revision-current', 'colspan' => 2);
5313
    }
532
    else {
5333
      $row[] = t('!date by !username', array('!date' =>
l(format_date($revision->timestamp, 'small'),
"node/$node->nid/revisions/$revision->vid/view"), '!username' =>
theme('username', $revision)))
5343
               . (($revision->log != '') ? '<p class="revision-log">' .
filter_xss($revision->log) . '</p>' : '');
5353
      if ($revert_permission) {
5363
        $operations[] = l(t('revert'),
"node/$node->nid/revisions/$revision->vid/revert");
5373
      }
5383
      if ($delete_permission) {
5393
        $operations[] = l(t('delete'),
"node/$node->nid/revisions/$revision->vid/delete");
5403
      }
541
    }
5423
    $rows[] = array_merge($row, $operations);
5433
  }
544
5453
  return theme('table', $header, $rows);
5460
}
547
548
/**
549
 * Ask for confirmation of the reversion to prevent against CSRF attacks.
550
 */
551368
function node_revision_revert_confirm($form_state, $node_revision) {
5522
  $form['#node_revision'] = $node_revision;
5532
  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'));
5540
}
555
556368
function node_revision_revert_confirm_submit($form, &$form_state) {
5571
  $node_revision = $form['#node_revision'];
5581
  $node_revision->revision = 1;
5591
  $node_revision->log = t('Copy of the revision from %date.', array('%date'
=> format_date($node_revision->revision_timestamp)));
5601
  if (module_exists('taxonomy')) {
5611
    $node_revision->taxonomy = array_keys($node_revision->taxonomy);
5621
  }
563
5641
  node_save($node_revision);
565
5661
  watchdog('content', '@type: reverted %title revision %revision.',
array('@type' => $node_revision->type, '%title' => $node_revision->title,
'%revision' => $node_revision->vid));
5671
  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))));
5681
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
5691
}
570
571368
function node_revision_delete_confirm($form_state, $node_revision) {
5722
  $form['#node_revision'] = $node_revision;
5732
  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'));
5740
}
575
576368
function node_revision_delete_confirm_submit($form, &$form_state) {
5771
  $node_revision = $form['#node_revision'];
5781
  db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d",
$node_revision->nid, $node_revision->vid);
5791
  node_invoke_nodeapi($node_revision, 'delete_revision');
5801
  watchdog('content', '@type: deleted %title revision %revision.',
array('@type' => $node_revision->type, '%title' => $node_revision->title,
'%revision' => $node_revision->vid));
5811
  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)));
5821
  $form_state['redirect'] = 'node/' . $node_revision->nid;
5831
  if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid
= %d', $node_revision->nid)) > 1) {
5841
    $form_state['redirect'] .= '/revisions';
5851
  }
5861
}
587368