You are here

function kernest_cron in @font-your-face 6

Implements hook_cron().

1 call to kernest_cron()
kernest_import in modules/kernest/kernest.module
Callback for import.

File

modules/kernest/kernest.module, line 34

Code

function kernest_cron() {
  $last_updated = variable_get('kernest_last_api_update', 0);
  if (time() - 60 * 60 * 24 > $last_updated) {

    // update every 24 hours
    // KERNEST's Joyent server throws 500 error on Drupal's user agent
    $api_result = drupal_http_request('http://kernest.com/styles/web-native.json', array(
      'User-Agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
    ));
    if ($api_result->code == '200') {
      $decoded = json_decode(str_replace("\n", ' ', $api_result->data));
      if (is_array($decoded->fonts)) {
        foreach ($decoded->fonts as $font) {
          $select_results = db_query("SELECT * FROM {kernest} g WHERE name = '%s'", $font->name);
          if ($select_result = db_fetch_object($select_results)) {
            $changed = FALSE;
            if ($select_result->url != $font->url) {
              $select_result->url = $font->url;
              $changed = TRUE;
            }

            // if
            if ($select_result->css != $font->{'recommended css'}) {
              $select_result->css = $font->{'recommended css'};
              $changed = TRUE;
            }

            // if
            if ($select_result->foundry != $font->foundry) {
              $select_result->foundry = $font->foundry;
              $changed = TRUE;
            }

            // if
            if ($select_result->license != $font->license->name) {
              $select_result->license = $font->license->name;
              $changed = TRUE;
            }

            // if
            if ($select_result->license_url != $font->license->url) {
              $select_result->license_url = $font->license->url;
              $changed = TRUE;
            }

            // if
            if ($changed) {
              drupal_write_record('kernest', $select_result, 'fid');
            }

            // if
          }
          else {
            $insert_sql = "INSERT INTO {kernest}\n            (name, url, css, foundry, license, license_url)\n            VALUES\n            ('%s', '%s', '%s', '%s', '%s', '%s')";
            db_query($insert_sql, $font->name, $font->url, $font->{'recommended css'}, $font->foundry, $font->license->name, $font->license->url);
          }

          // else
        }

        // foreach
      }

      // if
    }

    // if
    variable_set('kernest_last_api_update', time());
  }

  // if
}