2003-08-30 10:47:01 +02:00
< ? php
2005-06-18 22:43:14 +02:00
/************************************************************************** \
* eGroupWare API - JavaScript *
* Written by Dave Hall skwashd at phpgroupware . org *
2009-10-12 11:46:36 +02:00
* Copyright ( C ) 2003 Free Software Foundation Inc *
2005-06-18 22:43:14 +02:00
* -------------------------------------------------------------------------*
* This library is part of the eGroupWare API *
2009-10-12 11:46:36 +02:00
* http :// www . egroupware . org / api *
2005-06-18 22:43:14 +02:00
* ------------------------------------------------------------------------ *
* This program is Free Software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
* option ) any later version . *
\ **************************************************************************/
/* $Id$ */
/**
* eGroupWare javascript support class
*
* Only instanstiate this class using :
* < code >
* if ( !@ is_object ( $GLOBALS [ 'egw' ] -> js ))
* {
* $GLOBALS [ 'egw' ] -> js =& CreateObject ( 'phpgwapi.javascript' );
* }
* </ code >
*
* This way a theme can see if this is a defined object and include the data ,
2009-10-12 11:46:36 +02:00
* while the is_object () wrapper prevents whiping out existing data held in
2005-06-18 22:43:14 +02:00
* this instance variables , primarily the $files variable .
*
* Note : The package argument is the subdirectory of js - all js should live in subdirectories
*
* @ package phpgwapi
* @ subpackage sessions
* @ abstract
* @ author Dave Hall
* @ copyright & copy ; 2003 Free Software Foundation
* @ license GPL
* @ uses template
*
*/
2003-08-30 10:47:01 +02:00
class javascript
{
/**
* @ var array elements to be used for the on ( Un ) Load attributes of the body tag
*/
var $body ;
/**
* @ var array list of validated files to be included in the head section of a page
*/
var $files ;
/**
* @ var object used for holding an instance of the Template class
*/
var $t ;
2005-06-18 22:43:14 +02:00
/**
* @ var boolean Load JS API ?
*/
var $js_api ;
2009-10-12 11:46:36 +02:00
2003-08-30 10:47:01 +02:00
/**
* Constructor
*
* Initialize the instance variables
*/
function javascript ()
{
2005-06-18 22:43:14 +02:00
//$this->t =& CreateObject('phpgwapi.Template', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir','phpgwapi'));
2003-08-30 10:47:01 +02:00
}
2009-10-12 11:46:36 +02:00
2003-08-30 10:47:01 +02:00
/**
* Returns the javascript required for displaying a popup message box
*
* @ param string $msg the message to be displayed to user
* @ returns string the javascript to be used for displaying the message
*/
function get_alert ( $msg )
{
2005-06-18 22:43:14 +02:00
return 'return alert("' . lang ( $msg ) . '");' ;
2003-08-30 10:47:01 +02:00
}
/**
* Adds on ( Un ) Load = attributes to the body tag of a page
*
* @ returns string the attributes to be used
*/
function get_body_attribs ()
{
2004-05-09 12:18:56 +02:00
$js = '' ;
foreach ( array ( 'onLoad' , 'onUnload' , 'onResize' ) as $what )
{
if ( ! empty ( $this -> body [ $what ]))
{
2006-10-22 13:35:58 +02:00
$js .= ' ' . $what . '="' . str_replace ( array ( '\\\'' , '"' , '\\' , ''' ), array ( ''' , '\\"' , '\\\\' , '\\\'' ), $this -> body [ $what ]) . '"' ;
2004-05-09 12:18:56 +02:00
}
}
2003-08-30 10:47:01 +02:00
return $js ;
}
/**
* Returns the javascript required for displaying a confirmation message box
*
* @ param string $msg the message to be displayed to user
* @ returns string the javascript to be used for displaying the message
*/
function get_confirm ( $msg )
{
return 'return confirm("' . lang ( $msg ) . '");' ;
}
2009-10-12 11:46:36 +02:00
2003-08-30 10:47:01 +02:00
/**
* Used for generating the list of external js files to be included in the head of a page
*
* NOTE : This method should only be called by the template class .
* The validation is done when the file is added so we don ' t have to worry now
*
* @ returns string the html needed for importing the js into a page
*/
function get_script_links ()
{
2006-10-10 15:39:07 +02:00
$links = " <!--JS Imports from phpGW javascript class --> \n " ;
2003-08-30 10:47:01 +02:00
if ( ! empty ( $this -> files ) && is_array ( $this -> files ))
{
foreach ( $this -> files as $app => $packages )
{
if ( ! empty ( $packages ) && is_array ( $packages ))
{
foreach ( $packages as $pkg => $files )
{
if ( ! empty ( $files ) && is_array ( $files ))
{
2004-11-25 20:41:07 +01:00
foreach ( $files as $file => $browser )
2003-08-30 10:47:01 +02:00
{
2006-10-10 15:39:07 +02:00
$pkg = $pkg == '.' ? '' : $pkg . '/' ;
$browser = $browser == '.' ? '' : $browser . '/' ;
2009-10-12 11:46:36 +02:00
2006-10-10 15:39:07 +02:00
$f = " / $app /js/ $pkg $browser $file " . '.js?' . filectime ( EGW_INCLUDE_ROOT . " / $app /js/ $pkg $browser $file .js " ) . '">' ;
$links .= '<script type="text/javascript" src="' . $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . $f . " </script> \n " ;
2003-08-30 10:47:01 +02:00
}
}
}
}
}
}
2006-10-10 15:39:07 +02:00
return $links ;
2003-08-30 10:47:01 +02:00
}
/**
* Sets an onLoad action for a page
*
* @ param string javascript to be used
*/
function set_onload ( $code )
{
2004-01-08 10:06:15 +01:00
$this -> body [ 'onLoad' ] .= $code ;
2003-08-30 10:47:01 +02:00
}
/**
* Sets an onUnload action for a page
*
* @ param string javascript to be used
*/
function set_onunload ( $code )
{
2004-01-08 10:06:15 +01:00
$this -> body [ 'onUnload' ] .= $code ;
2003-08-30 10:47:01 +02:00
}
2004-05-07 23:18:13 +02:00
/**
* Sets an onResize action for a page
*
* @ param string javascript to be used
*/
function set_onresize ( $code )
{
$this -> body [ 'onResize' ] .= $code ;
}
2003-08-30 10:47:01 +02:00
/**
* DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES !!!!
* Used for removing a file or package of files to be included in the head section of a page
*
* @ param string $app application to use
* @ param string $package the name of the package to be removed
* @ param string $file the name of a file in the package to be removed - if ommitted package is removed
*/
function unset_script_link ( $app , $package , $file = False )
{
/* THIS DOES NOTHING ATM : P
if ( $file !== False )
{
unset ( $this -> files [ $app ][ $package ][ $file ]);
}
else
{
unset ( $this -> files [ $app ][ $package ]);
}
*/
}
/**
* Checks to make sure a valid package and file name is provided
*
* @ param string $package package to be included
* @ param string $file file to be included - no " .js " on the end
* @ param string $app application directory to search - default = phpgwapi
2004-11-25 20:41:07 +01:00
* @ param bool $browser insert specific browser javascript .
2005-06-18 22:43:14 +02:00
*
2004-11-25 20:41:07 +01:00
* @ discuss The browser specific option loads the file which is in the correct
* browser folder . Supported folder are those supported by class . browser . inc . php
*
2003-08-30 10:47:01 +02:00
* @ returns bool was the file found ?
*/
2004-11-25 20:41:07 +01:00
function validate_file ( $package , $file , $app = 'phpgwapi' , $browser = true )
2003-08-30 10:47:01 +02:00
{
2004-11-25 20:41:07 +01:00
if ( $browser )
{
2008-03-21 21:11:59 +01:00
$browser_folder = html :: $user_agent ;
2004-11-25 20:41:07 +01:00
}
else
{
2009-10-12 11:46:36 +02:00
$browser_folder = '.' ;
2004-11-25 20:41:07 +01:00
}
2009-10-12 11:46:36 +02:00
//echo "<p>".__METHOD__."($package,$file,$app,$browser) --> ".EGW_INCLUDE_ROOT ."/$app/js/$package/$browser_folder/$file.js</p>\n";
2005-06-18 22:43:14 +02:00
if ( $this -> included_files [ $app ][ $package ][ $file ]) return true ;
2006-10-10 15:39:07 +02:00
if ( is_readable ( EGW_INCLUDE_ROOT . " / $app /js/ $package / $browser_folder / $file .js " ))
2003-08-30 10:47:01 +02:00
{
2004-11-25 20:41:07 +01:00
$this -> files [ $app ][ $package ][ $file ] = $browser_folder ;
return True ;
}
2006-10-10 15:39:07 +02:00
elseif ( is_readable ( EGW_INCLUDE_ROOT . " / $app /js/ $package / $file .js " ))
2004-11-25 20:41:07 +01:00
{
$this -> files [ $app ][ $package ][ $file ] = '.' ;
2003-08-30 10:47:01 +02:00
return True ;
}
elseif ( $app != 'phpgwapi' )
{
2006-10-10 15:39:07 +02:00
if ( is_readable ( EGW_INCLUDE_ROOT . " /phpgwapi/js/ $package / $browser_folder / $file .js " ))
2004-11-25 20:41:07 +01:00
{
$this -> files [ 'phpgwapi' ][ $package ][ $file ] = $browser_folder ;
return True ;
}
2009-10-12 11:46:36 +02:00
elseif ( is_readable ( EGW_INCLUDE_ROOT . " /phpgwapi/js/ $package / $file .js " ))
2003-08-30 10:47:01 +02:00
{
2004-11-25 20:41:07 +01:00
$this -> files [ 'phpgwapi' ][ $package ][ $file ] = '.' ;
2003-08-30 10:47:01 +02:00
return True ;
}
return False ;
}
}
2005-06-18 22:43:14 +02:00
function validate_jsapi ()
{
if ( EGW_UNCOMPRESSED_THYAPI )
{
$this -> validate_file ( 'plugins' , 'thyPlugins' );
}
/* This was included together with javascript globals to garantee prior load of dynapi . But it doesn ' t seems
* right to me ... maybe on class common , it should load dynapi before everything ... */
$this -> validate_file ( 'dynapi' , 'dynapi' );
// Initialize DynAPI
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= '<script language="javascript">' . " \n " ;
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " dynapi.library.setPath(GLOBALS['serverRoot']+'/phpgwapi/js/dynapi/') \n " ;
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " dynapi.library.include('dynapi.library') \n " ;
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " dynapi.library.include('dynapi.api') \n " ;
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " </script> \n " ; /**/
//FIXME: These files are temporary! They should be included inside DynAPI or substituted by
// other ones
$this -> validate_file ( 'jsapi' , 'jsapi' );
$this -> validate_file ( 'wz_dragdrop' , 'wz_dragdrop' );
$this -> validate_file ( 'dJSWin' , 'dJSWin' );
$this -> validate_file ( 'dTabs' , 'dTabs' );
$this -> validate_file ( 'connector' , 'connector' );
$this -> validate_file ( 'xmlrpcMsgCreator' , 'xmlrpc' );
$this -> validate_file ( 'jsolait' , 'init' );
return true ;
}
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
function get_javascript_globals ()
{
/* Default Global Messages */
$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.' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'messages' ][ 'jsapi' ][ 'serverTimeout' ] = lang ( 'Could not contact server. Operation Timed Out!' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'messages' ][ 'jsapi' ][ 'dataSourceStartup' ] = lang ( 'Starting Up...' );
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'messages' ][ 'jsapi' ][ 'connector_1' ] = lang ( 'Contacting Server...' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'messages' ][ 'jsapi' ][ 'connector_2' ] = lang ( 'Server Contacted. Waiting for response...' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'messages' ][ 'jsapi' ][ 'connector_3' ] = lang ( 'Server answered. Processing response...' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'preferences' ][ 'common' ] =& $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ];
/* Default Global API Variables */
$browser = strtolower ( ExecMethod ( 'phpgwapi.browser.get_agent' ));
switch ( $browser )
{
case 'ie' :
case 'opera' :
$thyapi_comp = 'thyapi_comp_' . $browser . '.js' ;
break ;
default :
$thyapi_comp = 'thyapi_comp_gecko.js' ;
}
2009-06-08 18:21:14 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'jsapi' ][ 'imgDir' ] = $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/phpgwapi/images' ;
2005-06-18 22:43:14 +02:00
if ( EGW_UNCOMPRESSED_THYAPI )
{
$jsCode = " <!-- JS Global Variables and ThyAPI Insertion --> \n " .
'<script type="text/javascript" src="' . ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/' : '/' ) .
'/phpgwapi/js/dynapi/dynapi.js"></script>' . " \n " .
'<script language="javascript">' . " \n " .
'var GLOBALS = new Object();' . " \n " .
" GLOBALS['serverRoot'] = ' " . ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/' : '/' ) . " '; \n " .
" GLOBALS['appname'] = ' " . $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] . " '; \n " .
" GLOBALS['httpProtocol'] = ' " . ( $_SERVER [ 'HTTPS' ] ? 'https://' : 'http://' ) . " '; \n " ;
}
else
{
$jsCode = " <!-- JS Global Variables and ThyAPI Insertion --> \n " .
'<script type="text/javascript" src="' . ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/' : '/' ) .
'/phpgwapi/js/' . $thyapi_comp . '"></script>' . " \n " .
'<script language="javascript">' . " \n " .
'var GLOBALS = new Object();' . " \n " .
" GLOBALS['serverRoot'] = ' " . ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/' : '/' ) . " '; \n " .
" GLOBALS['appname'] = ' " . $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] . " '; \n " .
" GLOBALS['httpProtocol'] = ' " . ( $_SERVER [ 'HTTPS' ] ? 'https://' : 'http://' ) . " '; \n " ;
}
if ( $GLOBALS [ 'egw_info' ][ 'extra_get_vars' ])
{
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ][ 'extra_get_vars' ] = $GLOBALS [ 'egw_info' ][ 'extra_get_vars' ];
}
$jsCode .= $this -> convert_phparray_jsarray ( " GLOBALS " , $GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script_globals' ], false );
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
if ( EGW_UNCOMPRESSED_THYAPI )
{
$jsCode .= " \n dynapi.library.setPath(GLOBALS['serverRoot']+'/phpgwapi/js/dynapi/'); \n " .
" dynapi.library.include('dynapi.library'); \n " .
" dynapi.library.include('dynapi.api'); \n \n " ;
}
// Enable Debug?
$config =& CreateObject ( 'phpgwapi.config' , 'phpgwapi' );
$config_values = $config -> read_repository ();
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
if ( $config_values [ 'js_debug' ])
{
$jsCode .= " if (dynapi.ua.gecko) dynapi.library.include('dynapi.debug') \n " ;
}
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
$jsCode .= '</script>' . " \n " ;
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
return $jsCode ;
}
function convert_phparray_jsarray ( $name , $array , $new = true )
{
if ( ! is_array ( $array ))
{
return '' ;
}
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
if ( $new )
{
$jsCode = " $name = new Object(); \n " ;
}
else
{
$jsCode = '' ;
}
foreach ( $array as $index => $value )
{
if ( is_array ( $value ))
{
$jsCode .= $name . " [' " . $index . " '] = new Object(); \n " ;
$jsCode .= $this -> convert_phparray_jsarray ( $name . " [' " . $index . " '] " , $value , false );
continue ;
}
switch ( gettype ( $value ))
{
case 'string' :
$value = " ' " . str_replace ( array ( " \n " , " \r " ), '\n' , addslashes ( $value )) . " ' " ;
break ;
case 'boolean' :
if ( $value )
{
$value = 'true' ;
}
else
{
$value = 'false' ;
}
break ;
default :
$value = 'null' ;
}
2009-10-12 11:46:36 +02:00
2005-06-18 22:43:14 +02:00
$jsCode .= $name . " [' " . $index . " '] = " . $value . " ; \n " ;
}
return $jsCode ;
}
2003-08-30 10:47:01 +02:00
}
?>