comparison modules/node/node.pages.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 165d43f946a8
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: node.pages.inc,v 1.28 2008/02/03 19:26:10 goba 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 */
13 function node_page_edit($node) {
14 drupal_set_title($node->title);
15 return drupal_get_form($node->type .'_node_form', $node);
16 }
17
18 function node_add_page() {
19 $item = menu_get_item();
20 $content = system_admin_menu_block($item);
21 return theme('node_add_list', $content);
22 }
23
24 /**
25 * Display the list of available node types for node creation.
26 *
27 * @ingroup themeable
28 */
29 function theme_node_add_list($content) {
30 $output = '';
31
32 if ($content) {
33 $output = '<dl class="node-type-list">';
34 foreach ($content as $item) {
35 $output .= '<dt>'. l($item['title'], $item['href'], $item['options']) .'</dt>';
36 $output .= '<dd>'. filter_xss_admin($item['description']) .'</dd>';
37 }
38 $output .= '</dl>';
39 }
40 return $output;
41 }
42
43
44 /**
45 * Present a node submission form or a set of links to such forms.
46 */
47 function node_add($type) {
48 global $user;
49
50 $types = node_get_types();
51 $type = isset($type) ? str_replace('-', '_', $type) : NULL;
52 // If a node type has been specified, validate its existence.
53 if (isset($types[$type]) && node_access('create', $type)) {
54 // Initialize settings:
55 $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => '');
56
57 drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)));
58 $output = drupal_get_form($type .'_node_form', $node);
59 }
60
61 return $output;
62 }
63
64 function node_form_validate($form, &$form_state) {
65 node_validate($form_state['values'], $form);
66 }
67
68 function node_object_prepare(&$node) {
69 // Set up default values, if required.
70 $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
71 // If this is a new node, fill in the default values.
72 if (!isset($node->nid)) {
73 foreach (array('status', 'promote', 'sticky') as $key) {
74 $node->$key = in_array($key, $node_options);
75 }
76 global $user;
77 $node->uid = $user->uid;
78 $node->created = time();
79 }
80 else {
81 $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
82 }
83 // Always use the default revision setting.
84 $node->revision = in_array('revision', $node_options);
85
86 node_invoke($node, 'prepare');
87 node_invoke_nodeapi($node, 'prepare');
88 }
89
90 /**
91 * Generate the node add/edit form array.
92 */
93 function node_form(&$form_state, $node) {
94 global $user;
95
96 if (isset($form_state['node'])) {
97 $node = $form_state['node'] + (array)$node;
98 }
99 if (isset($form_state['node_preview'])) {
100 $form['#prefix'] = $form_state['node_preview'];
101 }
102 $node = (object)$node;
103 foreach (array('body', 'title', 'format') as $key) {
104 if (!isset($node->$key)) {
105 $node->$key = NULL;
106 }
107 }
108 if (!isset($form_state['node_preview'])) {
109 node_object_prepare($node);
110 }
111 else {
112 $node->build_mode = NODE_BUILD_PREVIEW;
113 }
114
115 // Set the id of the top-level form tag
116 $form['#id'] = 'node-form';
117
118 // Basic node information.
119 // These elements are just values so they are not even sent to the client.
120 foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
121 $form[$key] = array(
122 '#type' => 'value',
123 '#value' => isset($node->$key) ? $node->$key : NULL,
124 );
125 }
126
127 // Changed must be sent to the client, for later overwrite error checking.
128 $form['changed'] = array(
129 '#type' => 'hidden',
130 '#default_value' => isset($node->changed) ? $node->changed : NULL,
131 );
132 // Get the node-specific bits.
133 if ($extra = node_invoke($node, 'form', $form_state)) {
134 $form = array_merge_recursive($form, $extra);
135 }
136 if (!isset($form['title']['#weight'])) {
137 $form['title']['#weight'] = -5;
138 }
139
140 $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.
144 if (!empty($node->revision) || user_access('administer nodes')) {
145 $form['revision_information'] = array(
146 '#type' => 'fieldset',
147 '#title' => t('Revision information'),
148 '#collapsible' => TRUE,
149 // Collapsed by default when "Create new revision" is unchecked
150 '#collapsed' => !$node->revision,
151 '#weight' => 20,
152 );
153 $form['revision_information']['revision'] = array(
154 '#access' => user_access('administer nodes'),
155 '#type' => 'checkbox',
156 '#title' => t('Create new revision'),
157 '#default_value' => $node->revision,
158 );
159 $form['revision_information']['log'] = array(
160 '#type' => 'textarea',
161 '#title' => t('Log message'),
162 '#rows' => 2,
163 '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
164 );
165 }
166
167 // Node author information for administrators
168 $form['author'] = array(
169 '#type' => 'fieldset',
170 '#access' => user_access('administer nodes'),
171 '#title' => t('Authoring information'),
172 '#collapsible' => TRUE,
173 '#collapsed' => TRUE,
174 '#weight' => 20,
175 );
176 $form['author']['name'] = array(
177 '#type' => 'textfield',
178 '#title' => t('Authored by'),
179 '#maxlength' => 60,
180 '#autocomplete_path' => 'user/autocomplete',
181 '#default_value' => $node->name ? $node->name : '',
182 '#weight' => -1,
183 '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
184 );
185 $form['author']['date'] = array(
186 '#type' => 'textfield',
187 '#title' => t('Authored on'),
188 '#maxlength' => 25,
189 '#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
192 if (isset($node->date)) {
193 $form['author']['date']['#default_value'] = $node->date;
194 }
195
196 // Node options for administrators
197 $form['options'] = array(
198 '#type' => 'fieldset',
199 '#access' => user_access('administer nodes'),
200 '#title' => t('Publishing options'),
201 '#collapsible' => TRUE,
202 '#collapsed' => TRUE,
203 '#weight' => 25,
204 );
205 $form['options']['status'] = array(
206 '#type' => 'checkbox',
207 '#title' => t('Published'),
208 '#default_value' => $node->status,
209 );
210 $form['options']['promote'] = array(
211 '#type' => 'checkbox',
212 '#title' => t('Promoted to front page'),
213 '#default_value' => $node->promote,
214 );
215 $form['options']['sticky'] = array(
216 '#type' => 'checkbox',
217 '#title' => t('Sticky at top of lists'),
218 '#default_value' => $node->sticky,
219 );
220
221 // These values are used when the user has no administrator access.
222 foreach (array('uid', 'created') as $key) {
223 $form[$key] = array(
224 '#type' => 'value',
225 '#value' => $node->$key,
226 );
227 }
228
229 // Add the buttons.
230 $form['buttons'] = array();
231 $form['buttons']['submit'] = array(
232 '#type' => 'submit',
233 '#value' => t('Save'),
234 '#weight' => 5,
235 '#submit' => array('node_form_submit'),
236 );
237 $form['buttons']['preview'] = array(
238 '#type' => 'submit',
239 '#value' => t('Preview'),
240 '#weight' => 10,
241 '#submit' => array('node_form_build_preview'),
242 );
243 if (!empty($node->nid) && node_access('delete', $node)) {
244 $form['buttons']['delete'] = array(
245 '#type' => 'submit',
246 '#value' => t('Delete'),
247 '#weight' => 15,
248 '#submit' => array('node_form_delete_submit'),
249 );
250 }
251 $form['#validate'][] = 'node_form_validate';
252 $form['#theme'] = array($node->type .'_node_form', 'node_form');
253 return $form;
254 }
255
256 /**
257 * Return a node body field, with format and teaser.
258 */
259 function node_body_field(&$node, $label, $word_count) {
260
261 // Check if we need to restore the teaser at the beginning of the body.
262 $include = !isset($node->teaser) || ($node->teaser == substr($node->body, 0, strlen($node->teaser)));
263
264 $form = array(
265 '#after_build' => array('node_teaser_js', 'node_teaser_include_verify'));
266
267 $form['#prefix'] = '<div class="body-field-wrapper">';
268 $form['#suffix'] = '</div>';
269
270 $form['teaser_js'] = array(
271 '#type' => 'textarea',
272 '#rows' => 10,
273 '#teaser' => 'edit-body',
274 '#teaser_checkbox' => 'edit-teaser-include',
275 '#disabled' => TRUE,
276 );
277
278 $form['teaser_include'] = array(
279 '#type' => 'checkbox',
280 '#title' => t('Show summary in full view'),
281 '#default_value' => $include,
282 '#prefix' => '<div class="teaser-checkbox">',
283 '#suffix' => '</div>',
284 );
285
286 $form['body'] = array(
287 '#type' => 'textarea',
288 '#title' => check_plain($label),
289 '#default_value' => $include ? $node->body : ($node->teaser . $node->body),
290 '#rows' => 20,
291 '#required' => ($word_count > 0),
292 );
293
294 $form['format'] = filter_form($node->format);
295
296 return $form;
297 }
298
299 /**
300 * Button sumit function: handle the 'Delete' button on the node form.
301 */
302 function node_form_delete_submit($form, &$form_state) {
303 $destination = '';
304 if (isset($_REQUEST['destination'])) {
305 $destination = drupal_get_destination();
306 unset($_REQUEST['destination']);
307 }
308 $node = $form['#node'];
309 $form_state['redirect'] = array('node/'. $node->nid .'/delete', $destination);
310 }
311
312
313 function node_form_build_preview($form, &$form_state) {
314 $node = node_form_submit_build_node($form, $form_state);
315 $form_state['node_preview'] = node_preview($node);
316 }
317
318 /**
319 * Present a node submission form.
320 *
321 * @ingroup themeable
322 */
323 function theme_node_form($form) {
324 $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().
329 $admin = '';
330 if (isset($form['author'])) {
331 $admin .= " <div class=\"authored\">\n";
332 $admin .= drupal_render($form['author']);
333 $admin .= " </div>\n";
334 }
335 if (isset($form['options'])) {
336 $admin .= " <div class=\"options\">\n";
337 $admin .= drupal_render($form['options']);
338 $admin .= " </div>\n";
339 }
340 $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.
344 $output .= " <div class=\"standard\">\n";
345 $output .= drupal_render($form);
346 $output .= " </div>\n";
347
348 if (!empty($admin)) {
349 $output .= " <div class=\"admin\">\n";
350 $output .= $admin;
351 $output .= " </div>\n";
352 }
353 $output .= $buttons;
354 $output .= "</div>\n";
355
356 return $output;
357 }
358
359 /**
360 * Generate a node preview.
361 */
362 function node_preview($node) {
363 if (node_access('create', $node) || node_access('update', $node)) {
364 // Load the user's name when needed.
365 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.
368 if ($user = user_load(array('name' => $node->name))) {
369 $node->uid = $user->uid;
370 $node->picture = $user->picture;
371 }
372 else {
373 $node->uid = 0; // anonymous user
374 }
375 }
376 else if ($node->uid) {
377 $user = user_load(array('uid' => $node->uid));
378 $node->name = $user->name;
379 $node->picture = $user->picture;
380 }
381
382 $node->changed = time();
383
384 // Extract a teaser, if it hasn't been set (e.g. by a module-provided
385 // 'teaser' form item).
386 if (!isset($node->teaser)) {
387 $node->teaser = empty($node->body) ? '' : node_teaser($node->body, $node->format);
388 // Chop off the teaser from the body if needed.
389 if (!$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
390 $node->body = substr($node->body, strlen($node->teaser));
391 }
392 }
393
394 // Display a preview of the node.
395 // Previewing alters $node so it needs to be cloned.
396 if (!form_get_errors()) {
397 $cloned_node = drupal_clone($node);
398 $cloned_node->build_mode = NODE_BUILD_PREVIEW;
399 $output = theme('node_preview', $cloned_node);
400 }
401 drupal_set_title(t('Preview'));
402
403 return $output;
404 }
405 }
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 */
415 function theme_node_preview($node) {
416 $output = '<div class="preview">';
417
418 $preview_trimmed_version = FALSE;
419 // Do we need to preview trimmed version of post as well as full version?
420 if (isset($node->teaser) && isset($node->body)) {
421 $teaser = trim($node->teaser);
422 $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.
427 if ($teaser != $body || ($body && strpos($node->body, '<!--break-->') === 0)) {
428 $preview_trimmed_version = TRUE;
429 }
430 }
431
432 if ($preview_trimmed_version) {
433 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>'));
434 $output .= '<h3>'. t('Preview trimmed version') .'</h3>';
435 $output .= node_view(drupal_clone($node), 1, FALSE, 0);
436 $output .= '<h3>'. t('Preview full version') .'</h3>';
437 $output .= node_view($node, 0, FALSE, 0);
438 }
439 else {
440 $output .= node_view($node, 0, FALSE, 0);
441 }
442 $output .= "</div>\n";
443
444 return $output;
445 }
446
447 function node_form_submit($form, &$form_state) {
448 global $user;
449
450 $node = node_form_submit_build_node($form, $form_state);
451 $insert = empty($node->nid);
452 node_save($node);
453 $node_link = l(t('view'), 'node/'. $node->nid);
454 $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
455 $t_args = array('@type' => node_get_types('name', $node), '%title' => $node->title);
456
457 if ($insert) {
458 watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
459 drupal_set_message(t('@type %title has been created.', $t_args));
460 }
461 else {
462 watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
463 drupal_set_message(t('@type %title has been updated.', $t_args));
464 }
465 if ($node->nid) {
466 unset($form_state['rebuild']);
467 $form_state['nid'] = $node->nid;
468 $form_state['redirect'] = 'node/'. $node->nid;
469 }
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.
473 drupal_set_message(t('The post could not be saved.'), 'error');
474 }
475 }
476
477 /**
478 * Build a node by processing submitted form values and prepare for a form rebuild.
479 */
480 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.
483 unset($form_state['submit_handlers']);
484 form_execute_handlers('submit', $form, $form_state);
485 $node = node_submit($form_state['values']);
486 $form_state['node'] = (array)$node;
487 $form_state['rebuild'] = TRUE;
488 return $node;
489 }
490
491 /**
492 * Menu callback -- ask for confirmation of node deletion
493 */
494 function node_delete_confirm(&$form_state, $node) {
495 $form['nid'] = array(
496 '#type' => 'value',
497 '#value' => $node->nid,
498 );
499
500 return confirm_form($form,
501 t('Are you sure you want to delete %title?', array('%title' => $node->title)),
502 isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid,
503 t('This action cannot be undone.'),
504 t('Delete'),
505 t('Cancel')
506 );
507 }
508
509 /**
510 * Execute node deletion
511 */
512 function node_delete_confirm_submit($form, &$form_state) {
513 if ($form_state['values']['confirm']) {
514 node_delete($form_state['values']['nid']);
515 }
516
517 $form_state['redirect'] = '<front>';
518 }
519
520 /**
521 * Generate an overview table of older revisions of a node.
522 */
523 function node_revision_overview($node) {
524 drupal_set_title(t('Revisions for %title', array('%title' => $node->title)));
525
526 $header = array(t('Revision'), array('data' => t('Operations'), 'colspan' => 2));
527
528 $revisions = node_revision_list($node);
529
530 $rows = array();
531 $revert_permission = FALSE;
532 if ((user_access('revert revisions') || user_access('administer nodes')) && node_access('update', $node)) {
533 $revert_permission = TRUE;
534 }
535 $delete_permission = FALSE;
536 if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
537 $delete_permission = TRUE;
538 }
539 foreach ($revisions as $revision) {
540 $row = array();
541 $operations = array();
542
543 if ($revision->current_vid > 0) {
544 $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid"), '!username' => theme('username', $revision)))
545 . (($revision->log != '') ? '<p class="revision-log">'. filter_xss($revision->log) .'</p>' : ''),
546 'class' => 'revision-current');
547 $operations[] = array('data' => theme('placeholder', t('current revision')), 'class' => 'revision-current', 'colspan' => 2);
548 }
549 else {
550 $row[] = t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => theme('username', $revision)))
551 . (($revision->log != '') ? '<p class="revision-log">'. filter_xss($revision->log) .'</p>' : '');
552 if ($revert_permission) {
553 $operations[] = l(t('revert'), "node/$node->nid/revisions/$revision->vid/revert");
554 }
555 if ($delete_permission) {
556 $operations[] = l(t('delete'), "node/$node->nid/revisions/$revision->vid/delete");
557 }
558 }
559 $rows[] = array_merge($row, $operations);
560 }
561
562 return theme('table', $header, $rows);
563 }
564
565 /**
566 * Ask for confirmation of the reversion to prevent against CSRF attacks.
567 */
568 function node_revision_revert_confirm($form_state, $node_revision) {
569 $form['#node_revision'] = $node_revision;
570 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'));
571 }
572
573 function node_revision_revert_confirm_submit($form, &$form_state) {
574 $node_revision = $form['#node_revision'];
575 $node_revision->revision = 1;
576 $node_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($node_revision->revision_timestamp)));
577 if (module_exists('taxonomy')) {
578 $node_revision->taxonomy = array_keys($node_revision->taxonomy);
579 }
580
581 node_save($node_revision);
582
583 watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
584 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))));
585 $form_state['redirect'] = 'node/'. $node_revision->nid .'/revisions';
586 }
587
588 function node_revision_delete_confirm($form_state, $node_revision) {
589 $form['#node_revision'] = $node_revision;
590 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'));
591 }
592
593 function node_revision_delete_confirm_submit($form, &$form_state) {
594 $node_revision = $form['#node_revision'];
595 db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $node_revision->nid, $node_revision->vid);
596 node_invoke_nodeapi($node_revision, 'delete revision');
597 watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
598 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)));
599 $form_state['redirect'] = 'node/'. $node_revision->nid;
600 if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $node_revision->nid)) > 1) {
601 $form_state['redirect'] .= '/revisions';
602 }
603 }