You are here

function fontsquirrel_cron in @font-your-face 6

Implements hook_cron().

1 call to fontsquirrel_cron()
fontsquirrel_import in modules/fontsquirrel/fontsquirrel.module
Callback for import.

File

modules/fontsquirrel/fontsquirrel.module, line 34

Code

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

    // update every 24 hours
    $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_group) {
        $select_results = db_query('SELECT * FROM {fontsquirrel_group} g WHERE gid = %d', $font_group->id);
        if ($select_result = db_fetch_object($select_results)) {
          $changed = FALSE;
          if ($select_result->gid != $font_group->id) {
            $select_result->gid = $font_group->id;
            $changed = TRUE;
          }

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

          // if
          if ($select_result->path != $font_group->family_urlname) {
            $select_result->path = $font_group->family_urlname;
            $changed = TRUE;
          }

          // if
          if ($select_result->foundry_name != $font_group->foundry_name) {
            $select_result->foundry_name = $font_group->foundry_name;
            $changed = TRUE;
          }

          // if
          if ($select_result->foundry_path != $font_group->foundry_urlname) {
            $select_result->foundry_path = $font_group->foundry_urlname;
            $changed = TRUE;
          }

          // if
          if ($select_result->classification != $font_group->classification) {
            $select_result->classification = $font_group->classification;
            $changed = TRUE;
          }

          // if
          if ($select_result->filename != $font_group->font_filename) {
            $select_result->filename = $font_group->font_filename;
            $changed = TRUE;
          }

          // if
          if ($changed) {
            drupal_write_record('fontsquirrel_group', $select_result, 'gid');
          }

          // if
        }
        else {
          $insert_sql = "INSERT INTO {fontsquirrel_group}\n          (gid, name, path, foundry_name, foundry_path, classification, filename)\n          VALUES\n          (%d, '%s', '%s', '%s', '%s', '%s', '%s')";
          db_query($insert_sql, $font_group->id, $font_group->family_name, $font_group->family_urlname, $font_group->foundry_name, $font_group->foundry_urlname, $font_group->classification, $font_group->font_filename);
        }

        // else
      }

      // foreach
    }

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

  // if
}