View source
<?php
define('CSS3PIE_CSS_CACHE_KEY', 'css3pie_css_cache');
function css3pie_permission() {
return array(
'administer css3pie' => array(
'title' => t('Administer CSS3PIE selectors'),
'description' => t('user can change the used css3pie selectors'),
),
);
}
function css3pie_menu() {
$items = array();
$items['admin/config/user-interface/css3pie'] = array(
'title' => 'CSS3PIE',
'description' => 'Adds css3pie support to Drupal',
'access arguments' => array(
'administer css3pie',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css3pie_admin',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'css3pie.admin.inc',
);
return $items;
}
function css3pie_menu_alter(&$items) {
$items['admin/appearance/css3pie']['type'] = MENU_LOCAL_TASK;
}
function css3pie_css3pie() {
$css3pie_selectors = variable_get('css3pie_css_selectors', '');
$css3pie_theme_selectors = FALSE;
$css3pie_namespace = 'css3pie';
$result = db_query("SELECT s.name FROM {system} s WHERE s.status=1 AND s.type='theme'");
if (variable_get('css3pie_css_use_theme_settings', FALSE)) {
foreach ($result as $record) {
if ($css3pie_theme_selectors = theme_get_setting('css3pie', $record->name)) {
if (isset($css3pie_theme_selectors['selectors']) && is_array($css3pie_theme_selectors['selectors'])) {
$css3pie_selectors .= "\n" . implode("\n", $css3pie_theme_selectors['selectors']);
}
}
}
}
if ($css3pie_selectors) {
$css3pie_selectors = explode("\n", $css3pie_selectors);
$css3pie_selectors = array_filter(array_map('trim', $css3pie_selectors));
return array(
$css3pie_namespace => $css3pie_selectors,
);
}
}
function css3pie_preprocess_html(&$vars) {
ctools_include('css');
$path_to_css3pie_css_file = _css3pie_build_css3pie_css();
$path_to_css3pie_js_file = file_default_scheme() . '://css3pie/css3pie.js';
$piejslibrary = drupal_get_library('css3pie', 'css3pie_js');
$path_to_pie_js_file = NULL;
if (is_array($piejslibrary)) {
foreach ($piejslibrary['js'] as $file => $options) {
$path_to_pie_js_file = $file;
}
}
if ($path_to_css3pie_css_file && !variable_get('css3pie_css_use_js_mode')) {
drupal_add_css($path_to_css3pie_css_file, array(
'browsers' => array(
'IE' => 'lt IE 10',
'!IE' => FALSE,
),
));
}
if ($path_to_pie_js_file && variable_get('css3pie_css_use_js_mode') && $path_to_css3pie_js_file) {
$pie_js_to_html_head = array(
'#type' => 'markup',
'#markup' => '<!--[if lt IE 10]><script language="javascript" type="text/javascript" src="' . file_create_url($path_to_pie_js_file) . '"></script>' . PHP_EOL . '<script language="javascript" type="text/javascript" src="' . file_create_url($path_to_css3pie_js_file) . '"></script><![endif]-->' . PHP_EOL,
);
drupal_add_html_head($pie_js_to_html_head, 'css3pie');
}
}
function css3pie_library() {
if (file_exists(libraries_get_path('PIE') . '/PIE.htc')) {
$libraries = array();
$libraries['css3pie_htc'] = array(
'title' => 'CSS3PIE',
'website' => 'http://www.css3pie.com',
'version' => '1.0beta4',
'js' => array(
libraries_get_path('PIE') . '/PIE.htc' => array(),
),
);
$libraries['css3pie_php'] = array(
'title' => 'CSS3PIE',
'website' => 'http://www.css3pie.com',
'version' => '1.0beta4',
'js' => array(
libraries_get_path('PIE') . '/PIE.php' => array(),
),
);
$libraries['css3pie_js'] = array(
'title' => 'CSS3PIE',
'website' => 'http://www.css3pie.com',
'version' => '1.0beta4',
'js' => array(
libraries_get_path('PIE') . '/PIE.js' => array(),
),
);
return $libraries;
}
}
function _css3pie_build_css3pie_functionality() {
if (variable_get('css3pie_css_use_js_mode', FALSE)) {
_css3pie_build_css3pie_js();
}
else {
cache_clear_all(CSS3PIE_CSS_CACHE_KEY, 'cache');
}
}
function _css3pie_build_css3pie_css() {
$cache = cache_get(CSS3PIE_CSS_CACHE_KEY);
if (!empty($cache) && !empty($cache->data)) {
if ($filename = ctools_css_retrieve($cache->data)) {
return $filename;
}
}
$output = '';
$css3pie_selectors = module_invoke_all('css3pie');
if ($css3pie_selectors) {
$show_namespaces_as_comments = variable_get('css3pie_css_comment', TRUE);
$cnt_namespaces = count($css3pie_selectors);
$i = 0;
foreach ($css3pie_selectors as $namespace => $selectors) {
$i++;
if ($show_namespaces_as_comments) {
$output .= '/* ' . $namespace . ' */' . "\n";
}
$output .= implode(', ', $selectors);
if ($i < $cnt_namespaces) {
$output .= ',' . "\n";
}
}
if (!variable_get('css3pie_css_use_js_mode')) {
if (!variable_get('css3pie_css_use_php_wrapper', FALSE)) {
$css3library = drupal_get_library('css3pie', 'css3pie_htc');
}
else {
$css3library = drupal_get_library('css3pie', 'css3pie_php');
}
}
$css3file = '';
foreach ($css3library['js'] as $file => $options) {
$css3file = $file;
}
$output .= "\n" . '{' . "\n" . ' behavior: url(' . base_path() . $css3file . ');' . "\n" . '}';
}
$id = md5($output);
cache_set(CSS3PIE_CSS_CACHE_KEY, $id);
if ($filename = ctools_css_retrieve($id)) {
return $filename;
}
else {
return ctools_css_store($id, $output, FALSE);
}
}
function _css3pie_build_css3pie_js() {
$output = '';
$css3pie_selectors = module_invoke_all('css3pie');
$output .= "\$(function() {\n";
$output .= "if (window.PIE) {\n";
$output .= "\$(function() {\n";
foreach ($css3pie_selectors as $namespace => $selectors) {
foreach ($selectors as $a => $selector) {
$output .= "\$('" . $selector . "').each(function() {\n";
$output .= "PIE.attach(this);\n";
$output .= "});\n";
}
}
$output .= "}\n";
$output .= "});";
return _css3pie_create_css3pie_js($output);
}
function _css3pie_create_css3pie_js($js = NULL) {
if (!$js) {
drupal_set_message(t('No js content given.'), 'error');
return FALSE;
}
$css3pie_js_path = file_default_scheme() . '://css3pie';
if (!file_prepare_directory($css3pie_js_path, FILE_CREATE_DIRECTORY)) {
drupal_set_message(t('Unable to create CSS3PIE cache directory. Check the permissions on your files directory.'), 'error');
return;
}
$filename = $css3pie_js_path . '/css3pie.js';
$filename = file_unmanaged_save_data($js, $filename, FILE_EXISTS_REPLACE);
drupal_clear_js_cache();
return $filename;
}