Projet

Général

Profil

Révision d50a36e0

Ajouté par Assos Assos il y a presque 4 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/fivestar/includes/fivestar.field.inc
2 2

  
3 3
/**
4 4
 * @file
5
 * Provides CCK integration for fivestar module
5
 * Provides CCK integration for fivestar module.
6 6
 */
7 7

  
8 8
/**
9
 * Implementation of hook_field_info().
9
 * Implements hook_field_info().
10 10
 */
11 11
function fivestar_field_info() {
12 12
  return array(
......
28 28
  );
29 29
}
30 30

  
31
/**
32
 *
33
 */
31 34
function fivestar_form_field_ui_field_edit_form_alter(&$form, $form_state) {
32 35
  $field = $form['#field'];
33 36
  $instance = $form['#instance'];
......
43 46
}
44 47

  
45 48
/**
46
 * Implementation of hook_field_settings_form().
49
 * Implements hook_field_settings_form().
47 50
 */
48 51
function fivestar_field_settings_form($field, $instance) {
49 52
  $form['axis'] = array(
......
59 62
  return $form;
60 63
}
61 64

  
65
/**
66
 *
67
 */
62 68
function fivestar_field_instance_settings_form($field, $instance) {
63 69
  $form = array();
64 70

  
......
105 111
}
106 112

  
107 113
/**
108
 * Implementation of hook_field_insert().
114
 * Implements hook_field_insert().
109 115
 */
110 116
function fivestar_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
111 117
  _fivestar_field_helper($entity_type, $entity, $field, $instance, $langcode, $items);
112 118
}
113 119

  
114 120
/**
115
 * Implementation of hook_field_update().
121
 * Implements hook_field_update().
116 122
 */
117 123
function fivestar_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
118 124
  _fivestar_field_helper($entity_type, $entity, $field, $instance, $langcode, $items);
119 125
}
120 126

  
121 127
/**
122
 * Implementation of hook_field_delete().
128
 * Implements hook_field_delete().
123 129
 */
124 130
function fivestar_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
125 131
  _fivestar_field_helper($entity_type, $entity, $field, $instance, $langcode, $items, 'delete');
126 132
}
127 133

  
134
/**
135
 * Implements hook_field_views_data().
136
 */
137
function fivestar_field_views_data($field) {
138
  $data = field_views_field_default_views_data($field);
139
  foreach ($data as $table_name => $table_data) {
140
    // Add our handler to the rating views field.
141
    $data[$table_name][$field['field_name'] . '_rating']['filter']['handler'] = 'fivestar_views_handler_filter_in_operator';
142
  }
143
  return $data;
144
}
145

  
146
/**
147
 *
148
 */
128 149
function _fivestar_field_helper($entity_type, $entity, $field, $instance, $langcode, &$items, $op = '') {
129 150
  foreach ($items as $delta => $item) {
130 151
    if ((isset($entity->status) && !$entity->status) || $op == 'delete') {
......
144 165
      _fivestar_cast_vote($target['entity_type'], $target['entity_id'], $rating, $field['settings']['axis'], $entity->uid, TRUE, $target['vote_source']);
145 166
      votingapi_recalculate_results($target['entity_type'], $target['entity_id']);
146 167
    }
147
    // The original callback is only called for a single updated field, but the Field API
148
    // then updates all fields of the entity. For an update, the Field API first deletes
149
    // the equivalent row in the database and then adds a new row based on the
150
    // information in $items here. If there are multiple Fivestar fields on an entity, the
151
    // one being updated is handled ok ('rating' having already been set to the new value),
152
    // but other Fivestar fields are set to NULL as 'rating' isn't set - not what an update
153
    // would be expected to do. So set 'rating' for all of the Fivestar fields from the
154
    // existing user data in $items. This preserves the user vote thru Field API's
155
    // delete/re-insert process.
168
    // The original callback is only called for a single updated field, but the
169
    // Field API then updates all fields of the entity. For an update, the Field
170
    // API first deletes the equivalent row in the database and then adds a new
171
    // row based on the information in $items here. If there are multiple
172
    // Fivestar fields on an entity, the one being updated is handled OK
173
    // ('rating' having already been set to the new value), but other Fivestar
174
    // fields are set to NULL as 'rating' isn't set - not what an update would
175
    // be expected to do. So set 'rating' for all of the Fivestar fields from
176
    // the existing user data in $items. This preserves the user vote thru Field
177
    // API's delete/re-insert process.
156 178
    if (!isset($items[$delta]['rating'])) {
157 179
      $items[$delta]['rating'] = $items[$delta]['user'];
158 180
    }
......
191 213
}
192 214

  
193 215
/**
194
 * Implementation of hook_field_is_empty().
216
 * Implements hook_field_is_empty().
195 217
 */
196 218
function fivestar_field_is_empty($item, $field) {
197 219
  return empty($item['rating']) || $item['rating'] == '-';
198 220
}
199 221

  
200 222
/**
201
 * Implementation of hook_field_widget_info().
223
 * Implements hook_field_widget_info().
202 224
 */
203 225
function fivestar_field_widget_info() {
204 226
  return array(
......
214 236
      'settings' => array(
215 237
        'widget' => array(
216 238
          'fivestar_widget' => 'default',
217
        )
218
      )
239
        ),
240
      ),
219 241
    ),
