commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / ctools / plugins / contexts / terms.inc
1 <?php
2
3 /**
4 * @file
5 *
6 * Plugin to provide a terms context
7 */
8
9 /**
10 * Plugins are described by creating a $plugin array which will be used
11 * by the system that includes this file.
12 */
13 $plugin = array(
14 'title' => t("Taxonomy terms"),
15 'description' => t('Multiple taxonomy terms, as a group.'),
16 'context' => 'ctools_context_create_terms',
17 'keyword' => 'terms',
18 // This context is deprecated and should not be usable in the UI.
19 'no ui' => TRUE,
20 'context name' => 'terms',
21 'convert list' => array(
22 'tid' => t('Term ID of first term'),
23 'tids' => t('Term ID of all term, separated by + or ,'),
24 'name' => t('Term name of first term'),
25 'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
26 'names' => t('Term name of all terms, separated by + or ,'),
27 'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
28 'vid' => t('Vocabulary ID of first term'),
29 ),
30 'convert' => 'ctools_context_terms_convert',
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_terms($empty, $data = NULL, $conf = FALSE) {
38 // The input is expected to be an object as created by ctools_break_phrase
39 // which contains a group of terms.
40
41 $context = new ctools_context(array('terms', 'entity:taxonomy_term'));
42 $context->plugin = 'terms';
43
44 if ($empty) {
45 return $context;
46 }
47
48 if (!empty($data) && is_object($data)) {
49 $context->operator = $data->operator;
50 $context->tids = $data->value;
51 if (!isset($data->term)) {
52 // load the first term:
53 reset($context->tids);
54 $data->term = taxonomy_term_load(current($context->tids));
55 }
56 $context->data = $data->term;
57 $context->title = $data->term->name;
58 $context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
59 return $context;
60 }
61 }
62
63 /**
64 * Convert a context into a string.
65 */
66 function ctools_context_terms_convert($context, $type) {
67 switch ($type) {
68 case 'tid':
69 return $context->data->tid;
70 case 'tids':
71 return $context->argument;
72 case 'name':
73 return $context->data->name;
74 case 'name_dashed':
75 return drupal_strtolower(str_replace(' ', '-', $context->data->name));
76 case 'names':
77 case 'names_dashed':
78 // We only run this query if this item was requested:
79 if (!isset($context->names)) {
80 if (empty($context->tids)) {
81 $context->names = '';
82 }
83 else {
84 $result = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid IN (:tids)', array(':tids' => $context->tids));
85 foreach ($result as $term) {
86 $names[$term->tid] = $term->name;
87 if ($type == 'names_dashed') {
88 $names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
89 }
90 }
91 $context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
92 }
93 }
94 return $context->names;
95 case 'vid':
96 return $context->data->vid;
97 }
98 }