Basic search API integration.
[sandbox/serialjaywalker/1195272.git] / modules / open_ring / commerce_pos_open_ring.module
1 <?php
2
3 /**
4 * @file
5 * Contains functions for the commerce_pos_open_ring module
6 */
7
8
9 /**
10 * Implements hook_commerce_product_type_info().
11 */
12 function commerce_pos_open_ring_commerce_product_type_info() {
13 return array(
14 'pos_open_ring' => array(
15 'type' => 'pos_open_ring',
16 'name' => t('Open Ring'),
17 'description' => t('This allows products to be sold for a price specified at the register.'),
18 ),
19 );
20 }
21
22 /**
23 * Disallow editing the price of an open ring product.
24 */
25 function commerce_pos_open_ring_field_access($op, $field, $entity_type, $entity, $account) {
26 if($op == 'edit' && $entity_type == 'commerce_product' && $entity->type == 'pos_open_ring' && $field['field_name'] == 'commerce_price') {
27 return FALSE;
28 }
29 }
30
31 /**
32 * Implements hook_commerce_pos_action_info()
33 */
34 function commerce_pos_open_ring_commerce_pos_action_info() {
35 return array(
36 'commerce_pos_open_ring' => array(
37 'weight' => 99,
38 'callbacks' => array(
39 'parse' => 'commerce_pos_open_ring_parse',
40 'extract' => 'commerce_pos_open_ring_extract',
41 'access' => 'commerce_pos_product_access',
42 'validate' => 'commerce_pos_open_ring_validate',
43 'execute' => 'commerce_pos_product_execute',
44 ),
45 ),
46 );
47 }
48
49 /**
50 * Parse callback for the open ring action.
51 */
52 function commerce_pos_open_ring_parse($input, $action_text) {
53 $args = explode('*', $input);
54 $sku = array_pop($args);
55 //It seems not great to be loading the product here, so think reworking the workflow
56 //to avoid this. However, it's not the worst thing since the product will be cached.
57 $product = commerce_product_load_by_sku($sku);
58
59 if ($product->type == 'pos_open_ring') {
60 return TRUE;
61 }
62 }
63
64 /**
65 * Extract callback for the open ring action.
66 */
67 function commerce_pos_open_ring_extract(&$variables, $values) {
68 if (!empty($values['order']['order_id'])) {
69 $variables['order'] = commerce_order_load($values['order']['order_id']);
70 }
71 else {
72 $variables['order'] = commerce_pos_order_new();
73 }
74
75 $args = explode('*', $variables['input']);
76 $sku = array_pop($args);
77 $price_decimal = array_pop($args);
78 $quantity = array_pop($args);
79 if (empty($quantity)) {
80 $quantity = 1;
81 }
82
83 $variables += array('sku' => $sku, 'price_decimal' => $price_decimal, 'quantity' => $quantity);
84 }
85
86 /**
87 * Validator for product action.
88 */
89 function commerce_pos_open_ring_validate(&$variables) {
90 $errors = array();
91
92 //TODO: we might want a different product code field instead of sku for this
93 $product = commerce_product_load_by_sku($variables['sku']);
94 $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
95
96 //Determine the price.
97 $currency_code = commerce_pos_default_currency();
98 $amount = commerce_currency_decimal_to_amount($variables['price_decimal'], $currency_code, TRUE);
99 $product->commerce_price[LANGUAGE_NONE][0] = array(
100 'amount' => $amount,
101 'currency_code' => $currency_code,
102 );
103
104 $order = $variables['order'];
105
106 $line_item = commerce_product_line_item_new($product, $variables['quantity'], $order->order_id);
107
108 rules_invoke_event('commerce_pos_line_item_add', $order, $line_item);
109 if (!empty($order->error)) {
110 $errors[] = $order->error;
111 }
112
113 $variables['line_item'] = $line_item;
114 $variables['product'] = $product;
115 $variables['order'] = $order;
116
117 return $errors;
118 }
119
120 /**
121 * Implements hook_commerce_pos_search_info_alter().
122 */
123 function commerce_pos_open_ring_commerce_pos_search_info_alter(&$search_info) {
124 //Add CSS and JS
125 $path = drupal_get_path('module', 'commerce_pos_open_ring');
126 $search_info['product']['attached']['css'][] = array(
127 'data' => $path . '/theme/commerce_pos_open_ring.css',
128 );
129 $search_info['product']['attached']['js'][] = array(
130 'data' => $path . '/commerce_pos_open_ring.js',
131 );
132 }