* if(!@is_object($GLOBALS['egw']->js))
* {
* $GLOBALS['egw']->js =& CreateObject('phpgwapi.javascript');
* }
*
*
* This way a theme can see if this is a defined object and include the data,
* while the is_object() wrapper prevents whiping out existing data held in
* 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 © 2003 Free Software Foundation
* @license GPL
* @uses template
*
*/
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;
/**
* @var boolean Load JS API ?
*/
var $js_api;
/**
* Constructor
*
* Initialize the instance variables
*/
function javascript()
{
//$this->t =& CreateObject('phpgwapi.Template', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir','phpgwapi'));
}
/**
* 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)
{
return 'return alert("'.lang($msg).'");';
}
/**
* Adds on(Un)Load= attributes to the body tag of a page
*
* @returns string the attributes to be used
*/
function get_body_attribs()
{
$js = '';
foreach(array('onLoad','onUnload','onResize') as $what)
{
if (!empty($this->body[$what]))
{
$js .= ' '.$what.'="' . str_replace(array('\\\'','"','\\','''),array(''','\\"','\\\\','\\\''),$this->body[$what]) . '"';
}
}
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).'");';
}
/**
* 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()
{
$links = "\n";
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))
{
foreach($files as $file => $browser)
{
$pkg = $pkg == '.' ? '' : $pkg.'/';
$browser = $browser == '.' ? '' : $browser.'/';
$f = "/$app/js/$pkg$browser$file" . '.js?'. filectime(EGW_INCLUDE_ROOT."/$app/js/$pkg$browser$file.js") .'">';
$links .= '\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;
}
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...');
$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';
}
$GLOBALS['egw_info']['flags']['java_script_globals']['jsapi']['imgDir'] = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/images';
if (EGW_UNCOMPRESSED_THYAPI)
{
$jsCode = "\n" .
''."\n".
''."\n".
''."\n";
return $jsCode;
}
function convert_phparray_jsarray($name, $array, $new=true)
{
if (!is_array($array))
{
return '';
}
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';
}
$jsCode .= $name."['".$index."'] = ".$value.";\n";
}
return $jsCode;
}
}
?>