uc_product_handler_field_price.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Views handler: Product price field.
  5. */
  6. /**
  7. * Returns a formatted price value to display in the View.
  8. */
  9. class uc_product_handler_field_price extends views_handler_field_numeric {
  10. /**
  11. * Overrides views_handler::option_definition().
  12. */
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['format'] = array('default' => 'uc_price');
  16. return $options;
  17. }
  18. /**
  19. * Overrides views_handler::options_form().
  20. */
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $options = $this->options;
  24. $form['format'] = array(
  25. '#title' => t('Format'),
  26. '#type' => 'radios',
  27. '#options' => array(
  28. 'uc_price' => t('Ubercart price'),
  29. 'numeric' => t('Numeric'),
  30. ),
  31. '#default_value' => $options['format'],
  32. '#weight' => 1,
  33. );
  34. // Change weight and dependency of the previous field on the parent numeric ones
  35. $weight = 2;
  36. foreach (array('set_precision', 'precision', 'decimal', 'separator', 'prefix', 'suffix') as $field) {
  37. if (isset($form[$field]['#dependency'])) {
  38. $form[$field]['#dependency'] += array('radio:options[format]' => array('numeric'));
  39. $form[$field]['#dependency_count'] = count($form[$field]['#dependency']);
  40. }
  41. else {
  42. $form[$field]['#dependency'] = array('radio:options[format]' => array('numeric'));
  43. }
  44. $form[$field]['#weight'] = ++$weight;
  45. }
  46. }
  47. /**
  48. * Overrides views_handler_field::render().
  49. */
  50. function render($values) {
  51. if ($this->options['format'] == 'numeric') {
  52. return parent::render($values);
  53. }
  54. if ($this->options['format'] == 'uc_price') {
  55. $value = $this->get_value($values);
  56. if (is_null($value) || ($value == 0 && $this->options['empty_zero'])) {
  57. return '';
  58. }
  59. return theme('uc_price', array('price' => $value));
  60. }
  61. }
  62. }