source: sipes/cord/modules/node/node.pages.inc @ 8a8efa8

stableversion-3.0
Last change on this file since 8a8efa8 was d7a822e, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

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