| 1 |
<?php |
<?php |
| 2 |
// $Id |
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 9 |
*/ |
*/ |
| 10 |
|
|
| 11 |
/** |
/** |
| 12 |
* Implementation of hook_init. |
* Implementation of hook_help(). |
| 13 |
|
*/ |
| 14 |
|
function css_injector_help($path, $arg) { |
| 15 |
|
$output = ''; |
| 16 |
|
switch ($path) { |
| 17 |
|
case 'admin/settings/modules#description': |
| 18 |
|
$output .= t('Allows administrators to inject CSS into the page output based on configurable rules.'); |
| 19 |
|
break; |
| 20 |
|
case 'admin/settings/css_injector': |
| 21 |
|
$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>'; |
| 22 |
|
$output .= '<p>'. t('An important point to keep in mind is that in Drupal, themes always get last crack at display. Theme CSS is applied after modules. That means themes can and will override CSS put into play by CSS Injector module. If you want to override CSS that a theme is already applying, use CSS rules the way they were intended -- higher specificity rules, or the !important flag.') .'</p>'; |
| 23 |
|
break; |
| 24 |
|
} |
| 25 |
|
return $output; |
| 26 |
|
} |
| 27 |
|
|
| 28 |
|
/** |
| 29 |
|
* Implementation of hook_init(). |
| 30 |
* Checks to see whether any CSS files should be added to the current page, |
* Checks to see whether any CSS files should be added to the current page, |
| 31 |
* based on rules configured by the site administrator. |
* based on rules configured by the site administrator. |
| 32 |
*/ |
*/ |
| 33 |
function css_injector_init() { |
function css_injector_init() { |
| 34 |
$css_rules = _css_injector_load_rule(); |
$css_rules = _css_injector_load_rule(); |
| 35 |
foreach($css_rules as $css_rule) { |
foreach ($css_rules as $css_rule) { |
| 36 |
if (_css_injector_evaluate_rule($css_rule)) { |
if (_css_injector_evaluate_rule($css_rule)) { |
| 37 |
drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']); |
drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']); |
| 38 |
} |
} |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
/** |
/** |
| 43 |
* Implementation of hook_menu. |
* Implementation of hook_menu(). |
| 44 |
* Defines menu callbacks for CSS Injector's configuration pages. |
* Defines menu callbacks for CSS Injector's configuration pages. |
| 45 |
*/ |
*/ |
| 46 |
function css_injector_menu() { |
function css_injector_menu() { |
| 81 |
return $items; |
return $items; |
| 82 |
} |
} |
| 83 |
|
|
| 84 |
|
/** |
| 85 |
|
* Implementation of hook_theme(). |
| 86 |
|
*/ |
| 87 |
function css_injector_theme() { |
function css_injector_theme() { |
| 88 |
$items['css_injector_admin_form'] = array( |
$items['css_injector_admin_form'] = array( |
| 89 |
'arguments' => array('form' => array()), |
'arguments' => array('form' => array()), |
| 92 |
return $items; |
return $items; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
|
/** |
| 96 |
|
* Implementation of hook_perm(). |
| 97 |
|
*/ |
| 98 |
function css_injector_perm() { |
function css_injector_perm() { |
| 99 |
return array('administer css injection'); |
return array('administer css injection'); |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
/** |
/** |
| 103 |
* Helper function to load all CSS injection rules |
* Helper function to load all CSS injection rules. |
| 104 |
*/ |
*/ |
| 105 |
function _css_injector_load_rule($crid = NULL, $reset = FALSE) { |
function _css_injector_load_rule($crid = NULL, $reset = FALSE) { |
| 106 |
static $rules; |
static $rules; |
| 127 |
} |
} |
| 128 |
|
|
| 129 |
/** |
/** |
| 130 |
* Helper function to delete an existing rule and its accompanying file |
* Helper function to delete an existing rule and its accompanying file. |
| 131 |
*/ |
*/ |
| 132 |
function _css_injector_delete_rule($crid) { |
function _css_injector_delete_rule($crid) { |
| 133 |
if ($rule = _css_injector_load_rule($crid)) { |
if ($rule = _css_injector_load_rule($crid)) { |
| 137 |
} |
} |
| 138 |
|
|
| 139 |
/** |
/** |
| 140 |
* Helper function to determine whether a rule's conditions are met |
* Helper function to determine whether a rule's conditions are met. |
| 141 |
*/ |
*/ |
| 142 |
function _css_injector_evaluate_rule($css_rule = array()) { |
function _css_injector_evaluate_rule($css_rule = array()) { |
| 143 |
// Match path if necessary |
// Match path if necessary. |
| 144 |
if (!empty($css_rule['rule_conditions'])) { |
if (!empty($css_rule['rule_conditions'])) { |
| 145 |
if ($css_rule['rule_type'] < 2) { |
if ($css_rule['rule_type'] < 2) { |
| 146 |
$path = drupal_get_path_alias($_GET['q']); |
$path = drupal_get_path_alias($_GET['q']); |
| 150 |
$page_match = $page_match || drupal_match_path($_GET['q'], $css_rule['rule_conditions']); |
$page_match = $page_match || drupal_match_path($_GET['q'], $css_rule['rule_conditions']); |
| 151 |
} |
} |
| 152 |
// When $css_rule['rule_type'] has a value of 0, the rule is matched on |
// When $css_rule['rule_type'] has a value of 0, the rule is matched on |
| 153 |
// all pages except those listed in $css_rule['rule_conditions']. When set to 1, it |
// all pages except those listed in $css_rule['rule_conditions']. |
| 154 |
// is displayed only on those pages listed in $css_rule['rule_type']. |
// When set to 1, it is displayed only on those pages listed in |
| 155 |
|
// $css_rule['rule_type']. |
| 156 |
$page_match = !($css_rule['rule_type'] xor $page_match); |
$page_match = !($css_rule['rule_type'] xor $page_match); |
| 157 |
} |
} |
| 158 |
else { |
else { |