View source
<?php
define('CSS_INJECTOR_PAGES_NOTLISTED', 0);
define('CSS_INJECTOR_PAGES_LISTED', 1);
define('CSS_INJECTOR_PHP', 2);
function css_injector_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/config/modules#description':
$output .= t('Allows administrators to inject CSS into the page output based on configurable rules.');
break;
case 'admin/config/development/css-injector':
$output .= '<p>' . t('Use CSS injection rules to add small snippets of CSS to the page output when specific criteria are met. For example, a simple rule could change the page background color at night or float a particular div to the right on node editing pages.') . '</p>';
break;
}
return $output;
}
function css_injector_init() {
$css_rules = _css_injector_load_rule();
foreach ($css_rules as $css_rule) {
if (!isset($css_rule['enabled']) || $css_rule['enabled']) {
if (_css_injector_evaluate_rule($css_rule)) {
$file_uri = _css_injector_rule_uri($css_rule['crid']);
$theme_rules = isset($css_rule['rule_themes']) ? unserialize($css_rule['rule_themes']) : '';
global $theme;
if (!is_array($theme_rules) || empty($theme_rules) || in_array($theme, $theme_rules, true)) {
switch ($css_rule['media']) {
case 'all':
case 'screen':
case 'print':
drupal_add_css($file_uri, array(
'type' => 'file',
'group' => CSS_THEME,
'media' => $css_rule['media'],
'preprocess' => $css_rule['preprocess'],
));
break;
case 'IE 7':
case 'IE 8':
case 'IE 9':
drupal_add_css($file_uri, array(
'group' => CSS_THEME,
'browsers' => array(
'IE' => $css_rule['media'],
'!IE' => FALSE,
),
'preprocess' => $css_rule['preprocess'],
));
break;
}
}
}
}
}
}
function css_injector_css_alter(&$css) {
$css_rules = _css_injector_load_rule();
foreach ($css_rules as $css_rule) {
$file_uri = _css_injector_rule_uri($css_rule['crid']);
if (!empty($css[$file_uri])) {
$css[$file_uri]['weight'] = 200 + $css_rule['crid'];
}
}
}
function css_injector_menu() {
$items = array(
'admin/config/development/css-injector/settings' => array(
'title' => 'CSS injector module settings',
'description' => 'Settings page for CSS injector module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css_injector_admin',
),
'access arguments' => array(
'administer css injection settings',
),
'type' => MENU_LOCAL_ACTION,
),
'admin/config/development/css-injector' => array(
'title' => 'CSS injector',
'description' => 'Add CSS to the page output based on configurable rules.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css_injector_admin_form',
),
'access arguments' => array(
'administer css injection',
),
'file' => 'css_injector.admin.inc',
'type' => MENU_NORMAL_ITEM,
),
'admin/config/development/css-injector/edit' => array(
'title' => 'Edit CSS injector rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css_injector_edit',
),
'access arguments' => array(
'administer css injection',
),
'file' => 'css_injector.admin.inc',
'type' => MENU_CALLBACK,
),
'admin/config/development/css-injector/add' => array(
'title' => 'Add CSS injector rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css_injector_edit',
),
'access arguments' => array(
'administer css injection',
),
'file' => 'css_injector.admin.inc',
'type' => MENU_CALLBACK,
),
'admin/config/development/css-injector/delete' => array(
'title' => 'Delete CSS injector rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'css_injector_delete_confirm',
),
'access arguments' => array(
'administer css injection',
),
'file' => 'css_injector.admin.inc',
'type' => MENU_CALLBACK,
),
);
return $items;
}
function css_injector_admin() {
$form = array();
$themes = _css_injector_get_themes();
$default_theme = variable_get('theme_default');
$rule_themes = variable_get('css_injector_default_theme', $default_theme);
$form['css_injector_default_theme'] = array(
'#type' => 'select',
'#title' => t('CSS injector default themes'),
'#options' => $themes,
'#default_value' => $rule_themes,
'#description' => t('Select themes css will be applied to. @theme theme is selected by default.', array(
'@theme' => $themes[$default_theme],
)),
'#multiple' => TRUE,
);
if (!module_exists('ace_editor')) {
$form['css_injector_use_ace'] = array(
'#type' => 'checkbox',
'#title' => t('Use Ace syntex highlighter'),
'#default_value' => variable_get('css_injector_use_ace', 1),
'#description' => t("Check if you want to enhance your editing experience with Ace syntex highlighter. If checked, Ace will be loaded via CDN."),
);
}
else {
$form['css_injector_use_ace_error'] = array(
'#type' => 'markup',
'#markup' => t('You already have Ace highlighter module installed. Please enable it to enhance your editing experience with Ace syntex highlighter.'),
);
}
$form['css_injector_import'] = array(
'#type' => 'markup',
'#markup' => '<br>' . t('If you want to import your css injector files from dev site to production, you can use !this sandox module. </br> Be aware, sandbox modules are not supported and you are using it on your own risk.', array(
'!this' => l('CSS Injector import', 'https://www.drupal.org/sandbox/stillfinder/2597592'),
)) . '</br>',
);
return system_settings_form($form);
}
function css_injector_theme() {
$items['css_injector_admin_form'] = array(
'render element' => 'form',
'file' => 'css_injector.admin.inc',
);
return $items;
}
function css_injector_permission() {
return array(
'administer css injection' => array(
'title' => t('Administer CSS Injection'),
),
);
}
function _css_injector_load_rule($crid = NULL, $reset = FALSE) {
static $rules;
if (!isset($rules) || $reset) {
if (!$reset && ($cache = cache_get('css_injector:rules')) && is_array($cache->data)) {
$rules = $cache->data;
}
else {
$rules = array();
$results = db_query("SELECT * FROM {css_injector_rule}", array(), array(
'fetch' => PDO::FETCH_ASSOC,
))
->fetchAllAssoc('crid');
foreach ($results as $id => $rule) {
$rules[$id] = $rule;
}
cache_set('css_injector:rules', $rules);
}
}
if (is_numeric($crid)) {
return $rules[$crid];
}
else {
return $rules;
}
}
function _css_injector_delete_rule($crid) {
if ($rule = _css_injector_load_rule($crid)) {
file_unmanaged_delete(_css_injector_rule_uri($crid));
db_delete('css_injector_rule')
->condition('crid', $crid)
->execute();
drupal_set_message(t('The CSS rule %title has been deleted.', array(
'%title' => $rule['title'],
)));
}
}
function _css_injector_evaluate_rule($css_rule = array()) {
if (!empty($css_rule['rule_conditions'])) {
if ($css_rule['rule_type'] < CSS_INJECTOR_PHP) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $css_rule['rule_conditions']);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $css_rule['rule_conditions']);
}
$page_match = !($css_rule['rule_type'] xor $page_match);
}
else {
if (module_exists('php')) {
$page_match = php_eval($css_rule['rule_conditions']);
}
else {
$page_match = FALSE;
}
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
function _css_injector_rule_uri($crid) {
if (!empty($crid)) {
$uri = 'public://css_injector/css_injector_' . $crid . '.css';
return $uri;
}
}
function _css_injector_get_themes() {
$themes = array();
$theme_list = list_themes();
foreach ($theme_list as $single_theme) {
if ($single_theme->status && file_exists($single_theme->filename)) {
$themes[$single_theme->name] = $single_theme->info['name'];
}
}
return $themes;
}
function _css_injector_update_breadcrumb() {
$breadcrumb = array(
l('Home', ''),
l('Administration', 'admin'),
l('Configuration', 'admin/config'),
l('Development', 'admin/config/development'),
l('CSS Injector', 'admin/config/development/css-injector'),
);
drupal_set_breadcrumb($breadcrumb);
}