220 242
    'fivestar_select' => array(
221 243
      'label' => t('Select list (rated while editing)'),
......
226 248
}
227 249

  
228 250
/**
229
 * Implementation of hook_field_widget_settings_form().
251
 * Implements hook_field_widget_settings_form().
230 252
 */
231 253
function fivestar_field_widget_settings_form($field, $instance) {
232 254
  $form = array();
......
256 278
  return $form;
257 279
}
258 280

  
281
/**
282
 *
283
 */
259 284
function fivestar_previews_expand($element) {
260 285

  
261 286
  foreach (element_children($element) as $css) {
......
270 295
}
271 296

  
272 297
/**
273
 * Implementation of hook_field_widget_form().
298
 * Implements hook_field_widget_form().
274 299
 */
275 300
function fivestar_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
276 301
  $element['#tree'] = TRUE;
......
286 311
    }
287 312
    $element['rating'] = array(
288 313
      '#type' => 'select',
289
      '#title' => isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE,
314
      '#title' => check_plain(isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE),
290 315
      '#options' => $options,
291 316
      '#default_value' => isset($items[$delta]['rating']) ? $items[$delta]['rating'] : NULL,
292
      '#description' => isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE,
317
      '#description' => check_plain(isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE),
293 318
      '#required' => isset($instance['required']) ? $instance['required'] : FALSE,
294 319
    );
295 320
  }
......
319 344
    );
320 345

  
321 346
    $element['rating'] = array(
322
      '#type'=> 'fivestar',
323
      '#title' => isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE,
347
      '#type' => 'fivestar',
348
      '#title' => check_plain(isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE),
324 349
      '#stars' => isset($instance['settings']['stars']) ? $instance['settings']['stars'] : 5,
325 350
      '#allow_clear' => isset($instance['settings']['allow_clear']) ? $instance['settings']['allow_clear'] : FALSE,
326 351
      '#allow_revote' => isset($instance['settings']['allow_revote']) ? $instance['settings']['allow_revote'] : FALSE,
......
329 354
      '#widget' => $widget,
330 355
      '#settings' => $settings,
331 356
      '#values' => $values,
332
      '#description' => isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE,
357
      '#description' => check_plain(isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE),
333 358
      '#required' => isset($instance['required']) ? $instance['required'] : FALSE,
334 359
    );
335 360
  }
......
338 363
}
339 364

  
340 365
/**
341
 * Implementation of hook_field_formatter_info().
366
 * Implements hook_field_formatter_info().
342 367
 */
