You are here

function fontsquirrel_fontyourface_enable in @font-your-face 7.2

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

Implements hook_fontyourface_enable().

File

modules/fontsquirrel/fontsquirrel.module, line 99

Code

function fontsquirrel_fontyourface_enable($font) {
  $success = TRUE;
  $metadata = unserialize($font->metadata);
  if ($font->css_family == '') {
    $api_result = drupal_http_request('http://www.fontsquirrel.com/api/familyinfo/' . $metadata['path']);
    if ($api_result->code == '200') {
      $decoded = json_decode($api_result->data);
      if (is_array($decoded) && is_object($decoded[0])) {
        $font->css_family = $decoded[0]->{'fontface_name'};
        fontyourface_save_font($font);
      }
      else {
        drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. Font not found in API.', array(
          '%fontname' => $font->name,
        )), 'error');
        $success = FALSE;
      }

      // else
    }
    else {
      drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. API not responding.', array(
        '%fontname' => $font - name,
      )), 'error');
      $success = FALSE;
    }

    // else
  }

  // if
  $directory_location = file_build_uri('fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit');
  if (!file_prepare_directory($directory_location)) {
    $zip_location = file_build_uri('fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit.zip');

    // Download file .zip file
    if (!file_exists(drupal_realpath($zip_location))) {
      $kit_url = 'http://www.fontsquirrel.com/fontfacekit/' . $metadata['path'];
      $kit_result = drupal_http_request($kit_url);
      if ($kit_result->code == 200) {

        // Save the .zip file
        $zip_location_directory = dirname($zip_location);
        if (file_prepare_directory($zip_location_directory, FILE_CREATE_DIRECTORY)) {
          file_unmanaged_save_data($kit_result->data, $zip_location, FILE_EXISTS_REPLACE);
        }
        else {
          drupal_set_message(t('There was an error saving font %fontname from Font Squirrel.', array(
            '%fontname' => $font->name,
          )), 'error');
          $success = FALSE;
        }

        // else
      }
      else {
        drupal_set_message(t('There was an error downloading font %fontname from Font Squirrel.', array(
          '%fontname' => $font->name,
        )), 'error');
        $success = FALSE;
      }

      // else
    }

    // if
    if ($success) {

      // Unzip the .zip file
      if (function_exists('zip_open') && ($zip = zip_open(drupal_realpath($zip_location)))) {
        file_prepare_directory($directory_location, FILE_CREATE_DIRECTORY);
        while ($entry = zip_read($zip)) {
          if (zip_entry_open($zip, $entry, 'r') && ($zip_entry_filesize = zip_entry_filesize($entry))) {
            $entry_name = zip_entry_name($entry);
            $data = zip_entry_read($entry, $zip_entry_filesize);
            file_unmanaged_save_data($data, $directory_location . '/' . $entry_name, FILE_EXISTS_REPLACE);
          }

          // if
          zip_entry_close($entry);
        }

        // while
        zip_close($zip);
      }
      else {
        drupal_set_message(t('Unable to unzip font %fontname at <code>@zip_location</code>. See !zipdocs to enable unzipping, or unzip the file manually and enable this font again.', array(
          '%fontname' => $font->name,
          '@zip_location' => $zip_location,
          '!zipdocs' => l(t('PHP documentation on zip'), 'http://www.php.net/manual/en/zip.installation.php'),
        )), 'error');
        $success = FALSE;
      }

      // else
    }

    // if
    if (!$success) {
      fontyourface_disable_font($font);
    }

    // if
  }

  // if
  return $success;
}