3 * Table Definition for avatar
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Avatar extends Managed_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'avatar'; // table name
13 public $profile_id; // int(4) primary_key not_null
14 public $original; // tinyint(1)
15 public $width; // int(4) primary_key not_null
16 public $height; // int(4) primary_key not_null
17 public $mediatype; // varchar(32) not_null
18 public $filename; // varchar(255)
19 public $url; // varchar(255) unique_key
20 public $created; // datetime() not_null
21 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
23 /* the code above is auto generated do not remove the tag below */
26 public static function schemaDef()
30 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
32 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
33 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
34 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
35 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
36 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
37 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
38 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
40 'primary key' => array('profile_id', 'width', 'height'),
41 'unique keys' => array(
42 'avatar_url_key' => array('url'),
44 'foreign keys' => array(
45 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
48 'avatar_profile_id_idx' => array('profile_id'),
53 // We clean up the file, too
54 function delete($useWhere=false)
56 $filename = $this->filename;
57 if (file_exists(Avatar::path($filename))) {
58 @unlink(Avatar::path($filename));
61 return parent::delete($useWhere);
65 * Deletes all avatars (but may spare the original) from a profile.
67 * @param Profile $target The profile we're deleting avatars of.
68 * @param boolean $original Whether original should be removed or not.
70 public static function deleteFromProfile(Profile $target, $original=true) {
72 $avatars = self::getProfileAvatars($target);
73 foreach ($avatars as $avatar) {
74 if ($avatar->original && !$original) {
79 } catch (NoAvatarException $e) {
80 // There are no avatars to delete, a sort of success.
86 static protected $_avatars = array();
89 * Get an avatar by profile. Currently can't call newSize with $height
91 public static function byProfile(Profile $target, $width=null, $height=null)
93 $width = intval($width);
94 $height = !is_null($height) ? intval($height) : null;
95 if (is_null($height)) {
99 $size = "{$width}x{$height}";
100 if (!isset(self::$_avatars[$target->id])) {
101 self::$_avatars[$target->id] = array();
102 } elseif (isset(self::$_avatars[$target->id][$size])){
103 return self::$_avatars[$target->id][$size];
107 if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
108 $avatar = self::pkeyGet(
110 'profile_id' => $target->id,
115 Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
118 if (is_null($avatar)) {
119 // Obviously we can't find an avatar, so let's resize the original!
120 $avatar = Avatar::newSize($target, $width);
121 } elseif (!($avatar instanceof Avatar)) {
122 throw new NoAvatarException($target, $avatar);
125 self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
129 public static function getUploaded(Profile $target)
131 $avatar = new Avatar();
132 $avatar->profile_id = $target->id;
133 $avatar->original = true;
134 if (!$avatar->find(true)) {
135 throw new NoAvatarException($target, $avatar);
137 if (!file_exists(Avatar::path($avatar->filename))) {
138 // The delete call may be odd for, say, unmounted filesystems
139 // that cause a file to currently not exist, but actually it does...
141 throw new NoAvatarException($target, $avatar);
146 public static function getProfileAvatars(Profile $target) {
147 $avatar = new Avatar();
148 $avatar->profile_id = $target->id;
149 if (!$avatar->find()) {
150 throw new NoAvatarException($target, $avatar);
152 return $avatar->fetchAll();
156 * Where should the avatar go for this user?
158 static function filename($id, $extension, $size=null, $extra=null)
161 return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
163 return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
167 static function path($filename)
169 $dir = common_config('avatar', 'dir');
171 if ($dir[strlen($dir)-1] != '/') {
175 return $dir . $filename;
178 static function url($filename)
180 $path = common_config('avatar', 'path');
182 if ($path[strlen($path)-1] != '/') {
186 if ($path[0] != '/') {
190 $server = common_config('avatar', 'server');
192 if (empty($server)) {
193 $server = common_config('site', 'server');
196 $ssl = common_config('avatar', 'ssl');
198 if (is_null($ssl)) { // null -> guess
199 if (common_config('site', 'ssl') == 'always' &&
200 !common_config('avatar', 'server')) {
207 $protocol = ($ssl) ? 'https' : 'http';
209 return $protocol.'://'.$server.$path.$filename;
212 function displayUrl()
214 $server = common_config('avatar', 'server');
215 if ($server && !empty($this->filename)) {
216 return Avatar::url($this->filename);
222 static function urlByProfile(Profile $target, $width=null, $height=null) {
224 return self::byProfile($target, $width, $height)->displayUrl();
225 } catch (Exception $e) {
226 return self::defaultImage($width);
230 static function defaultImage($size)
232 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
233 AVATAR_STREAM_SIZE => 'stream',
234 AVATAR_MINI_SIZE => 'mini');
235 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
238 static function newSize(Profile $target, $width) {
239 $width = intval($width);
240 if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
241 // TRANS: An error message when avatar size is unreasonable
242 throw new Exception(_m('Avatar size too large'));
245 $original = Avatar::getUploaded($target);
247 $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
248 $filename = $imagefile->resize($width);
250 $scaled = clone($original);
251 $scaled->original = false;
252 $scaled->width = $width;
253 $scaled->height = $width;
254 $scaled->url = Avatar::url($filename);
255 $scaled->filename = $filename;
256 $scaled->created = common_sql_now();
258 if (!$scaled->insert()) {
259 // TRANS: An error message when unable to insert avatar data into the db
260 throw new Exception(_m('Could not insert new avatar data to database'));
263 // Return the new avatar object