Projet

Général

Profil

Paste
Télécharger (3,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / advanced_forum / plugins / contexts / forum.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * Plugin to provide a user context
7
 */
8
/**
9
 * Plugins are described by creating a $plugin array which will be used
10
 * by the system that includes this file.
11
 */
12
$plugin = array(
13
  'title' => t("Forum"),
14
  'description' => t('A single forum object.'),
15
  'context' => 'advanced_forum_forum_context_create_forum',
16
  'settings form' => 'advanced_forum_forum_context_settings_form',
17
  'settings form validate' => 'advanced_forum_forum_context_settings_form_validate',
18
  'keyword' => 'forum',
19
  'context name' => 'forum',
20
  'convert list' => array(
21
    'tid' => t('Forum ID'),
22
    'name' => t('Forum name'),
23
  ),
24
  'convert' => 'advanced_forum_forum_context_convert',
25
  'defaults' => array('tid' => 0),
26
);
27

    
28
/**
29
 * Create a context, either from manual configuration or from an argument on the URL.
30
 *
31
 * @param $empty
32
 *   If true, just return an empty context.
33
 * @param $data
34
 *   If from settings form, an array as from a form. If from argument, a string.
35
 * @param $conf
36
 *   TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg.
37
 *
38
 * @return
39
 *   a Context object/
40
 */
41
function advanced_forum_forum_context_create_forum($empty, $data = NULL, $conf = FALSE) {
42
  $context = new ctools_context(array('forum', 'term'));
43
  $context->plugin = 'forum';
44

    
45
  if ($empty) {
46
    return $context;
47
  }
48

    
49
  if ($conf) {
50
    if (!empty($data['tid'])) {
51
      $data = taxonomy_term_load($data['tid']);
52
    }
53
    else {
54
      $data = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
55
      $data->tid = 0;
56
    }
57
  }
58

    
59
  if (!empty($data)) {
60
    $data->container = (!$data->tid || in_array($data->tid, variable_get('forum_containers', array())));
61
    $context->data = clone $data;
62
    $context->title = $data->name;
63
    $context->argument = $data->tid;
64
    $context->vid = variable_get('forum_nav_vocabulary', '');
65
    $context->vocabulary = taxonomy_vocabulary_load($context->vid);
66
    if ($data->tid) {
67
      $context->parents = taxonomy_get_parents_all($data->tid);
68
    }
69

    
70
    return $context;
71
  }
72
}
73

    
74
function advanced_forum_forum_context_settings_form($form, &$form_state) {
75
  $conf = $form_state['conf'];
76

    
77
  if (empty($conf)) {
78
    $conf = array('tid' => 0);
79
  }
80

    
81
  $options = array();
82
  $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
83
  $options[$vocabulary->vid] = $vocabulary->name;
84

    
85
  $tree = taxonomy_get_tree($vocabulary->vid);
86
  if ($tree) {
87
    foreach ($tree as $term) {
88
      $choice = new stdClass();
89
      $choice->option = array($term->tid => str_repeat('-', $term->depth + 1) . $term->name);
90
      $options[] = $choice;
91
    }
92
  }
93

    
94
  $form['tid'] = array(
95
    '#type' => 'select',
96
    '#title' => t('Forum'),
97
    '#default_value' => $conf['tid'],
98
    '#options' => $options,
99
  );
100

    
101
  return $form;
102
}
103

    
104
/**
105
 * Convert a context into a string.
106
 */
107
function advanced_forum_forum_context_convert($context, $type) {
108
  switch ($type) {
109
    case 'tid':
110
      return $context->data->tid;
111
    case 'name':
112
      $forum_term = $context->data;
113

    
114
      if (module_exists('i18n_taxonomy'))
115
        $forum_term = i18n_taxonomy_localize_terms($forum_term);
116

    
117
      return $forum_term->name;
118
  }
119
}