Skip to content

Commit

Permalink
IMP_Contacts now created by an injector factory
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Jul 28, 2014
1 parent 5b932e8 commit b7a4b47
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 43 deletions.
12 changes: 9 additions & 3 deletions imp/config/prefs.php
Expand Up @@ -889,7 +889,7 @@
'column' => _("Compose"),
'label' => _("Address Books"),
'desc' => _("Select address book sources for adding/searching."),
'members' => array(
'members' => array(
'save_recipients', 'display_contact', 'sourceselect', 'add_source'
),
'suppress' => function() {
Expand Down Expand Up @@ -929,7 +929,10 @@
// You can provide default values this way:
// 'value' => json_encode(array('source_one', 'source_two'))
$_prefs['search_sources'] = array(
'value' => ''
'value' => '',
'on_change' => function() {
$GLOBALS['injector']->getInstance('IMP_Contacts')->clearCache();
}
);

// Field(s) to use when expanding addresses
Expand All @@ -944,7 +947,10 @@
// will search the fields 'field_one' and 'field_two' in source_one and
// 'field_three' in source_two.
$_prefs['search_fields'] = array(
'value' => ''
'value' => '',
'on_change' => function() {
$GLOBALS['injector']->getInstance('IMP_Contacts')->clearCache();
}
);

// If NOT using shared address books in Turba, you can put a $cfgSources array
Expand Down
6 changes: 3 additions & 3 deletions imp/lib/Ajax/Imple/ContactAutoCompleter.php
Expand Up @@ -46,11 +46,11 @@ protected function _getAutoCompleterParams()
*/
protected function _getAddressbookSearchParams()
{
$params = $GLOBALS['injector']->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$contacts = $GLOBALS['injector']->getInstance('IMP_Contacts');

$ob = new stdClass;
$ob->fields = $params['fields'];
$ob->sources = $params['sources'];
$ob->fields = $contacts->fields;
$ob->sources = $contacts->sources;

return $ob;
}
Expand Down
1 change: 1 addition & 0 deletions imp/lib/Application.php
Expand Up @@ -97,6 +97,7 @@ protected function _bootstrap()
/* Add IMP-specific factories. */
$factories = array(
'IMP_AuthImap' => 'IMP_Factory_AuthImap',
'IMP_Contacts' => 'IMP_Factory_Contacts',
'IMP_Crypt_Pgp' => 'IMP_Factory_Pgp',
'IMP_Crypt_Smime' => 'IMP_Factory_Smime',
'IMP_Flags' => 'IMP_Factory_Flags',
Expand Down
7 changes: 4 additions & 3 deletions imp/lib/Basic/Contacts.php
Expand Up @@ -43,8 +43,9 @@ protected function _init()
throw $e;
}

/* Get the lists of address books through the API. */
$source_list = $registry->call('contacts/sources');
/* Get the lists of address books. */
$contacts = $injector->getInstance('IMP_Contacts');
$source_list = $contacts->source_list;

/* If we self-submitted, use that source. Otherwise, choose a good
* source. */
Expand All @@ -55,7 +56,7 @@ protected function _init()
}

if ($this->vars->searched || $prefs->getValue('display_contact')) {
$a_list = iterator_to_array($injector->getInstance('IMP_Contacts')->searchEmail($this->vars->get('search', ''), array(
$a_list = iterator_to_array($contacts->searchEmail($this->vars->get('search', ''), array(
'sources' => array($this->vars->source)
)));
} else {
Expand Down
104 changes: 91 additions & 13 deletions imp/lib/Contacts.php
Expand Up @@ -19,9 +19,72 @@
* @copyright 2012-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*
* @property-read boolean $changed Has the internal data changed?
* @property-read array $fields The list of configured search fields.
* @property-read array $sources The list of configured sources.
* @property-read array $source_list The list of sources in the contacts
* backend.
*/
class IMP_Contacts
class IMP_Contacts implements Serializable
{
/**
* Has the internal data changed?
*
* @var boolean
*/
private $_changed = false;

/**
* The list of search fields.
*
* @var array
*/
private $_fields;

/**
* The list of sources.
*
* @var array
*/
private $_sources;

/**
*/
public function __get($name)
{
global $registry;

switch ($name) {
case 'changed':
return $this->_changed;

case 'fields':
case 'sources':
if (!isset($this->_fields)) {
$this->_init();
}
return $this->_{$name};

case 'source_list':
if ($registry->hasMethod('contacts/sources')) {
try {
return $registry->call('contacts/sources');
} catch (Horde_Exception $e) {}
}
return array();
}
}

/**
* Clear cached contacts data.
*/
public function clearCache()
{
unset($this->_fields, $this->_sources);
$this->_changed = true;
}

/**
* Adds a contact to the user defined address book.
*
Expand Down Expand Up @@ -74,15 +137,13 @@ public function searchEmail($str, array $opts = array())
return new Horde_Mail_Rfc822_List();
}

$spref = $this->getAddressbookSearchParams();

try {
$search = $registry->call('contacts/search', array($str, array(
'customStrict' => empty($opts['email_exact']) ? array() : array('email'),
'fields' => $spref['fields'],
'fields' => $this->fields,
'returnFields' => array('email', 'name'),
'rfc822Return' => true,
'sources' => empty($opts['sources']) ? $spref['sources'] : $opts['sources']
'sources' => empty($opts['sources']) ? $this->sources : $opts['sources']
)));
} catch (Horde_Exception $e) {
Horde::log($e, 'ERR');
Expand All @@ -103,21 +164,38 @@ public function searchEmail($str, array $opts = array())
}

/**
* Determines parameters needed to do an address search
*
* @return array An array with two keys: 'fields' and 'sources'.
* Initializes parameters needed to do an address search.
*/
public function getAddressbookSearchParams()
private function _init()
{
global $prefs;

$fields = json_decode($prefs->getValue('search_fields'), true);
$src = json_decode($prefs->getValue('search_sources'));

return array(
'fields' => empty($fields) ? array() : $fields,
'sources' => empty($src) ? array() : $src
);
$this->_fields = empty($fields) ? array() : $fields;
$this->_sources = empty($src) ? array() : $src;

$this->_changed = true;
}

/* Serializable methods. */

/**
*/
public function serialize()
{
return json_encode(array(
$this->_fields,
$this->_sources
));
}

/**
*/
public function unserialize($data)
{
list($this->_fields, $this->_sources) = json_decode($data, true);
}

}
6 changes: 3 additions & 3 deletions imp/lib/Contacts/Avatar/Addressbook.php
Expand Up @@ -29,16 +29,16 @@ public function avatarImg($email)
global $injector, $registry;

if ($registry->hasMethod('contacts/search')) {
$sparams = $injector->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$contacts = $injector->getInstance('IMP_Contacts');

try {
$res = $registry->call('contacts/search', array(
$email,
array(
'customStrict' => array('email'),
'fields' => array_fill_keys($sparams['sources'], array('email')),
'fields' => array_fill_keys($contacts->sources, array('email')),
'returnFields' => array('photo', 'phototype'),
'sources' => $sparams['sources']
'sources' => $contacts->sources
)
));

Expand Down
28 changes: 21 additions & 7 deletions imp/lib/Crypt/Pgp.php
Expand Up @@ -225,10 +225,18 @@ public function getPublicKey($address, $options = array())
} catch (Horde_Exception_HookNotSet $e) {}

/* Try retrieving by e-mail only first. */
$params = $injector->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$result = null;
try {
$result = $registry->call('contacts/getField', array($address, self::PUBKEY_FIELD, $params['sources'], true, true));
$result = $registry->call(
'contacts/getField',
array(
$address,
self::PUBKEY_FIELD,
$injector->getInstance('IMP_Contacts')->sources,
true,
true
)
);
} catch (Horde_Exception $e) {}

if (is_null($result)) {
Expand Down Expand Up @@ -290,11 +298,11 @@ public function getPublicKey($address, $options = array())
*/
public function listPublicKeys()
{
$params = $GLOBALS['injector']->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$sources = $GLOBALS['injector']->getInstance('IMP_Contacts')->sources;

return empty($params['sources'])
return empty($sources)
? array()
: $GLOBALS['registry']->call('contacts/getAllAttributeValues', array(self::PUBKEY_FIELD, $params['sources']));
: $GLOBALS['registry']->call('contacts/getAllAttributeValues', array(self::PUBKEY_FIELD, $sources));
}

/**
Expand All @@ -306,8 +314,14 @@ public function listPublicKeys()
*/
public function deletePublicKey($email)
{
$params = $GLOBALS['injector']->getInstance('IMP_Contacts')->getAddressbookSearchParams();
return $GLOBALS['registry']->call('contacts/deleteField', array($email, self::PUBKEY_FIELD, $params['sources']));
return $GLOBALS['registry']->call(
'contacts/deleteField',
array(
$email,
self::PUBKEY_FIELD,
$GLOBALS['injector']->getInstance('IMP_Contacts')->sources
)
);
}

/**
Expand Down
21 changes: 14 additions & 7 deletions imp/lib/Crypt/Smime.php
Expand Up @@ -220,10 +220,10 @@ public function getPublicKey($address)
}
} catch (Horde_Exception_HookNotSet $e) {}

$params = $injector->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$contacts = $injector->getInstance('IMP_Contacts');

try {
$key = $registry->call('contacts/getField', array($address, self::PUBKEY_FIELD, $params['sources'], true, true));
$key = $registry->call('contacts/getField', array($address, self::PUBKEY_FIELD, $contacts->sources, true, true));
} catch (Horde_Exception $e) {
/* See if the address points to the user's public key. */
$personal_pubkey = $this->getPersonalPublicKey();
Expand All @@ -249,11 +249,11 @@ public function getPublicKey($address)
*/
public function listPublicKeys()
{
$params = $GLOBALS['injector']->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$sources = $GLOBALS['injector']->getInstance('IMP_Contacts')->sources;

return empty($params['sources'])
return empty($sources)
? array()
: $GLOBALS['registry']->call('contacts/getAllAttributeValues', array(self::PUBKEY_FIELD, $params['sources']));
: $GLOBALS['registry']->call('contacts/getAllAttributeValues', array(self::PUBKEY_FIELD, $sources));
}

/**
Expand All @@ -265,8 +265,15 @@ public function listPublicKeys()
*/
public function deletePublicKey($email)
{
$params = $GLOBALS['injector']->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$GLOBALS['registry']->call('contacts/deleteField', array($email, self::PUBKEY_FIELD, $params['sources']));
$GLOBALS['registry']->call(
'contacts/deleteField',
array(
$email,
self::PUBKEY_FIELD,
$injector->getInstance('IMP_Contacts')->sources,
$params['sources']
)
);
}

/**
Expand Down

0 comments on commit b7a4b47

Please sign in to comment.