Skip to content

Commit

Permalink
[mms] Add support for showing a contact image in the contacts popup b…
Browse files Browse the repository at this point in the history
…ox in dynamic view.
  • Loading branch information
slusarz committed Oct 4, 2013
1 parent a1ea1cf commit ffce87f
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 10 deletions.
13 changes: 13 additions & 0 deletions imp/config/conf.xml
Expand Up @@ -142,6 +142,19 @@
</configsection>
</configtab>

<configtab name="contactsimage" desc="Contacts Images">
<configsection name="contactsimage">
<configmultienum name="backends" required="false" desc="The backends to
query for contacts images.">IMP_Contacts_Image_Addressbook,IMP_Contacts_Image_Unknown
<values>
<value desc="Horde addressbook application">IMP_Contacts_Image_Addressbook</value>
<value desc="Gravatar">IMP_Contacts_Image_Gravatar</value>
<value desc="Default (unknown user)">IMP_Contacts_Image_Unknown</value>
</values>
</configmultienum>
</configsection>
</configtab>

<configtab name="otherapps" desc="Other Applications">
<configsection name="tasklist">
<configboolean name="use_tasklist" desc="If Nag is installed on the local
Expand Down
2 changes: 2 additions & 0 deletions imp/docs/CHANGES
Expand Up @@ -2,6 +2,8 @@
v6.2.0-git
----------

[mms] Add support for showing a contact image in the contacts popup box in
dynamic view.
[mms] Add ability to create a new filter rule from the contacts dropdown in the
dynamic view.
[mms] More intelligent sizing of raw message parts opened in a popup window.
Expand Down
10 changes: 10 additions & 0 deletions imp/docs/UPGRADING
Expand Up @@ -29,6 +29,16 @@ Administration => Configuration and update anything that's highlighted as
outdated.


Upgrading IMP From 6.1.x To 6.2
=================================

Configuration Options (conf.php)
--------------------------------

The $conf['contactsimage']['backends'] options has been added.



Upgrading IMP From 6.1.x To 6.1.4
=================================

Expand Down
31 changes: 22 additions & 9 deletions imp/js/dimpcore.js
Expand Up @@ -250,22 +250,35 @@ var DimpCore = {

contextOnShow: function(e)
{
var tmp;
var remove = [],
tmp, tmp2;

switch (e.memo) {
case 'ctx_contacts':
tmp = $(e.memo).down('DIV.contactAddr');
if (tmp) {
tmp.next().remove();
tmp.remove();
tmp = $(e.memo);
remove.push(tmp.down('IMG.contactimg'));
tmp2 = tmp.down('DIV.contactAddr');
if (tmp2) {
remove.push(tmp2.next());
remove.push(tmp2);
}
remove.compact().invoke('remove');

// Add e-mail info to context menu if personal name is shown on
// page.
if ((tmp = e.element().retrieve('email')) && !tmp.g && tmp.p) {
$(e.memo)
.insert({ top: new Element('DIV', { className: 'sep' }) })
.insert({ top: new Element('DIV', { className: 'contactAddr' }).insert(tmp.b.escapeHTML()) });
if ((tmp2 = e.element().retrieve('email')) && !tmp2.g && tmp2.p) {
tmp.insert({ top: new Element('DIV', { className: 'sep' }) })
.insert({ top: new Element('DIV', { className: 'contactAddr' }).insert(tmp2.b.escapeHTML()) });

this.doAction('getContactsImage', {
addr: tmp2.b
}, {
callback: function (r) {
tmp.insert({
top: new Element('IMG', { src: r.url }).addClassName('contactimg')
});
}
});
}
break;
}
Expand Down
25 changes: 25 additions & 0 deletions imp/lib/Ajax/Application/Handler/Dynamic.php
Expand Up @@ -1129,4 +1129,29 @@ public function newFilter()
}
}

/**
* AJAX action: Return the contacts image for a given e-mail address.
*
* Variables used:
* <pre>
* - addr: (string) The e-mail address.
* </pre>
*
* @return object An object with the following properties:
* <pre>
* - url: (string) The URL of the image.
* </pre>
*/
public function getContactsImage()
{
try {
$out = new stdClass;
$out->url = strval(new IMP_Contacts_Image($this->vars->addr));
} catch (IMP_Exception $e) {
$out = false;
}

return $out;
}

}
78 changes: 78 additions & 0 deletions imp/lib/Contacts/Image.php
@@ -0,0 +1,78 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Generates a contact image to use for a given e-mail address.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image
{
/**
* The e-mail address.
*
* @var string
*/
protected $_email;

/**
* Constructor.
*
* @param string $email The e-mail address.
*/
public function __construct($email)
{
$this->_email = $email;
}

/**
* URL.
*
* @return string URL.
*/
public function __toString()
{
return strval($this->getUrlOb());
}

/**
* Return a URL object representing the contact image.
*
* @return Horde_Url|Horde_Url_Data URL object
*
* @throws IMP_Exception
*/
public function getUrlOb()
{
global $conf;

if (!empty($conf['contactsimage']['backends'])) {
foreach ($conf['contactsimage']['backends'] as $val) {
if (class_exists($val)) {
$backend = new $val();
if (($url = $backend->rawImage($this->_email)) ||
($url = $backend->urlImage($this->_email))) {
return $url;
}
}
}
}

throw new IMP_Exception('No backend found to generate contact image.');
}

}
66 changes: 66 additions & 0 deletions imp/lib/Contacts/Image/Addressbook.php
@@ -0,0 +1,66 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Generate contact image by using local addressbook.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image_Addressbook implements IMP_Contacts_Image_Backend
{
/**
*/
public function rawImage($email)
{
global $injector, $registry;

return null;

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

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

if (isset($res[$email][0]['photo'])) {
return Horde_Url_Data::create(
$res[$email][0]['photo'],
$res[$email][0]['phototype']
);
}
} catch (Horde_Exception $e) {}
}

return null;
}

/**
*/
public function urlImage($email)
{
return null;
}

}
49 changes: 49 additions & 0 deletions imp/lib/Contacts/Image/Backend.php
@@ -0,0 +1,49 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Interface for a profile image backend.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
interface IMP_Contacts_Image_Backend
{
/**
* Raw image data of a contact image.
*
* @param string $email An email address.
*
* @return array Null if image can't be generated. Otherwise, an array
* with these keys:
* <pre>
* - data: (string) Image data.
* - type: (string) MIME type of the image data.
* </pre>
*/
public function rawImage($email);

/**
* URL of the contact image.
*
* @param string $email An email address.
*
* @return Horde_Url Null if image can't be generated. Otherwise, the URL
* of the contact image.
*/
public function urlImage($email);

}
56 changes: 56 additions & 0 deletions imp/lib/Contacts/Image/Gravatar.php
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Generate contact image using the Gravatar service.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image_Gravatar implements IMP_Contacts_Image_Backend
{
/**
*/
public function rawImage($email)
{
if (class_exists('Horde_Service_Gravatar')) {
$gravatar = new Horde_Service_Gravatar();
$data = $gravatar->fetchAvatar($email, array(
'default' => 404,
'size' => 80
));
rewind($data);
$img_data = stream_get_contents($data);

if (strlen($img_data)) {
return Horde_Url_Data::create(
'image/jpeg',
$img_data
);
}
}

return null;
}

/**
*/
public function urlImage($email)
{
return null;
}

}

0 comments on commit ffce87f

Please sign in to comment.