From 5e0ac9b76fb4425332b8dec0ab67f02b109f775e Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 14 Feb 2014 13:48:45 +0000 Subject: [PATCH] moved favorites code to new class egw_favorites --- phpgwapi/inc/class.egw_favorites.inc.php | 195 +++++++++++++++++++++++ phpgwapi/inc/class.egw_framework.inc.php | 158 ++---------------- 2 files changed, 205 insertions(+), 148 deletions(-) create mode 100644 phpgwapi/inc/class.egw_favorites.inc.php diff --git a/phpgwapi/inc/class.egw_favorites.inc.php b/phpgwapi/inc/class.egw_favorites.inc.php new file mode 100644 index 0000000000..c790ed0f61 --- /dev/null +++ b/phpgwapi/inc/class.egw_favorites.inc.php @@ -0,0 +1,195 @@ + + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @package api + * @subpackage framework + * @access public + * @version $Id$ + */ + +/** + * EGroupware favorites service-side: + * + * Favorites are generated on serverside by following code in apps sidebox hook: + * + * display_sidebox($appname, lang('Favorites'), egw_favorites::favorite_list($appname)); + * + * Clientside code resides in: + * - phpgwapi/js/jsapi/app_base.js + * - etemplate/js/et2_widget_favorites.js + * + * Favorites are stored with prefix "favorite_" in app preferences. + */ +class egw_favorites +{ + /** + * Include favorites when generating the page server-side + * + * Use this function in your sidebox (or anywhere else, I suppose) to + * get the favorite list when a nextmatch is _not_ on the page. If + * a nextmatch is on the page, it will update / replace this list. + * + * @param string $app application, needed to find preferences + * @param string $default=null preference name for default favorite, default "nextmatch-$app.index.rows-favorite" + * + * @return array with a single sidebox menu item (array) containing html for favorites + */ + public static function list_favorites($app, $default=null) + { + if(!$app) return ''; + + if (!$default) $default = "nextmatch-$app.index.rows-favorite"; + + // This target is used client-side to find & enable adding new favorites + $target = 'favorite_sidebox_'.$app; + $pref_prefix = 'favorite_'; + $filters = array( + 'blank' => array( + 'name' => lang('No filters'), + // Old + 'filter' => array(), + // New + 'state' => array(), + 'group' => true + ) + ); + $is_admin = $GLOBALS['egw_info']['user']['apps']['admin']; + $html = "'; + + return array( + array( + 'no_lang' => true, + 'text' => $html, + 'link' => false, + 'icon' => false, + ), + ); + } + + /** + * Create or delete a favorite for multiple users + * + * Current user needs to be an admin or it will just do nothing quietly + * + * @param string $app Current application, needed to save preference + * @param string $name Name of the favorite + * @param string $action "add" or "delete" + * @param boolean|int|String $group ID of the group to create the favorite for, or 'all' for all users + * @param array $filters=array() key => value pairs for the filter + * @return boolean Success + */ + public static function set_favorite($app, $name, $action, $group, $filters = array()) + { + // Only use alphanumeric for preference name, so it can be used directly as DOM ID + $name = strip_tags($name); + $pref_name = "favorite_".preg_replace('/[^A-Za-z0-9-_]/','_',$name); + + // older group-favorites have just true as their group and are not deletable, if we dont find correct group + if ($group === true || $group === '1') + { + if (isset($GLOBALS['egw']->preferences->default[$app][$pref_name])) + { + $group = 'all'; + } + else + { + foreach($GLOBALS['egw']->accounts->memberships($GLOBALS['egw_info']['user']['account_id'], true) as $gid) + { + $prefs = new preferences($gid); + $prefs->read_repository(); + if (isset($prefs->user[$app][$pref_name])) + { + $group = $gid; + break; + } + } + } + } + if($group && $GLOBALS['egw_info']['apps']['admin'] && $group !== 'all') + { + $prefs = new preferences(is_numeric($group) ? $group : $GLOBALS['egw_info']['user']['account_id']); + } + else + { + $prefs = $GLOBALS['egw']->preferences; + } + $prefs->read_repository(); + $type = $group === "all" ? "default" : "user"; + //error_log(__METHOD__."('$app', '$name', '$action', ".array2string($group).", ...) pref_name=$pref_name, type=$type"); + if($action == "add") + { + $filters = array( + // This is the name as user entered it, minus tags + 'name' => $name, + 'group' => $group ? $group : false, + 'state' => $filters + ); + $result = $prefs->add($app,$pref_name,$filters,$type); + $pref = $prefs->save_repository(false,$type); + + // Update preferences client side, or it could disappear + egw_json_response::get()->call('egw.set_preferences', (array)$pref[$app], $app); + + egw_json_response::get()->data(isset($result[$app][$pref_name])); + return isset($result[$app][$pref_name]); + } + else if ($action == "delete") + { + $result = $prefs->delete($app,$pref_name, $type); + $pref = $prefs->save_repository(false,$type); + + // Update preferences client side, or it could come back + egw_json_response::get()->call('egw.set_preferences', (array)$pref[$app], $app); + + egw_json_response::get()->data(!isset($result[$app][$pref_name])); + return !isset($result[$app][$pref_name]); + } + } +} diff --git a/phpgwapi/inc/class.egw_framework.inc.php b/phpgwapi/inc/class.egw_framework.inc.php index 263e63c1d6..2f9854a4d2 100644 --- a/phpgwapi/inc/class.egw_framework.inc.php +++ b/phpgwapi/inc/class.egw_framework.inc.php @@ -1896,172 +1896,34 @@ if ($app == 'home') continue; } } - /** + /** * Include favorites when generating the page server-side * - * Use this function in your sidebox (or anywhere else, I suppose) to - * get the favorite list when a nextmatch is _not_ on the page. If - * a nextmatch is on the page, it will update / replace this list. - * * @param string $app application, needed to find preferences * @param string $default=null preference name for default favorite, default "nextmatch-$app.index.rows-favorite" - * + * @deprecated use egw_favorites::favorite_list * @return array with a single sidebox menu item (array) containing html for favorites */ public static function favorite_list($app, $default=null) { - if(!$app) return ''; - - if (!$default) $default = "nextmatch-$app.index.rows-favorite"; - - // This target is used client-side to find & enable adding new favorites - $target = 'favorite_sidebox_'.$app; - $pref_prefix = 'favorite_'; - $filters = array( - 'blank' => array( - 'name' => lang('No filters'), - // Old - 'filter' => array(), - // New - 'state' => array(), - 'group' => true - ) - ); - $is_admin = $GLOBALS['egw_info']['user']['apps']['admin']; - $html = "'; - - return array( - array( - 'no_lang' => true, - 'text' => $html, - 'link' => false, - 'icon' => false, - ), - ); + return egw_favorites::list_favorites($app, $default); } /** * Create or delete a favorite for multiple users * - * Current user needs to be an admin or it will just do nothing quietly - * - * @param $app Current application, needed to save preference - * @param $name String Name of the favorite - * @param $action String add or delete - * @param $group boolean|int|String ID of the group to create the favorite for, or 'all' for all users - * @param $filters Array of key => value pairs for the filter + * Need to be in egw_framework to be called with .template postfix from json.php! * + * @param string $app Current application, needed to save preference + * @param string $name Name of the favorite + * @param string $action "add" or "delete" + * @param boolean|int|string $group ID of the group to create the favorite for, or 'all' for all users + * @param array $filters=array() key => value pairs for the filter * @return boolean Success */ public static function ajax_set_favorite($app, $name, $action, $group, $filters = array()) { - // Only use alphanumeric for preference name, so it can be used directly as DOM ID - $name = strip_tags($name); - $pref_name = "favorite_".preg_replace('/[^A-Za-z0-9-_]/','_',$name); - - // older group-favorites have just true as their group and are not deletable, if we dont find correct group - if ($group === true || $group === '1') - { - if (isset($GLOBALS['egw']->preferences->default[$app][$pref_name])) - { - $group = 'all'; - } - else - { - foreach($GLOBALS['egw']->accounts->memberships($GLOBALS['egw_info']['user']['account_id'], true) as $gid) - { - $prefs = new preferences($gid); - $prefs->read_repository(); - if (isset($prefs->user[$app][$pref_name])) - { - $group = $gid; - break; - } - } - } - } - if($group && $GLOBALS['egw_info']['apps']['admin'] && $group !== 'all') - { - $prefs = new preferences(is_numeric($group) ? $group : $GLOBALS['egw_info']['user']['account_id']); - } - else - { - $prefs = $GLOBALS['egw']->preferences; - } - $prefs->read_repository(); - $type = $group === "all" ? "default" : "user"; - //error_log(__METHOD__."('$app', '$name', '$action', ".array2string($group).", ...) pref_name=$pref_name, type=$type"); - if($action == "add") - { - $filters = array( - // This is the name as user entered it, minus tags - 'name' => $name, - 'group' => $group ? $group : false, - 'state' => $filters - ); - $result = $prefs->add($app,$pref_name,$filters,$type); - $pref = $prefs->save_repository(false,$type); - - // Update preferences client side, or it could disappear - egw_json_response::get()->call('egw.set_preferences', (array)$pref[$app], $app); - - egw_json_response::get()->data(isset($result[$app][$pref_name])); - return isset($result[$app][$pref_name]); - } - else if ($action == "delete") - { - $result = $prefs->delete($app,$pref_name, $type); - $pref = $prefs->save_repository(false,$type); - - // Update preferences client side, or it could come back - egw_json_response::get()->call('egw.set_preferences', (array)$pref[$app], $app); - - egw_json_response::get()->data(!isset($result[$app][$pref_name])); - return !isset($result[$app][$pref_name]); - } + return egw_favorites::set_favorite($app, $name, $action, $group, $filters); } }