Basic search API integration.
[sandbox/serialjaywalker/1195272.git] / modules / search / modules / product / commerce_pos_product_search.module
1 <?php
2
3 /**
4 * @file
5 * Add a product search to the POS interface.
6 */
7
8
9 /**
10 * Implements hook_commerce_pos_search_info().
11 */
12 function commerce_pos_product_search_commerce_pos_search_info() {
13 $search_info = array();
14
15 $search_info['product'] = array(
16 'title' => t('Products'),
17 'index' => 'commerce_pos_product_search',
18 'entity_type' => 'commerce_product',
19 'view_mode' => 'commerce_pos_product_search_result',
20 'per_page' => 18,
21 'attached' => array(
22 'js' => array(
23 array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/commerce_pos_product_search.js'),
24 ),
25 'css' => array(
26 array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/theme/commerce_pos_product_search.css'),
27 ),
28 ),
29 );
30
31 return $search_info;
32 }
33
34 /**
35 * Preprocess function for entity theme hook.
36 */
37 function commerce_pos_product_search_preprocess_entity(&$variables) {
38 $elements = &$variables['elements'];
39 if ($elements['#entity_type'] == 'commerce_product' && $elements['#view_mode'] == 'commerce_pos_product_search_result') {
40 //Get rid of title links.
41 $variables['url'] = '';
42 }
43 }
44
45 /**
46 * Implements hook_entity_info_alter().
47 */
48 function commerce_pos_product_search_entity_info_alter(&$entity_info) {
49 $entity_info['commerce_product']['view modes']['commerce_pos_product_search_result'] = array(
50 'label' => t('POS Search Result'),
51 'custom settings' => TRUE,
52 );
53 }
54
55 /**
56 * Implements hook_commerce_product_view().
57 */
58 function commerce_pos_product_search_commerce_product_view($product, $view_mode) {
59 if ($view_mode == 'commerce_pos_product_search_result') {
60 //We need to add a copy of the SKU here in case the view mode settings hide the default SKU.
61 $product->content['commerce_pos_product_search_result_sku'] = array(
62 '#prefix' => '<div class = \'commerce-pos-product-search-sku\'>',
63 '#markup' => check_plain($product->sku),
64 '#suffix' => '</div>',
65 //For the POS screen, CSS is added elsewhere, but we add the CSS here to hide this element
66 //if this view mode is used somewhere else for some reason.
67 '#attached' => array(
68 'css' => array(
69 array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/theme/commerce_pos_product_search.css')
70 ),
71 ),
72 );
73 unset($product->content['#url']);
74 $product->content['#cache'] = 'commerce_pos_product_search_result:' . $product->product_id;
75 }
76 }
77
78 /**
79 * Implements hook_default_search_api_index().
80 */
81 function commerce_pos_product_search_default_search_api_index() {
82 $defaults = array();
83
84 $field = field_info_field('commerce_price');
85 //Clear the field cache to be sure that the commerce_price field will index properly.
86 field_cache_clear();
87
88 $defaults['commerce_pos_product_search'] = entity_create('search_api_index', array(
89 'name' => t('Commerce POS Product Search'),
90 'machine_name' => 'commerce_pos_product_search',
91 'description' => t('The index for the product search in the POS interface.'),
92 //'server' => 'db_server',
93 'item_type' => 'commerce_product',
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 'sku' => array('type' => 'text'),
102 'type' => array('type' => 'string'),
103 'title' => array('type' => 'text'),
104 //'search_api_language' => array('type' => 'string'),
105 //'commerce_price:amount' => array('type' => 'decimal'),
106 //'commerce_price:currency_code' => array('type' => 'string'),
107 ),
108 'data_alter_callbacks' => array(
109 'commerce_pos_search_status_filter' => array(
110 'status' => 1,
111 'weight' => -10,
112 ),
113 ),
114 'processors' => array(
115 'search_api_case_ignore' => array(
116 'status' => 1,
117 'weight' => 0,
118 'settings' => array(
119 'fields' => array(
120 'sku' => TRUE,
121 'title' => TRUE,
122 ),
123 ),
124 ),
125 'search_api_html_filter' => array(
126 'status' => 1,
127 'weight' => 10,
128 'settings' => array(
129 'fields' => array(
130 'sku' => FALSE,
131 'title' => TRUE,
132 'alt' => 1,
133 'tags' => 'h1 = 5',//More here...
134 ),
135 ),
136 ),
137 ),
138 ),
139 ));
140 return $defaults;
141 }