Projet

Général

Profil

Paste
Télécharger (6,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / content_types / search / search_result.inc @ 651307cd

1
<?php
2

    
3
if (module_exists('search')) {
4
  /**
5
   * Plugins are described by creating a $plugin array which will be used
6
   * by the system that includes this file.
7
   */
8
  $plugin = array(
9
    'single' => TRUE,
10
    'title' => t('Search results'),
11
    'icon' => 'icon_search.png',
12
    'description' => t('The results of a search using keywords.'),
13
    'required context' => new ctools_context_required(t('Keywords'), 'string'),
14
    'category' => t('Widgets'),
15
    'defaults' => array(
16
      'type' => 'node',
17
      'log' => TRUE,
18
      'override_empty' => FALSE,
19
      'empty_title' => '',
20
      'empty' => '',
21
      'empty_format' => filter_fallback_format(),
22
      'override_no_key' => FALSE,
23
      'no_key_title' => '',
24
      'no_key' => '',
25
      'no_key_format' => filter_fallback_format(),
26
    ),
27
  );
28
}
29

    
30
/**
31
 * Render the custom content type.
32
 */
33
function ctools_search_result_content_type_render($subtype, $conf, $panel_args, $context) {
34
  $search_info = search_get_info();
35
  if (empty($search_info[$conf['type']])) {
36
    return;
37
  }
38
  $info = $search_info[$conf['type']];
39

    
40
  $keys = NULL;
41
  if (!empty($context) && isset($context->data)) {
42
    $keys = $context->data;
43
  }
44

    
45
  $conditions =  NULL;
46
  if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
47
    // Build an optional array of more search conditions.
48
    $conditions = $info['conditions_callback']($keys);
49
  }
50

    
51
  // Display nothing at all if no keywords were entered.
52
  if (empty($keys) && empty($conditions)) {
53
    if (!empty($conf['override_no_key'])) {
54
      $block->title = $conf['no_key_title'];
55
      $block->content = check_markup($conf['no_key'], $conf['no_key_format'], FALSE);
56
      return $block;
57
    }
58
    return;
59
  }
60

    
61
  // Build the content type block.
62
  $block = new stdClass();
63
  $block->module  = 'search';
64
  $block->delta   = 'result';
65

    
66
  $results = '';
67

    
68
  // Only search if there are keywords or non-empty conditions.
69
  if ($keys || !empty($conditions)) {
70

    
71
    // Collect the search results.
72
    $results = search_data($keys, $info['module'], $conditions);
73

    
74
    // A workaround for ApacheSolr.
75
    // @todo see http://drupal.org/node/1343142#comment-5495248
76
    // This workaround is to be removed when a better one can be written.
77
    if (!empty($results['search_results']['#results'])) {
78
      $results['#results'] = $results['search_results']['#results'];
79
    }
80
  }
81

    
82
  if (!empty($conf['log'])) {
83
    // Log the search keys:
84
    watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), $_GET['q']));
85
  }
86

    
87
  if (!empty($results['#results'])) {
88
    $output = "<ol class=\"search-results $conf[type]-results\">\n";
89
    foreach ($results['#results'] as $result) {
90
      $output .= theme('search_result', array('result' => $result, 'module' => $conf['type']));
91
    }
92
    $output .= "</ol>\n";
93
    $output .= theme('pager', array('tags' => NULL));
94

    
95
    $block->title = t('Search results');
96
    $block->content = $output;
97
  }
98
  else {
99
    if (empty($conf['override_empty'])) {
100
      $block->title = t('Your search yielded no results');
101
      $block->content = search_help('search#noresults', drupal_help_arg());
102
    }
103
    else {
104
      $block->title = $conf['empty_title'];
105
      $block->content = check_markup($conf['empty'], $conf['empty_format'], FALSE);
106
    }
107
  }
108

    
109
  return $block;
110
}
111

    
112
/**
113
 * Returns an edit form for custom type settings.
114
 */
115
function ctools_search_result_content_type_edit_form($form, &$form_state) {
116
  $conf = $form_state['conf'];
117

    
118
  $types = array();
119
  foreach (search_get_info() as $module => $info) {
120
    $types[$module] = $info['title'];
121
  }
122

    
123
  $form['type'] = array(
124
    '#type' => 'select',
125
    '#title' => t('Search type'),
126
    '#options' => $types,
127
    '#default_value' => $conf['type'],
128
  );
129

    
130
  $form['log'] = array(
131
    '#type' => 'checkbox',
132
    '#default_value' => $conf['log'],
133
    '#title' => t('Record a watchdog log entry when searches are made'),
134
  );
135

    
136
  $form['override_empty'] = array(
137
    '#type' => 'checkbox',
138
    '#default_value' => $conf['override_empty'],
139
    '#title' => t('Override "no result" text'),
140
  );
141

    
142
  $form['empty_title'] = array(
143
    '#title' => t('Title'),
144
    '#type' => 'textfield',
145
    '#default_value' => $conf['empty_title'],
146
    '#dependency' => array('edit-override-empty' => array(1)),
147
  );
148

    
149
  $form['empty_field'] = array(
150
    '#type' => 'text_format',
151
    '#title' => t('No result text'),
152
    '#default_value' => $conf['empty'],
153
    '#format' => $conf['empty_format'],
154
    '#dependency' => array('edit-override-empty' => array(1)),
155
  );
156

    
157
  $form['override_no_key'] = array(
158
    '#type' => 'checkbox',
159
    '#default_value' => $conf['override_no_key'],
160
    '#title' => t('Display text if no search keywords were submitted'),
161
  );
162

    
163

    
164
  $form['no_key_title'] = array(
165
    '#title' => t('Title'),
166
    '#type' => 'textfield',
167
    '#default_value' => $conf['no_key_title'],
168
    '#dependency' => array('edit-override-no-key' => array(1)),
169
  );
170

    
171
  $form['no_key_field'] = array(
172
    '#type' => 'text_format',
173
    '#title' => t('No result text'),
174
    '#default_value' => $conf['no_key'],
175
    '#format' => $conf['no_key_format'],
176
    '#dependency' => array('edit-override-no-key' => array(1)),
177
  );
178

    
179
  return $form;
180
}
181

    
182
/**
183
 * Submit handler for search form.
184
 */
185
function ctools_search_result_content_type_edit_form_submit($form, &$form_state) {
186
  // Copy the text_format values over to where we normally store them.
187
  $form_state['values']['empty'] = $form_state['values']['empty_field']['value'];
188
  $form_state['values']['empty_format'] = $form_state['values']['empty_field']['format'];
189
  $form_state['values']['no_key'] = $form_state['values']['no_key_field']['value'];
190
  $form_state['values']['no_key_format'] = $form_state['values']['no_key_field']['format'];
191
  // Copy everything from our defaults.
192
  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
193
    $form_state['conf'][$key] = $form_state['values'][$key];
194
  }
195
}
196

    
197
/**
198
 * Returns the administrative title for a type.
199
 */
200
function ctools_search_result_content_type_admin_title($subtype, $conf, $context) {
201
  $info = search_get_info();
202
  $type = isset($info[$conf['type']]['title']) ? $info[$conf['type']]['title'] : t('Missing/broken type');
203
  return t('@type search result', array('@type' => $type));
204
}