343 368
function fivestar_field_formatter_info() {
344 369
  return array(
345 370
    'fivestar_formatter_default' => array(
346 371
      'label' => t('As Stars'),
347 372
      'field types' => array('fivestar'),
348
      'settings' =>  array(
373
      'settings' => array(
349 374
        // Note: Do not set to widget to 'default' by
350 375
        // default. "Stars (rated while editing)" should
351 376
        // default to whatever was selected as a widget
......
405 430
      '#type' => 'checkbox',
406 431
      '#title' => t('Allow voting on the entity.'),
407 432
      '#default_value' => $settings['expose'],
408
      '#return_value' => 1
433
      '#return_value' => 1,
409 434
    );
410 435
  }
411 436

  
......
454 479
      '@widget' => isset($widgets[$settings['widget']['fivestar_widget']]) ? strtolower($widgets[$settings['widget']['fivestar_widget']]) : t('default'),
455 480
      '@expose' => ($settings['expose']) ? 'yes' : 'no',
456 481
      '@style' => strtolower($settings['style']),
457
      '@text' => strtolower($settings['text'])));
482
      '@text' => strtolower($settings['text']),
483
    ));
458 484
    return $summary;
459 485
  }
460 486

  
461 487
  $summary = t("Style: @widget, Stars display: @style, Text display: @text", array(
462 488
    '@widget' => isset($widgets[$settings['widget']['fivestar_widget']]) ? $widgets[$settings['widget']['fivestar_widget']] : t('default'),
463 489
    '@style' => strtolower($settings['style']),
464
    '@text' => strtolower($settings['text'])));
490
    '@text' => strtolower($settings['text']),
491
  ));
465 492

  
466 493
  return $summary;
467 494
}
......
476 503
 * fivestar theme function.
477 504
 *
478 505
 * @param $items
479
 *  Array. Generated by fivestar_field_prepare_view(). This array contains
480
 *  processed voting info.
506
 *   Array. Generated by fivestar_field_prepare_view(). This array contains
507
 *   processed voting info.
481 508
 *
482
 * @return $element
483
 *  Renderable array. This array will always be $element[0], with only one
484
 *  top level item, because Fivestar does not offer multi-value fields.
509
 * @return element
510
 *   Renderable array. This array will always be $element[0], with only one
511
 *   top level item, because Fivestar does not offer multi-value fields.
485 512
 */
486 513
function fivestar_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
487 514
  $element = array();
......
496 523
  // fivestar_custom_widget form, and let it take care of the rest.
497 524
  // Note: Stars will only be displayed in a form in the following circumstance:
498 525
  // - Fivestar widget selected is "Stars (rated while viewing)"
499
  // - Fivestar display setting = "exposed"
526
  // - Fivestar display setting = "exposed".
500 527
  $is_form = ($instance['widget']['type'] == 'exposed'
501 528
              && user_access('rate content')
502 529
              && $display['type'] == 'fivestar_formatter_default'
503 530
              && $display['settings']['expose']) ? TRUE : FALSE;
504 531
  if ($is_form) {
505
    // TODO. Get rid of voting categories setting, then change this so
532
    // @todo Get rid of voting categories setting, then change this so
506 533
    // axis = field name.
507 534
    $tag = (isset($field['settings']['axis'])) ? $field['settings']['axis'] : 'vote';
508 535
    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
......
584 611
    'votes' => NULL,
585 612
  );
586 613

  
587
 $summary_options['microdata'] = _fivestar_get_microdata_property_info($entity_type, $entity, $field, $instance);
614
  $summary_options['microdata'] = _fivestar_get_microdata_property_info($entity_type, $entity, $field, $instance);
588 615

  
589 616
  // If we're displaying both user and average ratings, add a description to
590 617
  // both the 'user' and 'average' elements.
......
633 660
 *
634 661
 * @params
635 662
 *
636
 * @return $settings
637
 *  Array. @see fivestar_custom_widget().
663
 * @return settings
664
 *   Array. @see fivestar_custom_widget().
638 665
 */
