New method egw_framework::include_css_js_response to add javascript and

CSS included via egw_framework::validate_file or egw_framework::includeCSS
to an ajax response.
Fixed jscalendar to use that service and sitemgr to include it manually.
This commit is contained in:
Ralf Becker 2010-06-23 23:01:57 +00:00
parent 48bc5c8119
commit 8e98f6cea3
3 changed files with 91 additions and 44 deletions

View File

@ -1,16 +1,17 @@
<?php
/**************************************************************************\
* eGroupWare SiteMgr - Web Content Management *
* http://www.egroupware.org *
* -------------------------------------------- *
* 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 - Calendar planner block for sitemgr
*
* @link http://www.egroupware.org
* @package calendar
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* Calendar day selection for sitemgr
*/
class module_calendar extends Module
{
function module_calendar()
@ -28,12 +29,10 @@ class module_calendar extends Module
function get_content(&$arguments,$properties)
{
if (!is_object($GLOBALS['egw']->jscalendar))
{
$GLOBALS['egw']->jscalendar =& CreateObject('phpgwapi.jscalendar');
}
$date = (int) (strtotime(get_var('date',array('POST','GET'))));
$redirect = $arguments['redirect'] ? $arguments['redirect'] : '#';
return $GLOBALS['egw']->jscalendar->flat($redirect,$date);
return $GLOBALS['egw']->jscalendar->get_javascript().
$GLOBALS['egw']->jscalendar->flat($redirect,$date);
}
}

View File

