| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Adds a CSS file to the stylesheet queue. |
* Adds a CSS file to the stylesheet queue. |
| 6 |
* |
* |
| 7 |
* @param $path |
* @param $path |
| 8 |
* (optional) The path to the CSS file relative to the base_path(), e.g., |
* (optional) The path to the CSS file relative to the base_path(), e.g., |
| 9 |
* /modules/devel/devel.css. |
* /modules/devel/devel.css. |
| 10 |
* @param $type |
* @param $type |
| 11 |
* (optional) The type of stylesheet that is being added. Types are: module |
* (optional) The type of stylesheet that is being added. Types are: module |
| 12 |
* or theme. |
* or theme. |
| 13 |
* @param $media |
* @param $media |
| 14 |
* (optional) The media type for the stylesheet, e.g., all, print, screen. |
* (optional) The media type for the stylesheet, e.g., all, print, screen. |
| 15 |
* @param $preprocess |
* @param $preprocess |
| 16 |
* (optional) Should this CSS file be aggregated and compressed if this |
* (optional) Should this CSS file be aggregated and compressed if this |
| 17 |
* feature has been turned on under the performance section? |
* feature has been turned on under the performance section? |
| 18 |
* |
* |
| 19 |
* What does this actually mean? |
* What does this actually mean? |
| 20 |
* CSS preprocessing is the process of aggregating a bunch of separate CSS |
* CSS preprocessing is the process of aggregating a bunch of separate CSS |
| 21 |
* files into one file that is then compressed by removing all extraneous |
* files into one file that is then compressed by removing all extraneous |
| 22 |
* white space. |
* white space. |
| 23 |
* |
* |
| 24 |
* The reason for merging the CSS files is outlined quite thoroughly here: |
* The reason for merging the CSS files is outlined quite thoroughly here: |
| 25 |
* http://www.die.net/musings/page_load_time/ |
* http://www.die.net/musings/page_load_time/ |
| 26 |
* "Load fewer external objects. Due to request overhead, one bigger file |
* "Load fewer external objects. Due to request overhead, one bigger file |
| 27 |
* just loads faster than two smaller ones half its size." |
* just loads faster than two smaller ones half its size." |
| 28 |
* |
* |
| 29 |
* However, you should *not* preprocess every file as this can lead to |
* However, you should *not* preprocess every file as this can lead to |
| 30 |
* redundant caches. You should set $preprocess = FALSE when: |
* redundant caches. You should set $preprocess = FALSE when: |
| 31 |
* |
* |
| 32 |
* - Your styles are only used rarely on the site. This could be a special |
* - Your styles are only used rarely on the site. This could be a special |
| 33 |
* admin page, the homepage, or a handful of pages that does not represent |
* admin page, the homepage, or a handful of pages that does not represent |
| 34 |
* the majority of the pages on your site. |
* the majority of the pages on your site. |
| 35 |
* |
* |
| 36 |
* Typical candidates for caching are for example styles for nodes across |
* Typical candidates for caching are for example styles for nodes across |
| 37 |
* the site, or used in the theme. |
* the site, or used in the theme. |
| 38 |
* @return |
* @return |
| 39 |
* An array of CSS files. |
* An array of CSS files. |
| 40 |
*/ |
*/ |
| 41 |
function oainjection_css($name, $data = null, $type = 'module', $media = 'all', $preprocess = TRUE, $hold = FALSE) |
function oainjection_css($name, $data = null, $type = 'module', $media = 'all', $preprocess = TRUE, $hold = FALSE) |
| 42 |
{ |
{ |
| 43 |
//drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE) |
//drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE) |
| 44 |
|
|
| 45 |
static $modulepath; |
static $modulepath; |
| 46 |
|
|
| 47 |
if(!isset($modulepath)) |
if(!isset($modulepath)) |
| 48 |
{ |
{ |
| 49 |
//TODO change from module folder to files folder (drupal multisite installation) |
//TODO change from module folder to files folder (drupal multisite installation) |
| 50 |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/css'; |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/css'; |
| 51 |
$modulepath = file_directory_path().'/injection/css'; |
$modulepath = file_directory_path().'/injection/css'; |
| 52 |
|
|
| 53 |
if(!file_exists($modulepath)) |
if(!file_exists($modulepath)) |
| 54 |
_oainjection_create_dirs($modulepath); |
_oainjection_create_dirs($modulepath); |
| 55 |
} |
} |
| 56 |
|
|
| 57 |
$filename = oainjection_cssKey($name, $type, $media, $preprocess); |
$filename = oainjection_cssKey($name, $type, $media, $preprocess); |
| 58 |
|
|
| 59 |
if($data==null || !trim($data)) |
if($data==null || !trim($data)) |
| 60 |
{ |
{ |
| 61 |
if(file_exists($modulepath.'/'.$filename.'.css')) |
if(file_exists($modulepath.'/'.$filename.'.css')) |
| 62 |
@unlink($modulepath.'/'.$filename.'.css'); |
@unlink($modulepath.'/'.$filename.'.css'); |
| 63 |
|
|
| 64 |
return $filename; |
return $filename; |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
static $css; |
static $css; |
| 68 |
|
|
| 69 |
if(!isset($css)) |
if(!isset($css)) |
| 70 |
{ |
{ |
| 71 |
$css = array(); |
$css = array(); |
| 72 |
} |
} |
| 73 |
|
|
| 74 |
if(!isset($css[$filename])) |
if(!isset($css[$filename])) |
| 75 |
{ |
{ |
| 76 |
$file = $modulepath.'/'.$filename.'.css'; |
$file = $modulepath.'/'.$filename.'.css'; |
| 77 |
|
|
| 78 |
if(!file_exists($file)) |
if(!file_exists($file)) |
| 79 |
{ |
{ |
| 80 |
$handle = fopen($file, 'w') or die("problem with the oainjection module: $file"); |
$handle = fopen($file, 'w') or die("problem with the oainjection module: $file"); |
| 81 |
fwrite($handle, $data); |
fwrite($handle, $data); |
| 82 |
fclose($handle); |
fclose($handle); |
| 83 |
} |
} |
| 84 |
|
|
| 85 |
if(!$hold) |
if(!$hold) |
| 86 |
drupal_add_css($file, $type, $media, $preprocess); |
drupal_add_css($file, $type, $media, $preprocess); |
| 87 |
|
|
| 88 |
$css[$filename] = array('filename' => $filename, 'file' => $file); |
$css[$filename] = array('filename' => $filename, 'file' => $file); |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
return $css[$filename]; |
return $css[$filename]; |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
/** |
/** |
| 95 |
* Add a JavaScript file, setting or inline code to the page. |
* Add a JavaScript file, setting or inline code to the page. |
| 96 |
* |
* |
| 97 |
* The behavior of this function depends on the parameters it is called with. |
* The behavior of this function depends on the parameters it is called with. |
| 98 |
* Generally, it handles the addition of JavaScript to the page, either as |
* Generally, it handles the addition of JavaScript to the page, either as |
| 99 |
* reference to an existing file or as inline code. The following actions can be |
* reference to an existing file or as inline code. The following actions can be |
| 100 |
* performed using this function: |
* performed using this function: |
| 101 |
* |
* |
| 102 |
* - Add a file ('core', 'module' and 'theme'): |
* - Add a file ('core', 'module' and 'theme'): |
| 103 |
* Adds a reference to a JavaScript file to the page. JavaScript files |
* Adds a reference to a JavaScript file to the page. JavaScript files |
| 104 |
* are placed in a certain order, from 'core' first, to 'module' and finally |
* are placed in a certain order, from 'core' first, to 'module' and finally |
| 105 |
* 'theme' so that files, that are added later, can override previously added |
* 'theme' so that files, that are added later, can override previously added |
| 106 |
* files with ease. |
* files with ease. |
| 107 |
* |
* |
| 108 |
* - Add inline JavaScript code ('inline'): |
* - Add inline JavaScript code ('inline'): |
| 109 |
* Executes a piece of JavaScript code on the current page by placing the code |
* Executes a piece of JavaScript code on the current page by placing the code |
| 110 |
* directly in the page. This can, for example, be useful to tell the user that |
* directly in the page. This can, for example, be useful to tell the user that |
| 111 |
* a new message arrived, by opening a pop up, alert box etc. |
* a new message arrived, by opening a pop up, alert box etc. |
| 112 |
* |
* |
| 113 |
* - Add settings ('setting'): |
* - Add settings ('setting'): |
| 114 |
* Adds a setting to Drupal's global storage of JavaScript settings. Per-page |
* Adds a setting to Drupal's global storage of JavaScript settings. Per-page |
| 115 |
* settings are required by some modules to function properly. The settings |
* settings are required by some modules to function properly. The settings |
| 116 |
* will be accessible at Drupal.settings. |
* will be accessible at Drupal.settings. |
| 117 |
* |
* |
| 118 |
* @param $data |
* @param $data |
| 119 |
* (optional) If given, the value depends on the $type parameter: |
* (optional) If given, the value depends on the $type parameter: |
| 120 |
* - 'core', 'module' or 'theme': Path to the file relative to base_path(). |
* - 'core', 'module' or 'theme': Path to the file relative to base_path(). |
| 121 |
* - 'inline': The JavaScript code that should be placed in the given scope. |
* - 'inline': The JavaScript code that should be placed in the given scope. |
| 122 |
* - 'setting': An array with configuration options as associative array. The |
* - 'setting': An array with configuration options as associative array. The |
| 123 |
* array is directly placed in Drupal.settings. You might want to wrap your |
* array is directly placed in Drupal.settings. You might want to wrap your |
| 124 |
* actual configuration settings in another variable to prevent the pollution |
* actual configuration settings in another variable to prevent the pollution |
| 125 |
* of the Drupal.settings namespace. |
* of the Drupal.settings namespace. |
| 126 |
* @param $type |
* @param $type |
| 127 |
* (optional) The type of JavaScript that should be added to the page. Allowed |
* (optional) The type of JavaScript that should be added to the page. Allowed |
| 128 |
* values are 'core', 'module', 'theme', 'inline' and 'setting'. You |
* values are 'core', 'module', 'theme', 'inline' and 'setting'. You |
| 129 |
* can, however, specify any value. It is treated as a reference to a JavaScript |
* can, however, specify any value. It is treated as a reference to a JavaScript |
| 130 |
* file. Defaults to 'module'. |
* file. Defaults to 'module'. |
| 131 |
* @param $scope |
* @param $scope |
| 132 |
* (optional) The location in which you want to place the script. Possible |
* (optional) The location in which you want to place the script. Possible |
| 133 |
* values are 'header' and 'footer' by default. If your theme implements |
* values are 'header' and 'footer' by default. If your theme implements |
| 134 |
* different locations, however, you can also use these. |
* different locations, however, you can also use these. |
| 135 |
* @param $defer |
* @param $defer |
| 136 |
* (optional) If set to TRUE, the defer attribute is set on the <script> tag. |
* (optional) If set to TRUE, the defer attribute is set on the <script> tag. |
| 137 |
* Defaults to FALSE. This parameter is not used with $type == 'setting'. |
* Defaults to FALSE. This parameter is not used with $type == 'setting'. |
| 138 |
* @param $cache |
* @param $cache |
| 139 |
* (optional) If set to FALSE, the JavaScript file is loaded anew on every page |
* (optional) If set to FALSE, the JavaScript file is loaded anew on every page |
| 140 |
* call, that means, it is not cached. Defaults to TRUE. Used only when $type |
* call, that means, it is not cached. Defaults to TRUE. Used only when $type |
| 141 |
* references a JavaScript file. |
* references a JavaScript file. |
| 142 |
* @return |
* @return |
| 143 |
* If the first parameter is NULL, the JavaScript array that has been built so |
* If the first parameter is NULL, the JavaScript array that has been built so |
| 144 |
* far for $scope is returned. |
* far for $scope is returned. |
| 145 |
*/ |
*/ |
| 146 |
function oainjection_js($name, $data = null, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $hold = FALSE) |
function oainjection_js($name, $data = null, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $hold = FALSE) |
| 147 |
{ |
{ |
| 148 |
//drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) |
//drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) |
| 149 |
|
|
| 150 |
static $modulepath; |
static $modulepath; |
| 151 |
|
|
| 152 |
if(!isset($modulepath)) |
if(!isset($modulepath)) |
| 153 |
{ |
{ |
| 154 |
//TODO change from module folder to files folder (drupal multisite installation) |
//TODO change from module folder to files folder (drupal multisite installation) |
| 155 |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/js'; |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/js'; |
| 156 |
$modulepath = file_directory_path().'/injection/js'; |
$modulepath = file_directory_path().'/injection/js'; |
| 157 |
|
|
| 158 |
if(!file_exists($modulepath)) |
if(!file_exists($modulepath)) |
| 159 |
_oainjection_create_dirs($modulepath); |
_oainjection_create_dirs($modulepath); |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
$filename = oainjection_jsKey($name, $type, $scope, $defer, $cache); |
$filename = oainjection_jsKey($name, $type, $scope, $defer, $cache); |
| 163 |
|
|
| 164 |
if($data==null || !trim($data)) |
if($data==null || !trim($data)) |
| 165 |
{ |
{ |
| 166 |
if(file_exists($modulepath.'/'.$filename.'.js')) |
if(file_exists($modulepath.'/'.$filename.'.js')) |
| 167 |
@unlink($modulepath.'/'.$filename.'.js'); |
@unlink($modulepath.'/'.$filename.'.js'); |
| 168 |
|
|
| 169 |
return $filename; |
return $filename; |
| 170 |
} |
} |
| 171 |
|
|
| 172 |
static $js; |
static $js; |
| 173 |
|
|
| 174 |
if(!isset($js)) |
if(!isset($js)) |
| 175 |
{ |
{ |
| 176 |
$js = array(); |
$js = array(); |
| 177 |
} |
} |
| 178 |
|
|
| 179 |
if(!isset($js[$filename])) |
if(!isset($js[$filename])) |
| 180 |
{ |
{ |
| 181 |
$file = $modulepath.'/'.$filename.'.js'; |
$file = $modulepath.'/'.$filename.'.js'; |
| 182 |
|
|
| 183 |
if(!file_exists($file)) |
if(!file_exists($file)) |
| 184 |
{ |
{ |
| 185 |
$handle = fopen($file, 'w') or die("problem with the oainjection module: $file"); |
$handle = fopen($file, 'w') or die("problem with the oainjection module: $file"); |
| 186 |
fwrite($handle, $data); |
fwrite($handle, $data); |
| 187 |
fclose($handle); |
fclose($handle); |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
if(!$hold) |
if(!$hold) |
| 191 |
drupal_add_js($file, $type, $scope, $defer, $cache); |
drupal_add_js($file, $type, $scope, $defer, $cache); |
| 192 |
|
|
| 193 |
$js[$filename] = array('filename' => $filename, 'file' => $file); |
$js[$filename] = array('filename' => $filename, 'file' => $file); |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
return $js[$filename]; |
return $js[$filename]; |
| 197 |
} |
} |
| 198 |
|
|
| 199 |
function oainjection_cssKey($name, $type = 'module', $media = 'all', $preprocess = TRUE) |
function oainjection_cssKey($name, $type = 'module', $media = 'all', $preprocess = TRUE) |
| 200 |
{ |
{ |
| 201 |
$filename = $name . '_' . $type . '_' . $media . '_' . ($preprocess? 'true' : 'false'); |
$filename = $name . '_' . $type . '_' . $media . '_' . ($preprocess? 'true' : 'false'); |
| 202 |
$filename = preg_replace("/[^a-zA-Z0-9\-_\.]+/", "_", $filename); |
$filename = preg_replace("/[^a-zA-Z0-9\-_\.]+/", "_", $filename); |
| 203 |
return strtolower($filename); |
return strtolower($filename); |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
function oainjection_jsKey($name, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) |
function oainjection_jsKey($name, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) |
| 207 |
{ |
{ |
| 208 |
$filename = $name . '_' . $type . '_' . $scope . '_' . ($defer? 'true' : 'false') . '_' . ($cache? 'true' : 'false'); |
$filename = $name . '_' . $type . '_' . $scope . '_' . ($defer? 'true' : 'false') . '_' . ($cache? 'true' : 'false'); |
| 209 |
$filename = preg_replace("/[^a-zA-Z0-9\-_\.]+/", "_", $filename); |
$filename = preg_replace("/[^a-zA-Z0-9\-_\.]+/", "_", $filename); |
| 210 |
return strtolower($filename); |
return strtolower($filename); |
| 211 |
} |
} |
| 212 |
|
|
| 213 |
function oainjection_cssDelete($name) |
function oainjection_cssDelete($name) |
| 214 |
{ |
{ |
| 215 |
static $modulepath; |
static $modulepath; |
| 216 |
|
|
| 217 |
if(!isset($modulepath)) |
if(!isset($modulepath)) |
| 218 |
{ |
{ |
| 219 |
//TODO change from module folder to files folder (drupal multisite installation) |
//TODO change from module folder to files folder (drupal multisite installation) |
| 220 |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/css'; |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/css'; |
| 221 |
$modulepath = file_directory_path().'/injection/css'; |
$modulepath = file_directory_path().'/injection/css'; |
| 222 |
|
|
| 223 |
if(!file_exists($modulepath)) |
if(!file_exists($modulepath)) |
| 224 |
_oainjection_create_dirs($modulepath); |
_oainjection_create_dirs($modulepath); |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
if(file_exists($modulepath.'/'.$name.'.css')) |
if(file_exists($modulepath.'/'.$name.'.css')) |
| 228 |
@unlink($modulepath.'/'.$name.'.css'); |
@unlink($modulepath.'/'.$name.'.css'); |
| 229 |
} |
} |
| 230 |
|
|
| 231 |
function oainjection_jsDelete($name) |
function oainjection_jsDelete($name) |
| 232 |
{ |
{ |
| 233 |
static $modulepath; |
static $modulepath; |
| 234 |
|
|
| 235 |
if(!isset($modulepath)) |
if(!isset($modulepath)) |
| 236 |
{ |
{ |
| 237 |
//TODO change from module folder to files folder (drupal multisite installation) |
//TODO change from module folder to files folder (drupal multisite installation) |
| 238 |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/js'; |
//$modulepath = dirname(drupal_get_filename('module', 'oainjection')).'/js'; |
| 239 |
$modulepath = file_directory_path().'/injection/js'; |
$modulepath = file_directory_path().'/injection/js'; |
| 240 |
|
|
| 241 |
if(!file_exists($modulepath)) |
if(!file_exists($modulepath)) |
| 242 |
_oainjection_create_dirs($modulepath); |
_oainjection_create_dirs($modulepath); |
| 243 |
} |
} |
| 244 |
|
|
| 245 |
if(file_exists($modulepath.'/'.$name.'.js')) |
if(file_exists($modulepath.'/'.$name.'.js')) |
| 246 |
@unlink($modulepath.'/'.$name.'.js'); |
@unlink($modulepath.'/'.$name.'.js'); |
| 247 |
} |
} |
| 248 |
|
|
| 249 |
//------------------------------------------------------------------------------- |
//------------------------------------------------------------------------------- |
| 250 |
|
|
| 251 |
function oainjection_create_jsclass($values, $classname = null) |
function oainjection_create_jsclass($values, $classname = null) |
| 252 |
{ |
{ |
| 253 |
$js = ''; |
$js = ''; |
| 254 |
|
|
| 255 |
if($classname) |
if($classname) |
| 256 |
{ |
{ |
| 257 |
$count = 0; |
$count = 0; |
| 258 |
|
|
| 259 |
$js = 'var ' . $classname . "={\n"; |
$js = 'var ' . $classname . "={\n"; |
| 260 |
foreach($values as $k=>$v) |
foreach($values as $k=>$v) |
| 261 |
{ |
{ |
| 262 |
$js .= $k . ': ' . _oainjection_format_type($v); |
$js .= $k . ': ' . _oainjection_format_type($v); |
| 263 |
|
|
| 264 |
if(++$count<count($values)) |
if(++$count<count($values)) |
| 265 |
$js .= ",\n"; |
$js .= ",\n"; |
| 266 |
else |
else |
| 267 |
$js .= "\n"; |
$js .= "\n"; |
| 268 |
} |
} |
| 269 |
$js .= "}\n"; |
$js .= "}\n"; |
| 270 |
} |
} |
| 271 |
else |
else |
| 272 |
{ |
{ |
| 273 |
foreach($values as $k=>$v) |
foreach($values as $k=>$v) |
| 274 |
{ |
{ |
| 275 |
$js .= 'var ' . $k . '= ' . _oainjection_format_type($v) . ";\n"; |
$js .= 'var ' . $k . '= ' . _oainjection_format_type($v) . ";\n"; |
| 276 |
} |
} |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
return $js; |
return $js; |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
function _oainjection_format_type($v) |
function _oainjection_format_type($v) |
| 283 |
{ |
{ |
| 284 |
switch(gettype($v)) |
switch(gettype($v)) |
| 285 |
{ |
{ |
| 286 |
case 'boolean': |
case 'boolean': |
| 287 |
$v = ($v? 'true' : 'false'); |
$v = ($v? 'true' : 'false'); |
| 288 |
break; |
break; |
| 289 |
case 'integer': |
case 'integer': |
| 290 |
case 'double': |
case 'double': |
| 291 |
$v = strval($v); |
$v = strval($v); |
| 292 |
break; |
break; |
| 293 |
case 'NULL': |
case 'NULL': |
| 294 |
$v = "''"; |
$v = "''"; |
| 295 |
break; |
break; |
| 296 |
default: //case 'string': |
default: //case 'string': |
| 297 |
$v = "'$v'"; |
$v = "'$v'"; |
| 298 |
break; |
break; |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
return $v; |
return $v; |
| 302 |
} |
} |
| 303 |
|
|
| 304 |
function _oainjection_create_dirs($path) |
function _oainjection_create_dirs($path) |
| 305 |
{ |
{ |
| 306 |
if (!is_dir($path)) |
if (!is_dir($path)) |
| 307 |
{ |
{ |
| 308 |
$directory_path = ""; |
$directory_path = ""; |
| 309 |
$directories = explode("/",$path); |
$directories = explode("/",$path); |
| 310 |
//array_pop($directories); |
//array_pop($directories); |
| 311 |
|
|
| 312 |
foreach($directories as $directory) |
foreach($directories as $directory) |
| 313 |
{ |
{ |
| 314 |
$directory_path .= $directory."/"; |
$directory_path .= $directory."/"; |
| 315 |
|
|
| 316 |
if (!is_dir($directory_path)) |
if (!is_dir($directory_path)) |
| 317 |
{ |
{ |
| 318 |
mkdir($directory_path); |
mkdir($directory_path); |
| 319 |
chmod($directory_path, 0777); |
chmod($directory_path, 0777); |
| 320 |
} |
} |
| 321 |
} |
} |
| 322 |
} |
} |
| 323 |
} |
} |