uc_coupon_handler_field_all_orders_total.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Coupon order total field handler
  6. */
  7. class uc_coupon_handler_field_all_orders_total extends uc_order_handler_field_money_amount {
  8. /**
  9. * Add 'cid' field.
  10. */
  11. function construct() {
  12. parent::construct();
  13. $this->additional_fields['cid'] = 'cid';
  14. }
  15. /**
  16. * Define option default values.
  17. */
  18. function option_definition() {
  19. $options = parent::option_definition();
  20. $options['statuses'] = array('default' => array());
  21. return $options;
  22. }
  23. /**
  24. * Add option for order status.
  25. */
  26. function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $statuses = array();
  29. foreach (uc_order_status_list() as $status) {
  30. $statuses[$status['id']] = $status['title'];
  31. }
  32. $form['statuses'] = array(
  33. '#title' => t('Order statuses'),
  34. '#type' => 'select',
  35. '#description' => t('Only orders with selected statuses will be counted.
  36. If you do not select any statuses, then all orders will be counted.')
  37. . '<br />' . t('Hold Ctrl + click to select multiple statuses.'),
  38. '#options' => $statuses,
  39. '#default_value' => $this->options['statuses'],
  40. '#multiple' => TRUE,
  41. '#size' => 8,
  42. );
  43. }
  44. /**
  45. * Here we add the aggregate field that will sum the orders.
  46. */
  47. function query() {
  48. $this->ensure_my_table();
  49. $uco = $this->query->ensure_table('uc_coupons_orders');
  50. $uo = $this->query->ensure_table('uc_orders');
  51. $this->add_additional_fields();
  52. if (empty($this->options['statuses'])) {
  53. // If no status specified, then we show all.
  54. $in_status = "1";
  55. }
  56. else {
  57. // An array of statuses was specified.
  58. $in_status = "$uo.order_status IN('" . implode("', '", $this->options['statuses']) . "')";
  59. }
  60. $this->field_alias = $this->query->add_field(
  61. NULL,
  62. "CASE WHEN $in_status THEN $uo.order_total ELSE 0 END",
  63. $this->table_alias . '_' . $this->field . '_' . implode('_', $this->options['statuses']),
  64. array('function' => 'sum')
  65. );
  66. }
  67. }