uc_attribute.rules.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Rules hooks for Ubercart attributes.
  5. */
  6. /**
  7. * Implements hook_rules_condition_info().
  8. */
  9. function uc_attribute_rules_condition_info() {
  10. $conditions = array();
  11. $conditions['uc_attribute_ordered_product_option'] = array(
  12. 'label' => t('Order has a product with a particular attribute option'),
  13. 'description' => t('Search the products of an order for a particular option.'),
  14. 'group' => t('Order: Product'),
  15. 'base' => 'uc_attribute_condition_ordered_product_option',
  16. 'parameter' => array(
  17. 'order' => array('type' => 'uc_order', 'label' => t('Order')),
  18. 'option' => array('type' => 'integer', 'label' => t('Attribute option'), 'options list' => 'uc_attribute_condition_ordered_product_options_list'),
  19. ),
  20. );
  21. return $conditions;
  22. }
  23. /**
  24. * Returns TRUE if a product in the given order has the selected option.
  25. *
  26. * @see uc_attribute_condition_ordered_product_option_form()
  27. */
  28. function uc_attribute_condition_ordered_product_option($order, $oid) {
  29. $option = uc_attribute_option_load($oid);
  30. $attribute = uc_attribute_load($option->aid);
  31. foreach ($order->products as $product) {
  32. if (!isset($product->data['attributes'])) {
  33. continue;
  34. }
  35. $attributes = $product->data['attributes'];
  36. // Once the order is made, the attribute data is changed to just the names.
  37. // If we can't find it by ID, check the names.
  38. if (is_int(key($attributes))) {
  39. if (isset($attributes[$oid])) {
  40. return TRUE;
  41. }
  42. }
  43. elseif (isset($attributes[$attribute->name][$oid])) {
  44. return TRUE;
  45. }
  46. }
  47. return FALSE;
  48. }
  49. /**
  50. * Options callback.
  51. *
  52. * @see uc_attribute_condition_ordered_product_option()
  53. */
  54. function uc_attribute_condition_ordered_product_options_list() {
  55. $options = array();
  56. $result = db_query("SELECT a.aid, a.name AS attr_name, a.ordering, o.oid, o.name AS opt_name, o.ordering FROM {uc_attributes} a JOIN {uc_attribute_options} o ON a.aid = o.aid ORDER BY a.ordering, o.ordering");
  57. foreach ($result as $option) {
  58. $options[$option->attr_name][$option->oid] = $option->opt_name;
  59. }
  60. return $options;
  61. }