Move common_avatar_* functions to Avatar

Moved the common_avatar_* functions to the Avatar class. Typically
either as methods on the object or as static methods. Replaced all the
uses of the functions in other modules.
This commit is contained in:
Evan Prodromou 2009-02-06 03:13:08 -05:00
parent d3ff8bfec6
commit 343cd6f205
22 changed files with 107 additions and 112 deletions

View File

@ -90,9 +90,9 @@ class AvatarbynicknameAction extends Action
$url = $avatar->url;
} else {
if ($size == 'original') {
$url = common_default_avatar(AVATAR_PROFILE_SIZE);
$url = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
} else {
$url = common_default_avatar($size+0);
$url = Avatar::defaultImage($size+0);
}
}
common_redirect($url, 302);

View File

@ -200,7 +200,7 @@ class AvatarsettingsAction extends AccountSettingsAction
'class' => 'avatar_view'));
$this->element('h2', null, _("Original"));
$this->elementStart('div', array('id'=>'avatar_original_view'));
$this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => $this->filedata['width'],
'height' => $this->filedata['height'],
'alt' => $user->nickname));
@ -212,7 +212,7 @@ class AvatarsettingsAction extends AccountSettingsAction
'class' => 'avatar_view'));
$this->element('h2', null, _("Preview"));
$this->elementStart('div', array('id'=>'avatar_preview_view'));
$this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $user->nickname));
@ -281,12 +281,12 @@ class AvatarsettingsAction extends AccountSettingsAction
$cur = common_current_user();
$filename = common_avatar_filename($cur->id,
$filename = Avatar::filename($cur->id,
image_type_to_extension($imagefile->type),
null,
'tmp'.common_timestamp());
$filepath = common_avatar_path($filename);
$filepath = Avatar::path($filename);
move_uploaded_file($imagefile->filepath, $filepath);

View File

@ -263,7 +263,7 @@ class GrouplogoAction extends Action
'class' => 'avatar_view'));
$this->element('h2', null, _("Original"));
$this->elementStart('div', array('id'=>'avatar_original_view'));
$this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => $this->filedata['width'],
'height' => $this->filedata['height'],
'alt' => $this->group->nickname));
@ -275,7 +275,7 @@ class GrouplogoAction extends Action
'class' => 'avatar_view'));
$this->element('h2', null, _("Preview"));
$this->elementStart('div', array('id'=>'avatar_preview_view'));
$this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->group->nickname));
@ -343,12 +343,12 @@ class GrouplogoAction extends Action
return;
}
$filename = common_avatar_filename($this->group->id,
$filename = Avatar::filename($this->group->id,
image_type_to_extension($imagefile->type),
null,
'group-temp-'.common_timestamp());
$filepath = common_avatar_path($filename);
$filepath = Avatar::path($filename);
move_uploaded_file($imagefile->filepath, $filepath);

View File

@ -166,7 +166,7 @@ class NoticesearchAction extends SearchAction
$avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
$this->elementStart('a', array('href' => $profile->profileurl,
'class' => 'url'));
$this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
$this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
'class' => 'avatar photo',
'width' => AVATAR_STREAM_SIZE,
'height' => AVATAR_STREAM_SIZE,

View File

@ -242,7 +242,7 @@ class ShowstreamAction extends Action
$this->elementStart('dl', 'entity_depiction');
$this->element('dt', null, _('Photo'));
$this->elementStart('dd');
$this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
$this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,

View File

@ -80,7 +80,7 @@ class TagotherAction extends Action
$this->elementStart('dl', 'entity_depiction');
$this->element('dt', null, _('Photo'));
$this->elementStart('dd');
$this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
$this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,

View File

@ -250,8 +250,8 @@ class TwittersettingsAction extends ConnectSettingsAction
$avatar = $other->getAvatar(AVATAR_MINI_SIZE);
$avatar_url = ($avatar) ?
common_avatar_display_url($avatar) :
common_default_avatar(AVATAR_MINI_SIZE);
$avatar->displayUrl() :
Avatar::defaultImage(AVATAR_MINI_SIZE);
$this->element('img', array('src' => $avatar_url,
'width' => AVATAR_MINI_SIZE,

View File

@ -33,7 +33,7 @@ class Avatar extends Memcached_DataObject
{
$filename = $this->filename;
if (parent::delete()) {
@unlink(common_avatar_path($filename));
@unlink(Avatar::path($filename));
}
}
@ -42,4 +42,42 @@ class Avatar extends Memcached_DataObject
return Memcached_DataObject::pkeyGet('Avatar', $kv);
}
// where should the avatar go for this user?
static function filename($id, $extension, $size=null, $extra=null)
{
if ($size) {
return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
} else {
return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
}
}
static function path($filename)
{
return INSTALLDIR . '/avatar/' . $filename;
}
static function url($filename)
{
return common_path('avatar/'.$filename);
}
function displayUrl()
{
$server = common_config('avatar', 'server');
if ($server) {
return 'http://'.$server.'/'.$this->filename;
} else {
return $this->url;
}
}
static function defaultAvatar($size)
{
static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
AVATAR_STREAM_SIZE => 'stream',
AVATAR_MINI_SIZE => 'mini');
return theme_path('default-avatar-'.$sizenames[$size].'.png');
}
}

View File

@ -71,7 +71,7 @@ class Profile extends Memcached_DataObject
function setOriginal($filename)
{
$imagefile = new ImageFile($this->id, common_avatar_path($filename));
$imagefile = new ImageFile($this->id, Avatar::path($filename));
$avatar = new Avatar();
$avatar->profile_id = $this->id;
@ -80,13 +80,13 @@ class Profile extends Memcached_DataObject
$avatar->mediatype = image_type_to_mime_type($imagefile->type);
$avatar->filename = $filename;
$avatar->original = true;
$avatar->url = common_avatar_url($filename);
$avatar->url = Avatar::url($filename);
$avatar->created = DB_DataObject_Cast::dateTime(); # current time
# XXX: start a transaction here
if (!$this->delete_avatars() || !$avatar->insert()) {
@unlink(common_avatar_path($filename));
@unlink(Avatar::path($filename));
return null;
}
@ -104,7 +104,7 @@ class Profile extends Memcached_DataObject
$scaled->original = false;
$scaled->mediatype = image_type_to_mime_type($imagefile->type);
$scaled->filename = $scaled_filename;
$scaled->url = common_avatar_url($scaled_filename);
$scaled->url = Avatar::url($scaled_filename);
$scaled->created = DB_DataObject_Cast::dateTime(); # current time
if (!$scaled->insert()) {
@ -194,4 +194,13 @@ class Profile extends Memcached_DataObject
}
}
function avatarUrl($size=AVATAR_PROFILE_SIZE)
{
$avatar = $this->getAvatar($size);
if ($avatar) {
return $avatar->displayUrl();
} else {
return Avatar::defaultImage($size);
}
}
}

View File

@ -90,13 +90,13 @@ class User_group extends Memcached_DataObject
function setOriginal($filename)
{
$imagefile = new ImageFile($this->id, common_avatar_path($filename));
$imagefile = new ImageFile($this->id, Avatar::path($filename));
$orig = clone($this);
$this->original_logo = common_avatar_url($filename);
$this->homepage_logo = common_avatar_url($imagefile->resize(AVATAR_PROFILE_SIZE));
$this->stream_logo = common_avatar_url($imagefile->resize(AVATAR_STREAM_SIZE));
$this->mini_logo = common_avatar_url($imagefile->resize(AVATAR_MINI_SIZE));
$this->original_logo = Avatar::url($filename);
$this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
$this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
$this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
common_debug(common_log_objstring($this));
return $this->update($orig);
}

View File

@ -153,12 +153,12 @@ class ImageFile
imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
$outname = common_avatar_filename($this->id,
$outname = Avatar::filename($this->id,
image_type_to_extension($this->type),
$size,
common_timestamp());
$outpath = common_avatar_path($outname);
$outpath = Avatar::path($outname);
switch ($this->type) {
case IMAGETYPE_GIF:

View File

@ -178,7 +178,7 @@ function jabber_format_entry($profile, $notice)
$entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
$entry .= "<link rel='self' type='application/rss+xml' href='" . $self_url . "'/>\n";
$entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
$entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
$entry .= "<icon>" . $profile->avatarUrl(AVATAR_PROFILE_SIZE) . "</icon>\n";
$entry .= "</source>\n";
$entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
$entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";

View File

@ -183,8 +183,8 @@ class MailboxAction extends PersonalAction
'class' => 'url'));
$avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
$this->element('img', array('src' => ($avatar) ?
common_avatar_display_url($avatar) :
common_default_avatar(AVATAR_STREAM_SIZE),
$avatar->displayUrl() :
Avatar::defaultImage(AVATAR_STREAM_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_STREAM_SIZE,
'height' => AVATAR_STREAM_SIZE,

View File

@ -285,8 +285,8 @@ class NoticeListItem extends Widget
$avatar = $this->profile->getAvatar($avatar_size);
$this->out->element('img', array('src' => ($avatar) ?
common_avatar_display_url($avatar) :
common_default_avatar($avatar_size),
$avatar->displayUrl() :
Avatar::defaultImage($avatar_size),
'class' => 'avatar photo',
'width' => $avatar_size,
'height' => $avatar_size,

View File

@ -82,7 +82,7 @@ class NoticeSection extends Section
$profile->nickname,
'href' => $profile->profileurl,
'class' => 'url'));
$this->out->element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_MINI_SIZE)),
$this->out->element('img', array('src' => (($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_MINI_SIZE)),
'width' => AVATAR_MINI_SIZE,
'height' => AVATAR_MINI_SIZE,
'class' => 'avatar photo',

View File

@ -97,7 +97,7 @@ class ProfileList extends Widget
$avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
$this->out->elementStart('a', array('href' => $this->profile->profileurl,
'class' => 'url'));
$this->out->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
$this->out->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_STREAM_SIZE,
'height' => AVATAR_STREAM_SIZE,

View File

@ -74,7 +74,7 @@ class ProfileMiniList extends ProfileList
'rel' => 'contact member',
'class' => 'url'));
$avatar = $this->profile->getAvatar(AVATAR_MINI_SIZE);
$this->out->element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_MINI_SIZE)),
$this->out->element('img', array('src' => (($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_MINI_SIZE)),
'width' => AVATAR_MINI_SIZE,
'height' => AVATAR_MINI_SIZE,
'class' => 'avatar photo',

View File

@ -86,7 +86,7 @@ class ProfileSection extends Section
'rel' => 'contact member',
'class' => 'url'));
$avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
$this->out->element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_MINI_SIZE)),
$this->out->element('img', array('src' => (($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_MINI_SIZE)),
'width' => AVATAR_MINI_SIZE,
'height' => AVATAR_MINI_SIZE,
'class' => 'avatar photo',

View File

@ -199,7 +199,7 @@ class Rss10Action extends Action
$this->element('dc:date', null, common_date_w3dtf($notice->created));
$this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
$this->element('sioc:has_creator', array('rdf:resource' => $creator_uri));
$this->element('laconica:postIcon', array('rdf:resource' => common_profile_avatar_url($profile)));
$this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
$this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
$this->elementEnd('item');
$this->creators[$creator_uri] = $profile;
@ -216,7 +216,7 @@ class Rss10Action extends Action
$this->element('foaf:name', null, $profile->fullname);
}
$this->element('sioc:id', null, $id);
$avatar = common_profile_avatar_url($profile);
$avatar = $profile->avatarUrl();
$this->element('sioc:avatar', array('rdf:resource' => $avatar));
$this->elementEnd('sioc:User');
}

View File

@ -43,7 +43,7 @@ class TwitterapiAction extends Action
$avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
$twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
$twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE);
$twitter_user['protected'] = 'false'; # not supported by Laconica yet
$twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;

View File

@ -653,48 +653,6 @@ function common_relative_profile($sender, $nickname, $dt=null)
return null;
}
// where should the avatar go for this user?
function common_avatar_filename($id, $extension, $size=null, $extra=null)
{
global $config;
if ($size) {
return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
} else {
return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
}
}
function common_avatar_path($filename)
{
global $config;
return INSTALLDIR . '/avatar/' . $filename;
}
function common_avatar_url($filename)
{
return common_path('avatar/'.$filename);
}
function common_avatar_display_url($avatar)
{
$server = common_config('avatar', 'server');
if ($server) {
return 'http://'.$server.'/'.$avatar->filename;
} else {
return $avatar->url;
}
}
function common_default_avatar($size)
{
static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
AVATAR_STREAM_SIZE => 'stream',
AVATAR_MINI_SIZE => 'mini');
return theme_path('default-avatar-'.$sizenames[$size].'.png');
}
function common_local_url($action, $args=null, $fragment=null)
{
$url = null;
@ -1511,16 +1469,6 @@ function common_markup_to_html($c)
return Markdown($c);
}
function common_profile_avatar_url($profile, $size=AVATAR_PROFILE_SIZE)
{
$avatar = $profile->getAvatar($size);
if ($avatar) {
return common_avatar_display_url($avatar);
} else {
return common_default_avatar($size);
}
}
function common_profile_uri($profile)
{
if (!$profile) {

View File

@ -74,7 +74,7 @@ class EnjitQueueHandler extends QueueHandler
$atom .= "<link href='" . $profile->profileurl . "'/>\n";
$atom .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
$atom .= "<author><name>" . $profile->nickname . "</name></author>\n";
$atom .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
$atom .= "<icon>" . $profile->avatarUrl(AVATAR_PROFILE_SIZE) . "</icon>\n";
$atom .= "</source>\n";
$atom .= "<title>" . htmlspecialchars($msg) . "</title>\n";
$atom .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";