summaryrefslogtreecommitdiffstats
path: root/modules/search/modules/customer/commerce_pos_customer_search.module
blob: d9f9cf955adbad9f134de0dc7888f84e117458d2 (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
<?php

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


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

  $search_info['customer'] = array(
    'title' => t('Customers'),
    'index' => 'commerce_pos_customer_search',
    'entity_type' => 'commerce_customer_profile',
    'view_mode' => 'commerce_pos_customer_search_result',
    'per_page' => 18,
    'attached' => array(
      'js' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_customer_search') . '/commerce_pos_customer_search.js'),
       ),
       'css' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_customer_search') . '/theme/commerce_pos_customer_search.css'),
       ),
    ),
    'weight' => 10,
  );

  return $search_info;
}

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

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

/**
 * Implements hook_commerce_customer_profile_view().
 */
function commerce_pos_customer_search_commerce_customer_profile_view($customer_profile, $view_mode) {
  if ($view_mode == 'commerce_pos_customer_search_result') {
    //We need to add a copy of the customer number here in case the view mode settings hide the default customer number.
    $wrapper = entity_metadata_wrapper('commerce_customer_profile', $customer_profile);
    $customer_profile->content['commerce_pos_customer_search_result_customer_number'] = array(
      '#prefix' => '<div class = \'commerce-pos-customer-search-customer-number\'>',
      '#markup' => check_plain($wrapper->commerce_pos_customer_number->value()),
      '#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_customer_search') . '/theme/commerce_pos_customer_search.css')
          ),
      ),
    );
    unset($customer_profile->content['#url']);
    $customer_profile->content['#cache'] = 'commerce_pos_customer_search_result:' . $customer_profile->profile_id;
  }
}

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

  $defaults['commerce_pos_customer_search'] = entity_create('search_api_index', array(
    'name' => t('Commerce POS Customer Search'),
    'machine_name' => 'commerce_pos_customer_search',
    'description' => t('The index for the customer search in the POS interface.'),
    //'server' => 'db_server',
    'item_type' => 'commerce_customer_profile',
    'enabled' => 1,
    'read_only' => 0,
    'rdf_mapping' => NULL,
    'options' => array(
      'index_directly' => 0,
      'cron_limit' => 50,
      'fields' => array(
        'profile_id' => array('type' => 'integer'),
        'type' => array('type' => 'string'),
      ),
      'data_alter_callbacks' => array(
        'commerce_pos_search_status_filter' => array(
          'status' => 1,
          'weight' => -10,
        ),
        'search_api_alter_bundle_filter' => array(
          'status' => 1,
          'weight' => -10,
          'settings' => array(
            'default' => 0,
            'bundles' => array('pos'),
          ),
        ),        
      ),
    ),
  ));
  return $defaults;
}