You are here

function media_unsplash_search in Media Unsplash 7

Main function for interaction with unsplash API.

Parameters

string $search_term: String upon which we call unsplash api.

int $page: Request page of results.

Return value

string Return HTML.

2 calls to media_unsplash_search()
media_unsplash_ajax in ./media_unsplash.pages.inc
Return search results as html. Use in ajax fetching.
media_unsplash_external_ajax_callback in ./media_unsplash.module
AJAX callback function for media_unsplash_external().

File

./media_unsplash.module, line 256
Provides definition for unsplash media integration.

Code

function media_unsplash_search($search_term, $page) {

  // Define default items per page.
  $num_per_page = 30;

  // Unsplash Client ID.
  $unsplash_api = variable_get('media_unsplash_api', "");
  if (empty($unsplash_api)) {
    return t('Please insert Client ID key on module settings page');
  }
  $cid = 'media:unsplash:' . $search_term . ':' . $page;

  // If a cached entry exists, return it.
  if ($cached = cache_get($cid)) {
    $content = $cached->data;
  }
  else {

    // Generate API url for request.
    $context = array(
      'client_id' => $unsplash_api,
      'query' => $search_term,
      'per_page' => $num_per_page,
      'page' => $page,
    );
    $api_url = 'https://api.unsplash.com/search/photos?';
    $url = url($api_url, array(
      'query' => $context,
    ));

    // Make request.
    $request = drupal_http_request($url);

    // If response is valid.
    if ($request->code == 200) {

      // Get response data.
      $json_response = drupal_json_decode($request->data);

      // Total result count.
      $total = $json_response['total'];

      // Set default message if no images.
      $content = t('No pictures for current search');

      // Loop trough content.
      if (!empty($json_response['results'])) {

        // Define result as array.
        $images = array();
        foreach ($json_response['results'] as $key => $response_data) {
          $thumb = $response_data['urls']['thumb'];
          $id = $response_data['id'];
          $download_url = $response_data['urls']['full'];

          // Get author data.
          $user = $response_data['user']['name'];
          $user_link = $response_data['user']['links']['html'];

          // Output link to user. Unsplash require UTM links.
          $output_link = l($user, $user_link, array(
            'query' => array(
              'utm_source' => 'drupal_media_module',
              'utm_medium' => 'referral',
              'utm_campaign' => 'api-credit',
            ),
            'external' => TRUE,
            'attributes' => array(
              'target' => '_blank',
            ),
          ));
          $images[] = array(
            'download' => $download_url,
            'thumb' => $thumb,
            'link' => $output_link,
          );
        }
        $content = array(
          'images' => $images,
          'total' => $total,
        );

        // Set cache to three minutes, respect Unsplash api.
        cache_set($cid, $content, 'cache', time() + 180);
      }
    }
    else {

      // Print error message from Unsplash API.
      $content = $request->data;
    }
  }
  return $content;
}