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
56 $filename = $this->filename;
57 if (parent::delete()) {
58 @unlink(Avatar::path($filename));
63 * Deletes all avatars (but may spare the original) from a profile.
65 * @param Profile $target The profile we're deleting avatars of.
66 * @param boolean $original Whether original should be removed or not.
68 public static function deleteFromProfile(Profile $target, $original=true) {
70 $avatars = self::getProfileAvatars($target);
71 foreach ($avatars as $avatar) {
72 if ($avatar->original && !$original) {
77 } catch (NoResultException $e) {
78 // There are no avatars to delete, a sort of success.
84 public static function getUploaded(Profile $target)
86 $avatar = new Avatar();
87 $avatar->profile_id = $target->id;
88 $avatar->original = true;
89 if (!$avatar->find(true)) {
90 throw new NoResultException($avatar);
95 public static function hasUploaded(Profile $profile) {
97 $avatar = Avatar::getUploaded($profile);
98 } catch (NoResultException $e) {
101 return file_exists(Avatar::path($avatar->filename));
104 public static function getProfileAvatars(Profile $target) {
105 $avatar = new Avatar();
106 $avatar->profile_id = $target->id;
107 if (!$avatar->find()) {
108 throw new NoResultException($avatar);
110 return $avatar->fetchAll();
114 * Where should the avatar go for this user?
116 static function filename($id, $extension, $size=null, $extra=null)
119 return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
121 return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
125 static function path($filename)
127 $dir = common_config('avatar', 'dir');
129 if ($dir[strlen($dir)-1] != '/') {
133 return $dir . $filename;
136 static function url($filename)
138 $path = common_config('avatar', 'path');
140 if ($path[strlen($path)-1] != '/') {
144 if ($path[0] != '/') {
148 $server = common_config('avatar', 'server');
150 if (empty($server)) {
151 $server = common_config('site', 'server');
154 $ssl = common_config('avatar', 'ssl');
156 if (is_null($ssl)) { // null -> guess
157 if (common_config('site', 'ssl') == 'always' &&
158 !common_config('avatar', 'server')) {
165 $protocol = ($ssl) ? 'https' : 'http';
167 return $protocol.'://'.$server.$path.$filename;
170 function displayUrl()
172 $server = common_config('avatar', 'server');
173 if ($server && !empty($this->filename)) {
174 return Avatar::url($this->filename);
180 static function defaultImage($size)
182 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
183 AVATAR_STREAM_SIZE => 'stream',
184 AVATAR_MINI_SIZE => 'mini');
185 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
188 static function newSize(Profile $target, $size) {
189 $size = floor($size);
190 if ($size < 1 || $size > common_config('avatar', 'maxsize')) {
191 // TRANS: An error message when avatar size is unreasonable
192 throw new Exception(_m('Avatar size too large'));
195 $original = Avatar::getUploaded($target);
197 $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
198 $filename = $imagefile->resize($size);
200 $scaled = clone($original);
201 $scaled->original = false;
202 $scaled->width = $size;
203 $scaled->height = $size;
204 $scaled->url = Avatar::url($filename);
205 $scaled->filename = $filename;
206 $scaled->created = common_sql_now();
208 if (!$scaled->insert()) {
209 // TRANS: An error message when unable to insert avatar data into the db
210 throw new Exception(_m('Could not insert new avatar data to database'));
213 // Return the new avatar object