summaryrefslogtreecommitdiffstats
path: root/modules/search/commerce_pos_search.module
blob: fe2b332995bb434b000f2f910fa4ec419c4165f8 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php

/**
 * @file
 * Common functions for searches embedded in the POS interface.
 */

/**
 * Implements hook_commerce_pos_panel_info().
 */
function commerce_pos_search_commerce_pos_panel_info() {
  $search_info = commerce_pos_search_info();

  foreach ($search_info as $name => $search) {
    $panels['search_' . $name] = array(
      'name' => 'search_' . $name,
      'title' => $search['title'],
      'content_callback' => 'commerce_pos_search_panel_content',
      'content_arguments' => array($name),
      'weight' => $search['weight'],
    );
  }

  return $panels;
}

/**
 * Get all POS searches defined by modules.
 */
function commerce_pos_search_info($name = NULL) {
  $search_info = drupal_static(__FUNCTION__);

  if (empty($search_info)) {
    $search_info = module_invoke_all('commerce_pos_search_info');

    foreach ($search_info as $key => $search) {
      $search_info[$key] = $search + array(
        'weight' => 0,
        'sort' => array(
          'search_api_relevance' => 'DESC',
        ),
      );
    }
    drupal_alter('commerce_pos_search_info', $search_info);
  }

  if (empty($name)) {
    return $search_info;
  }
  else {
    return $search_info[$name];
  }
}

/**
 * Build the search panel content
 */
function commerce_pos_search_panel_content($name) {
  $search_info = commerce_pos_search_info($name);

  commerce_pos_search_refresh_results($name);
  $render = array(
    'form' => drupal_get_form('commerce_pos_search_form', $name),
    'facets' => array(
      '#prefix' => "<div class='commerce-pos-search-facets'>",
      '#suffix' => "</div>",
    ),
    'results' => array(
        '#prefix' => "<div class='commerce-pos-search-results'>",
        '#suffix' => "</div>",
    ),
    '#attributes' => array(
      'class' => array(
        'commerce-pos-search-panel-' . commerce_pos_search_name_to_class($name),
        'commerce-pos-search-panel',
       ),
    ),
    '#name' => 'commerce-pos-search-' . $name,
  );
  $render['#attached'] = array(
    'js' => array(array('data' => drupal_get_path('module', 'commerce_pos_search') . '/commerce_pos_search.js')),
    'css' => array(array('data' => drupal_get_path('module', 'commerce_pos_search') . '/theme/commerce_pos_search.css')),
  );
  if (!empty($search_info['attached'])) {
    $render['#attached'] = drupal_array_merge_deep($render['#attached'], $search_info['attached']);
  }

  return $render;
}

/**
 * Returns a path argument from a product type.
 */
function commerce_pos_search_name_to_class($name) {
  return strtr($name, '_', '-');
}

/**
 * Refresh the search results for a POS search.
 */
function commerce_pos_search_refresh_results($search_name) {
  //Would this be better done with #attached?
  drupal_add_js(drupal_get_path('module', 'commerce_pos_search') . '/commerce_pos_search.js');
  drupal_add_js(array('commercePosSearch' => array('refresh' => array($search_name => TRUE))), 'setting');
}

/**
 * Form callback for search_form.
 */
function commerce_pos_search_form($form_state, $form_state, $search_name) {
  $form['query'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#attributes' => array(
      'class' => array('commerce-pos-search-form-query'),
    ),
    '#suffix' => "<a class = 'commerce-pos-search-query-clear'>" . t('Clear') . "</a>",
  );
  
  $form['search_name'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array('commerce-pos-search-form-search-name'),
    ),
    '#value' => $search_name
  );

  return $form;
}

/**
 * Form callback for facet form.
 */
function commerce_pos_search_facets_form($form_state, $form_state, $search_name) {
  $form = facetapi_build_realm(commerce_pos_search_facet_api_searcher($search_name), 'commerce_pos_search');
  $form['#facet_names'] = element_children($form);

  return $form;
}

/**
 * The name of a searcher as it is known to Facet API module.
 * 	@param $search_name
 */
function commerce_pos_search_facet_api_searcher($search_name) {
  $search_info = commerce_pos_search_info($search_name);
  return 'search_api@' . $search_info['index'];
}

/**
 * Get facets for a search.
 */
function commerce_pos_search_facets($search_name) {
  return drupal_get_form('commerce_pos_search_facets_form', $search_name);
}

/**
 * Implements hook_menu().
 */
function commerce_pos_search_menu() {
  $items = array();
  $search_info = commerce_pos_search_info();

  foreach ($search_info as $name => $search) {
    $items['commerce_pos_search/' . $name] = array(
      'title' => 'POS Search',
      'type' => MENU_CALLBACK,
      'description' => 'AJAX callback for POS search',
      'access callback' => TRUE,
      'page callback' => 'commerce_pos_search_results_ajax',
      'page arguments' => array(1),
    );
  }

  return $items;
}

/**
 * Callback for search results.
 */
function commerce_pos_search_results($search_name, $keys) {
  $search = commerce_pos_search_info($search_name);
  if (!$search) {
    return MENU_NOT_FOUND;
  }
  else{
    $index = search_api_index_load($search['index']);
    if (!$index->enabled) {
      return array(
        'error' => t('Please make sure that a search server has been created and the index has been enabled.'),
      );
    }
  }
  
  if ($keys) {
    $results = commerce_pos_search_execute($search_name, $keys);

    $render['results']['#theme'] = 'commerce_pos_search_results';
    $render['results']['#view_mode'] = $search['view_mode'];
    $render['results']['#index'] = search_api_index_load($search['index']);
    $render['results']['#keys'] = $keys;
    $render['results']['#search_name'] = $search_name;
    $render['results']['#results'] = $results;

    if ($results['result count'] > $search['per_page']) {
      pager_default_initialize($results['result count'], $search['per_page']);
      $render['pager']['#theme'] = 'pager';
      $render['pager']['#quantity'] = 5;
    }

    return $render;
  }
}

