Projet

Général

Profil

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

root / htmltest / sites / all / modules / fivestar / includes / fivestar.theme.inc @ a5572547

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
    '#widget' => array(
34
      'name' => isset($variables['name']) ? $variables['name'] : 'default',
35
      'css' => isset($variables['css']) && $variables['css'] != 'default' ? $variables['css'] : FALSE,
36
    ),
37
  );
38

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

    
49
  drupal_add_js(array('fivestar' => $settings), 'setting');
50

    
51
  $form = form_builder('fivestar_preview', $form, $form_state);
52

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

    
57
  return $output;
58
}
59

    
60
function theme_fivestar_preview($variables) {
61
  extract($variables, EXTR_SKIP);
62
  $values = array(
63
    'average' => 50,
64
    'user' => 80,
65
    'count' => 20,
66
  );
67
  $settings = array(
68
    'stars' => $stars,
69
    'allow_clear' => $unvote,
70
    'style' => $style,
71
    'text' => $text,
72
    'title' => $title,
73
    'autosubmit' => FALSE,
74
    'tag' => 'vote',
75
  );
76

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

    
87
function theme_fivestar_preview_wrapper($variables) {
88
  return '<div class="fivestar-preview fivestar-preview-'. $variables['type'] .'">'. $variables['content'] .'</div>';
89
}
90

    
91
/**
92
 * Theme function for 'default' fivestar field formatter.
93
 *
94
 * This themes static stars. That is, pairs of stars where neither set
95
 * of stars is "exposed". Exposed stars are clickable and displayed in a
96
 * form. Theming of exposed stars is handled by the form array (and calls
97
 * the same theme functions called at the end of this function).
98
 */
99
function theme_fivestar_formatter_default($variables) {
100
  $element = $variables['element'];
101
  if (empty($element['#instance_settings']['stars'])) {
102
    $element['#instance_settings']['stars'] = 5;
103
  }
104

    
105
  // Add CSS and JS
106
  $path = drupal_get_path('module', 'fivestar');
107
  drupal_add_js($path . '/js/fivestar.js');
108
  drupal_add_css($path . '/css/fivestar.css');
109

    
110
  $variables = array(
111
    'rating' => $element['#rating'],
112
    'stars' => $element['#instance_settings']['stars'],
113
    'widget' => $element['#widget'],
114
  );
115
  $star_display = theme('fivestar_static', $variables);
116
  return theme('fivestar_static_element', array('description' => $element['#description'], 'star_display' => $star_display, 'is_form' => FALSE));
117
}
118

    
119
/**
120
 * Theme function for 'rating' fivestar field formatter.
121
 */
122
function theme_fivestar_formatter_rating($variables) {
123
  $element = $variables['element'];
124

    
125
  if (empty($element['#item']['average'])) {
126
    $element['#item']['average'] = 0;
127
  }
128
  // Get number of stars.
129
  $stars = (empty($element['#instance_settings']['stars'])) ? 5 : $element['#instance_settings']['stars'];
130
  $average = $element['#item']['average'];
131
  // Rating is X out of Y stars.
132
  $rating = round(($average/100) * $stars, 1);
133
  $output = $rating . '/' . $stars;
134

    
135
  return $output;
136
}
137

    
138
/**
139
 * Theme function for 'percentage' fivestar field formatter.
140
 */
141
function theme_fivestar_formatter_percentage($variables) {
142
  $element = $variables['element'];
143

    
144
  if (empty($element['#item']['average'])) {
145
    $element['#item']['average'] = 0;
146
  }
147

    
148
  return round($element['#item']['average'], 1) . '%';
149
}
150

    
151
/**
152
 * Theme the fivestar form element by adding necessary css and javascript.
153
 */
154
function theme_fivestar($variables) {
155
  $element = $variables['element'];
156

    
157
  return theme('form_element', array('element' => $element));
158
}
159

    
160
/**
161
 * Theme the straight HTML version of the fivestar select list. This is used
162
 * to remove the wrapping 'form-item' div from the select list.
163
 */
164
function theme_fivestar_select($variables) {
165
  $element = $variables['element'];
166
  element_set_attributes($element, array('id', 'name', 'size'));
167
  _form_set_class($element, array('form-select'));
168
  return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
169
}
170
/**
171
 * Display a plain HTML view-only version of the widget with a specified rating.
172
 *
173
 * @param $rating
174
 *   The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
175
 * @param $stars
176
 *   The total number of stars this rating is out of.
177
 * @param $tag
178
 *   Allows multiple ratings per node.
179
 * @return
180
 *   A themed HTML string representing the star widget.
181
 */
182
function theme_fivestar_static($variables) {
183
  $rating  = $variables['rating'];
184
  $stars = $variables['stars'];
185
  $tag = $variables['tag'];
186
  $widget = $variables['widget'];
187
  if($widget['name'] != 'default') {
188
    drupal_add_css($widget['css']);
189
  }
190

    
191
  $output = '<div class="fivestar-' . $widget['name'] . '">';
192
  $output .= '<div class="fivestar-widget-static fivestar-widget-static-'. $tag .' fivestar-widget-static-'. $stars . ' clearfix">';
193
  if (empty($stars)) {
194
    $stars = 5;
195
  }
196
  $numeric_rating = $rating/(100/$stars);
197
  for ($n=1; $n <= $stars; $n++) {
198
    $star_value = ceil((100/$stars) * $n);
199
    $prev_star_value = ceil((100/$stars) * ($n-1));
200
    $zebra = ($n % 2 == 0) ? 'even' : 'odd';
201
    $first = $n == 1 ? ' star-first' : '';
202
    $last = $n == $stars ? ' star-last' : '';
203
    $output .= '<div class="star star-'. $n .' star-'. $zebra . $first . $last .'">';
204
    if ($rating < $star_value && $rating > $prev_star_value) {
205
      $percent = (($rating - $prev_star_value) / ($star_value - $prev_star_value)) * 100;
206
      $output .= '<span class="on" style="width: '. $percent .'%">';
207
    }
208
    elseif ($rating >= $star_value) {
209
      $output .= '<span class="on">';
210
    }
211
    else {
212
      $output .= '<span class="off">';
213
    }
214
    if ($n == 1)$output .= $numeric_rating;
215
    $output .= '</span></div>';
216
  }
217
  $output .= '</div></div>';
218
  return $output;
219
}
220

    
221
/**
222
 * Display the text associated with a static star display.
223
 *
224
 * Note that passing in explicit data types is extremely important when using
225
 * this function. A NULL value will exclude the value entirely from display,
226
 * while a 0 value indicates that the text should be shown but it has no value
227
 * yet.
228
 *
229
 * All ratings are from 0 to 100.
230
 *
231
 * @param $user_rating
232
 *   The current user's rating.
233
 * @param $average
234
 *   The average rating.
235
 * @param $votes
236
 *   The total number of votes.
237
 * @param $stars
238
 *   The number of stars being displayed.
239
 * @return
240
 *   A themed HTML string representing the star widget.
241
 */
242
function theme_fivestar_summary($variables) {
243
  $microdata = $variables['microdata'];
244
  extract($variables, EXTR_SKIP);
245
  $output = '';
246
  $div_class = '';
247
  $average_rating_microdata = '';
248
  if (isset($user_rating)) {
249
    $div_class = isset($votes) ? 'user-count' : 'user';
250
    $user_stars = round(($user_rating * $stars) / 100, 1);
251
    $output .= '<span class="user-rating">'. t('Your rating: <span>!stars</span>', array('!stars' => $user_rating ? $user_stars : t('None'))) .'</span>';
252
  }
253
  if (isset($user_rating) && isset($average_rating)) {
254
    $output .= ' ';
255
  }
256
  if (isset($average_rating)) {
257
    $div_class = isset($votes) ? 'average-count' : 'average';
258
    $average_stars = round(($average_rating * $stars) / 100, 1);
259
    if (!empty($microdata['average_rating']['#attributes'])) {
260
      $average_rating_microdata = drupal_attributes($microdata['average_rating']['#attributes']);
261
    }
262
    $output .= '<span class="average-rating">'. t('Average: <span !microdata>!stars</span>', array('!stars' => $average_stars, '!microdata' => $average_rating_microdata)) .'</span>';
263
  }
264
  if (isset($user_rating) && isset($average_rating)) {
265
    $div_class = 'combo';
266
  }
267

    
268
  if (isset($votes) && !(isset($user_rating) || isset($average_rating))) {
269
    $output .= ' <span class="total-votes">'. format_plural($votes, '<span>@count</span> vote', '<span>@count</span> votes') .'</span>';
270
    $div_class = 'count';
271
  }
272
  elseif (isset($votes)) {
273
    $output .= ' <span class="total-votes">('. format_plural($votes, '<span>@count</span> vote', '<span>@count</span> votes') .')</span>';
274
  }
275

    
276
  if ($votes === 0) {
277
    $output = '<span class="empty">'. t('No votes yet') .'</span>';
278
  }
279

    
280
  $output = '<div class="fivestar-summary fivestar-summary-'. $div_class . '">'. $output .'</div>';
281
  return $output;
282
}
283

    
284
/**
285
 * Display a static fivestar value as stars with a title and description.
286
 */
287
function theme_fivestar_static_element($variables) {
288
  $output = '';
289
  if (isset($variables['is_form']) && !$variables['is_form']) {
290
    $output .= '<div class="fivestar-static-item">';
291
  }
292
  else {
293
    $output .= '<div class="fivestar-static-form-item">';
294
  }
295
  $element = array(
296
    '#type' => 'item',
297
    '#title' => $variables['title'],
298
    '#description' => $variables['description'],
299
    '#children' => $variables['star_display'],
300
  );
301

    
302
  $output .= theme('form_element', array('element' => $element));
303
  $output .= '</div>';
304
  return $output;
305
}