| Commit | Line | Data |
|---|---|---|
| db205271 EM |
1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| 4 | ||
| 5 | /** | |
| 6 | * @file contexts/node.inc | |
| 7 | * | |
| 8 | * Plugin to provide a node context | |
| 9 | */ | |
| 10 | ||
| 11 | function panels_node_panels_contexts() { | |
| 12 | $args['node'] = array( | |
| 13 | 'title' => t("Node"), | |
| 14 | 'description' => t('A node object.'), | |
| 15 | 'context' => 'panels_context_create_node', | |
| 16 | 'settings form' => 'panels_context_node_settings_form', | |
| 17 | 'settings form validate' => 'panels_context_node_settings_form_validate', | |
| dcc5ebb8 EM |
18 | 'settings form submit' => 'panels_context_node_settings_form_submit', |
| 19 | 'keyword' => 'node', | |
| db205271 EM |
20 | 'context name' => 'node', |
| 21 | ); | |
| 22 | return $args; | |
| 23 | } | |
| 24 | ||
| 25 | /** | |
| 26 | * It's important to remember that $conf is optional here, because contexts | |
| 27 | * are not always created from the UI. | |
| 28 | */ | |
| 29 | function panels_context_create_node($empty, $data = NULL, $conf = FALSE) { | |
| d9ddf0ea SB |
30 | $types = array('node'); |
| 31 | if (!empty($conf['types'])) { | |
| 32 | foreach ($conf['types'] as $type) { | |
| 33 | if ($type) { | |
| 34 | $types[] = 'node-' . $type; | |
| 35 | } | |
| 36 | } | |
| 37 | } | |
| 38 | $context = new panels_context($types); | |
| db205271 EM |
39 | $context->plugin = 'node'; |
| 40 | ||
| 41 | if ($empty) { | |
| 42 | return $context; | |
| 43 | } | |
| 44 | ||
| 45 | if ($conf) { | |
| d9ddf0ea | 46 | $nid = is_array($data) ? $data['nid'] : $data->nid; |
| db205271 EM |
47 | |
| 48 | if (module_exists('translation')) { | |
| 49 | if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['locale'])) { | |
| 50 | $nid = $translation; | |
| d9ddf0ea | 51 | $reload = TRUE; |
| db205271 EM |
52 | } |
| 53 | } | |
| 54 | ||
| d9ddf0ea SB |
55 | if (is_array($data) || !empty($reload)) { |
| 56 | $data = node_load($nid); | |
| 57 | } | |
| db205271 EM |
58 | } |
| 59 | ||
| 60 | if (!empty($data)) { | |
| 61 | $context->data = $data; | |
| 62 | $context->title = $data->title; | |
| 63 | $context->argument = $data->nid; | |
| 64 | return $context; | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | function panels_context_node_settings_form($conf, $external = FALSE) { | |
| 69 | if ($external) { | |
| 70 | $form['external'] = array( | |
| 71 | '#type' => 'checkbox', | |
| 72 | '#default_value' => $conf['external'], | |
| 73 | '#title' => t('Require this context from an external source (such as a containing panel page).'), | |
| 74 | '#description' => t('If selected, node selection (below) will be ignored.'), | |
| 75 | ); | |
| 76 | } | |
| 77 | ||
| db205271 EM |
78 | $form['node'] = array( |
| 79 | '#prefix' => '<div class="no-float">', | |
| 80 | '#suffix' => '</div>', | |
| 81 | '#title' => t('Enter the title or NID of a post'), | |
| 82 | '#type' => 'textfield', | |
| 83 | '#maxlength' => 512, | |
| 84 | '#autocomplete_path' => 'panels/node/autocomplete', | |
| 85 | '#weight' => -10, | |
| 86 | ); | |
| 87 | ||
| 88 | if (!empty($conf['nid'])) { | |
| 89 | $info = db_fetch_object(db_query("SELECT * FROM {node} n WHERE n.nid = %d", $conf['nid'])); | |
| 90 | if ($info) { | |
| 91 | $link = l("'$info->title' [node id: $info->nid]", "node/$info->nid", array('target' => '_blank', 'title' => t('Open in new window'))); | |
| 92 | $form['node']['#description'] = t('Currently set to !link', array('!link' => $link)); | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | $form['nid'] = array( | |
| 97 | '#type' => 'value', | |
| 98 | '#value' => $conf['nid'], | |
| 99 | ); | |
| 100 | ||
| dcc5ebb8 EM |
101 | $form['set_identifier'] = array( |
| 102 | '#type' => 'checkbox', | |
| 103 | '#default_value' => FALSE, | |
| 104 | '#title' => t('Reset identifier to node title'), | |
| 105 | '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'), | |
| 106 | ); | |
| 107 | ||
| db205271 EM |
108 | return $form; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Validate a node. | |
| 113 | */ | |
| dcc5ebb8 | 114 | function panels_context_node_settings_form_validate($form, &$form_values) { |
| db205271 EM |
115 | // Validate the autocomplete |
| 116 | if (empty($form_values['external']) && empty($form_values['nid']) && empty($form_values['node'])) { | |
| 117 | form_error($form['node'], t('You must select a node.')); | |
| 118 | return; | |
| 119 | } | |
| 120 | ||
| 121 | if (empty($form_values['node'])) { | |
| 122 | return; | |
| 123 | } | |
| 124 | ||
| 125 | $nid = $form_values['node']; | |
| 126 | $preg_matches = array(); | |
| 127 | $match = preg_match('/\[nid: (\d+)\]/', $nid, $preg_matches); | |
| 128 | if (!$match) { | |
| 129 | $match = preg_match('/^nid: (\d+)/', $nid, $preg_matches); | |
| 130 | } | |
| 131 | ||
| 132 | if ($match) { | |
| 133 | $nid = $preg_matches[1]; | |
| 134 | } | |
| 135 | if (is_numeric($nid)) { | |
| 136 | $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid)); | |
| 137 | } | |
| 138 | else { | |
| 139 | $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $nid)); | |
| 140 | } | |
| 141 | ||
| 142 | if (!$node) { | |
| 143 | form_error($form['node'], t('Invalid node selected.')); | |
| 144 | } | |
| 145 | else { | |
| dcc5ebb8 | 146 | $form_values['nid'] = $node->nid; |
| db205271 EM |
147 | } |
| 148 | } | |
| 149 | ||
| dcc5ebb8 EM |
150 | function panels_context_node_settings_form_submit($form, &$form_values, &$form_state) { |
| 151 | if ($form_values['set_identifier']) { | |
| 152 | $node = node_load($form_values['nid']); | |
| 153 | $form_state['values']['context']['identifier'] = $node->title; | |
| 154 | } | |
| 155 | ||
| 156 | // Don't let this be stored. | |
| 157 | unset($form_values['set_identifier']); | |
| 158 | } |