You are here

function bynder_search_results in Bynder 7

Perform the search.

Parameters

unknown $search: the query entered.

unknown $selected_facets: the selected facets.

Return value

mixed the search results.

1 call to bynder_search_results()
bynder_generate_search in includes/bynder.browser.inc
Generate search page.

File

includes/bynder.browser.inc, line 657
Manages the Bynder media browser functionality

Code

function bynder_search_results($search, $selected_facets, $meta_properties, $nr_of_results = 5) {
  $start_index = 0;
  $query = array();
  if (isset($_GET['page'])) {
    $current_page = (int) filter_xss($_GET['page']);
    $current_page += 1;
    $start_index = $current_page * $nr_of_results;
  }
  $selected_facets_meta_properties = [];
  foreach ($meta_properties as $meta_property) {
    foreach ($meta_property['options'] as $meta_property_option) {
      $selected_facets_meta_properties[$meta_property['name']][$meta_property_option['name']] = $meta_property_option['id'];
    }
  }
  if (isset($selected_facets)) {
    foreach ($selected_facets as $facet) {
      $facets = $facet->value;
      $facet_category = $facet->key;
      if (isset($selected_facets_meta_properties[$facet_category])) {
        $meta_facets = [];
        foreach (explode(',', $facets[0]) as $facet) {
          $meta_facets[] = $selected_facets_meta_properties[$facet_category][$facet];
        }
        $query['propertyOptionId'] = implode(',', $meta_facets);
      }
      else {
        $query[$facet_category] = $facets;
      }
    }
  }
  if (isset($search) && strlen($search) > 0) {
    $query['keyword'] = urlencode($search);
  }
  $page = 1;
  if (isset($start_index) && $start_index > 0) {
    $page = $start_index / $nr_of_results;
  }
  $query = $query + array(
    'page' => $page,
    'limit' => $nr_of_results,
    'count' => 1,
  );
  $query['type'] = 'image';
  $result = BynderMediaApi::getBynderApi()
    ->getMediaList($query);
  if ($result) {
    if (isset($result['count']['total'])) {
      $result['totalNrOfResults'] = $result['count']['total'];
    }
  }
  else {
    drupal_set_message(t('Could not retrieve search results:') . filter_xss($result->error), 'error');
    return false;
  }
  return $result;
}