Added pluggable Avatar providers

This commit is contained in:
Frederic Guillot
2016-03-18 23:06:32 -04:00
parent c4c200b530
commit fa86542f90
25 changed files with 216 additions and 42 deletions

View File

@@ -41,6 +41,7 @@ use Pimple\Container;
* @property \Kanboard\Core\Session\FlashMessage $flash
* @property \Kanboard\Core\Session\SessionManager $sessionManager
* @property \Kanboard\Core\Session\SessionStorage $sessionStorage
* @property \Kanboard\Core\User\Avatar\AvatarManager $avatarManager
* @property \Kanboard\Core\User\GroupSync $groupSync
* @property \Kanboard\Core\User\UserProfile $userProfile
* @property \Kanboard\Core\User\UserSync $userSync

View File

@@ -0,0 +1,62 @@
<?php
namespace Kanboard\Core\User\Avatar;
/**
* Avatar Manager
*
* @package user
* @author Frederic Guillot
*/
class AvatarManager
{
/**
* Providers
*
* @access private
* @var AvatarProviderInterface[]
*/
private $providers = array();
/**
* Register a new Avatar provider
*
* @access public
* @param AvatarProviderInterface $provider
* @return $this
*/
public function register(AvatarProviderInterface $provider)
{
$this->providers[] = $provider;
return $this;
}
/**
* Render avatar html element
*
* @access public
* @param string $user_id
* @param string $username
* @param string $name
* @param string $email
* @param int $size
* @return string
*/
public function render($user_id, $username, $name, $email, $size)
{
$user = array(
'id' => $user_id,
'username' => $username,
'name' => $name,
'email' => $email,
);
foreach ($this->providers as $provider) {
if ($provider->isActive($user)) {
return $provider->render($user, $size);
}
}
return '';
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Kanboard\Core\User\Avatar;
/**
* Avatar Provider Interface
*
* @package user
* @author Frederic Guillot
*/
interface AvatarProviderInterface
{
/**
* Render avatar html
*
* @access public
* @param array $user
* @param int $size
*/
public function render(array $user, $size);
/**
* Determine if the provider is active
*
* @access public
* @param array $user
* @return boolean
*/
public function isActive(array $user);
}

View File

@@ -160,19 +160,18 @@ class UserHelper extends Base
}
/**
* Display gravatar image
* Display avatar
*
* @access public
* @param string $email
* @param string $alt
* @param string $user_id
* @param string $username
* @param string $name
* @param string $email
* @return string
*/
public function avatar($email, $alt = '')
public function avatar($user_id, $username, $name, $email)
{
if (! empty($email) && $this->config->get('integration_gravatar') == 1) {
return '<img class="avatar" src="https://www.gravatar.com/avatar/'.md5(strtolower($email)).'?s=25" alt="'.$this->helper->text->e($alt).'" title="'.$this->helper->text->e($alt).'">';
}
return '';
$html = $this->avatarManager->render($user_id, $username, $name, $email, 25);
return '<div class="avatar">'.$html.'</div>';
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Kanboard\ServiceProvider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Kanboard\Core\User\Avatar\AvatarManager;
use Kanboard\User\Avatar\GravatarProvider;
/**
* Avatar Provider
*
* @package serviceProvider
* @author Frederic Guillot
*/
class AvatarProvider implements ServiceProviderInterface
{
/**
* Register providers
*
* @access public
* @param \Pimple\Container $container
* @return \Pimple\Container
*/
public function register(Container $container)
{
$container['avatarManager'] = new AvatarManager;
$container['avatarManager']->register(new GravatarProvider($container));
return $container;
}
}

View File

@@ -1,9 +1,7 @@
<div class="comment <?= isset($preview) ? 'comment-preview' : '' ?>" id="comment-<?= $comment['id'] ?>">
<p class="comment-title">
<?php if (! empty($comment['email'])): ?>
<?= $this->user->avatar($comment['email'], $comment['name'] ?: $comment['username']) ?>
<?php endif ?>
<?= $this->user->avatar($comment['user_id'], $comment['username'], $comment['name'], $comment['email']) ?>
<?php if (! empty($comment['username'])): ?>
<span class="comment-username"><?= $this->text->e($comment['name'] ?: $comment['username']) ?></span> @

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s commented the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s updated a comment on the task %s',
$this->text->e($author),

View File

@@ -16,7 +16,16 @@
<?php endif ?>
&nbsp;<?= $this->dt->datetime($event['date_creation']) ?>
</p>
<div class="activity-content"><?= $event['event_content'] ?></div>
<div class="activity-content">
<?= $this->user->avatar(
$event['creator_id'],
$event['author_username'],
$event['author_name'],
$event['email']
) ?>
<?= $event['event_content'] ?>
</div>
</div>
<?php endforeach ?>

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s created a subtask for the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s updated a subtask for the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?php $assignee = $task['assignee_name'] ?: $task['assignee_username'] ?>

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s closed the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s created the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s attached a new file to the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s moved the task %s to the column "%s"',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s moved the task %s to the position #%d in the column "%s"',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?php if ($task['swimlane_id'] == 0): ?>
<?= e('%s moved the task %s to the first swimlane',

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s opened the task %s',
$this->text->e($author),

View File

@@ -1,5 +1,3 @@
<?= $this->user->avatar($email, $author) ?>
<p class="activity-title">
<?= e('%s updated the task %s',
$this->text->e($author),

View File

@@ -0,0 +1,35 @@
<?php
namespace Kanboard\User\Avatar;
use Kanboard\Core\Base;
use Kanboard\Core\User\Avatar\AvatarProviderInterface;
class GravatarProvider extends Base implements AvatarProviderInterface
{
/**
* Render avatar html
*
* @access public
* @param array $user
* @param int $size
*/
public function render(array $user, $size)
{
$url = sprintf('https://www.gravatar.com/avatar/%s?s=%d', md5(strtolower($user['email'])), $size);
$title = $this->helper->text->e($user['name'] ?: $user['username']);
return '<img src="'.$url.'" alt="'.$title.'" title="'.$title.'">';
}
/**
* Determine if the provider is active
*
* @access public
* @param array $user
* @return boolean
*/
public function isActive(array $user)
{
return !empty($user['email']) && $this->config->get('integration_gravatar') == 1;
}
}

View File

@@ -38,4 +38,5 @@ $container->register(new Kanboard\ServiceProvider\GroupProvider);
$container->register(new Kanboard\ServiceProvider\RouteProvider);
$container->register(new Kanboard\ServiceProvider\ActionProvider);
$container->register(new Kanboard\ServiceProvider\ExternalLinkProvider);
$container->register(new Kanboard\ServiceProvider\AvatarProvider);
$container->register(new Kanboard\ServiceProvider\PluginProvider);