/**
 * AJAX callback for search results.
 */
function commerce_pos_search_results_ajax($search_name, $keys='', $get_facets = 1) {
  if (empty($keys)) {
    $keys = ' ';
  }
  $results = commerce_pos_search_results($search_name, $keys);
  if (!empty($results['error'])) {
    $return = array(
      'results' => $results['error'],
    );
  }
  else {
    $return = array(
      'results' => drupal_render($results),
    );
    if (!empty($get_facets)) {
      $facets = commerce_pos_search_facets($search_name);
      $return += array(
        'facets' => drupal_render($facets),
        'facetNames' => $facets['#facet_names'],
      );
    }
  }

  return drupal_json_output($return);
}

/**
 * Get search results.
 */
function commerce_pos_search_execute($search_name, $keys) {
  $search = commerce_pos_search_info($search_name);

  $limit = $search['per_page'];
  $offset = pager_find_page() * $limit;
  $options = array(
    'search id' => 'commerce_pos_search:' . $search_name,
  );

  $query = search_api_query($search['index'], $options)
    ->keys($keys)
    ->range($offset, $limit);
    
  foreach($search['sort'] as $field => $direction) {
    $query->sort($field, $direction);
  }

  return $query->execute();
}

/**
 * Implements hook_theme().
 */
function commerce_pos_search_theme() {
  $themes['commerce_pos_search_results'] = array(
    'variables' => array(
      'index' => NULL,
      'results' => array('result count' => 0),
      'items' => array(),
      'view_mode' => '',
      'keys' => '',
      'search_name' => '',
    ),
  );
  $themes['commerce_pos_search_result'] = array(
    'variables' => array(
      'entity_type' => '',
      'entity_id' => 0,
      'view_mode' => ''
    ),
  );

  return $themes;
}

/**
 * Function for preprocessing the variables for the search_api_page_results
 * theme.
 *
 * @param array $variables
 *   An associative array containing:
 *   - index: The index this search was executed on.
 *   - results: An array of search results, as returned by
 *     SearchApiQueryInterface::execute().
 *   - keys: The keywords of the executed search.
 */
function template_preprocess_commerce_pos_search_results(&$variables) {

  $index = $variables['index'];
  $results = $variables['results'];
  $items = $variables['items'];
  $view_mode = $variables['view_mode'];

  $variables['results']['render'] = array();

  foreach ($variables['results']['results'] as $entity_id => $item) {
    $variables['results']['render'][] = array(
      '#theme' => 'commerce_pos_search_result',
      '#cache' => 'commerce_pos_search_result:' . $variables['search_name'] . ':' . $entity_id,
      '#entity_type' => $index->item_type,
      '#entity_id' => $entity_id,
      '#view_mode' => $view_mode,
    );
  }
}

/**
 * Theme function for displaying search results.
 *
 * @param array $variables
 *   An associative array containing:
 *   - index: The index this search was executed on.
 *   - results: An array of search results, as returned by
 *     SearchApiQueryInterface::execute().
 *   - items: The loaded items for all results, in an array keyed by ID.
 *   - view_mode: The view mode to use for displaying the individual results,
 *     or the special mode "search_api_page_result" to use the theme function
 *     of the same name.
 *   - keys: The keywords of the executed search.
 */
function theme_commerce_pos_search_results(array $variables) {
  drupal_add_css(drupal_get_path('module', 'commerce_pos_search') . '/theme/commerce_pos_search.css');

  $index = $variables['index'];
  $results = $variables['results'];
  $items = $variables['items'];
  $keys = $variables['keys'];
  $class = 'commerce-pos-search-results-container-' . commerce_pos_search_name_to_class($variables['search_name']);

  $render = array(
    '#prefix' => '<div class="commerce-pos-search-results-container ' . $class . '">',
    'content' => $results['render'],
    '#suffix' => '</div>',
  );

  if (!$results['result count']) {
    $render['results'] = array(
      '#markup' => t('No results.'),
    );
  }

  return drupal_render($render);
}

/**
 * Theme function for displaying a single result.
 *
 * @param array $variables
 *   An associative array with the following keys:
 *   --entity_type
 *   --entity_id
 *   --view_mode
 */
function theme_commerce_pos_search_result(array $variables) {
  $entity_type = $variables['entity_type'];
  $entity_id = $variables['entity_id'];
  $view_mode = $variables['view_mode'];
  $render = entity_view($entity_type, entity_load($entity_type, array($entity_id)), $view_mode);
  return drupal_render($render);
}

/**
 * Implements hook_search_api_alter_callback_info().
 */
function commerce_pos_search_search_api_alter_callback_info() {
  $callbacks['commerce_pos_search_status_filter'] = array(
    'name' => t('Status filter'),
    'description' => t("Exclude inactive Drupal Commerce entities."),
    'class' => 'CommercePosSearchStatusFilter',
    // Filters should be executed first.
    'weight' => -10,
  );
  return $callbacks;
}

/**
 * Implements hook_entity_update().
 */
function commerce_pos_search_entity_update($entity, $type) {
  $search_info = commerce_pos_search_info();
  $entity_info = entity_get_info();
  foreach($search_info as $name => $search) {
    if ($search['entity_type'] == $type) {
      $key = $entity_info[$type]['entity keys']['id'];
      cache_clear_all('commerce_pos_search_result:' . $name . ':' . $key, 'cache');
    }
  }
}