Révision 4853591f
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/fivestar/includes/fivestar.field.inc | ||
---|---|---|
62 | 62 |
function fivestar_field_instance_settings_form($field, $instance) { |
63 | 63 |
$form = array(); |
64 | 64 |
|
65 |
$widget_title = ($instance['widget']['type'] == 'select') ? t('Number of options') : t('Number of stars'); |
|
65 | 66 |
$form['stars'] = array( |
66 | 67 |
'#type' => 'select', |
67 |
'#title' => $instance['widget']['type'] == 'select' ? t('Number of options') : t('Number of stars'),
|
|
68 |
'#title' => check_plain($widget_title),
|
|
68 | 69 |
'#options' => drupal_map_assoc(range(1, 10)), |
69 | 70 |
'#default_value' => isset($instance['settings']['stars']) ? $instance['settings']['stars'] : 5, |
70 | 71 |
); |
71 |
|
|
72 |
|
|
72 | 73 |
$form['allow_clear'] = array( |
73 | 74 |
'#type' => 'checkbox', |
74 | 75 |
'#title' => t('Allow users to cancel their ratings.'), |
... | ... | |
85 | 86 |
'#description' => t('The voting target will make the value of this field cast a vote on another node. Use node reference fields module to create advanced reviews. Use the Parent Node Target when using fivestar with comments. More information available on the <a href="http://drupal.org/handbook/modules/fivestar">Fivestar handbook page</a>.'), |
86 | 87 |
'#access' => (count($options) > 1 && $instance['widget']['type'] != 'exposed'), |
87 | 88 |
); |
88 |
|
|
89 |
|
|
89 | 90 |
return $form; |
90 | 91 |
} |
91 | 92 |
|
... | ... | |
123 | 124 |
_fivestar_cast_vote($target['entity_type'], $target['entity_id'], $rating, $field['settings']['axis'], $entity->uid, TRUE); |
124 | 125 |
votingapi_recalculate_results($target['entity_type'], $target['entity_id']); |
125 | 126 |
} |
127 |
// The original callback is only called for a single updated field, but the Field API |
|
128 |
// then updates all fields of the entity. For an update, the Field API first deletes |
|
129 |
// the equivalent row in the database and then adds a new row based on the |
|
130 |
// information in $items here. If there are multiple Fivestar fields on an entity, the |
|
131 |
// one being updated is handled ok ('rating' having already been set to the new value), |
|
132 |
// but other Fivestar fields are set to NULL as 'rating' isn't set - not what an update |
|
133 |
// would be expected to do. So set 'rating' for all of the Fivestar fields from the |
|
134 |
// existing user data in $items. This preserves the user vote thru Field API's |
|
135 |
// delete/re-insert process. |
|
136 |
if (!isset($items[$delta]['rating'])) { |
|
137 |
$items[$delta]['rating'] = $items[$delta]['user']; |
|
138 |
} |
|
126 | 139 |
} |
127 | 140 |
} |
128 | 141 |
|
... | ... | |
131 | 144 |
*/ |
132 | 145 |
function _fivestar_field_target($entity, $field, $instance, $item, $langcode) { |
133 | 146 |
if ($instance['widget']['type'] == 'exposed') { |
134 |
return null;
|
|
147 |
return NULL;
|
|
135 | 148 |
} |
136 | 149 |
if (isset($instance['settings']['target'])) { |
137 | 150 |
$target = fivestar_get_targets($field, $instance, $instance['settings']['target'], $entity, $langcode); |
... | ... | |
152 | 165 |
*/ |
153 | 166 |
function _fivestar_update_field_value($entity_type, $entity, $field_name, $langcode, $value) { |
154 | 167 |
$entity->{$field_name}[$langcode][0]['rating'] = $value; |
168 |
$entity->original = isset($entity->original) ? $entity->original : NULL; |
|
155 | 169 |
field_attach_presave($entity_type, $entity); |
156 | 170 |
field_attach_update($entity_type, $entity); |
157 | 171 |
} |
... | ... | |
223 | 237 |
} |
224 | 238 |
|
225 | 239 |
function fivestar_previews_expand($element) { |
226 |
|
|
240 |
|
|
227 | 241 |
foreach (element_children($element) as $css) { |
228 | 242 |
$vars = array( |
229 | 243 |
'css' => $css, |
... | ... | |
231 | 245 |
); |
232 | 246 |
$element[$css]['#description'] = theme('fivestar_preview_widget', $vars); |
233 | 247 |
} |
234 |
|
|
248 |
|
|
235 | 249 |
return $element; |
236 | 250 |
} |
237 | 251 |
|
... | ... | |
240 | 254 |
*/ |
241 | 255 |
function fivestar_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { |
242 | 256 |
$element['#tree'] = TRUE; |
243 |
|
|
257 |
$i18n = function_exists('i18n_field_translate_property'); |
|
244 | 258 |
if ($instance['widget']['type'] == 'fivestar_select' || ($instance['widget']['type'] == 'stars' && isset($form['#title']) && $form['#title'] == 'Default value')) { |
245 | 259 |
$options = array(0 => t('No stars')); |
246 | 260 |
if (empty($instance['settings']['stars'])) { |
247 | 261 |
$instance['settings']['stars'] = 5; |
248 | 262 |
} |
249 |
for($i = 1; $i <= $instance['settings']['stars']; $i++) { |
|
263 |
for ($i = 1; $i <= $instance['settings']['stars']; $i++) {
|
|
250 | 264 |
$percentage = ceil($i * 100 / $instance['settings']['stars']); |
251 | 265 |
$options[$percentage] = format_plural($i, '1 star', '@count stars'); |
252 | 266 |
} |
253 | 267 |
$element['rating'] = array( |
254 | 268 |
'#type' => 'select', |
255 |
'#title' => isset($instance['label']) ? $instance['label'] : FALSE,
|
|
269 |
'#title' => isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE,
|
|
256 | 270 |
'#options' => $options, |
257 | 271 |
'#default_value' => isset($items[$delta]['rating']) ? $items[$delta]['rating'] : NULL, |
258 |
'#description' => isset($instance['description']) ? $instance['description'] : FALSE,
|
|
272 |
'#description' => isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE,
|
|
259 | 273 |
'#required' => isset($instance['required']) ? $instance['required'] : FALSE, |
260 | 274 |
); |
261 | 275 |
} |
... | ... | |
281 | 295 |
'text' => 'none', |
282 | 296 |
'widget' => $widget, |
283 | 297 |
); |
284 |
|
|
298 |
|
|
285 | 299 |
$element['rating'] = array( |
286 | 300 |
'#type'=> 'fivestar', |
287 |
'#title' => isset($instance['label']) ? $instance['label'] : FALSE,
|
|
301 |
'#title' => isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE,
|
|
288 | 302 |
'#stars' => isset($instance['settings']['stars']) ? $instance['settings']['stars'] : 5, |
289 | 303 |
'#allow_clear' => isset($instance['settings']['allow_clear']) ? $instance['settings']['allow_clear'] : FALSE, |
290 | 304 |
'#default_value' => isset($items[$delta]['rating']) ? $items[$delta]['rating'] : (isset($instance['default_value'][$delta]['rating']) ? $instance['default_value'][$delta]['rating'] : 0), |
291 | 305 |
'#widget' => $widget, |
292 | 306 |
'#settings' => $settings, |
293 | 307 |
'#values' => $values, |
294 |
'#description' => isset($instance['description']) ? $instance['description'] : FALSE,
|
|
308 |
'#description' => isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE,
|
|
295 | 309 |
'#required' => isset($instance['required']) ? $instance['required'] : FALSE, |
296 | 310 |
); |
297 | 311 |
} |
... | ... | |
336 | 350 |
function fivestar_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { |
337 | 351 |
$display = $instance['display'][$view_mode]; |
338 | 352 |
$settings = $display['settings']; |
339 |
|
|
353 |
|
|
340 | 354 |
if ($display['type'] != 'fivestar_formatter_default') { |
341 | 355 |
return; |
342 | 356 |
} |
... | ... | |
352 | 366 |
); |
353 | 367 |
|
354 | 368 |
$widgets = module_invoke_all('fivestar_widgets'); |
355 |
|
|
369 |
|
|
356 | 370 |
$element['widget']['fivestar_widget'] = array( |
357 | 371 |
'#type' => 'radios', |
358 | 372 |
'#options' => array('default' => t('Default')) + $widgets, |
... | ... | |
363 | 377 |
); |
364 | 378 |
|
365 | 379 |
if ($instance['widget']['type'] == 'exposed') { |
366 |
// Removed entity_get_info here and simplified the text. This is not saved |
|
367 |
// to used here and is honestly not need it. See http://drupal.org/node/1268972 |
|
368 | 380 |
$element['expose'] = array( |
369 | 381 |
'#type' => 'checkbox', |
370 |
'#title' => t('Expose this Fivestar field for voting on the @type type.'),
|
|
382 |
'#title' => t('Allow voting on the entity.'),
|
|
371 | 383 |
'#default_value' => $settings['expose'], |
372 | 384 |
'#return_value' => 1 |
373 | 385 |
); |
... | ... | |
421 | 433 |
'@text' => strtolower($settings['text']))); |
422 | 434 |
return $summary; |
423 | 435 |
} |
424 |
|
|
436 |
|
|
425 | 437 |
$summary = t("Style: @widget, Stars display: @style, Text display: @text", array( |
426 | 438 |
'@widget' => isset($widgets[$settings['widget']['fivestar_widget']]) ? $widgets[$settings['widget']['fivestar_widget']] : t('default'), |
427 | 439 |
'@style' => strtolower($settings['style']), |
... | ... | |
614 | 626 |
'labels_enable' => FALSE, |
615 | 627 |
'labels' => array(), |
616 | 628 |
'widget' => $widget, |
629 |
'description' => $instance['description'], |
|
617 | 630 |
); |
618 | 631 |
|
619 | 632 |
return $settings; |
... | ... | |
652 | 665 |
// (which is the same as nothing). |
653 | 666 |
$fivestar_widget = 'default'; |
654 | 667 |
} |
655 |
else if ($type == 'stars') {
|
|
668 |
elseif ($type == 'stars') { |
|
656 | 669 |
// Stars rated while editing, that is, $type = stars, |
657 | 670 |
// falls back on whatever the user selected to be displayed on node/add and node/%/edit |
658 | 671 |
$fivestar_widget = $instance['widget']['settings']['widget']['fivestar_widget']; |
... | ... | |
668 | 681 |
} |
669 | 682 |
|
670 | 683 |
/** |
671 |
* Implements hook_field_prepare_view() |
|
684 |
* Implements hook_field_prepare_view().
|
|
672 | 685 |
*/ |
673 | 686 |
function fivestar_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { |
674 | 687 |
// TODO: Clean this function up! |
675 | 688 |
foreach ($entities as $id => $entity) { |
676 |
// Populating the $items[$id] array even for items with no value forces |
|
677 |
// the render system to output a widget. |
|
678 |
if ($instances[$id]['widget']['type'] == 'exposed') { |
|
679 |
// If the widget type is exposed then we want to look up the voting api values. |
|
680 |
$tag = $field['settings']['axis']; |
|
681 |
$votes = fivestar_get_votes($entity_type, $id, $tag); |
|
682 |
$values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0; |
|
683 |
$values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0; |
|
684 |
$values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0; |
|
685 |
$items[$id] = array($values); |
|
686 |
} |
|
687 |
else { |
|
688 |
// If the widget type is not exposed, then the count is always 1 or 0. |
|
689 |
// The count is pointless to display. |
|
690 |
if (!empty($items[$id][0]['rating'])) { |
|
691 |
$values['count'] = 1; |
|
692 |
$values['user'] = $items[$id][0]['rating']; |
|
693 |
$values['average'] = $items[$id][0]['rating']; |
|
689 |
// Ensure the items aren't processed twice. |
|
690 |
if (!isset($items[$id][0]['count'])) { |
|
691 |
// Populating the $items[$id] array even for items with no value forces |
|
692 |
// the render system to output a widget. |
|
693 |
if ($instances[$id]['widget']['type'] == 'exposed') { |
|
694 |
// If the widget type is exposed then we want to look up the voting api |
|
695 |
// values. |
|
696 |
$tag = $field['settings']['axis']; |
|
697 |
$votes = fivestar_get_votes($entity_type, $id, $tag); |
|
698 |
$values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0; |
|
699 |
$values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0; |
|
700 |
$values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0; |
|
701 |
$items[$id] = array($values); |
|
694 | 702 |
} |
695 | 703 |
else { |
696 |
$values['count'] = 0; |
|
697 |
$values['user'] = 0; |
|
698 |
$values['average'] = 0; |
|
704 |
// If the widget type is not exposed, then the count is always 1 or 0. |
|
705 |
// The count is pointless to display. |
|
706 |
if (!empty($items[$id][0]['rating'])) { |
|
707 |
$values['count'] = 1; |
|
708 |
$values['user'] = $items[$id][0]['rating']; |
|
709 |
$values['average'] = $items[$id][0]['rating']; |
|
710 |
} |
|
711 |
else { |
|
712 |
$values['count'] = 0; |
|
713 |
$values['user'] = 0; |
|
714 |
$values['average'] = 0; |
|
715 |
} |
|
716 |
$items[$id] = array($values); |
|
699 | 717 |
} |
700 |
$items[$id] = array($values); |
|
701 | 718 |
} |
702 | 719 |
} |
703 | 720 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules