Basic search API integration.
[sandbox/serialjaywalker/1195272.git] / modules / search / modules / order / commerce_pos_order_search.module
1 <?php
2
3 /**
4 * @file
5 * Add a order search to the POS interface.
6 */
7
8
9 /**
10 * Implements hook_commerce_pos_search_info().
11 */
12 function commerce_pos_order_search_commerce_pos_search_info() {
13 $search_info = array();
14
15 $search_info['order'] = array(
16 'title' => t('Orders'),
17 'index' => 'commerce_pos_order_search',
18 'entity_type' => 'commerce_order',
19 'view_mode' => 'commerce_pos_order_search_result',
20 'per_page' => 18,
21 'attached' => array(
22 'js' => array(
23 array('data' => drupal_get_path('module', 'commerce_pos_order_search') . '/commerce_pos_order_search.js'),
24 ),
25 'css' => array(
26 array('data' => drupal_get_path('module', 'commerce_pos_order_search') . '/theme/commerce_pos_order_search.css'),
27 ),
28 ),
29 'weight' => 5,
30 'sort' => array(
31 'search_api_relevance' => 'DESC',
32 'order_id' => 'DESC',
33 ),
34 );
35
36 return $search_info;
37 }
38
39 /**
40 * Preprocess function for entity theme hook.
41 */
42 function commerce_pos_order_search_preprocess_entity(&$variables) {
43 $elements = &$variables['elements'];
44 if ($elements['#entity_type'] == 'commerce_order' && $elements['#view_mode'] == 'commerce_pos_order_search_result') {
45 //Get rid of title links.
46 $variables['url'] = '';
47 }
48 }
49
50 /**
51 * Implements hook_entity_info_alter().
52 */
53 function commerce_pos_order_search_entity_info_alter(&$entity_info) {
54 $entity_info['commerce_order']['view modes']['commerce_pos_order_search_result'] = array(
55 'label' => t('POS Search Result'),
56 'custom settings' => TRUE,
57 );
58 }
59
60 /**
61 * Implements hook_commerce_order_view().
62 */
63 function commerce_pos_order_search_commerce_order_view($order, $view_mode) {
64 if ($view_mode == 'commerce_pos_order_search_result') {
65 //We need to add a copy of the order number here in case the view mode settings hide the default order number.
66 $order->content['commerce_pos_order_search_result_order_number'] = array(
67 '#prefix' => '<div class = \'commerce-pos-order-search-order-number\'>',
68 '#markup' => check_plain($order->order_number),
69 '#suffix' => '</div>',
70 //For the POS screen, CSS is added elsewhere, but we add the CSS here to hide this element
71 //if this view mode is used somewhere else for some reason.
72 '#attached' => array(
73 'css' => array(
74 array('data' => drupal_get_path('module', 'commerce_pos_order_search') . '/theme/commerce_pos_order_search.css')
75 ),
76 ),
77 );
78 unset($order->content['#url']);
79 $order->content['#cache'] = 'commerce_pos_order_search_result:' . $order->order_id;
80 }
81 }
82
83 /**
84 * Implements hook_default_search_api_index().
85 */
86 function commerce_pos_order_search_default_search_api_index() {
87 $defaults = array();
88
89 $defaults['commerce_pos_order_search'] = entity_create('search_api_index', array(
90 'name' => t('Commerce POS Order Search'),
91 'machine_name' => 'commerce_pos_order_search',
92 'description' => t('The index for the order search in the POS interface.'),
93 'item_type' => 'commerce_order',
94 'enabled' => 1,
95 'read_only' => 0,
96 'rdf_mapping' => NULL,
97 'options' => array(
98 'index_directly' => 0,
99 'cron_limit' => 50,
100 'fields' => array(
101 'order_id' => array('type' => 'integer'),
102 'order_number' => array('type' => 'fulltext'),
103 'status' => array('type' => 'string'),
104 'type' => array('type' => 'string'),
105 //'search_api_language' => array('type' => 'string'),
106 //'commerce_price:amount' => array('type' => 'decimal'),
107 //'commerce_price:currency_code' => array('type' => 'string'),
108 ),
109 'data_alter_callbacks' => array(
110 'commerce_pos_order_search_pos_filter' => array(
111 'status' => 1,
112 'weight' => -10,
113 ),
114 ),
115 ),
116 ));
117
118 return $defaults;
119 }
120
121 /**
122 * Implements hook_search_api_alter_callback_info().
123 */
124 function commerce_pos_order_search_search_api_alter_callback_info() {
125 $callbacks['commerce_pos_order_search_pos_filter'] = array(
126 'name' => t('POS filter'),
127 'description' => t("Exclude non-POS orders."),
128 'class' => 'CommercePosOrderSearchPosFilter',
129 // Filters should be executed first.
130 'weight' => -10,
131 );
132 return $callbacks;
133 }
134
135
136 /**
137 * Implements hook_facetapi_facet_info().
138 */
139 function commerce_pos_order_search_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
140 if (isset($searcher_info['types']['commerce_order'])) {
141 $facet_info['status']['map callback'] = 'commerce_pos_order_search_map_status';
142 }
143 }
144
145 /**
146 * Map callback for order statuses.
147 */
148 function commerce_pos_order_search_map_status(array $values) {
149 $map = array();
150 $statuses = commerce_order_statuses();
151 foreach ($statuses as $status => $info) {
152 $map[$status] = $info['title'];
153 }
154 return $map;
155 }
156
157 /**
158 * Implements hook_commerce_pos_action_info_alter().
159 */
160 function commerce_pos_order_search_commerce_pos_action_info_alter(&$action_info) {
161 $action_info['commerce_pos_clear']['callbacks']['execute'][] = 'commerce_pos_order_clear_execute';
162 }
163
164 /**
165 * Execute callback for clearing. Refreshes order list.
166 */
167 function commerce_pos_order_clear_execute(&$variables) {
168 commerce_pos_search_refresh_results('order');
169 }
170