@ -1048,9 +1048,15 @@ abstract class egw_framework
/**
* 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
* Example call syntax:
* a) egw_framework::validate_file('jscalendar','calendar')
* --> /phpgwapi/js/jscalendar/calendar.js
* b) egw_framework::validate_file('/phpgwapi/inc/calendar-setup.js',array('lang'=>'de'))
* --> /phpgwapi/inc/calendar-setup.js?lang=de
*
* @param string $package package or complete path (relative to EGW_SERVER_ROOT) to be included
* @param string|array $file=null file to be included - no ".js" on the end or array with get params
* @param string $app='phpgwapi' application directory to search - default = phpgwapi
* @param boolean $append=true should the file be added
*
* @discuss The browser specific option loads the file which is in the correct
@ -1058,18 +1064,28 @@ abstract class egw_framework
*
* @returns bool was the file found?
*/
static function validate_file($package, $file, $app='phpgwapi')
static function validate_file($package, $file=null, $app='phpgwapi')
{
//echo "<p>".__METHOD__."($package,$file,$app) --> ".EGW_INCLUDE_ROOT ."/$app/js/$package/$file.js</p>\n";
if (is_readable(EGW_INCLUDE_ROOT.($path="/$app/js/$package/$file.js")) ||
$app != 'phpgwapi' && is_readable(EGW_INCLUDE_ROOT.($path="/phpgwapi/js/$package/$file.js")))
if ($package[0] == '/' && is_readable(EGW_SERVER_ROOT.($path = $package)) ||
is_readable(EGW_SERVER_ROOT.($path="/$app/js/$package/$file.js")) ||
$app != 'phpgwapi' && is_readable(EGW_SERVER_ROOT.($path="/phpgwapi/js/$package/$file.js")))
{
if (is_array($file))
{
foreach($file as $name => $val)
{
$args .= (empty($args) ? '?' : '&').$name.'='.urlencode($val);
}
$path .= $args;
}
if (!self::$js_include_files || !in_array($path,self::$js_include_files))
{
self::$js_include_files[] = $path;
}
return True;
}
error_log(__METHOD__."($package,$file,$app) $path NOT found!");
return False;
}
@ -1103,7 +1119,8 @@ abstract class egw_framework
{
foreach(self::$js_include_files as $file)
{
$file .= '?'. filectime(EGW_INCLUDE_ROOT.$file);
list($file,$params) = explode('?',$file,2);
$file .= '?'. filectime(EGW_INCLUDE_ROOT.$file).($params ? '&'.$params : '');
$links .= '<script type="text/javascript" src="'. $GLOBALS['egw_info']['server']['webserver_url']. $file.'">'."</script>\n";
}
}
@ -1120,23 +1137,27 @@ abstract class egw_framework
/**
* Include a css file, either speicified by it's path (relative to EGW_SERVER_ROOT) or appname and css file name
*
* @param string $path path (relative to EGW_SERVER_ROOT) or appname (if !is_null($name))
* @param string $app path (relative to EGW_SERVER_ROOT) or appname (if !is_null($name))
* @param string $name=null name of css file in $app/templates/{default|$this->template}/$name.css
* @return boolean false: css file not found, true: file found
*/
public static function includeCSS($path,$name=null)
public static function includeCSS($app,$name=null)
{
if (!is_null($name))
{
$app = basename($path);
$path = '/'.$app.'/templates/'.$GLOBALS['egw_info']['server']['template_set'].'/'.$name.'.css';
if (!file_exists(EGW_SERVER_ROOT.$path))
{
$path = '/'.$app.'/templates/default/'.$name.'.css';
}
}
else
{
$path = $app;
}
if (!file_exists(EGW_SERVER_ROOT.$path))
{
error_log(__METHOD__."($app,$name) $path NOT found!");
return false;
}
if (!in_array($path,self::$css_include_files))
@ -1145,6 +1166,33 @@ abstract class egw_framework
}
return true;
}
/**
* Add registered CSS and javascript to ajax response
*/
public static function include_css_js_response()
{
$response = egw_json_response::get();
$app = $GLOBALS['egw_info']['flags']['currentapp'];
// try to add app specific css file
self::includeCSS($app,'app');
// add all css files from egw_framework::includeCSS()
foreach(self::$css_include_files as $path)
{
$response->includeCSS($GLOBALS['egw_info']['server']['webserver_url'].$path);
}
// try to add app specific js file
self::validate_file('.', 'app', $app);
// add all js files from egw_framework::validate_file()
foreach(self::$js_include_files as $path)
{
$response->includeScript($GLOBALS['egw_info']['server']['webserver_url'].$path);
}
}
}
/**

View File

@ -51,24 +51,29 @@ class jscalendar
{
$args['app'] = 'home'; // home can be granted to anyone.
}
if ($do_header && (strpos($GLOBALS['egw_info']['flags']['java_script'],'jscalendar')===false))
if ($do_header)
{
$GLOBALS['egw_info']['flags']['java_script'] .= $this->get_javascript();
egw_framework::includeCSS('/phpgwapi/js/jscalendar/calendar-blue.css');
egw_framework::validate_file('jscalendar','calendar');
$args = array_intersect_key($GLOBALS['egw_info']['user']['preferences']['common'],array('lang'=>1,'dateformat'=>1));
egw_framework::validate_file('/phpgwapi/inc/jscalendar-setup.php',$args);
}
}
/**
/**
* return javascript needed for jscalendar
*
* @return string
*/
function get_javascript()
{
$args = array_intersect_key($GLOBALS['egw_info']['user']['preferences']['common'],array('lang'=>1,'dateformat'=>1));
return
'<link rel="stylesheet" type="text/css" media="all" href="'.$this->jscalendar_url.'/calendar-blue.css" title="blue" />
<script type="text/javascript" src="'.$this->jscalendar_url.'/calendar.js"></script>
<script type="text/javascript" src="'.egw::link('/phpgwapi/inc/jscalendar-setup.php',$args,false).'"></script>
*
* Only needed if jscalendar runs outside of egw_framework, eg. in sitemgr
*
* @return string
*/
function get_javascript()
{
$args = array_intersect_key($GLOBALS['egw_info']['user']['preferences']['common'],array('lang'=>1,'dateformat'=>1));
return
'<link rel="stylesheet" type="text/css" media="all" href="'.$this->jscalendar_url.'/calendar-blue.css" title="blue" />
<script type="text/javascript" src="'.$this->jscalendar_url.'/calendar.js"></script>
<script type="text/javascript" src="'.egw::link('/phpgwapi/inc/jscalendar-setup.php',$args,false).'"></script>
';
}
@ -161,10 +166,6 @@ class jscalendar
*/
function flat($url,$date=null,$weekUrl='',$weekTTip='',$monthUrl='',$monthTTip='',$id='calendar-container')
{
if (strpos($GLOBALS['egw_info']['flags']['java_script'],'jscalendar') === false)
{
$javascript = $this->get_javascript();
}
if ($date) // string if format YYYYmmdd or timestamp
{
$date = is_int($date) ? adodb_date('m/d/Y',$date) :
@ -172,7 +173,6 @@ class jscalendar
}
return '
<div id="'.$id.'"></div>
'.$javascript.'
<script type="text/javascript">
function dateChanged(calendar) {
'. // Beware that this function is called even if the end-user only