[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/ctools/plugins/contexts/ -> node.inc (source)

   1  <?php
   2  // $Id: node.inc,v 1.11.2.5 2010/05/20 01:43:03 sdboyer Exp $
   3  
   4  /**
   5   * @file
   6   *
   7   * Plugin to provide a node context. A node context is a node wrapped in a
   8   * context object that can be utilized by anything that accepts contexts.
   9   */
  10  
  11  /**
  12   * Plugins are described by creating a $plugin array which will be used
  13   * by the system that includes this file.
  14   */
  15  $plugin = array(
  16    'title' => t("Node"),
  17    'description' => t('A node object.'),
  18    'context' => 'ctools_context_create_node',
  19    'settings form' => 'ctools_context_node_settings_form',
  20    'settings form validate' => 'ctools_context_node_settings_form_validate',
  21    'settings form submit' => 'ctools_context_node_settings_form_submit',
  22    'defaults' => array('nid' => ''),
  23    'keyword' => 'node',
  24    'context name' => 'node',
  25    'convert list' => 'ctools_context_node_convert_list',
  26    'convert' => 'ctools_context_node_convert',
  27    'placeholder form' => array(
  28      '#type' => 'textfield',
  29      '#description' => t('Enter the node ID of a node for this context.'),
  30    ),
  31  );
  32  
  33  /**
  34   * It's important to remember that $conf is optional here, because contexts
  35   * are not always created from the UI.
  36   */
  37  function ctools_context_create_node($empty, $data = NULL, $conf = FALSE) {
  38    $context = new ctools_context('node');
  39    $context->plugin = 'node';
  40  
  41    if ($empty) {
  42      return $context;
  43    }
  44  
  45    if ($conf) {
  46      $nid = is_array($data) && isset($data['nid']) ? $data['nid'] : (is_object($data) ? $data->nid : 0);
  47  
  48      if (module_exists('translation')) {
  49        if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
  50          $nid = $translation;
  51          $reload = TRUE;
  52        }
  53      }
  54  
  55      if (is_array($data) || !empty($reload)) {
  56        $data = node_load($nid);
  57      }
  58    }
  59  
  60    if (!empty($data)) {
  61      $context->data     = $data;
  62      $context->title    = $data->title;
  63      $context->argument = $data->nid;
  64  
  65      $context->restrictions['type'] = array($data->type);
  66      return $context;
  67    }
  68  }
  69  
  70  function ctools_context_node_settings_form($conf) {
  71    if (empty($conf)) {
  72      $conf = array(
  73        'nid' => '',
  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 node'),
  81      '#type' => 'textfield',
  82      '#maxlength' => 512,
  83      '#autocomplete_path' => 'ctools/autocomplete/node',
  84      '#weight' => -10,
  85    );
  86  
  87    if (!empty($conf['nid'])) {
  88      $info = db_fetch_object(db_query("SELECT * FROM {node} n WHERE n.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('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  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['set_identifier'] = array(
 101      '#type' => 'checkbox',
 102      '#default_value' => FALSE,
 103      '#title' => t('Reset identifier to node title'),
 104      '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
 105    );
 106  
 107    return $form;
 108  }
 109  
 110  /**
 111   * Validate a node.
 112   */
 113  function ctools_context_node_settings_form_validate($form, &$form_values, &$form_state) {
 114    // Validate the autocomplete
 115    if (empty($form_values['nid']) && empty($form_values['node'])) {
 116      form_error($form['node'], t('You must select a node.'));
 117      return;
 118    }
 119  
 120    if (empty($form_values['node'])) {
 121      return;
 122    }
 123  
 124    $nid          = $form_values['node'];
 125    $preg_matches = array();
 126    $match        = preg_match('/\[nid: (\d+)\]/', $nid, $preg_matches);
 127    if (!$match) {
 128      $match = preg_match('/^nid: (\d+)/', $nid, $preg_matches);
 129    }
 130  
 131    if ($match) {
 132      $nid = $preg_matches[1];
 133    }
 134    if (is_numeric($nid)) {
 135      $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid));
 136    }
 137    else {
 138      $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $nid));
 139    }
 140  
 141    // Do not allow unpublished nodes to be selected by unprivileged users
 142    if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
 143      form_error($form['node'], t('Invalid node selected.'));
 144    }
 145    else {
 146      form_set_value($form['nid'], $node->nid, $form_state);
 147      // $form_values['nid'] = $node->nid;
 148    }
 149  }
 150  
 151  function ctools_context_node_settings_form_submit($form, &$form_values, &$form_state) {
 152    if ($form_values['set_identifier']) {
 153      $node = node_load($form_values['nid']);
 154      $form_state['values']['context']['identifier'] = $node->title;
 155    }
 156  
 157    // Don't let this be stored.
 158    unset($form_values['set_identifier']);
 159  }
 160  
 161  /**
 162   * Provide a list of ways that this context can be converted to a string.
 163   */
 164  function ctools_context_node_convert_list() {
 165    $list = array(
 166      'nid' => t('Node ID'),
 167      'vid' => t('Node revision ID'),
 168      'title' => t('Node title'),
 169      'uid' => t('Author UID'),
 170      'type' => t('Node type'),
 171    );
 172  
 173    if (module_exists('token')) {
 174      $list += reset(token_get_list(array('node')));
 175    }
 176  
 177    return $list;
 178  }
 179  
 180  /**
 181   * Convert a context into a string.
 182   */
 183  function ctools_context_node_convert($context, $type) {
 184    switch ($type) {
 185      case 'nid':
 186        return $context->data->nid;
 187      case 'vid':
 188        return $context->data->vid;
 189      case 'title':
 190        return $context->data->title;
 191      case 'uid':
 192        return $context->data->uid;
 193      case 'type':
 194        return $context->data->type;
 195    }
 196    if (module_exists('token')) {
 197      $values = token_get_values('node', $context->data);
 198      if ($key = array_search($type, $values->tokens)) {
 199        return $values->values[$key];
 200      }
 201    }
 202  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7