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);
74 foreach ($avatars as $avatar) {
75 assert($avatar instanceof Avatar);
77 if ($avatar->original && !$original) {
83 } catch (NoAvatarException $e) {
84 // There are no avatars to delete, a sort of success.
90 static protected $_avatars = array();
93 * Get an avatar by profile. Currently can't call newSize with $height
95 public static function byProfile(Profile $target, $width=null, $height=null)
97 $width = intval($width);
98 $height = !is_null($height) ? intval($height) : null;
99 if (is_null($height)) {
103 $size = "{$width}x{$height}";
105 if (!isset(self::$_avatars[$target->id])) {
106 self::$_avatars[$target->id] = array();
107 } elseif (isset(self::$_avatars[$target->id][$size])){
108 return self::$_avatars[$target->id][$size];
113 if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
114 $avatar = self::pkeyGet(
116 'profile_id' => $target->id,
122 Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
125 if (is_null($avatar)) {
126 // Obviously we can't find an avatar, so let's resize the original!
127 $avatar = Avatar::newSize($target, $width);
128 } elseif (!($avatar instanceof Avatar)) {
129 throw new NoAvatarException($target, $avatar);
132 self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
136 public static function getUploaded(Profile $target)
138 $avatar = new Avatar();
139 $avatar->profile_id = $target->id;
140 $avatar->original = true;
141 if (!$avatar->find(true)) {
142 throw new NoAvatarException($target, $avatar);
144 if (!file_exists(Avatar::path($avatar->filename))) {
145 // The delete call may be odd for, say, unmounted filesystems
146 // that cause a file to currently not exist, but actually it does...
148 throw new NoAvatarException($target, $avatar);
153 public static function getProfileAvatars(Profile $target) {
154 $avatar = new Avatar();
155 $avatar->profile_id = $target->id;
156 if (!$avatar->find()) {
157 throw new NoAvatarException($target, $avatar);
159 return $avatar->fetchAll();
163 * Where should the avatar go for this user?
165 static function filename($id, $extension, $size=null, $extra=null)
168 return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
170 return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
174 static function path($filename)
176 $dir = common_config('avatar', 'dir');
178 if ($dir[strlen($dir)-1] != '/') {
182 return $dir . $filename;
185 static function url($filename)
187 $path = common_config('avatar', 'path');
189 if ($path[strlen($path)-1] != '/') {
193 if ($path[0] != '/') {
197 $server = common_config('avatar', 'server');
199 if (empty($server)) {
200 $server = common_config('site', 'server');
203 $ssl = common_config('avatar', 'ssl');
205 if (is_null($ssl)) { // null -> guess
206 if (common_config('site', 'ssl') == 'always' &&
207 !common_config('avatar', 'server')) {
214 $protocol = ($ssl) ? 'https' : 'http';
216 return $protocol.'://'.$server.$path.$filename;
219 function displayUrl()
221 $server = common_config('avatar', 'server');
222 if ($server && !empty($this->filename)) {
223 return Avatar::url($this->filename);
229 static function urlByProfile(Profile $target, $width=null, $height=null) {
231 return self::byProfile($target, $width, $height)->displayUrl();
232 } catch (Exception $e) {
233 common_debug(sprintf('target=>id=%s,width=%s,height=%s,message=%s',
239 return self::defaultImage($width);
243 static function defaultImage($size)
245 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
246 AVATAR_STREAM_SIZE => 'stream',
247 AVATAR_MINI_SIZE => 'mini');
248 return Theme::path('default-avatar-' . $sizenames[$size] . '.png');
251 static function newSize(Profile $target, $width) {
252 $width = intval($width);
253 if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
254 // TRANS: An error message when avatar size is unreasonable
255 throw new Exception(_m('Avatar size too large'));
258 $original = Avatar::getUploaded($target);
260 $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
261 $filename = $imagefile->resize($width);
263 $scaled = clone($original);
264 $scaled->original = false;
265 $scaled->width = $width;
266 $scaled->height = $width;
267 $scaled->url = Avatar::url($filename);
268 $scaled->filename = $filename;
269 $scaled->created = common_sql_now();
271 if (!$scaled->insert()) {
272 // TRANS: An error message when unable to insert avatar data into the db
273 throw new Exception(_m('Could not insert new avatar data to database'));
276 // Return the new avatar object