From 48fa6611f33fba545433ad0534f8a6bf034602ca Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Wed, 23 Apr 2014 19:00:59 +0000 Subject: [PATCH] Use a single client-side cache for account selection so we don't have to send it for every widget --- .../class.etemplate_widget_menupopup.inc.php | 97 ------------------- etemplate/js/et2_widget_selectAccount.js | 15 ++- phpgwapi/inc/class.egw_framework.inc.php | 22 +++++ phpgwapi/js/jsapi/egw_user.js | 41 ++++++++ 4 files changed, 76 insertions(+), 99 deletions(-) diff --git a/etemplate/inc/class.etemplate_widget_menupopup.inc.php b/etemplate/inc/class.etemplate_widget_menupopup.inc.php index 5e6647df55..36edb81d5f 100644 --- a/etemplate/inc/class.etemplate_widget_menupopup.inc.php +++ b/etemplate/inc/class.etemplate_widget_menupopup.inc.php @@ -511,103 +511,6 @@ class etemplate_widget_menupopup extends etemplate_widget } break; } - // Popup is the only preference option that doesn't need more select options - if($select_pref == 'popup') break; - - // Determine if users or groups (or both) are needed - $groups = in_array($type, array('owngroups','groups','both')) && $select_pref != 'none'; - $accounts = ($type == 'both') || (!$groups && $select_pref != 'none'); - - // If there are group, type or preference based restrictions, find those - if($type == 'owngroups' || $select_pref == 'groupmembers') - { - $owngroups = true; - foreach($GLOBALS['egw']->accounts->membership() as $group) - { - $mygroups[] = $group['account_id']; - } - } - elseif (in_array($type, array('', 'accounts', 'both')) && $select_pref == 'primary_group') - { - $owngroups = true; - $mygroups[] = $GLOBALS['egw_info']['user']['account_primary_group']; - } - - - //error_log("$widget Type $type Pref: $select_pref Groups:".array2string($mygroups)); - $accs = array(); - - // No restrictions, just search for the right type - if(!$mygroups) - { - $accs = $GLOBALS['egw']->accounts->search(array( - 'type' => empty($type) ? 'accounts' : $type, // default is accounts - 'order' => 'account_fullname', // order according to pref of how to display accounts - )); - } - - // Add restricted groups, and expand restricted groups into users if needed - if($owngroups) - { - foreach($mygroups as $group) - { - if($groups) - { - $accs[$group] = $GLOBALS['egw']->accounts->read($group); - } - if($accounts) - { - $accs += $GLOBALS['egw']->accounts->search(array( - 'type' => $group, - 'order' => 'account_fullname', // order according to pref of how to display accounts - )); - } - } - } - - // Primary group and search includes users from primary group, - // and all groups, not just primary group, so add those in - if($groups && $select_pref == 'primary_group') - { - foreach($GLOBALS['egw']->accounts->membership() as $group) - { - $accs[$group['account_id']] = $GLOBALS['egw']->accounts->read($group['account_id']); - } - } - //error_log("$widget Accs: " . implode(',',array_keys($accs))); - - // Go through list of accounts (users & groups) and format them for display - foreach($accs as $acc) - { - if ($acc['account_type'] == 'u' && $accounts) - { - $options[$acc['account_id']] = self::accountInfo($acc['account_id'],$acc,$type2,$type=='both'); - } - } - foreach($accs as $acc) - { - if ($acc['account_type'] == 'g' && $groups) - { - $options[$acc['account_id']] = self::accountInfo($acc['account_id'],$acc,$type2,$type=='both'); - } - //error_log(" {$acc['account_id']}:" . $options[$acc['account_id']]); - } - - // Make sure all values are present, even if not normally sent (according to preferences) - if(is_array($value)) - { - $remaining = array_diff($value, array_keys($options)); - } - if(is_array($remaining)) - { - foreach($remaining as $id) - { - if ($id) // default label will be added on clientside, it doubles, if added here too - { - $options[$id] = self::accountInfo($id,null,$type2,$type=='both'); - } - } - } break; case 'select-year': // options: #rows,#before(default=3),#after(default=2) diff --git a/etemplate/js/et2_widget_selectAccount.js b/etemplate/js/et2_widget_selectAccount.js index c5c4992ba4..6425c41595 100644 --- a/etemplate/js/et2_widget_selectAccount.js +++ b/etemplate/js/et2_widget_selectAccount.js @@ -104,10 +104,21 @@ var et2_selectAccount = et2_selectbox.extend( */ createInputWidget: function() { - this._super.apply(this, arguments); - var type = this.egw().preference('account_selection', 'common'); + switch(type) + { + case 'none': + break; + case 'selectbox': + case 'groupmembers': + default: + jQuery.extend(this.options.select_options, this.egw().accounts(this.options.account_type)); + break; + } + + this._super.apply(this, arguments); + // Add search button if(type == 'primary_group') { diff --git a/phpgwapi/inc/class.egw_framework.inc.php b/phpgwapi/inc/class.egw_framework.inc.php index 7cad8c30fc..3967ef4a84 100644 --- a/phpgwapi/inc/class.egw_framework.inc.php +++ b/phpgwapi/inc/class.egw_framework.inc.php @@ -1991,6 +1991,28 @@ if ($app == 'home') continue; { return egw_favorites::set_favorite($app, $name, $action, $group, $filters); } + + /** + * Get a cachable list of users for the client + * + * The account source takes care of access and filtering according to preference + */ + public static function ajax_user_list() + { + $list = array('accounts' => array(),'groups' => array(), 'owngroups' => array()); + if($GLOBALS['egw_info']['user']['preferences']['common']['account_selection'] == 'primary_group') + { + $list['accounts']['filter']['group'] = $GLOBALS['egw_info']['user']['account_primary_group']; + } + foreach($list as $type => &$accounts) + { + $options = array('account_type' => $type) + $accounts; + $accounts = accounts::link_query('',$options); + } + + egw_json_response::get()->data($list); + return $list; + } } // Init all static variables diff --git a/phpgwapi/js/jsapi/egw_user.js b/phpgwapi/js/jsapi/egw_user.js index 652bc2d744..9c8ceb66f9 100644 --- a/phpgwapi/js/jsapi/egw_user.js +++ b/phpgwapi/js/jsapi/egw_user.js @@ -25,6 +25,17 @@ egw.extend('user', egw.MODULE_GLOBAL, function() */ var userData = {apps: {}}; + /** + * Client side cache of accounts user has access to + * Used by account select widgets + */ + var accountStore = { + // Filled by AJAX when needed + //accounts: {}, + //groups: {}, + //owngroups: {} + }; + return { /** * Set data of current user @@ -63,6 +74,36 @@ egw.extend('user', egw.MODULE_GLOBAL, function() { return typeof _name == 'undefined' || typeof userData.apps[_app] == 'undefined' ? userData.apps[_app] : userData.apps[_app][_name]; + }, + + /** + * Get a list of accounts the user has access to + * The list is filtered by type, one of 'accounts','groups','both', 'owngroups' + * + * @param {string} type + * @returns {Object} + */ + accounts: function(type) + { + if(typeof type == 'undefined') type = 'accounts'; + + var list = {}; + if(jQuery.isEmptyObject(accountStore)) + { + // Synchronous + egw.json('home.egw_framework.ajax_user_list.template',[], + function(data) {accountStore = data||{};} + ).sendRequest(); + } + if(type == 'both') + { + list = jQuery.extend(list, accountStore['accounts'],accountStore['groups']); + } + else + { + list = jQuery.extend(list, accountStore[type]); + } + return list; } };