summaryrefslogtreecommitdiffstats
path: root/modules/search/modules/product/commerce_pos_product_search.module
blob: 308ed6a40ed096c64a998733d37a44399fe5efe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/**
 * @file
 * Add a product search to the POS interface.
 */


/**
 * Implements hook_commerce_pos_search_info().
 */
function commerce_pos_product_search_commerce_pos_search_info() {
  $search_info = array();

  $search_info['product'] = array(
    'title' => t('Products'),
    'index' => 'commerce_pos_product_search',
    'entity_type' => 'commerce_product',
    'view_mode' => 'commerce_pos_product_search_result',
    'per_page' => 18,
    'attached' => array(
      'js' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/commerce_pos_product_search.js'),
       ),
       'css' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/theme/commerce_pos_product_search.css'),
       ),
    ),
  );

  return $search_info;
}

/**
 * Preprocess function for entity theme hook.
 */
function commerce_pos_product_search_preprocess_entity(&$variables) {
  $elements = &$variables['elements'];
  if ($elements['#entity_type'] == 'commerce_product' && $elements['#view_mode'] == 'commerce_pos_product_search_result') {
    //Get rid of title links.
    $variables['url'] = '';
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function commerce_pos_product_search_entity_info_alter(&$entity_info) {
  $entity_info['commerce_product']['view modes']['commerce_pos_product_search_result'] = array(
    'label' => t('POS Search Result'),
    'custom settings' => TRUE,
  );
}

/**
 * Implements hook_commerce_product_view().
 */
function commerce_pos_product_search_commerce_product_view($product, $view_mode) {
  if ($view_mode == 'commerce_pos_product_search_result') {
    //We need to add a copy of the SKU here in case the view mode settings hide the default SKU.
    $product->content['commerce_pos_product_search_result_sku'] = array(
      '#prefix' => '<div class = \'commerce-pos-product-search-sku\'>',
      '#markup' => check_plain($product->sku),
      '#suffix' => '</div>',
      //For the POS screen, CSS is added elsewhere, but we add the CSS here to hide this element
      //if this view mode is used somewhere else for some reason.
      '#attached' => array(
          'css' => array(
              array('data' => drupal_get_path('module', 'commerce_pos_product_search') . '/theme/commerce_pos_product_search.css')
          ),
      ),
    );
    unset($product->content['#url']);
    $product->content['#cache'] = 'commerce_pos_product_search_result:' . $product->product_id;
  }
}

/**
 * Implements hook_default_search_api_index().
 */
function commerce_pos_product_search_default_search_api_index() {
  $defaults = array();

  $field = field_info_field('commerce_price');
  //Clear the field cache to be sure that the commerce_price field will index properly.
  field_cache_clear();

  $defaults['commerce_pos_product_search'] = entity_create('search_api_index', array(
    'name' => t('Commerce POS Product Search'),
    'machine_name' => 'commerce_pos_product_search',
    'description' => t('The index for the product search in the POS interface.'),
    //'server' => 'db_server',
    'item_type' => 'commerce_product',
    'enabled' => 1,
    'read_only' => 0,
    'rdf_mapping' => NULL,
    'options' => array(
      'index_directly' => 0,
      'cron_limit' => 50,
      'fields' => array(
        'sku' => array('type' => 'text'),
        'type' => array('type' => 'string'),
        'title' => array('type' => 'text'),
        //'search_api_language' => array('type' => 'string'),
        //'commerce_price:amount' => array('type' => 'decimal'),
        //'commerce_price:currency_code' => array('type' => 'string'),
      ),
      'data_alter_callbacks' => array(
        'commerce_pos_search_status_filter' => array(
          'status' => 1,
          'weight' => -10,
        ),
      ),
      'processors' => array(
        'search_api_case_ignore' => array(
          'status' => 1,
          'weight' => 0,
          'settings' => array(
            'fields' => array(
              'sku' => TRUE,
              'title' => TRUE,
            ),
          ),
        ),
        'search_api_html_filter' => array(
          'status' => 1,
          'weight' => 10,
          'settings' => array(
            'fields' => array(
              'sku' => FALSE,
              'title' => TRUE,
              'alt' => 1,
              'tags' => 'h1 = 5',//More here...
            ),
          ),
        ),
      ),
    ),
  ));
  return $defaults;
}