]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Avatar.php
Do not name anything getOriginal (because DB_DataObject calls that)
[quix0rs-gnu-social.git] / classes / Avatar.php
index 8cff14273c8abc66999a94539cb579845de86bb4..d498df56d2351d9d020f39762a251e3de3f03776 100644 (file)
@@ -22,11 +22,6 @@ class Avatar extends Managed_DataObject
 
     /* 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()
     {
@@ -54,8 +49,8 @@ class Avatar extends Managed_DataObject
             ),
         );
     }
-    // We clean up the file, too
 
+    // We clean up the file, too
     function delete()
     {
         $filename = $this->filename;
@@ -64,6 +59,57 @@ class Avatar extends Managed_DataObject
         }
     }
 
+    /*
+     * 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?
      */
@@ -138,4 +184,33 @@ class Avatar extends Managed_DataObject
                                   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;
+    }
 }