[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare API - JavaScript * 4 * Written by Dave Hall skwashd at phpgroupware.org * 5 * Copyright (C) 2003 Free Software Foundation Inc * 6 * -------------------------------------------------------------------------* 7 * This library is part of the eGroupWare API * 8 * http://www.egroupware.org/api * 9 * ------------------------------------------------------------------------ * 10 * This program is Free Software; you can redistribute it and/or modify it * 11 * under the terms of the GNU General Public License as published by the * 12 * Free Software Foundation; either version 2 of the License, or (at your * 13 * option) any later version. * 14 \**************************************************************************/ 15 /* $Id: class.javascript.inc.php 20295 2006-02-15 12:31:25Z $ */ 16 17 // On production sites, remove this lines below! \\ 18 if (file_exists(EGW_INCLUDE_ROOT . '/uncompressed_thyapi')) define(EGW_UNCOMPRESSED_THYAPI, true); 19 else define(EGW_UNCOMPRESSED_THYAPI, false); 20 21 $GLOBALS['concisus_system']['javascript'] = array(); 22 23 /** 24 * eGroupWare javascript support class 25 * 26 * Only instanstiate this class using: 27 * <code> 28 * if(!@is_object($GLOBALS['egw']->js)) 29 * { 30 * $GLOBALS['egw']->js =& CreateObject('phpgwapi.javascript'); 31 * } 32 * </code> 33 * 34 * This way a theme can see if this is a defined object and include the data, 35 * while the is_object() wrapper prevents whiping out existing data held in 36 * this instance variables, primarily the $files variable. 37 * 38 * Note: The package argument is the subdirectory of js - all js should live in subdirectories 39 * 40 * @package phpgwapi 41 * @subpackage sessions 42 * @abstract 43 * @author Dave Hall 44 * @copyright © 2003 Free Software Foundation 45 * @license GPL 46 * @uses template 47 * 48 */ 49 class javascript 50 { 51 /** 52 * @var array elements to be used for the on(Un)Load attributes of the body tag 53 */ 54 var $body; 55 56 /** 57 * @var array list of validated files to be included in the head section of a page 58 */ 59 var $files; 60 61 /** 62 * @var object used for holding an instance of the Template class 63 */ 64 var $t; 65 66 /** 67 * @var boolean Load JS API ? 68 */ 69 var $js_api; 70 71 /** 72 * Constructor 73 * 74 * Initialize the instance variables 75 */ 76 function javascript() 77 { 78 //$this->t =& CreateObject('phpgwapi.Template', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir','phpgwapi')); 79 //not currently used, but will be soon - I hope :) 80 $this->included_files =& $GLOBALS['concisus_system']['javascript']['included_files']; 81 } 82 83 84 /** 85 * Returns the javascript required for displaying a popup message box 86 * 87 * @param string $msg the message to be displayed to user 88 * @returns string the javascript to be used for displaying the message 89 */ 90 function get_alert($msg) 91 { 92 return 'return alert("'.lang($msg).'");'; 93 } 94 95 /** 96 * Adds on(Un)Load= attributes to the body tag of a page 97 * 98 * @returns string the attributes to be used 99 */ 100 function get_body_attribs() 101 { 102 $js = ''; 103 foreach(array('onLoad','onUnload','onResize') as $what) 104 { 105 if (!empty($this->body[$what])) 106 { 107 $js .= ' '.$what.'="' . str_replace(array('"','\\'),array('\\"','\\\\'),$this->body[$what]) . '"'; 108 } 109 } 110 return $js; 111 } 112 113 /** 114 * Returns the javascript required for displaying a confirmation message box 115 * 116 * @param string $msg the message to be displayed to user 117 * @returns string the javascript to be used for displaying the message 118 */ 119 function get_confirm($msg) 120 { 121 return 'return confirm("'.lang($msg).'");'; 122 } 123 124 /** 125 * Used for generating the list of external js files to be included in the head of a page 126 * 127 * NOTE: This method should only be called by the template class. 128 * The validation is done when the file is added so we don't have to worry now 129 * 130 * @returns string the html needed for importing the js into a page 131 */ 132 function get_script_links() 133 { 134 $links = ''; 135 $links_api = ''; 136 $links_api_main = ''; 137 138 if(!empty($this->files) && is_array($this->files)) 139 { 140 $links_head = "<!--JS Imports from phpGW javascript class -->\n"; 141 foreach($this->files as $app => $packages) 142 { 143 if(!empty($packages) && is_array($packages)) 144 { 145 foreach($packages as $pkg => $files) 146 { 147 if(!empty($files) && is_array($files)) 148 { 149 foreach($files as $file => $browser) 150 { 151 if ($app === 'phpgwapi' && $pkg === 'jsapi' && $file === 'jsapi') 152 { 153 $links_api_main = '<script type="text/javascript" src="' 154 . $GLOBALS['egw_info']['server']['webserver_url'] 155 . "/$app/js/$pkg/$browser/$file" . '.js">' 156 . "</script>\n"; 157 158 if ($browser !== '.') 159 { 160 $links_api_main = '<script type="text/javascript" src="' 161 . $GLOBALS['egw_info']['server']['webserver_url'] 162 . "/$app/js/$pkg/$file" . '.js">' 163 . "</script>\n"; 164 } 165 } 166 else if ($app === 'phpgwapi') 167 { 168 if ($browser !== '.') 169 { 170 $links_api .= '<script type="text/javascript" src="' 171 . $GLOBALS['egw_info']['server']['webserver_url'] 172 . "/$app/js/$pkg/$file" . '.js">' 173 . "</script>\n"; 174 } 175 176 $links_api .= '<script type="text/javascript" src="' 177 . $GLOBALS['egw_info']['server']['webserver_url'] 178 . "/$app/js/$pkg/$browser/$file" . '.js">' 179 . "</script>\n"; 180 } 181 else 182 { 183 if ($browser !== '.') 184 { 185 $links .= '<script type="text/javascript" src="' 186 . $GLOBALS['egw_info']['server']['webserver_url'] 187 . "/$app/js/$pkg/$file" . '.js">' 188 . "</script>\n"; 189 } 190 191 $links .= '<script type="text/javascript" src="' 192 . $GLOBALS['egw_info']['server']['webserver_url'] 193 . "/$app/js/$pkg/$browser/$file" . '.js">' 194 . "</script>\n"; 195 } 196 } 197 } 198 } 199 } 200 } 201 } 202 return $links_head.$links_api_main.$links_api.$links; 203 } 204 205 /** 206 * Sets an onLoad action for a page 207 * 208 * @param string javascript to be used 209 */ 210 function set_onload($code) 211 { 212 $this->body['onLoad'] .= $code; 213 } 214 215 /** 216 * Sets an onUnload action for a page 217 * 218 * @param string javascript to be used 219 */ 220 function set_onunload($code) 221 { 222 $this->body['onUnload'] .= $code; 223 } 224 225 /** 226 * Sets an onResize action for a page 227 * 228 * @param string javascript to be used 229 */ 230 function set_onresize($code) 231 { 232 $this->body['onResize'] .= $code; 233 } 234 235 /** 236 * DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES!!!! 237 * Used for removing a file or package of files to be included in the head section of a page 238 * 239 * @param string $app application to use 240 * @param string $package the name of the package to be removed 241 * @param string $file the name of a file in the package to be removed - if ommitted package is removed 242 */ 243 function unset_script_link($app, $package, $file=False) 244 { 245 /* THIS DOES NOTHING ATM :P 246 if($file !== False) 247 { 248 unset($this->files[$app][$package][$file]); 249 } 250 else 251 { 252 unset($this->files[$app][$package]); 253 } 254 */ 255 } 256 257 /** 258 * Checks to make sure a valid package and file name is provided 259 * 260 * @param string $package package to be included 261 * @param string $file file to be included - no ".js" on the end 262 * @param string $app application directory to search - default = phpgwapi 263 * @param bool $browser insert specific browser javascript. 264 * 265 * @discuss The browser specific option loads the file which is in the correct 266 * browser folder. Supported folder are those supported by class.browser.inc.php 267 * 268 * @returns bool was the file found? 269 */ 270 function validate_file($package, $file, $app='phpgwapi', $browser=true) 271 { 272 if ($browser) 273 { 274 $browser_folder = strtolower(ExecMethod('phpgwapi.browser.get_agent')); 275 } 276 else 277 { 278 $browser_folder = '.'; 279 } 280 281 if ($this->included_files[$app][$package][$file]) return true; 282 283 if(is_readable(EGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $package . SEP . $browser_folder . SEP . $file . '.js')) 284 { 285 $this->files[$app][$package][$file] = $browser_folder; 286 $this->included_files[$app][$package][$file] = $browser_folder; 287 return True; 288 } 289 elseif (is_readable(EGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $package . SEP . $file . '.js')) 290 { 291 $this->files[$app][$package][$file] = '.'; 292 $this->included_files[$app][$package][$file] = '.'; 293 return True; 294 } 295 elseif($app != 'phpgwapi') 296 { 297 if(is_readable(EGW_INCLUDE_ROOT .SEP .'phpgwapi' .SEP .'js' .SEP . $package .SEP . $browser_folder . SEP . $file . '.js')) 298 { 299 $this->files['phpgwapi'][$package][$file] = $browser_folder; 300 $this->included_files['phpgwapi'][$package][$file] = $browser_folder; 301 return True; 302 } 303 elseif(is_readable(EGW_INCLUDE_ROOT .SEP .'phpgwapi' .SEP .'js' .SEP . $package .SEP . $file . '.js')) 304 { 305 $this->files['phpgwapi'][$package][$file] = '.'; 306 $this->included_files['phpgwapi'][$package][$file] = '.'; 307 return True; 308 } 309 return False; 310 } 311 } 312 313 function validate_jsapi() 314 { 315 if (EGW_UNCOMPRESSED_THYAPI) 316 { 317 $this->validate_file('plugins', 'thyPlugins'); 318 } 319 320 /* This was included together with javascript globals to garantee prior load of dynapi. But it doesn't seems 321 * right to me... maybe on class common, it should load dynapi before everything... */ 322 $this->validate_file('dynapi','dynapi'); 323 324 // Initialize DynAPI 325 $GLOBALS['egw_info']['flags']['java_script'] .= '<script language="javascript">'."\n"; 326 $GLOBALS['egw_info']['flags']['java_script'] .= "dynapi.library.setPath(GLOBALS['serverRoot']+'/phpgwapi/js/dynapi/')\n"; 327 $GLOBALS['egw_info']['flags']['java_script'] .= "dynapi.library.include('dynapi.library')\n"; 328 $GLOBALS['egw_info']['flags']['java_script'] .= "dynapi.library.include('dynapi.api')\n"; 329 $GLOBALS['egw_info']['flags']['java_script'] .= "</script>\n";/**/ 330 331 //FIXME: These files are temporary! They should be included inside DynAPI or substituted by 332 // other ones 333 $this->validate_file('jsapi', 'jsapi'); 334 $this->validate_file('wz_dragdrop', 'wz_dragdrop'); 335 $this->validate_file('dJSWin', 'dJSWin'); 336 $this->validate_file('dTabs', 'dTabs'); 337 $this->validate_file('connector', 'connector'); 338 $this->validate_file('xmlrpcMsgCreator','xmlrpc'); 339 $this->validate_file('jsolait','init'); 340 return true; 341 } 342 343 function get_javascript_globals() 344 { 345 /* Default Global Messages */ 346 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['parseError'] = lang('Failed to Contact Server or Invalid Response from Server. Try to relogin. Contact Admin in case of faliure.'); 347 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['serverTimeout'] = lang('Could not contact server. Operation Timed Out!'); 348 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['dataSourceStartup'] = lang('Starting Up...'); 349 350 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['connector_1'] = lang('Contacting Server...'); 351 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['connector_2'] = lang('Server Contacted. Waiting for response...'); 352 $GLOBALS['egw_info']['flags']['java_script_globals']['messages']['jsapi']['connector_3'] = lang('Server answered. Processing response...'); 353 $GLOBALS['egw_info']['flags']['java_script_globals']['preferences']['common'] =& $GLOBALS['egw_info']['user']['preferences']['common']; 354 355 /* Default Global API Variables */ 356 $browser = strtolower(ExecMethod('phpgwapi.browser.get_agent')); 357 switch ($browser) 358 { 359 case 'ie': 360 case 'opera': 361 $thyapi_comp = 'thyapi_comp_'.$browser.'.js'; 362 break; 363 default: 364 $thyapi_comp = 'thyapi_comp_gecko.js'; 365 } 366 367 $GLOBALS['egw_info']['flags']['java_script_globals']['jsapi']['imgDir'] = $GLOBALS['egw_info']['server']['webserver_url'].'/'.'phpgwapi/images'; 368 if (EGW_UNCOMPRESSED_THYAPI) 369 { 370 $jsCode = "<!-- JS Global Variables and ThyAPI Insertion -->\n" . 371 '<script type="text/javascript" src="'.($GLOBALS['egw_info']['server']['webserver_url'] ? $GLOBALS['egw_info']['server']['webserver_url'].'/' : '/'). 372 '/phpgwapi/js/dynapi/dynapi.js"></script>'."\n". 373 '<script language="javascript">'."\n". 374 'var GLOBALS = new Object();'."\n" . 375 "GLOBALS['serverRoot'] = '".($GLOBALS['egw_info']['server']['webserver_url'] ? $GLOBALS['egw_info']['server']['webserver_url'].'/' : '/')."';\n". 376 "GLOBALS['appname'] = '".$GLOBALS['egw_info']['flags']['currentapp']."';\n". 377 378 "GLOBALS['httpProtocol'] = '".($_SERVER['HTTPS'] ? 'https://' : 'http://')."';\n"; 379 } 380 else 381 { 382 $jsCode = "<!-- JS Global Variables and ThyAPI Insertion -->\n" . 383 '<script type="text/javascript" src="'.($GLOBALS['egw_info']['server']['webserver_url'] ? $GLOBALS['egw_info']['server']['webserver_url'].'/' : '/'). 384 '/phpgwapi/js/'.$thyapi_comp.'"></script>'."\n". 385 '<script language="javascript">'."\n". 386 'var GLOBALS = new Object();'."\n" . 387 "GLOBALS['serverRoot'] = '".($GLOBALS['egw_info']['server']['webserver_url'] ? $GLOBALS['egw_info']['server']['webserver_url'].'/' : '/')."';\n". 388 "GLOBALS['appname'] = '".$GLOBALS['egw_info']['flags']['currentapp']."';\n". 389 390 "GLOBALS['httpProtocol'] = '".($_SERVER['HTTPS'] ? 'https://' : 'http://')."';\n"; 391 } 392 393 if ($GLOBALS['egw_info']['extra_get_vars']) 394 { 395 $GLOBALS['egw_info']['flags']['java_script_globals']['extra_get_vars'] = $GLOBALS['egw_info']['extra_get_vars']; 396 } 397 398 $jsCode .= $this->convert_phparray_jsarray("GLOBALS", $GLOBALS['egw_info']['flags']['java_script_globals'], false); 399 400 if (EGW_UNCOMPRESSED_THYAPI) 401 { 402 $jsCode .= "\ndynapi.library.setPath(GLOBALS['serverRoot']+'/phpgwapi/js/dynapi/');\n". 403 "dynapi.library.include('dynapi.library');\n". 404 "dynapi.library.include('dynapi.api');\n\n"; 405 } 406 407 // Enable Debug? 408 $config =& CreateObject('phpgwapi.config', 'phpgwapi'); 409 $config_values = $config->read_repository(); 410 411 if ($config_values['js_debug']) 412 { 413 $jsCode .= "if (dynapi.ua.gecko) dynapi.library.include('dynapi.debug')\n"; 414 } 415 416 $jsCode .= '</script>'."\n"; 417 418 return $jsCode; 419 } 420 421 function convert_phparray_jsarray($name, $array, $new=true) 422 { 423 if (!is_array($array)) 424 { 425 return ''; 426 } 427 428 if ($new) 429 { 430 $jsCode = "$name = new Object();\n"; 431 } 432 else 433 { 434 $jsCode = ''; 435 } 436 437 foreach ($array as $index => $value) 438 { 439 if (is_array($value)) 440 { 441 $jsCode .= $name."['".$index."'] = new Object();\n"; 442 $jsCode .= $this->convert_phparray_jsarray($name."['".$index."']", $value,false); 443 continue; 444 } 445 446 switch(gettype($value)) 447 { 448 case 'string': 449 $value = "'".str_replace(array("\n","\r"),'\n',addslashes($value))."'"; 450 break; 451 452 case 'boolean': 453 if ($value) 454 { 455 $value = 'true'; 456 } 457 else 458 { 459 $value = 'false'; 460 } 461 break; 462 463 default: 464 $value = 'null'; 465 } 466 467 $jsCode .= $name."['".$index."'] = ".$value.";\n"; 468 } 469 470 return $jsCode; 471 } 472 } 473 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |