Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 7ae55d4

Browse files
author
Charles Marion
committed
ENH: 0009594: Add gravatar suppport
1 parent d84a378 commit 7ae55d4

File tree

9 files changed

+120
-29
lines changed

9 files changed

+120
-29
lines changed

core/controllers/UserController.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public function settingsAction()
289289
$submitPassword = $this->_getParam('modifyPassword');
290290
$modifyAccount = $this->_getParam('modifyAccount');
291291
$modifyPicture = $this->_getParam('modifyPicture');
292+
$modifyPictureGravatar = $this->_getParam('modifyPictureGravatar');
292293
if(isset($submitPassword) && $this->logged)
293294
{
294295
$oldPass = $this->_getParam('oldPassword');
@@ -369,14 +370,38 @@ public function settingsAction()
369370
{
370371
$this->userSession->Dao = $userDao;
371372
}
372-
echo JsonComponent::encode(array(true, $this->t('Changes saved'), $userDao->getThumbnail()));
373+
echo JsonComponent::encode(array(true, $this->t('Changes saved'), $this->view->webroot.'/'.$userDao->getThumbnail()));
373374
}
374375
else
375376
{
376377
echo JsonComponent::encode(array(false, 'Error'));
377378
}
378379
}
379380
}
381+
if(isset($modifyPictureGravatar) && $this->logged)
382+
{
383+
$gravatarUrl = $this->User->getGravatarUrl($userDao->getEmail());
384+
if($gravatarUrl != false)
385+
{
386+
$userDao = $this->User->load($userDao->getKey());
387+
$oldThumbnail = $userDao->getThumbnail();
388+
if(!empty($oldThumbnail))
389+
{
390+
unlink(BASE_PATH.'/'.$oldThumbnail);
391+
}
392+
$userDao->setThumbnail($gravatarUrl);
393+
$this->User->save($userDao);
394+
if(!isset($userId))
395+
{
396+
$this->userSession->Dao = $userDao;
397+
}
398+
echo JsonComponent::encode(array(true, $this->t('Changes saved'), $userDao->getThumbnail()));
399+
}
400+
else
401+
{
402+
echo JsonComponent::encode(array(false, 'Error'));
403+
}
404+
}
380405
}
381406

382407
$communities = array();
@@ -395,6 +420,8 @@ public function settingsAction()
395420
$this->Component->Sortdao->order = 'asc';
396421
usort($communities, array($this->Component->Sortdao, 'sortByName'));
397422

423+
$this->view->isGravatar = $this->User->getGravatarUrl($userDao->getEmail());
424+
398425
$this->view->communities = $communities;
399426
$this->view->user = $userDao;
400427
$this->view->thumbnail = $userDao->getThumbnail();

core/models/base/UserModelBase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ public function createUser($email, $password, $firstname, $lastname, $admin = 0)
109109
$userDao->setCreation(date('c'));
110110
$userDao->setPassword(md5($password));
111111
$userDao->setAdmin($admin);
112+
113+
// check gravatar
114+
$gravatarUrl = $this->getGravatarUrl($email);
115+
if($gravatarUrl != false)
116+
{
117+
$userDao->setThumbnail($gravatarUrl);
118+
}
119+
112120
parent::save($userDao);
113121

114122
$this->ModelLoader = new MIDAS_ModelLoader();
@@ -145,4 +153,38 @@ public function createUser($email, $password, $firstname, $lastname, $admin = 0)
145153
return $userDao;
146154
}
147155

156+
157+
/**
158+
* Get either a Gravatar URL or complete image tag for a specified email address.
159+
*
160+
* @param string $email The email address
161+
* @param string $s Size in pixels, defaults to 80px [ 1 - 512 ]
162+
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
163+
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
164+
* @param boole $img True to return a complete IMG tag False for just the URL
165+
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
166+
* @return String containing either just a URL or a complete image tag
167+
* @source http://gravatar.com/site/implement/images/php/
168+
*/
169+
public function getGravatarUrl($email, $s = 32, $d = '404', $r = 'g', $img = false, $atts = array() ) {
170+
$url = 'http://www.gravatar.com/avatar/';
171+
$url .= md5(strtolower(trim($email)));
172+
$url .= "?s=$s&d=$d&r=$r";
173+
if($img)
174+
{
175+
$url = '<img src="' . $url . '"';
176+
foreach ( $atts as $key => $val )
177+
{
178+
$url .= ' ' . $key . '="' . $val . '"';
179+
}
180+
$url .= ' />';
181+
}
182+
183+
$header = get_headers($url, 1);
184+
if(strpos($header[0], '404 Not Found') != false)
185+
{
186+
return false;
187+
}
188+
return $url;
189+
}
148190
} // end class UserModelBase

core/models/dao/UserDao.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ public function getFullName()
2222
{
2323
return $this->getFirstname()." ".$this->getLastname();
2424
}//end class
25+
26+
/** toArray */
27+
public function toArray()
28+
{
29+
$return = parent::toArray();
30+
unset($return['email']);
31+
unset($return['password']);
32+
return $return;
33+
}
2534
}
2635
?>

core/public/js/user/user.settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function successPictureChange(responseText, statusText, xhr, $form)
9494
}
9595
if(jsonResponse[0])
9696
{
97-
$('img#userTopThumbnail').attr('src',json.global.webroot+'/'+jsonResponse[2]);
97+
$('img#userTopThumbnail').attr('src', jsonResponse[2]);
9898
createNotive(jsonResponse[1],4000);
9999
}
100100
else

core/translation/fr-main.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,5 @@ Send invitation;Envoyer des invitations
220220
Invite the user ...;Inviter l utilisateur ...
221221
has been invited;a �t� invit�
222222
invited you to the community;vous a invit� � la communaut�
223-
Create top level Folder;Cr�er un r�pertoire racine
223+
Create top level Folder;Cr�er un r�pertoire racine
224+
Use gravatar;Utiliser Gravatar

core/views/element/feed.phtml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@
3535
}
3636
echo "<div class='feedUserImage'>";
3737
$thumbnail=$feed->getUser()->getThumbnail();
38-
if(!empty($thumbnail))
39-
{
40-
echo "<img class='thumbnailSmall' src='{$view->webroot}/{$thumbnail}' alt=''/>";
41-
}
42-
else
43-
{
44-
echo "<img class='thumbnailSmall' src='{$view->coreWebroot}/public/images/icons/unknownUser.png' alt=''/>";
45-
}
38+
39+
echo $view->userthumbnail($thumbnail);
4640
echo "</div>";
4741
echo "<div class='feedInfo'>";
4842
echo $view->linkuser($feed->getUser()).' ';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
class Zend_View_Helper_Userthumbnail
3+
{
4+
/** translation helper */
5+
function userthumbnail($thumbnail, $id = '')
6+
{
7+
if(!empty($thumbnail) && strpos($thumbnail, 'http://') === false)
8+
{
9+
echo "<img id='{$id}' class='thumbnailSmall' src='{$this->view->webroot}/{$thumbnail}' alt=''/>";
10+
}
11+
else if(!empty($thumbnail) && strpos($thumbnail, 'http://') !== false)
12+
{
13+
echo "<img id='{$id}' class='thumbnailSmall' src='{$thumbnail}' alt=''/>";
14+
}
15+
else
16+
{
17+
echo "<img id='{$id}' class='thumbnailSmall' src='{$this->view->coreWebroot}/public/images/icons/unknownUser.png' alt=''/>";
18+
}
19+
}
20+
21+
22+
/** Set view*/
23+
public function setView(Zend_View_Interface $view)
24+
{
25+
$this->view = $view;
26+
}
27+
}// end class

core/views/user/settings.phtml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ echo '<script type="text/javascript" src="' . $this->coreWebroot . '/public/js/u
5050
<div id="imageActual">
5151
<h4><?php echo $this->t('Current Avatar')?>:</h4>
5252
<?php
53-
$thumbnail=$this->thumbnail;
54-
if(!empty($thumbnail))
55-
{
56-
echo "<img id='userTopThumbnail' src='{$this->webroot}/{$thumbnail}' alt=''/>";
57-
}
58-
else
59-
{
60-
echo "<img id='userTopThumbnail' src='{$this->coreWebroot}/public/images/icons/unknownUser.png' alt=''/>";
61-
}
53+
echo $this->userthumbnail($this->thumbnail,'userTopThumbnail');
6254
?>
6355
</div>
6456
<h3><?php echo $this->t('My Avatar')?>:</h3>
@@ -70,6 +62,13 @@ echo '<script type="text/javascript" src="' . $this->coreWebroot . '/public/js/u
7062
<br/>
7163
<input type="submit" name='modifyPicture' value="<?php echo $this->t('Upload new avatar')?>">
7264
<br/>
65+
<?php
66+
67+
if(strpos($this->thumbnail, 'http://') === false && $this->isGravatar)
68+
{
69+
echo ' <input type="submit" name="modifyPictureGravatar" value="'.$this->t('Use gravatar').'">';
70+
}
71+
?>
7372
<br/>
7473
</form>
7574
</div>

core/views/user/userpage.phtml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ $this->headScript()->appendFile($this->coreWebroot.'/public/js/common/common.bro
99
<div class="viewMain">
1010
<div class="genericThumbnail">
1111
<?php
12-
$thumbnail=$this->user->getThumbnail();
13-
if(!empty($thumbnail))
14-
{
15-
echo "<img class='thumbnailSmall' src='{$this->webroot}/{$thumbnail}' alt=''/>";
16-
}
17-
else
18-
{
19-
echo "<img class='thumbnailSmall' src='{$this->coreWebroot}/public/images/icons/unknownUser.png' alt=''/>";
20-
}
12+
echo $this->userthumbnail($this->user->getThumbnail());
2113
?>
2214
</div>
2315
<div class="genericInfo">

0 commit comments

Comments
 (0)