You are here

function fontsquirrel_fontyourface_import in @font-your-face 7.2

Same name and namespace in other branches
  1. 6.2 modules/fontsquirrel/fontsquirrel.module \fontsquirrel_fontyourface_import()
  2. 7 modules/fontsquirrel/fontsquirrel.module \fontsquirrel_fontyourface_import()

Implements hook_fontyourface_import().

File

modules/fontsquirrel/fontsquirrel.module, line 230

Code

function fontsquirrel_fontyourface_import() {
  $families = array();
  $api_result = drupal_http_request('http://www.fontsquirrel.com/api/fontlist/all');
  if ($api_result->code == '200') {
    $decoded = json_decode($api_result->data);
    foreach ($decoded as $font_import) {

      // If there are multiple variants, add to batch processing array.
      if ($font_import->family_count > 1) {
        $families[] = $font_import->family_urlname;
      }
      else {
        $metadata = array(
          'id' => $font_import->id,
          'path' => $font_import->family_urlname,
          'font_filename' => $font_import->font_filename,
        );
        $font = new StdClass();
        $font->name = $font_import->family_name;
        $font->url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname;
        $font->provider = 'fontsquirrel';
        $font->foundry = $font_import->foundry_name;
        $font->foundry_url = 'http://www.fontsquirrel.com/foundry/' . $font_import->foundry_urlname;
        $font->license = 'See Font Squirrel license page';
        $font->license_url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname . '#eula';
        $font->tags = array(
          $font_import->classification,
        );
        $font->metadata = serialize($metadata);
        fontyourface_save_font($font);

        // Add a tag if this is a monocase font.
        if ($font_import->is_monocase == 'Y') {
          fontyourface_add_tags_to_font(array(
            'monocase',
          ), $font->fid);
        }
      }
    }

    // foreach
    if (count($families) > 0) {
      $batch = array(
        'operations' => array(
          array(
            'fontsquirrel_batch_import',
            array(
              $families,
            ),
          ),
        ),
        'title' => t('@count font families contain multiple variants', array(
          '@count' => count($families),
        )),
        'progress_message' => t('Importing variants...'),
      );
      batch_set($batch);
    }
  }
  else {
    drupal_set_message(t('There was an error downloading font list from Font Squirrel.'), 'error');
    fontyourface_log('Invalid drupal_http_request response: @response', array(
      '@response' => print_r($response, TRUE),
    ));
  }

  // else
}