Projet

Général

Profil

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

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

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
    $path = drupal_get_path('module', 'fivestar');
189
    drupal_add_css($path . '/css/fivestar.css');
190
    drupal_add_css($widget['css']);
191
  }
192

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

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

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

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

    
283
  $output = '<div class="fivestar-summary fivestar-summary-' . $div_class . '">' . $output . '</div>';
284
  return $output;
285
}
286

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

    
305
  $output .= theme('form_element', array('element' => $element));
306
  $output .= '</div>';
307
  return $output;
308
}