Merging DRUPAL-6--2 back into HEAD before branching to DRUPAL-6--3.
[project/panels.git] / contexts / node_edit_form.inc
1 <?php
2 // $Id$
3
4
5 /**
6 * @file contexts/node_edit_form.inc
7 *
8 * Plugin to provide a node_edit_form context
9 */
10
11 function panels_node_edit_form_panels_contexts() {
12 $args['node_edit_form'] = array(
13 'title' => t("Node edit form"),
14 'description' => t('A node edit form.'),
15 'context' => 'panels_context_create_node_edit_form',
16 'settings form' => 'panels_context_node_edit_form_settings_form',
17 'settings form validate' => 'panels_context_node_edit_form_settings_form_validate',
18 'keyword' => 'node_edit',
19 'context name' => 'node_edit_form',
20 );
21 return $args;
22 }
23
24 /**
25 * It's important to remember that $conf is optional here, because contexts
26 * are not always created from the UI.
27 */
28 function panels_context_create_node_edit_form($empty, $node = NULL, $conf = FALSE) {
29 $context = new panels_context(array('form', 'node_edit', 'node_form', 'node'));
30 $context->plugin = 'node_edit_form';
31
32 if ($empty) {
33 return $context;
34 }
35
36 if ($conf) {
37 // In this case, $node is actually our $conf array.
38 $node = node_load($node['nid']);
39 }
40
41 if (!empty($node) && node_access('update', $node)) {
42 // This is from node_edit_page cause Drupal still doesn't use fapi right.
43 if ($_POST['op'] == t('Delete')) {
44 // Note: we redirect from node/nid/edit to node/nid/delete to make the tabs disappear.
45 if ($_REQUEST['destination']) {
46 $destination = drupal_get_destination();
47 unset($_REQUEST['destination']);
48 }
49 drupal_goto('node/'. $node->nid .'/delete', $destination);
50 }
51
52 $form = drupal_retrieve_form($node->type . '_node_form', $node);
53 drupal_process_form($node->type . '_node_form', $form);
54 // Fill in the 'node' portion of the context
55 $context->data = $node;
56 $context->title = $node->title;
57 $context->argument = $node->nid;
58
59 $context->form = $form;
60 $context->form_id = $node->type . '_node_form';
61 $context->form_title = $node->title;
62 $context->node_type = $node->type;
63 return $context;
64 }
65 }
66
67 function panels_context_node_edit_form_settings_form($conf, $external = FALSE) {
68 if ($external) {
69 $form['external'] = array(
70 '#type' => 'checkbox',
71 '#default_value' => $conf['external'],
72 '#title' => t('Require this context from an external source (such as a containing panel page).'),
73 '#description' => t('If selected, node selection (below) will be ignored.'),
74 );
75 }
76
77 $form['node'] = array(
78 '#prefix' => '<div class="no-float">',
79 '#suffix' => '</div>',
80 '#title' => t('Enter the title or NID of a post'),
81 '#type' => 'textfield',
82 '#maxlength' => 512,
83 '#autocomplete_path' => 'panels/node/autocomplete',
84 '#weight' => -10,
85 );
86
87 if (!empty($conf['nid'])) {
88 $info = db_fetch_object(db_query("SELECT * FROM {node} WHERE nid = %d", $conf['nid']));
89 if ($info) {
90 $link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('target' => '_blank', 'title' => t('Open in new window')));
91 $form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
92 }
93 }
94
95 $form['nid'] = array(
96 '#type' => 'value',
97 '#value' => $conf['nid'],
98 );
99
100 $form['external'] = array(
101 '#type' => 'value',
102 '#value' => $external,
103 );
104
105 return $form;
106 }
107
108 /**
109 * Validate a node.
110 */
111 function panels_context_node_edit_form_settings_form_validate($form, &$form_values, &$form_state) {
112 if (empty($form_values['external']) && empty($form_values['nid']) && empty($form_values['node'])) {
113 form_error($form['node'], t('You must select a node.'));
114 return;
115 }
116
117 if (empty($form_values['node'])) {
118 return;
119 }
120
121 $nid = $form_values['node'];
122 if (is_numeric($nid)) {
123 $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid));
124 }
125 else {
126 $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $nid));
127 if ($node) {
128 form_set_value($form['nid'], $node->nid, $form_state);
129 }
130 }
131
132 if (!$node) {
133 form_error($form['node'], t('Invalid node selected.'));
134 }
135 }
136