You are here

css3pie.module in css3pie 6

Same filename and directory in other branches
  1. 7.2 css3pie.module

css3pie.module a very simple Drupal module to implement the css3pie.com javascript to your drupal and make the css selectors configurable over ui. This module creates a real css file on drupal files folder and add them via drupal_add_css.

File

css3pie.module
View source
<?php

/**
 * @file css3pie.module
 * a very simple Drupal module to implement the css3pie.com javascript to your drupal and
 * make the css selectors configurable over ui. This module creates a real css file on drupal
 * files folder and add them via drupal_add_css.
 */

/**
 * Implements hook_perm()
 * only users with "administer css3pie" permission can edit
 * the settings form.
 *
 * @see hook_perm();
 * @return <array> permissions array
 */
function css3pie_perm() {
  return array(
    'administer css3pie',
  );
}

/**
 * Implements hook_menu()
 * admin settings page under themes page
 *
 * @see hook_menu();
 * @return string
 */
function css3pie_menu() {
  $items = array();
  $items['admin/build/themes/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',
    ),
    'file' => 'css3pie.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements of hook_menu_alter().
 */
function css3pie_menu_alter(&$items) {
  $items['admin/build/themes/css3pie']['type'] = MENU_LOCAL_TASK;
}

/**
 * Implements own hook_css3pie()
 * returns a array with selectors
 * that will be added to css3pie css file on next cache clear
 *
 * @return <array> multiarray with selectors use modulname as key for namespace
 */
function css3pie_css3pie() {
  $css3pie_selectors = variable_get('css3pie_css_selectors', '');
  $css3pie_namespace = 'css3pie';
  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_init() {

  // get the path of css3pie file
  $path_to_css3pie_css_file = file_directory_path() . '/css3pie/css3pie.css';
  $path_to_css3pie_js_file = file_directory_path() . '/css3pie/css3pie.js';
  $path_to_pie_js_file = libraries_get_path('PIE') . '/PIE.js';

  // add to drupal´ css only if we have a cssfile
  if ($path_to_css3pie_css_file && !variable_get('css3pie_css_use_js_mode', FALSE)) {
    drupal_add_css($path_to_css3pie_css_file);
  }
  if ($path_to_pie_js_file && variable_get('css3pie_css_use_js_mode', FALSE) && $path_to_css3pie_js_file) {
    $pie_js_to_html_head = '<!--[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('css3pie/css3pie.js') . '"></script><![endif]-->' . PHP_EOL;
    drupal_set_html_head($pie_js_to_html_head);
  }
}

/**
 * helper function to build the module functionality
 */
function _css3pie_build_css3pie_functionality() {
  if (variable_get('css3pie_css_use_js_mode', FALSE)) {
    _css3pie_build_css3pie_js();
  }
  else {
    _css3pie_build_css3pie_css();
  }
}

/**
 * helper function get all selectors and generates content for css file
 * @param <type> $css
 * @return <type> path to new created file
 */
function _css3pie_build_css3pie_css() {
  $output = '';

  // invoke hook_css3pie in all modules to get the selectors
  $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";
      }
    }
    $output .= "\n" . '{' . "\n" . '  behavior: url(' . base_path() . _css3pie_get_css3pie_library() . ');' . "\n" . '}';
  }
  return _css3pie_create_css3pie_css($output);
}

/**
 * helper function get all selectors and generates content for js file
 * @param <type> $js
 * @return <type> path to new created file
 */
function _css3pie_build_css3pie_js() {
  $output = '';

  // invoke hook_css3pie in all modules to get the selectors
  $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);
}

/**
 * helper function creates a real css file within files directory
 * that be can added via drupal_add_css();
 *
 * @param <type> $css - the css markup
 * @return <type>
 */
function _css3pie_create_css3pie_css($css = NULL) {
  if (!$css) {
    return FALSE;
  }
  $css3pie_css_path = file_create_path('css3pie');

  // check again for error message
  if (!file_check_directory($css3pie_css_path, FILE_CREATE_DIRECTORY)) {
    drupal_set_message(t('Unable to create CSS3PIE CSS Cache directory. Check permissions on your files directory.'), 'error');
    return;
  }
  $filename = $css3pie_css_path . '/css3pie.css';
  $filename = file_save_data($css, $filename, FILE_EXISTS_REPLACE);

  // clear the css cache
  drupal_clear_css_cache();
  return $filename;
}

/**
 * helper function creates a real js file within files directory using new drupal streamwrapper;
 *
 * @param <type> $js - the js markup
 * @return <type>
 */
function _css3pie_create_css3pie_js($js = NULL) {
  if (!$js) {
    drupal_set_message(t('No js content given.'), 'error');
    return FALSE;
  }
  $css3pie_js_path = file_create_path('css3pie');

  // create css3pie directory and check if successfull...
  if (!file_check_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;
  }

  // save css data to our real css file...
  $filename = $css3pie_js_path . '/css3pie.js';
  $filename = file_save_data($js, $filename, FILE_EXISTS_REPLACE);

  // clear the js cache
  drupal_clear_js_cache();
  return $filename;
}

/**
 * helper function to get the path of CSS3PIE library
 * it uses the library api.
 *
 * @return <string> the path to the CSS3PIE library
 */
function _css3pie_get_css3pie_library() {
  static $path_to_css3pie;

  // only search for library file
  if (!isset($path_to_css3pie)) {
    $path_to_css3pie = libraries_get_path('PIE');

    // Use php wrapper to set the correct header on old servers...
    if (!variable_get('css3pie_css_use_php_wrapper', FALSE)) {
      $path_to_css3pie = $path_to_css3pie . '/PIE.htc';
    }
    else {
      $path_to_css3pie = $path_to_css3pie . '/PIE.php';
    }
  }
  return $path_to_css3pie;
}

Functions

Namesort descending Description
css3pie_css3pie Implements own hook_css3pie() returns a array with selectors that will be added to css3pie css file on next cache clear
css3pie_init
css3pie_menu Implements hook_menu() admin settings page under themes page
css3pie_menu_alter Implements of hook_menu_alter().
css3pie_perm Implements hook_perm() only users with "administer css3pie" permission can edit the settings form.
_css3pie_build_css3pie_css helper function get all selectors and generates content for css file
_css3pie_build_css3pie_functionality helper function to build the module functionality
_css3pie_build_css3pie_js helper function get all selectors and generates content for js file
_css3pie_create_css3pie_css helper function creates a real css file within files directory that be can added via drupal_add_css();
_css3pie_create_css3pie_js helper function creates a real js file within files directory using new drupal streamwrapper;
_css3pie_get_css3pie_library helper function to get the path of CSS3PIE library it uses the library api.