Use a single client-side cache for account selection so we don't have to send it for every widget

This commit is contained in:
Nathan Gray 2014-04-23 19:00:59 +00:00
parent 857ed0e752
commit 48fa6611f3
4 changed files with 76 additions and 99 deletions

View File

@ -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)

View File

@ -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')
{

View File

@ -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

View File

@ -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;
}
};