You are here

function rooms_booking_views_ajax_autocomplete_unit in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Page callback for views rooms_unit autocomplete.

Parameters

$units_typed: The typed string of the user.

1 string reference to 'rooms_booking_views_ajax_autocomplete_unit'
rooms_booking_menu in modules/rooms_booking/rooms_booking.module
Implements hook_menu().

File

modules/rooms_booking/views/rooms_booking.views.inc, line 448
Providing extra functionality for the Room UI via views.

Code

function rooms_booking_views_ajax_autocomplete_unit($units_typed = '') {

  // The user enters a comma-separated list of units. We only autocomplete the last unit.
  $units_typed = drupal_explode_tags($units_typed);
  $unit_last = drupal_strtolower(array_pop($units_typed));
  $unit_matches = array();
  if ($unit_last != '') {
    $query = db_select('rooms_units', 't');

    // Do not select already entered units.
    if (!empty($units_typed)) {
      $query
        ->condition('t.name', $units_typed, 'NOT IN');
    }

    // Select rows that match by unit name.
    $units_return = $query
      ->fields('t', array(
      'unit_id',
      'name',
    ))
      ->condition('t.name', '%' . db_like($unit_last) . '%', 'LIKE')
      ->range(0, 10)
      ->execute()
      ->fetchAllKeyed();
    $prefix = count($units_typed) ? drupal_implode_tags($units_typed) . ', ' : '';
    foreach ($units_return as $unit_id => $name) {
      $unit = rooms_unit_load($unit_id);
      if (rooms_unit_access('update', $unit)) {
        $n = $name;

        // Unit names containing commas or quotes must be wrapped in quotes.
        if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
          $n = '"' . str_replace('"', '""', $name) . '"';
        }

        // Add unit name to list of matches.
        $unit_matches[$prefix . $n] = check_plain($name);
      }
    }
  }
  drupal_json_output($unit_matches);
}