fix not always working instantiation for contacts and accounts objects differing from global configured repository

This commit is contained in:
ralf 2022-07-06 21:27:21 +02:00
parent 46b172c88e
commit c151f7786f

View File

@ -57,26 +57,65 @@ class Import
throw new \InvalidArgumentException("Invalid account_import_source='{$GLOBALS['egw_info']['server']['account_import_source']}'!");
}
$class = 'EGroupware\\Api\\Contacts\\'.ucfirst($source);
/** @var Api\Contacts\Ldap $contacts */
$this->contacts = new $class($GLOBALS['egw_info']['server']);
$this->contacts_sql = new Api\Contacts\Sql();
// instantiate contacts object with SQL backend
$this->contacts_sql_frontend = new Api\Contacts();
$this->contacts_sql_frontend->somain = $this->contacts_sql_frontend->so_accounts = $this->contacts_sql;
$this->contacts = ($frontend = self::contactsFactory($source))->so_accounts ?: $frontend->somain;
$this->contacts_sql_frontend = self::contactsFactory('sql');
$this->contacts_sql = $this->contacts_sql_frontend->so_accounts ?: $this->contacts_sql_frontend->somain;
$class = 'EGroupware\\Api\\Accounts\\'.ucfirst($source);
/** @var Api\Accounts\Ldap $accounts */
$this->accounts = new $class($frontend = new Api\Accounts(['account_repository' => $source]+$GLOBALS['egw_info']['server']));
// instantiate accounts backend and frontend for SQL
$this->accounts_sql = new Api\Accounts\Sql();
$this->frontend_sql = new Api\Accounts(['account_repository' => 'sql']+$GLOBALS['egw_info']['server'], $this->accounts_sql);
$this->accounts_sql->setFrontend($this->frontend_sql);
$this->accounts_sql->setContacts($this->contacts_sql_frontend);
$this->accounts = self::accountsFactory($source)->backend;
$this->frontend_sql = self::accountsFactory('sql');
$this->accounts_sql = $this->frontend_sql->backend;
$this->_logger = $logger;
}
/**
* Instantiate accounts object with given accounts-backend
*
* @param string $account_repository backend to use
* @return Api\Accounts
*/
protected static function accountsFactory(string $account_repository)
{
static $cache = [];
if (!isset($cache[$account_repository]))
{
$backup_repo = $GLOBALS['egw_info']['server']['account_repository'];
$GLOBALS['egw_info']['server']['account_repository'] = $account_repository;
$cache[$account_repository] = new Api\Accounts();
$GLOBALS['egw_info']['server']['account_repository'] = $backup_repo;
}
return $cache[$account_repository];
}
/**
* Instantiate contacts object for given accounts backend
*
* @param string $account_repository
* @return Api\Contacts
*/
protected static function contactsFactory(string $account_repository)
{
static $cache = [];
if (!isset($cache[$account_repository]))
{
$backup_repo = $GLOBALS['egw_info']['server']['account_repository'];
$backup_accounts = $GLOBALS['egw']->accounts;
$GLOBALS['egw_info']['server']['account_repository'] = $account_repository;
if ($backup_repo !== $account_repository)
{
$GLOBALS['egw']->accounts = self::accountsFactory($account_repository);
}
$cache[$account_repository] = new Api\Contacts();
$GLOBALS['egw_info']['server']['account_repository'] = $backup_repo;
$GLOBALS['egw']->accounts = $backup_accounts;
}
return $cache[$account_repository];
}
/**
* @param string $message
* @param string $level log-level: "debug", "detail", "info", "error" or "fatal"