[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

   1  <?php
   2  // $Id: term.inc,v 1.5.2.4 2010/10/15 21:06:51 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   *
   7   * Plugin to provide a term context
   8   */
   9  
  10  /**
  11   * Plugins are described by creating a $plugin array which will be used
  12   * by the system that includes this file.
  13   */
  14  $plugin = array(
  15    'title' => t("Taxonomy term"),
  16    'description' => t('A single taxonomy term object.'),
  17    'context' => 'ctools_context_create_term',
  18    'settings form' => 'ctools_context_term_settings_form',
  19    'settings form validate' => 'ctools_context_term_settings_form_validate',
  20    'settings form submit' => 'ctools_context_term_settings_form_submit',
  21    'keyword' => 'term',
  22    'context name' => 'term',
  23    'convert list' => array(
  24      'tid' => t('Term ID'),
  25      'name' => t('Term name'),
  26      'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
  27      'description' => t('Term Description'),
  28      'vid' => t('Vocabulary ID'),
  29    ),
  30    'convert' => 'ctools_context_term_convert',
  31    'js' => array('misc/autocomplete.js'),
  32  );
  33  
  34  /**
  35   * It's important to remember that $conf is optional here, because contexts
  36   * are not always created from the UI.
  37   */
  38  function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
  39    $context = new ctools_context('term');
  40    $context->plugin = 'term';
  41  
  42    if ($empty) {
  43      return $context;
  44    }
  45  
  46    if ($conf && isset($data['tid'])) {
  47      $data = taxonomy_get_term($data['tid']);
  48    }
  49    
  50    if (!empty($data)) {
  51      $context->data        = $data;
  52      $context->title       = $data->name;
  53      $context->argument    = $data->tid;
  54      $context->description = $data->description;
  55      return $context;
  56    }
  57  }
  58  
  59  function ctools_context_term_settings_form($conf) {
  60    if (empty($conf)) {
  61      $conf = array(
  62        'vid' => '',
  63        'tid' => '',
  64        'term' => '',
  65        'description' => '',
  66      );
  67    }
  68  
  69    $form['vid'] = array(
  70      '#title' => t('Vocabulary'),
  71      '#type' => 'select',
  72      '#options' => array(),
  73      '#description' => t('Select the vocabulary for this form.'),
  74      '#id' => 'ctools-select-vid',
  75      '#default_value' => $conf['vid'],
  76    );
  77  
  78    $description = '';
  79    if (!empty($conf['tid'])) {
  80      $info = db_fetch_object(db_query("SELECT * FROM {term_data} n WHERE n.tid = %d", $conf['tid']));
  81      if ($info) {
  82        $description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
  83      }
  84    }
  85  
  86    ctools_include('dependent');
  87    $options = array();
  88  
  89    // A note: Dependency works strangely on these forms as they have never been
  90    // updated to a more modern system so they are not individual forms of their
  91    // own like the content types.
  92  
  93    $form['taxonomy']['#tree'] = TRUE;
  94  
  95    foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  96      $options[$vid] = $vocabulary->name;
  97      $form['taxonomy'][$vocabulary->vid] = array(
  98        '#type' => 'textfield',
  99        '#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
 100        '#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
 101        '#process' => array('ctools_dependent_process'),
 102        '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
 103      );
 104  
 105    }
 106  
 107    $form['vid']['#options'] = $options;
 108  
 109    $form['tid'] = array(
 110      '#type' => 'value',
 111      '#value' => $conf['tid'],
 112    );
 113  
 114    $form['set_identifier'] = array(
 115      '#type' => 'checkbox',
 116      '#default_value' => FALSE,
 117      '#title' => t('Reset identifier to term title'),
 118      '#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
 119    );
 120  
 121    return $form;
 122  }
 123  
 124  /**
 125   * Validate a term.
 126   */
 127  function ctools_context_term_settings_form_validate($form, &$form_values, &$form_state) {
 128    // Validate the autocomplete
 129    $vid = $form_values['vid'];
 130    if (empty($form_values['tid']) && empty($form_values['taxonomy'][$vid])) {
 131      form_error($form['taxonomy'][$vid], t('You must select a term.'));
 132      return;
 133    }
 134  
 135    if (empty($form_values['taxonomy'][$vid])) {
 136      return;
 137    }
 138  
 139    $term = db_fetch_object(db_query("SELECT t.tid FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d", $form_values['taxonomy'][$vid], $vid));
 140  
 141    if (!$term) {
 142      form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
 143    }
 144    else {
 145      form_set_value($form['tid'], $term->tid, $form_state);
 146    }
 147  }
 148  
 149  function ctools_context_term_settings_form_submit($form, &$form_values, &$form_state) {
 150    if ($form_values['set_identifier']) {
 151      $term = db_fetch_object(db_query("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.tid) = %d", $form_values['tid']));
 152      $form_state['values']['context']['identifier'] = $term->name;
 153    }
 154  
 155    // Don't let this be stored.
 156    unset($form_values['set_identifier']);
 157  }
 158  
 159  /**
 160   * Convert a context into a string.
 161   */
 162  function ctools_context_term_convert($context, $type) {
 163    switch ($type) {
 164      case 'tid':
 165        return $context->data->tid;
 166      case 'name':
 167        return $context->data->name;
 168      case 'name_dashed':
 169        return drupal_strtolower(str_replace(' ', '-', $context->data->name));
 170      case 'vid':
 171        return $context->data->vid;
 172      case 'description':
 173        return $context->data->description;
 174    }
 175  }


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