View source
<?php
define('FONTYOURFACE_ADD_PAGE_COUNT', 20);
function fontyourface_perm() {
return array(
'administer @font-your-face',
);
}
function fontyourface_menu() {
$items = array();
$items['admin/settings/fontyourface'] = array(
'title' => '@font-your-face settings',
'description' => 'Manage settings for how font providers work. (Manage site fonts ' . l(t('under themes'), 'admin/build/themes/fontyourface') . '.)',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fontyourface_settings_form',
),
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/themes/fontyourface'] = array(
'title' => '@font-your-face',
'page callback' => 'fontyourface_admin_page',
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['admin/build/themes/fontyourface/add/%'] = array(
'title' => 'Add font',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fontyourface_admin_add_form',
5,
6,
7,
),
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
$items['admin/build/themes/fontyourface/add'] = array(
'title' => 'Add font',
'page callback' => 'fontyourface_admin_add_page',
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/themes/fontyourface/edit/%'] = array(
'title' => 'Edit font',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fontyourface_admin_edit_form',
5,
),
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function fontyourface_settings_form() {
$form = array();
return system_settings_form($form);
}
function fontyourface_preprocess_page(&$vars) {
$fonts = array();
$destination = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/font.css';
$directory = dirname($destination);
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
$font_face = '';
$font_family = '';
if ($_GET['q'] == 'admin/build/themes/fontyourface/add') {
$available_fonts = fontyourface_available_fonts();
foreach ($available_fonts as $module_name => $module_info) {
$font_providers[$module_name] = array();
foreach ($module_info['fonts'] as $group_name => $group_info) {
foreach ($group_info['fonts'] as $font_name => $group_font) {
$font = new stdClass();
$font->provider = $module_name;
$font->group_name = $group_name;
$font->name = $font_name;
$fonts[] = $font;
}
}
}
usort($fonts, 'fontyourface_by_font_name');
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$fonts = array_slice($fonts, $page * FONTYOURFACE_ADD_PAGE_COUNT, FONTYOURFACE_ADD_PAGE_COUNT);
}
elseif (strpos($_GET['q'], 'admin/build/themes/fontyourface/add/') === 0) {
$adding_font = fontyourface_get_font_from_path(arg(5), arg(6), arg(7));
if (!$adding_font) {
$module_name = arg(6, $_SERVER['REDIRECT_URL']);
$group_path = arg(7, $_SERVER['REDIRECT_URL']);
$font_path = arg(8, $_SERVER['REDIRECT_URL']);
$adding_font = fontyourface_get_font_from_path($module_name, $group_path, $font_path);
}
if ($adding_font) {
$font = new stdClass();
$font->provider = $adding_font['module'];
$font->group_name = $adding_font['group'];
$font->name = $adding_font['font name'];
$fonts[] = $font;
}
}
$used_fonts = fontyourface_get_fonts();
foreach ($used_fonts as $font) {
if (module_exists($font->provider)) {
$css_function = $font->provider . '_fontyourface_css';
$font_css = $css_function($font);
if ($font_css['@font-face'] != '') {
$font_face .= '@font-face { ' . $font_css['@font-face'] . ' }' . "\n";
}
if ($font_css['font-family'] != '' && $font->css != '') {
$font_family .= $font->css . ' { font-family: ' . $font_css['font-family'] . '; }' . "\n";
}
$fonts[] = $font;
}
}
$css = $font_face . $font_family;
file_save_data($css, $destination, FILE_EXISTS_REPLACE);
}
if ($css != '') {
fontyourface_add_css_in_preprocess($vars, file_directory_path() . '/fontyourface/font.css');
}
$vars['fontyourface'] = $fonts;
}
function fontyourface_admin_page() {
$fonts = fontyourface_get_fonts();
$available_fonts = fontyourface_available_fonts();
$rows = array();
if (empty($fonts)) {
$rows[] = array(
array(
'data' => t('No fonts added.'),
'colspan' => '3',
),
);
}
else {
foreach ($fonts as $font) {
if (module_exists($font->provider)) {
$provider = l($available_fonts[$font->provider]['name'], $available_fonts[$font->provider]['url']);
}
else {
$provider = '<i class="error">Disabled</i>';
}
$rows[] = array(
$font->name,
$provider,
l('Edit', 'admin/build/themes/fontyourface/edit/' . $font->fid),
);
}
}
$header = array(
t('Name'),
t('Provider'),
t('Operations'),
);
return theme('table', $header, $rows, array(
'id' => 'font-your-face',
)) . '<div>' . l('Add a new font', 'admin/build/themes/fontyourface/add') . '</div>';
}
function fontyourface_admin_add_page() {
global $pager_page_array, $pager_total;
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[] = l('@font-your-face', 'admin/build/themes/fontyourface');
drupal_set_breadcrumb($breadcrumb);
$page = isset($_GET['page']) ? $_GET['page'] : '';
$pager_page_array = explode(',', $page);
$available_fonts = fontyourface_available_fonts();
$fonts = array();
$fonts_by_module = array();
foreach ($available_fonts as $module_name => $module_info) {
$fonts_by_module[$module_name] = array();
foreach ($module_info['fonts'] as $group_name => $group_info) {
foreach ($group_info['fonts'] as $font_name => $group_font) {
$fonts_by_module[$module_name][] = $group_font;
$fonts[] = array(
'module' => $module_name,
'provider' => $module_info['name'],
'provider url' => $module_info['url'],
'group' => $group_name,
'group path' => $group_info['path'],
'font name' => $font_name,
'font' => $group_font,
);
}
}
}
$header = array(
t('Name'),
t('Sample'),
t('Provider'),
t('Group'),
);
if (count($fonts) > 0) {
usort($fonts, 'fontyourface_by_font_name');
$pager_total[] = ceil(count($fonts) / FONTYOURFACE_ADD_PAGE_COUNT);
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$page_fonts = array_slice($fonts, $page * FONTYOURFACE_ADD_PAGE_COUNT, FONTYOURFACE_ADD_PAGE_COUNT);
foreach ($page_fonts as $font) {
$preview = $font['module'] . '_fontyourface_preview';
$view_link = l($font['font name'], 'admin/build/themes/fontyourface/add/' . $font['module'] . '/' . $font['group path'] . '/' . $font['font']['path']);
$rows[] = array(
$view_link,
$preview($font['font']),
l($font['provider'], $font['provider url']),
check_plain($font['group']),
);
}
}
else {
$rows[] = array(
array(
'data' => t('No fonts available. ') . l(t('Enable font providers'), 'admin/build/modules') . ' ' . t('to make fonts available.'),
'colspan' => '4',
),
);
}
return '<h1>Add font</h1><div>Click a font name to view details and add the font.</div>' . theme('table', $header, $rows, array(
'id' => 'font-your-face-options',
)) . theme('pager', NULL, FONTYOURFACE_ADD_PAGE_COUNT);
}
function fontyourface_admin_add_form(&$form_state, $module_name, $group_path, $font_path) {
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[] = l('@font-your-face', 'admin/build/themes/fontyourface');
$breadcrumb[] = l('Add', 'admin/build/themes/fontyourface/add');
drupal_set_breadcrumb($breadcrumb);
drupal_add_js(drupal_get_path('module', 'fontyourface') . '/js/add_form.js');
$font = fontyourface_get_font_from_path($module_name, $group_path, $font_path);
if (!$font) {
$font_path = str_replace(array(
' ',
'!',
), array(
'+',
'%21',
), $font_path);
$group_path = str_replace(array(
' ',
'!',
), array(
'+',
'%21',
), $group_path);
$font = fontyourface_get_font_from_path($module_name, $group_path, $font_path);
}
if ($font) {
$view = $module_name . '_fontyourface_view';
$font_in_use = new stdClass();
$font_in_use->name = $font['font name'];
$font_in_use->group_name = $font['group'];
$font_in_use->provider = $font['module'];
$css_function = $font_in_use->provider . '_fontyourface_css';
$font_css = $css_function($font_in_use);
$form = array(
'module' => array(
'#type' => 'hidden',
'#value' => $font['module'],
),
'font' => array(
'#type' => 'hidden',
'#value' => $font['font name'],
),
'group' => array(
'#type' => 'hidden',
'#value' => $font['group'],
),
'font_title' => array(
'#value' => '<h2>' . check_plain($font['font name']) . '</h2>',
),
);
$license_function = $font_in_use->provider . '_fontyourface_license';
if (function_exists($license_function)) {
$license = $license_function($font_in_use);
if ($license) {
if ($license['url']) {
$license_markup = l($license['name'], $license['url'], array(
'attributes' => array(
'rel' => 'license',
),
));
}
else {
$license_markup = check_plain($license['name']);
}
$form['license'] = array(
'#value' => '<div class="license">' . t('License: !license', array(
'!license' => $license_markup,
)) . '</div>',
);
}
}
$form += array(
'sample_text' => array(
'#type' => 'textfield',
'#title' => t('Sample text'),
'#default_value' => variable_get('fontyourface_sample_text', 'The quick brown fox jumps over the lazy dog'),
'#size' => 60,
),
'font_view' => array(
'#value' => '<div class="fontyourface-view">' . $view($font['font'], variable_get('fontyourface_sample_text', 'The quick brown fox jumps over the lazy dog')) . '</div>',
),
'css' => array(
'#type' => 'textarea',
'#title' => t('CSS selector'),
'#default_value' => '',
'#description' => t('Use commas to separate multiple selectors, just like you would in CSS. Leave blank to handle application of the font in your theme. e.g. "body" or "h1, h2, h3, h4"'),
),
'font-family' => array(
'#value' => '<div>' . t('To apply in your own CSS, use:') . '</div><div><code>font-family: ' . check_plain($font_css['font-family']) . ';</code></div>',
),
'buttons' => array(
'submit' => array(
'#type' => 'submit',
'#value' => t('Add font'),
),
'cancel' => array(
'#type' => 'submit',
'#value' => t('Cancel'),
),
),
);
return $form;
}
else {
$form = array(
'not_found' => array(
'#value' => t('Font not found. Looking for: !font', array(
'!font' => $font_path,
)),
),
);
return $form;
}
}
function fontyourface_admin_add_form_submit($form, &$form_state) {
variable_set('fontyourface_sample_text', $form['sample_text']['#value']);
if ($form['#post']['op'] == t('Add font')) {
$font = new stdClass();
$font->provider = $form['module']['#value'];
$font->group_name = $form['group']['#value'];
$font->name = $form['font']['#value'];
$font->css = $form['css']['#value'];
drupal_write_record('fontyourface', $font);
$enabled = TRUE;
$enable_function = $font->provider . '_fontyourface_enable';
if (function_exists($enable_function)) {
$enabled = $enable_function($font);
}
}
if ($enabled) {
drupal_set_message(t('Added font') . ' <i>' . $font->name . '</i>.');
}
drupal_goto('admin/build/themes/fontyourface');
}
function fontyourface_admin_edit_form(&$form_state, $fid) {
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[] = l('@font-your-face', 'admin/build/themes/fontyourface');
drupal_set_breadcrumb($breadcrumb);
$font = new stdClass();
$font->fid = $fid;
$font = fontyourface_get_font($font);
$css_function = $font->provider . '_fontyourface_css';
if (function_exists($css_function)) {
$font_css = $css_function($font);
}
$form = array(
'fid' => array(
'#type' => 'hidden',
'#value' => $font->fid,
),
'font_name' => array(
'#value' => '<h2>' . check_plain($font->name) . '</h2>',
),
'css' => array(
'#type' => 'textarea',
'#title' => t('CSS selector'),
'#default_value' => $font->css,
'#description' => t('Use commas to separate multiple selectors, just like you would in CSS. Leave blank to handle application of the font in your theme.'),
),
);
if ($font_css) {
$form += array(
'font-family' => array(
'#value' => '<div>' . t('To apply in your own CSS, use:') . '</div><div><code>font-family: ' . check_plain($font_css['font-family']) . ';</code></div>',
),
);
}
$form += array(
'buttons' => array(
'submit' => array(
'#type' => 'submit',
'#value' => t('Edit font'),
),
'delete' => array(
'#type' => 'submit',
'#value' => t('Disable'),
),
),
);
return $form;
}
function fontyourface_admin_edit_form_submit($form, &$form_state) {
$font = new stdClass();
$font->fid = $form['fid']['#value'];
$font = fontyourface_get_font($font);
if ($form['#post']['op'] == t('Disable')) {
fontyourface_delete_font($font);
drupal_set_message(t('Disabled font') . ' <i>' . $font->name . '</i>.');
drupal_goto('admin/build/themes/fontyourface');
}
else {
$font->css = $form['css']['#value'];
drupal_write_record('fontyourface', $font, 'fid');
drupal_set_message(t('Updated font') . ' <i>' . $font->name . '</i>.');
drupal_goto('admin/build/themes/fontyourface');
}
}
function fontyourface_get_fonts() {
$fonts = array();
$results = db_query('SELECT * FROM {fontyourface}');
while ($result = db_fetch_object($results)) {
$fonts[] = $result;
}
return $fonts;
}
function fontyourface_get_font($font) {
return db_fetch_object(db_query('SELECT * FROM {fontyourface} WHERE fid = %d', $font->fid));
}
function fontyourface_delete_font($font) {
db_query("DELETE FROM {fontyourface} WHERE fid = %d", $font->fid);
}
function fontyourface_get_font_from_path($module_path, $group_path, $font_path) {
$font = FALSE;
$available_fonts = fontyourface_available_fonts();
$module_info = $available_fonts[$module_path];
if ($module_info) {
foreach ($module_info['fonts'] as $group_name => $group_info) {
if ($group_info['path'] == $group_path) {
foreach ($group_info['fonts'] as $font_name => $group_font) {
if ($group_font['path'] == $font_path) {
$font = array(
'module' => $module_path,
'provider' => $module_info['name'],
'group' => $group_name,
'group path' => $group_info['path'],
'font name' => $font_name,
'font' => $group_font,
);
}
}
}
}
}
return $font;
}
function fontyourface_available_fonts() {
static $available_fonts = FALSE;
if (!$available_fonts) {
$available_fonts = module_invoke_all('fontyourface_info');
}
return $available_fonts;
}
function fontyourface_by_font_name($a, $b) {
if (is_object($a)) {
if ($a->name == $b->name) {
return 0;
}
return $a->name > $b->name ? 1 : -1;
}
if ($a['font name'] == $b['font name']) {
return 0;
}
return $a['font name'] > $b['font name'] ? 1 : -1;
}
function fontyourface_add_css_in_preprocess(&$vars, $css_path, $remote = FALSE) {
if ($remote) {
$link = '<link href="' . $css_path . '" rel="stylesheet" type="text/css" />';
drupal_set_html_head($link);
$vars['head'] = drupal_get_html_head();
}
else {
drupal_add_css($css_path, 'module');
$vars['css']['all']['module'][$css_path] = TRUE;
$vars['styles'] = drupal_get_css();
}
}