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'),
52 // We clean up the file, too
56 $filename = $this->filename;
57 if (parent::delete()) {
58 @unlink(Avatar::path($filename));
62 public static function deleteFromProfile(Profile $target) {
63 $avatars = Avatar::getProfileAvatars($target->id);
64 foreach ($avatars as $avatar) {
69 public static function getOriginal(Profile $target)
71 $avatar = new Avatar();
72 $avatar->profile_id = $target->id;
73 $avatar->original = true;
74 if (!$avatar->find(true)) {
75 throw new NoResultException($avatar);
80 public static function getProfileAvatars(Profile $target) {
81 $avatar = new Avatar();
82 $avatar->profile_id = $target->id;
83 return $avatar->fetchAll();
87 * Where should the avatar go for this user?
89 static function filename($id, $extension, $size=null, $extra=null)
92 return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
94 return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
98 static function path($filename)
100 $dir = common_config('avatar', 'dir');
102 if ($dir[strlen($dir)-1] != '/') {
106 return $dir . $filename;
109 static function url($filename)
111 $path = common_config('avatar', 'path');
113 if ($path[strlen($path)-1] != '/') {
117 if ($path[0] != '/') {
121 $server = common_config('avatar', 'server');
123 if (empty($server)) {
124 $server = common_config('site', 'server');
127 $ssl = common_config('avatar', 'ssl');
129 if (is_null($ssl)) { // null -> guess
130 if (common_config('site', 'ssl') == 'always' &&
131 !common_config('avatar', 'server')) {
138 $protocol = ($ssl) ? 'https' : 'http';
140 return $protocol.'://'.$server.$path.$filename;
143 function displayUrl()
145 $server = common_config('avatar', 'server');
146 if ($server && !empty($this->filename)) {
147 return Avatar::url($this->filename);
153 static function defaultImage($size)
155 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
156 AVATAR_STREAM_SIZE => 'stream',
157 AVATAR_MINI_SIZE => 'mini');
158 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
161 static function newSize(Profile $target, $size) {
162 $size = floor($size);
163 if ($size <1 || $size > 999) {
164 // TRANS: An error message when avatar size is unreasonable
165 throw new Exception(_m('Unreasonable avatar size'));
168 $original = Avatar::getOriginal($target);
170 $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
171 $filename = $imagefile->resize($size);
173 $scaled = clone($original);
174 $scaled->original = false;
175 $scaled->width = $size;
176 $scaled->height = $size;
177 $scaled->url = Avatar::url($filename);
178 $scaled->created = DB_DataObject_Cast::dateTime();
180 if (!$scaled->insert()) {
181 // TRANS: An error message when unable to insert avatar data into the db
182 throw new Exception(_m('Could not insert new avatar data to database'));
185 // Return the new avatar object