Basic search API integration.
[sandbox/serialjaywalker/1195272.git] / modules / search / modules / customer / commerce_pos_customer_search.module
1 <?php
2
3 /**
4 * @file
5 * Add a customer search to the POS interface.
6 */
7
8
9 /**
10 * Implements hook_commerce_pos_search_info().
11 */
12 function commerce_pos_customer_search_commerce_pos_search_info() {
13 $search_info = array();
14
15 $search_info['customer'] = array(
16 'title' => t('Customers'),
17 'index' => 'commerce_pos_customer_search',
18 'entity_type' => 'commerce_customer_profile',
19 'view_mode' => 'commerce_pos_customer_search_result',
20 'per_page' => 18,
21 'attached' => array(
22 'js' => array(
23 array('data' => drupal_get_path('module', 'commerce_pos_customer_search') . '/commerce_pos_customer_search.js'),
24 ),
25 'css' => array(
26 array('data' => drupal_get_path('module', 'commerce_pos_customer_search') . '/theme/commerce_pos_customer_search.css'),
27 ),
28 ),
29 'weight' => 10,
30 );
31
32 return $search_info;
33 }
34
35 /**
36 * Preprocess function for entity theme hook.
37 */
38 function commerce_pos_customer_search_preprocess_entity(&$variables) {
39 $elements = &$variables['elements'];
40 if ($elements['#entity_type'] == 'commerce_customer_profile' && $elements['#view_mode'] == 'commerce_pos_customer_search_result') {
41 //Get rid of title links.
42 $variables['url'] = '';
43 }
44 }
45
46 /**
47 * Implements hook_entity_info_alter().
48 */
49 function commerce_pos_customer_search_entity_info_alter(&$entity_info) {
50 $entity_info['commerce_customer_profile']['view modes']['commerce_pos_customer_search_result'] = array(
51 'label' => t('POS Search Result'),
52 'custom settings' => TRUE,
53 );
54 }
55
56 /**
57 * Implements hook_commerce_customer_profile_view().
58 */
59 function commerce_pos_customer_search_commerce_customer_profile_view($customer_profile, $view_mode) {
60 if ($view_mode == 'commerce_pos_customer_search_result') {
61 //We need to add a copy of the customer number here in case the view mode settings hide the default customer number.
62 $wrapper = entity_metadata_wrapper('commerce_customer_profile', $customer_profile);
63 $customer_profile->content['commerce_pos_customer_search_result_customer_number'] = array(
64 '#prefix' => '<div class = \'commerce-pos-customer-search-customer-number\'>',
65 '#markup' => check_plain($wrapper->commerce_pos_customer_number->value()),
66 '#suffix' => '</div>',
67 //For the POS screen, CSS is added elsewhere, but we add the CSS here to hide this element
68 //if this view mode is used somewhere else for some reason.
69 '#attached' => array(
70 'css' => array(
71 array('data' => drupal_get_path('module', 'commerce_pos_customer_search') . '/theme/commerce_pos_customer_search.css')
72 ),
73 ),
74 );
75 unset($customer_profile->content['#url']);
76 $customer_profile->content['#cache'] = 'commerce_pos_customer_search_result:' . $customer_profile->profile_id;
77 }
78 }
79
80 /**
81 * Implements hook_default_search_api_index().
82 */
83 function commerce_pos_customer_search_default_search_api_index() {
84 $defaults = array();
85
86 $defaults['commerce_pos_customer_search'] = entity_create('search_api_index', array(
87 'name' => t('Commerce POS Customer Search'),
88 'machine_name' => 'commerce_pos_customer_search',
89 'description' => t('The index for the customer search in the POS interface.'),
90 //'server' => 'db_server',
91 'item_type' => 'commerce_customer_profile',
92 'enabled' => 1,
93 'read_only' => 0,
94 'rdf_mapping' => NULL,
95 'options' => array(
96 'index_directly' => 0,
97 'cron_limit' => 50,
98 'fields' => array(
99 'profile_id' => array('type' => 'integer'),
100 'type' => array('type' => 'string'),
101 ),
102 'data_alter_callbacks' => array(
103 'commerce_pos_search_status_filter' => array(
104 'status' => 1,
105 'weight' => -10,
106 ),
107 'search_api_alter_bundle_filter' => array(
108 'status' => 1,
109 'weight' => -10,
110 'settings' => array(
111 'default' => 0,
112 'bundles' => array('pos'),
113 ),
114 ),
115 ),
116 ),
117 ));
118 return $defaults;
119 }