+
+ return parent::delete($useWhere);
+ }
+
+ /*
+ * 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 (NoAvatarException $e) {
+ // There are no avatars to delete, a sort of success.
+ }
+
+ return true;
+ }
+
+ static protected $_avatars = array();
+
+ /*
+ * Get an avatar by profile. Currently can't call newSize with $height
+ */
+ public static function byProfile(Profile $target, $width=null, $height=null)
+ {
+ $width = intval($width);
+ $height = !is_null($height) ? intval($height) : null;
+ if (is_null($height)) {
+ $height = $width;
+ }
+
+ $size = "{$width}x{$height}";
+ if (!isset(self::$_avatars[$target->id])) {
+ self::$_avatars[$target->id] = array();
+ } elseif (isset(self::$_avatars[$target->id][$size])){
+ return self::$_avatars[$target->id][$size];
+ }
+
+ $avatar = null;
+ if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
+ $avatar = self::pkeyGet(
+ array(
+ 'profile_id' => $target->id,
+ 'width' => $width,
+ 'height' => $height,
+ )
+ );
+ Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
+ }
+
+ if (is_null($avatar)) {
+ // Obviously we can't find an avatar, so let's resize the original!
+ $avatar = Avatar::newSize($target, $width);
+ } elseif (!($avatar instanceof Avatar)) {
+ throw new NoAvatarException($target, $avatar);
+ }
+
+ self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
+ return $avatar;
+ }
+
+ public static function getUploaded(Profile $target)
+ {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ $avatar->original = true;
+ if (!$avatar->find(true)) {
+ throw new NoAvatarException($target, $avatar);
+ }
+ if (!file_exists(Avatar::path($avatar->filename))) {
+ // The delete call may be odd for, say, unmounted filesystems
+ // that cause a file to currently not exist, but actually it does...
+ $avatar->delete();
+ throw new NoAvatarException($target, $avatar);
+ }
+ return $avatar;
+ }
+
+ public static function getProfileAvatars(Profile $target) {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ if (!$avatar->find()) {
+ throw new NoAvatarException($target, $avatar);
+ }
+ return $avatar->fetchAll();