You are here

function bynder_generate_search in Bynder 7

Generate search page.

1 call to bynder_generate_search()
bynder_add in includes/bynder.browser.inc
Create a form to add media from Bynder.

File

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

Code

function bynder_generate_search($state) {
  $meta_property_prefix = 'property_';
  $data = array(
    '#type' => 'fieldset',
  );
  $filter_labels = array(
    'brandId' => t('Brands'),
    'tags' => t('Tags'),
    'type' => t('Type'),
    'orientation' => t('Orientation'),
  );
  $meta_properties = BynderMediaApi::getBynderApi()
    ->getMetaProperties();
  if (!empty($meta_properties)) {
    foreach ($meta_properties as $meta_property) {
      $filter_labels[$meta_property_prefix . $meta_property['name']] = $meta_property['label'];
      foreach ($meta_property['options'] as $meta_property_option) {
        $filter_labels[$meta_property_option['name']] = $meta_property_option['label'];
      }
    }
  }
  $search = bynder_search_string($state);
  $filters = bynder_filters_json($state);
  if (isset($_POST['filters'])) {
    $filters = json_decode(filter_xss($_POST['filters']))->filters;
  }
  if (isset($_POST['source_filter'])) {
    if ($_POST['source_filter'] == 'bynder_remove_all_facets_filter_key') {
      $filters = array();
    }
  }
  $selected_facets = bynder_selected_facets($state);
  $results = bynder_search_results($search, $filters, $meta_properties, BYNDER_SEARCH_PAGE_SIZE);
  $available_facets = $results['count'];

  // Create search section
  $data['search'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search'),
    '#collapsible' => false,
    '#collapsed' => false,
    '#attributes' => array(
      'class' => array(
        'bynder-search-form',
      ),
    ),
  );
  $data['search']['field'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($search) ? $search : '',
    '#description' => "<p class='alert alert-warning' style='display:none'><span class='text'></span></p>",
  );
  $data['search']['button'] = array(
    '#type' => 'button',
    '#value' => t('Search'),
    '#submit' => array(
      'bynder_add_test',
    ),
  );
  $data['selected_facets'] = bynder_generate_selected_facets($filters);
  $data['facets'] = bynder_generate_facets($available_facets, $selected_facets);
  $data['filters'] = array(
    '#type' => 'hidden',
    '#default_value' => json_encode($filters) != '[]' ? json_encode($filters) : '{"filters": []}',
  );
  $data['results'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => array(
        'results_list',
      ),
      'class' => array(
        'grid',
      ),
    ),
  );

  // Create the results section
  $data['results'][] = bynder_generate_results($results);
  if (isset($results['totalNrOfResults'])) {
    $data['pager_container'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          '.js-form-type-entity-browser-pager',
        ),
      ),
    );
    $data['pager_container']['total_results'] = array(
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => array(
        'class' => 'total_results',
      ),
      '#value' => t('Total Results: ') . $results['totalNrOfResults'],
    );
    $data['pager_container']['pager'] = bynder_generate_pager($results['totalNrOfResults'], 0, array(
      'search' => $search,
    ));
  }
  return $data;
}