summaryrefslogtreecommitdiffstats
path: root/modules/search/modules/order/commerce_pos_order_search.module
blob: 1c16b63dfd674c49162b2d6051cb26b3fb96b696 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

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


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

  $search_info['order'] = array(
    'title' => t('Orders'),
    'index' => 'commerce_pos_order_search',
    'entity_type' => 'commerce_order',
    'view_mode' => 'commerce_pos_order_search_result',
    'per_page' => 18,
    'attached' => array(
      'js' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_order_search') . '/commerce_pos_order_search.js'),
       ),
       'css' => array(
         array('data' => drupal_get_path('module', 'commerce_pos_order_search') . '/theme/commerce_pos_order_search.css'),
       ),
    ),
    'weight' => 5,
    'sort' => array(
      'search_api_relevance' => 'DESC',
      'order_id' => 'DESC',
    ),
  );

  return $search_info;
}

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

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

/**
 * Implements hook_commerce_order_view().
 */
function commerce_pos_order_search_commerce_order_view($order, $view_mode) {
  if ($view_mode == 'commerce_pos_order_search_result') {
    //We need to add a copy of the order number here in case the view mode settings hide the default order number.
    $order->content['commerce_pos_order_search_result_order_number'] = array(
      '#prefix' => '<div class = \'commerce-pos-order-search-order-number\'>',
      '#markup' => check_plain($order->order_number),
      '#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_order_search') . '/theme/commerce_pos_order_search.css')
          ),
      ),
    );
    unset($order->content['#url']);
    $order->content['#cache'] = 'commerce_pos_order_search_result:' . $order->order_id;
  }
}

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

  $defaults['commerce_pos_order_search'] = entity_create('search_api_index', array(
    'name' => t('Commerce POS Order Search'),
    'machine_name' => 'commerce_pos_order_search',
    'description' => t('The index for the order search in the POS interface.'),
    'item_type' => 'commerce_order',
    'enabled' => 1,
    'read_only' => 0,
    'rdf_mapping' => NULL,
    'options' => array(
      'index_directly' => 0,
      'cron_limit' => 50,
      'fields' => array(
        'order_id' => array('type' => 'integer'),
        'order_number' => array('type' => 'fulltext'),
        'status' => array('type' => 'string'),
        'type' => array('type' => 'string'),
        //'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_order_search_pos_filter' => array(
          'status' => 1,
          'weight' => -10,
        ),
      ),
    ),
  ));
  
  return $defaults;
}

/**
 * Implements hook_search_api_alter_callback_info().
 */
function commerce_pos_order_search_search_api_alter_callback_info() {
  $callbacks['commerce_pos_order_search_pos_filter'] = array(
    'name' => t('POS filter'),
    'description' => t("Exclude non-POS orders."),
    'class' => 'CommercePosOrderSearchPosFilter',
    // Filters should be executed first.
    'weight' => -10,
  );
  return $callbacks;
}


/**
 * Implements hook_facetapi_facet_info().
 */
function commerce_pos_order_search_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
  if (isset($searcher_info['types']['commerce_order'])) {
    $facet_info['status']['map callback'] = 'commerce_pos_order_search_map_status';
  }  
}

/**
 * Map callback for order statuses.
 */
function commerce_pos_order_search_map_status(array $values) {
  $map = array();
  $statuses = commerce_order_statuses();
  foreach ($statuses as $status => $info) {
    $map[$status] = $info['title'];
  }
  return $map;
}

/**
 * Implements hook_commerce_pos_action_info_alter().
 */
function commerce_pos_order_search_commerce_pos_action_info_alter(&$action_info) {
  $action_info['commerce_pos_clear']['callbacks']['execute'][] = 'commerce_pos_order_clear_execute';
}

/**
 * Execute callback for clearing. Refreshes order list.
 */
function commerce_pos_order_clear_execute(&$variables) {
 commerce_pos_search_refresh_results('order');
}