[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: terms.inc,v 1.4.2.4 2010/10/15 21:06:51 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   *
   7   * Plugin to provide a terms 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 terms"),
  16    'description' => t('Multiple taxonomy terms, as a group.'),
  17    'context' => 'ctools_context_create_terms',
  18    'settings form' => 'ctools_context_terms_settings_form',
  19    'settings form validate' => 'ctools_context_terms_settings_form_validate',
  20    'keyword' => 'terms',
  21    'no ui' => TRUE,
  22    'context name' => 'terms',
  23    'convert list' => array(
  24      'tid' => t('Term ID of first term'),
  25      'tids' => t('Term ID of all term, separated by + or ,'),
  26      'name' => t('Term name of first term'),
  27      'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
  28      'names' => t('Term name of all terms, separated by + or ,'),
  29      'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
  30      'vid' => t('Vocabulary ID of first term'),
  31    ),
  32    'convert' => 'ctools_context_terms_convert',
  33  );
  34  
  35  /**
  36   * It's important to remember that $conf is optional here, because contexts
  37   * are not always created from the UI.
  38   */
  39  function ctools_context_create_terms($empty, $data = NULL, $conf = FALSE) {
  40    // The input is expected to be an object as created by ctools_break_phrase
  41    // which contains a group of terms.
  42  
  43    $context = new ctools_context(array('terms', 'term'));
  44    $context->plugin = 'terms';
  45  
  46    if ($empty) {
  47      return $context;
  48    }
  49  
  50    if (!empty($data) && is_object($data)) {
  51      $context->operator = $data->operator;
  52      $context->tids     = $data->value;
  53      if (!isset($data->term)) {
  54        // load the first term:
  55        reset($context->tids);
  56        $data->term = taxonomy_get_term(current($context->tids));
  57      }
  58      $context->data     = $data->term;
  59      $context->title    = $data->term->name;
  60      $context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
  61      return $context;
  62    }
  63  }
  64  
  65  /**
  66   * Convert a context into a string.
  67   */
  68  function ctools_context_terms_convert($context, $type) {
  69    switch ($type) {
  70      case 'tid':
  71        return $context->data->tid;
  72      case 'tids':
  73        return $context->argument;
  74      case 'name':
  75        return $context->data->name;
  76      case 'name_dashed':
  77        return drupal_strtolower(str_replace(' ', '-', $context->data->name));
  78      case 'names':
  79      case 'names_dashed':
  80        // We only run this query if this item was requested:
  81        if (!isset($context->names)) {
  82          if (empty($context->tids)) {
  83            $context->names = '';
  84          }
  85          else {
  86            $names = array();
  87            $result = db_query("SELECT tid, name FROM {term_data} WHERE tid IN (" . db_placeholders($context->tids) . ")", $context->tids);
  88            while ($term = db_fetch_object($result)) {
  89              $names[$term->tid] = $term->name;
  90              if ($type == 'names_dashed') {
  91                $names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
  92              }
  93            }
  94            $context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
  95          }
  96        }
  97        return $context->names;
  98      case 'vid':
  99        return $context->data->vid;
 100    }
 101  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7