/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
-
- static function pivotGet($keyCol, $keyVals, $otherCols)
- {
- return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
- }
public static function schemaDef()
{
),
);
}
- // We clean up the file, too
+ // We clean up the file, too
function delete()
{
$filename = $this->filename;
}
}
+ /*
+ * Deletes all avatars (but may spare the original) from a profile.
+ *
+ * @param Profile $target The profile we're deleting avatars of.
+ * @param boolean $original Whether original should be removed or not.
+ */
+ public static function deleteFromProfile(Profile $target, $original=true) {
+ try {
+ $avatars = self::getProfileAvatars($target);
+ foreach ($avatars as $avatar) {
+ if ($avatar->original && !$original) {
+ continue;
+ }
+ $avatar->delete();
+ }
+ } catch (NoResultException $e) {
+ // There are no avatars to delete, a sort of success.
+ }
+
+ return true;
+ }
+
+ public static function getUploaded(Profile $target)
+ {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ $avatar->original = true;
+ if (!$avatar->find(true)) {
+ throw new NoResultException($avatar);
+ }
+ return $avatar;
+ }
+
+ public static function hasUploaded(Profile $profile) {
+ try {
+ $avatar = Avatar::getUploaded($profile);
+ } catch (NoResultException $e) {
+ return false;
+ }
+ return !file_exists(Avatar::path($avatar->filename));
+ }
+
+ public static function getProfileAvatars(Profile $target) {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ if (!$avatar->find()) {
+ throw new NoResultException($avatar);
+ }
+ return $avatar->fetchAll();
+ }
+
/**
* Where should the avatar go for this user?
*/
AVATAR_MINI_SIZE => 'mini');
return Theme::path('default-avatar-'.$sizenames[$size].'.png');
}
+
+ static function newSize(Profile $target, $size) {
+ $size = floor($size);
+ if ($size < 1 || $size > common_config('avatar', 'maxsize')) {
+ // TRANS: An error message when avatar size is unreasonable
+ throw new Exception(_m('Avatar size too large'));
+ }
+
+ $original = Avatar::getUploaded($target);
+
+ $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
+ $filename = $imagefile->resize($size);
+
+ $scaled = clone($original);
+ $scaled->original = false;
+ $scaled->width = $size;
+ $scaled->height = $size;
+ $scaled->url = Avatar::url($filename);
+ $scaled->filename = $filename;
+ $scaled->created = common_sql_now();
+
+ if (!$scaled->insert()) {
+ // TRANS: An error message when unable to insert avatar data into the db
+ throw new Exception(_m('Could not insert new avatar data to database'));
+ }
+
+ // Return the new avatar object
+ return $scaled;
+ }
}