View source
<?php
function kernest_menu() {
$items = array();
$items['admin/build/themes/fontyourface/kernest/import'] = array(
'title' => 'KERNEST Import',
'page callback' => 'kernest_import',
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function kernest_import() {
kernest_cron();
drupal_goto($_GET['destination']);
}
function kernest_cron() {
$last_updated = variable_get('kernest_last_api_update', 0);
if (time() - 60 * 60 * 24 > $last_updated) {
$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 ($select_result->css != $font->{'recommended css'}) {
$select_result->css = $font->{'recommended css'};
$changed = TRUE;
}
if ($select_result->foundry != $font->foundry) {
$select_result->foundry = $font->foundry;
$changed = TRUE;
}
if ($select_result->license != $font->license->name) {
$select_result->license = $font->license->name;
$changed = TRUE;
}
if ($select_result->license_url != $font->license->url) {
$select_result->license_url = $font->license->url;
$changed = TRUE;
}
if ($changed) {
drupal_write_record('kernest', $select_result, 'fid');
}
}
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);
}
}
}
}
variable_set('kernest_last_api_update', time());
}
}
function kernest_fontyourface_info() {
$info = array(
'kernest' => array(
'name' => 'KERNEST',
'url' => 'http://kernest.com/',
'fonts' => kernest_list(),
),
);
return $info;
}
function kernest_fontyourface_preview($font) {
return '<span style="' . $font['css'] . ' font-size: 24px;">' . $font['name'] . '</span>';
}
function kernest_fontyourface_view($font, $text) {
$output = '';
$sizes = array(
32,
24,
18,
14,
12,
10,
);
foreach ($sizes as $size) {
$output .= '<div style="' . str_replace("\n", ' ', $font['css']) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</div>';
}
return $output;
}
function kernest_preprocess_page(&$vars) {
if (!empty($vars['fontyourface'])) {
$links = array();
$fonts = kernest_list();
foreach ($vars['fontyourface'] as $used_font) {
if ($used_font->provider == 'kernest') {
fontyourface_add_css_in_preprocess($vars, $fonts[$used_font->group_name]['fonts'][$used_font->name]['url'] . '.css', TRUE);
}
}
}
}
function kernest_fontyourface_css($used_font) {
$css = array(
'@font-face' => '',
'font-family' => '',
);
$list = kernest_list(FALSE);
$font = $list[$used_font->group_name]['fonts'][$used_font->name];
if (preg_match('#font-family:([^;]+)#', $font['css'], $match)) {
$css['font-family'] = trim($match[1]);
}
return $css;
}
function kernest_fontyourface_license($used_font) {
$list = kernest_list();
$font = $list[$used_font->group_name]['fonts'][$used_font->name];
return $font['license'];
}
function kernest_list($show_error = TRUE) {
static $fonts = FALSE;
if ($fonts === FALSE) {
$fonts = array();
$font_fid_to_name = array();
$results = db_query('SELECT k.* FROM {kernest} k');
while ($result = db_fetch_object($results)) {
if (!isset($fonts[$result->foundry])) {
$fonts[$result->foundry] = array(
'path' => urlencode($result->foundry),
'fonts' => array(),
);
}
$fonts[$result->foundry]['fonts'][$result->name] = array(
'name' => $result->name,
'path' => urlencode($result->name),
'css' => $result->css,
'license' => array(
'name' => $result->license,
'url' => $result->license_url,
),
'url' => $result->url,
);
}
if (count($fonts) == 0 && $show_error) {
drupal_set_message(t('The KERNEST font list has not yet been imported. !importlink or wait until the next cron run.', array(
'!importlink' => l('Import now', 'admin/build/themes/fontyourface/kernest/import', array(
'query' => drupal_get_destination(),
)),
)));
}
}
return $fonts;
}