639 666
function _fivestar_custom_widget_settings($entity_type, $instance, $display, $id, $tag, $widget) {
640 667
  $settings = $display['settings'];
......
661 688

  
662 689
/**
663 690
 * @param $widgets
664
 *  Array, $widgets = module_invoke_all('fivestar_widgets');
665
 *  $widgets = array('path/to/css' => 'Widget Name', 'path/to/more/css' => 'Widget 2');
691
 *   Array, $widgets = module_invoke_all('fivestar_widgets');
692
 *   $widgets = array('path/to/css' => 'Widget Name', 'path/to/more/css' => 'Widget 2');
666 693
 *
667 694
 * @param $display
668
 *  Array. This is the $display parameter passed to fivestar_field_formatter_view().
695
 *   Array. This is the $display parameter passed to fivestar_field_formatter_view().
669 696
 *
670 697
 * @param $instance
671
 *  Array. This is the $instance parameter passed to fivestar_field_formatter_view().
698
 *   Array. This is the $instance parameter passed to fivestar_field_formatter_view().
672 699
 *
673
 * @return $widget
674
 *  Array. $widget = array('name' => 'my widget', 'css' => 'sites/all/mymodule/mywidget.css');
700
 * @return array
701
 *   $widget = array('name' => 'my widget', 'css' => 'sites/all/mymodule/mywidget.css');
675 702
 */
676 703
function _fivestar_get_widget($widgets, $display, $instance) {
677 704
  // If the type doesn't required widgets lets get out of here.
678
  // TODO: Implement this WAY better.
705
  // @todo Implement this WAY better.
679 706
  if (in_array($display['type'], array('fivestar_formatter_rating', 'fivestar_formatter_percentage'))) {
680 707
    return FALSE;
681 708
  }
......
694 721
    }
695 722
    elseif ($type == 'stars') {
696 723
      // Stars rated while editing, that is, $type = stars,
697
      // falls back on whatever the user selected to be displayed on node/add and node/%/edit
724
      // falls back on whatever the user selected to be displayed on node/add and node/%/edit.
698 725
      $fivestar_widget = $instance['widget']['settings']['widget']['fivestar_widget'];
699 726
    }
700 727
  }
......
711 738
 * Implements hook_field_prepare_view().
712 739
 */
713 740
function fivestar_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
714
  // TODO: Clean this function up!
741
  // @todo Clean this function up!
715 742
  $exposed_widgets = array();
716 743
  foreach ($entities as $id => $entity) {
717 744
    // Ensure the items aren't processed twice.
......
735 762
        $items[$id] = array($values);
736 763
      }
737 764
    }
738
    if(!empty($exposed_widgets)) {
765
    if (!empty($exposed_widgets)) {
739 766
      $votes = fivestar_get_votes_multiple($entity_type, $exposed_widgets, $field['settings']['axis']);
740
      foreach($votes[$entity_type] as $id => $vote) {
767
      foreach ($votes[$entity_type] as $id => $vote) {
741 768
        // Populating the $items[$id] array even for items with no value forces
742 769
        // the render system to output a widget.
743 770
        $values['user'] = isset($vote['user']['value']) ? $vote['user']['value'] : 0;
......
763 790
  $name = $field['field_name'];
764 791
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
765 792

  
793
  $property['getter callback'] = '_fivestar_field_property_values';
766 794
  $property['property info'] = array(
767 795
    'average_rating' => array(
768 796
      'label' => t('Average Rating'),
......
782 810
  );
783 811
}
784 812

  
813
/**
814
 * Callback for getting field property values.
815
 */
816
function _fivestar_field_property_values($entity, array $options, $name, $entity_type, $info) {
817
  $field = field_info_field($name);
818
  $langcode = field_language($entity_type, $entity, $name, isset($options['language']) ? $options['language']->language : NULL);
819
  $values = array();
820
  if (isset($entity->{$name}[$langcode])) {
821
    foreach ($entity->{$name}[$langcode] as $delta => $data) {
822
      $values[$delta]['user_rating'] = $data['rating'];
823
      $result = array();
824
      $result = votingapi_select_results(array(
825
        'entity_type' => $entity_type,
826
        'entity_id' => entity_id($entity_type, $entity),
827
        'value_type' => 'percent',
828
        'tag' => $field['settings']['axis'],
829
        'function' => 'average',
830
      ));
831
      $values[$delta]['average_rating'] = (isset($result[0]['value']) ? $result[0]['value'] : 0);
832
    }
833
  }
834
  // For an empty single-valued field, we have to return NULL.
835
  return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
836
}
837

  
785 838
/**
786 839
 * Get microdata attributes for Fivestar field.
787 840
 */

Formats disponibles : Unified diff