Projet

Général

Profil

Paste
Télécharger (10,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / fivestar / includes / fivestar.theme.inc @ d50a36e0

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides the theming functions for fivestar.
6
 */
7

    
8
/**
9
 * Show a preview of a widget using a custom CSS file.
10
 */
11
function theme_fivestar_preview_widget($variables) {
12
  $path = drupal_get_path('module', 'fivestar');
13
  $form = array(
14
    '#post' => array(),
15
    '#programmed' => FALSE,
16
    '#tree' => FALSE,
17
    '#parents' => array(),
18
    '#array_parents' => array(),
19
    '#required' => FALSE,
20
    '#attributes' => array(),
21
    '#title_display' => 'before',
22
  );
23
  $form_state = form_state_defaults();
24
  $form_state['values'] = array();
25
  $form_state['process_input'] = array();
26
  $form_state['complete form'] = array();
27

    
28
  $form['vote'] = array(
29
    '#type' => 'fivestar',
30
    '#stars' => 5,
31
    '#auto_submit' => FALSE,
32
    '#allow_clear' => TRUE,
33
    '#allow_revote' => TRUE,
34
    '#allow_ownvote' => TRUE,
35
    '#widget' => array(
36
      'name' => isset($variables['name']) ? $variables['name'] : 'default',
37
      'css' => isset($variables['css']) && $variables['css'] != 'default' ? $variables['css'] : FALSE,
38
    ),
39
  );
40

    
41
  // Attach necessary JS settings.
42
  $settings = array(
43
    'titleUser' => t('Your rating') . ': ',
44
    'titleAverage' => t('Average') . ': ',
45
    'feedbackSavingVote' => t('Saving your vote...'),
46
    'feedbackVoteSaved' => t('Your vote has been saved.'),
47
    'feedbackDeletingVote' => t('Deleting your vote...'),
48
    'feedbackVoteDeleted' => t('Your vote has been deleted.'),
49
  );
50

    
51
  drupal_add_js(array('fivestar' => $settings), 'setting');
52

    
53
  $form = form_builder('fivestar_preview', $form, $form_state);
54

    
55
  $output = '<div class="fivestar-star-preview fivestar-' . $form['vote']['#widget']['name'] . '">';
56
  $output .= drupal_render_children($form);
57
  $output .= '</div>';
58

    
59
  return $output;
60
}
61

    
62
/**
63
 *
64
 */
65
function theme_fivestar_preview($variables) {
66
  extract($variables, EXTR_SKIP);
67
  $values = array(
68
    'average' => 50,
69
    'user' => 80,
70
    'count' => 20,
71
  );
72
  $settings = array(
73
    'stars' => $stars,
74
    'allow_clear' => $unvote,
75
    'allow_revote' => $revote,
76
    'allow_ownvote' => $ownvote,
77
    'style' => $style,
78
    'text' => $text,
79
    'title' => $title,
80
    'autosubmit' => FALSE,
81
    'tag' => 'vote',
82
  );
83

    
84
  $form = drupal_get_form('fivestar_custom_widget', $values, $settings);
85
  $form = drupal_render($form);
86
  // This regex is sadly necessary because having duplicate form_tokens or
87
  // form_id elements can cause the content type form to choke. Forms inside of
88
  // forms is also frowned upon, so this removes the wrapping form tag as well.
89
  $form = str_replace(array('<form', '</form>'), array('<div', '</div>'), $form);
90
  $form = preg_replace('/( method=".*?")|( action=".*?")|(<input.*?name="(form_token|form_id|destination|form_build_id)".*?\/>)/', '', $form);
91
  return $form;
92
}
93

    
94
/**
95
 *
96
 */
97
function theme_fivestar_preview_wrapper($variables) {
98
  return '<div class="fivestar-preview fivestar-preview-' . $variables['type'] . '">' . $variables['content'] . '</div>';
99
}
100

    
101
/**
102
 * Theme function for 'default' fivestar field formatter.
103
 *
104
 * This themes static stars. That is, pairs of stars where neither set
105
 * of stars is "exposed". Exposed stars are clickable and displayed in a
106
 * form. Theming of exposed stars is handled by the form array (and calls
107
 * the same theme functions called at the end of this function).
108
 */
109
function theme_fivestar_formatter_default($variables) {
110
  $element = $variables['element'];
111
  if (empty($element['#instance_settings']['stars'])) {
112
    $element['#instance_settings']['stars'] = 5;
113
  }
114

    
115
  // Add CSS and JS.
116
  $path = drupal_get_path('module', 'fivestar');
117
  drupal_add_js($path . '/js/fivestar.js');
118
  drupal_add_css($path . '/css/fivestar.css');
119

    
120
  $variables = array(
121
    'rating' => $element['#rating'],
122
    'stars' => $element['#instance_settings']['stars'],
123
    'widget' => $element['#widget'],
124
  );
125
  $star_display = theme('fivestar_static', $variables);
126
  return theme('fivestar_static_element', array('description' => $element['#description'], 'star_display' => $star_display, 'is_form' => FALSE));
127
}
128

    
129
/**
130
 * Theme function for 'rating' fivestar field formatter.
131
 */
132
function theme_fivestar_formatter_rating($variables) {
133
  $element = $variables['element'];
134

    
135
  if (empty($element['#item']['average'])) {
136
    $element['#item']['average'] = 0;
137
  }
138
  // Get number of stars.
139
  $stars = (empty($element['#instance_settings']['stars'])) ? 5 : $element['#instance_settings']['stars'];
140
  $average = $element['#item']['average'];
141
  // Rating is X out of Y stars.
142
  $rating = round(($average / 100) * $stars, 1);
143
  $output = $rating . '/' . $stars;
144

    
145
  return $output;
146
}
147

    
148
/**
149
 * Theme function for 'percentage' fivestar field formatter.
150
 */
151
function theme_fivestar_formatter_percentage($variables) {
152
  $element = $variables['element'];
153

    
154
  if (empty($element['#item']['average'])) {
155
    $element['#item']['average'] = 0;
156
  }
157

    
158
  return round($element['#item']['average'], 1) . '%';
159
}
160

    
161
/**
162
 * Theme the fivestar form element by adding necessary css and javascript.
163
 */
164
function theme_fivestar($variables) {
165
  $element = $variables['element'];
166

    
167
  return theme('form_element', array('element' => $element));
168
}
169

    
170
/**
171
 * Theme the straight HTML version of the fivestar select list. This is used
172
 * to remove the wrapping 'form-item' div from the select list.
173
 */
174
function theme_fivestar_select($variables) {
175
  $element = $variables['element'];
176
  element_set_attributes($element, array('id', 'name', 'size'));
177
  _form_set_class($element, array('form-select'));
178
  return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
179
}
180

    
181
/**
182
 * Display a plain HTML view-only version of the widget with a specified rating.
183
 *
184
 * @param $rating
185
 *   The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
186
 * @param $stars
187
 *   The total number of stars this rating is out of.
188
 * @param $tag
189
 *   Allows multiple ratings per node.
190
 *
191
 * @return
192
 *   A themed HTML string representing the star widget.
193
 */
194
function theme_fivestar_static($variables) {
195
  $rating = $variables['rating'];
196
  $stars = $variables['stars'];
197
  $tag = $variables['tag'];
198
  $widget = $variables['widget'];
199
  if ($widget['name'] != 'default') {
200
    $path = drupal_get_path('module', 'fivestar');
201
    drupal_add_css($path . '/css/fivestar.css');
202
    drupal_add_css($widget['css']);
203
  }
204

    
205
  $output = '<div class="fivestar-' . $widget['name'] . '">';
206
  $output .= '<div class="fivestar-widget-static fivestar-widget-static-' . $tag . ' fivestar-widget-static-' . $stars . ' clearfix">';
207
  if (empty($stars)) {
208
    $stars = 5;
209
  }
210
  $numeric_rating = $rating / (100 / $stars);
211
  for ($n = 1; $n <= $stars; $n++) {
212
    $star_value = ceil((100 / $stars) * $n);
213
    $prev_star_value = ceil((100 / $stars) * ($n - 1));
214
    $zebra = ($n % 2 == 0) ? 'even' : 'odd';
215
    $first = $n == 1 ? ' star-first' : '';
216
    $last = $n == $stars ? ' star-last' : '';
217
    $output .= '<div class="star star-' . $n . ' star-' . $zebra . $first . $last . '">';
218
    if ($rating < $star_value && $rating > $prev_star_value) {
219
      $percent = (($rating - $prev_star_value) / ($star_value - $prev_star_value)) * 100;
220
      $output .= '<span class="on" style="width: ' . $percent . '%">';
221
    }
222
    elseif ($rating >= $star_value) {
223
      $output .= '<span class="on">';
224
    }
225
    else {
226
      $output .= '<span class="off">';
227
    }
228
    if ($n == 1) {
229
      $output .= $numeric_rating;
230
    }
231
    $output .= '</span></div>';
232
  }
233
  $output .= '</div></div>';
234
  return $output;
235
}
236

    
237
/**
238
 * Display the text associated with a static star display.
239
 *
240
 * Note that passing in explicit data types is extremely important when using
241
 * this function. A NULL value will exclude the value entirely from display,
242
 * while a 0 value indicates that the text should be shown but it has no value
243
 * yet.
244
 *
245
 * All ratings are from 0 to 100.
246
 *
247
 * @param $user_rating
248
 *   The current user's rating.
249
 * @param $average
250
 *   The average rating.
251
 * @param $votes
252
 *   The total number of votes.
253
 * @param $stars
254
 *   The number of stars being displayed.
255
 *
256
 * @return
257
 *   A themed HTML string representing the star widget.
258
 */
259
function theme_fivestar_summary($variables) {
260
  $microdata = $variables['microdata'];
261
  extract($variables, EXTR_SKIP);
262
  $output = '';
263
  $div_class = '';
264
  $average_rating_microdata = '';
265
  $rating_count_microdata = '';
266
  if (isset($user_rating)) {
267
    $div_class = isset($votes) ? 'user-count' : 'user';
268
    $user_stars = round(($user_rating * $stars) / 100, 1);
269
    $output .= '<span class="user-rating">' . t('Your rating: <span>!stars</span>', array('!stars' => $user_rating ? $user_stars : t('None'))) . '</span>';
270
  }
271
  if (isset($user_rating) && isset($average_rating)) {
272
    $output .= ' ';
273
  }
274
  if (isset($average_rating)) {
275
    if (isset($user_rating)) {
276
      $div_class = 'combo';
277
    }
278
    else {
279
      $div_class = isset($votes) ? 'average-count' : 'average';
280
    }
281

    
282
    $average_stars = round(($average_rating * $stars) / 100, 1);
283
    if (!empty($microdata['average_rating']['#attributes'])) {
284
      $average_rating_microdata = drupal_attributes($microdata['average_rating']['#attributes']);
285
    }
286
    $output .= '<span class="average-rating">' . t('Average: !stars',
287
      array('!stars' => "<span $average_rating_microdata>$average_stars</span>")) . '</span>';
288
  }
289

    
290
  if (isset($votes)) {
291
    if (!isset($user_rating) && !isset($average_rating)) {
292
      $div_class = 'count';
293
    }
294
    if ($votes === 0) {
295
      $output = '<span class="empty">' . t('No votes yet') . '</span>';
296
    }
297
    else {
298
      if (!empty($microdata['rating_count']['#attributes'])) {
299
        $rating_count_microdata = drupal_attributes($microdata['rating_count']['#attributes']);
300
      }
301
      // We don't directly substitute $votes (i.e. use '@count') in
302
      // format_plural(), because it has a span around it which is not
303
      // translatable.
304
      $votes_str = format_plural($votes, '!cnt vote', '!cnt votes', array(
305
        '!cnt' => '<span ' . $rating_count_microdata . '>' . intval($votes) . '</span>',
306
      ));
307
      if (isset($user_rating) || isset($average_rating)) {
308
        $output .= ' <span class="total-votes">(' . $votes_str . ')</span>';
309
      }
310
      else {
311
        $output .= ' <span class="total-votes">' . $votes_str . '</span>';
312
      }
313
    }
314
  }
315

    
316
  $output = '<div class="fivestar-summary fivestar-summary-' . $div_class . '">' . $output . '</div>';
317
  return $output;
318
}
319

    
320
/**
321
 * Display a static fivestar value as stars with a title and description.
322
 */
323
function theme_fivestar_static_element($variables) {
324
  $output = '';
325
  if (isset($variables['is_form']) && !$variables['is_form']) {
326
    $output .= '<div class="fivestar-static-item">';
327
  }
328
  else {
329
    $output .= '<div class="fivestar-static-form-item">';
330
  }
331
  $element = array(
332
    '#type' => 'item',
333
    '#title' => $variables['title'],
334
    '#description' => $variables['description'],
335
    '#children' => $variables['star_display'],
336
  );
337

    
338
  $output .= theme('form_element', array('element' => $element));
339
  $output .= '</div>';
340
  return $output;
341
}