diff --git a/phpgwapi/inc/class.egw_framework.inc.php b/phpgwapi/inc/class.egw_framework.inc.php index 9935f48d18..8413b9ba99 100644 --- a/phpgwapi/inc/class.egw_framework.inc.php +++ b/phpgwapi/inc/class.egw_framework.inc.php @@ -1233,6 +1233,40 @@ abstract class egw_framework $response->includeScript($GLOBALS['egw_info']['server']['webserver_url'].$path); } } + + /** + * Set a preference via ajax + * + * User either need run rights for preference app, or setting of preference will be silently ignored! + * + * @param string $app + * @param string $name + * @param string $value + */ + public static function ajax_set_preference($app, $name, $value) + { + //error_log(__METHOD__."('$app', '$name', '$value')"); + if ($GLOBALS['egw_info']['user']['apps']['preferences']) + { + $GLOBALS['egw']->preferences->read_repository(); + $GLOBALS['egw']->preferences->change($app, $name, $value); + $GLOBALS['egw']->preferences->save_repository(True); + } + } + + /** + * Get preferences of a certain application via ajax + * + * @param string $app + */ + public static function ajax_get_preference($app) + { + if (preg_match('/^[a-z0-9_]+$/i', $app)) + { + $response = egw_json_response::get(); + $response->script('window.egw.set_preferences('.json_encode($GLOBALS['egw_info']['user']['preferences'][$app]).', "'.$app.'");'); + } + } } // Init all static variables diff --git a/phpgwapi/inc/class.egw_json.inc.php b/phpgwapi/inc/class.egw_json.inc.php index e64337d4eb..a633cb3a05 100644 --- a/phpgwapi/inc/class.egw_json.inc.php +++ b/phpgwapi/inc/class.egw_json.inc.php @@ -17,7 +17,7 @@ class egw_json_request { private static $_hadJSONRequest = false; - + public static function isJSONRequest() { return self::$_hadJSONRequest; @@ -34,7 +34,7 @@ class egw_json_request { // Remember that we currently are in a JSON request - e.g. used in the redirect code self::$_hadJSONRequest = true; - + if (empty($input_data)) { $this->handleRequest($menuaction, array()); @@ -109,6 +109,13 @@ class egw_json_request case 'template': $menuaction = $appName.'.'.$className.'.'.$functionName; list($template) = explode('_', $className); + if ($className == 'egw_framework') // allow to use egw_framework to call framework / template used by user + { + $template = $GLOBALS['egw_info']['user']['preferences']['common']['template_set']; + if (empty($template)) $template = 'idots'; + $className = $template.'_framework'; + //error_log(__METHOD__."('$menuaction', ".array($parameters).") --> template='$template', className='$className'"); + } break; } @@ -133,7 +140,7 @@ class egw_json_request $ajaxClass = CreateObject($appName.'.'.$className); } - // for Ajax: no need to load the "standard" javascript files, + // for Ajax: no need to load the "standard" javascript files, // they are already loaded, in fact jquery has a problem if loaded twice egw_framework::js_files(array()); @@ -189,7 +196,7 @@ class egw_json_response } return self::$response; } - + public static function isJSONResponse() { return isset(self::$response); @@ -449,7 +456,7 @@ class egw_json_response 'params' => $params ); } - + /** * Destructor */ diff --git a/phpgwapi/js/jsapi/egw.js b/phpgwapi/js/jsapi/egw.js index 95aed28c77..cdd7c8f13c 100644 --- a/phpgwapi/js/jsapi/egw.js +++ b/phpgwapi/js/jsapi/egw.js @@ -72,9 +72,12 @@ else /** * Query an EGroupware user preference * + * If a prefernce is not already loaded (only done for "common" by default), it is synchroniosly queryed from the server! + * * @param string _name name of the preference, eg. 'dateformat' * @param string _app='common' * @return string preference value + * @todo add a callback to query it asynchron */ preference: function(_name, _app) { @@ -82,11 +85,30 @@ else if (typeof this.prefs[_app] == 'undefined') { - throw 'Prefs for application "'+_app+'" are NOT loaded!'; + xajax_doXMLHTTPsync('home.egw_framework.ajax_get_preference.template', _app); + + if (typeof this.prefs[_app] == 'undefined') this.prefs[_app] = {}; } return this.prefs[_app][_name]; }, + /** + * Set a preference and sends it to the server + * + * Server will silently ignore setting preferences, if user has no right to do so! + * + * @param string _app application name or "common" + * @param string _name name of the pref + * @param string _val value of the pref + */ + set_preference: function(_app, _name, _val) + { + xajax_doXMLHTTP('home.egw_framework.ajax_set_preference.template', _app, _name, _val); + + // update own preference cache, if _app prefs are loaded (dont update otherwise, as it would block loading of other _app prefs!) + if (typeof this.prefs[_app] != 'undefined') this.prefs[_app][_name] = _val; + }, + /** * Translations *