]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
closes #1145 - deprecated current "copy constructor" for all entities and implemented...
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 12 Jan 2010 12:56:19 +0000 (12:56 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 12 Jan 2010 12:56:19 +0000 (12:56 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3802 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/entities.php
engine/lib/group.php
engine/lib/metadata.php
engine/lib/objects.php
engine/lib/sites.php
engine/lib/users.php
engine/tests/objects/objects.php
engine/tests/objects/users.php

index 1c833ab2930e08d3568f74b8823f9a9d3385ea25..7b5cd22a3e433640b638e59c37a45ea28cb034be 100644 (file)
@@ -123,6 +123,47 @@ abstract class ElggEntity implements
                $this->attributes['tables_loaded'] = 0;
        }
 
+       /**
+        * Clone an entity 
+        * 
+        * Resets the guid so that the entity can be saved as a distinct entity from 
+        * the original. Creation time will be set when this new entity is saved. 
+        * The owner and container guids come from the original entity. The clone 
+        * method copies metadata but does not copy over annotations, or private settings.
+        * 
+        * Note: metadata will have its owner and access id set when the entity is saved
+        * and it will be the same as that off the entity.
+        */
+       public function __clone() {
+               
+               $orig_entity = get_entity($this->guid);
+               if (!$orig_entity) {
+                       elgg_log("Failed to clone entity with GUID $this->guid", "ERROR");
+                       return;
+               }
+               
+               $metadata_array = get_metadata_for_entity($this->guid);
+
+               $this->attributes['guid'] = "";
+
+               // copy metadata over to new entity - slightly convoluted due to
+               // handling of metadata arrays
+               if (is_array($metadata_array)) {
+                       // create list of metadata names
+                       $metadata_names = array();
+                       foreach ($metadata_array as $metadata) {
+                               $metadata_names[] = $metadata['name'];
+                       }
+                       // arrays are stored with multiple enties per name
+                       $metadata_names = array_unique($metadata_names);
+                       
+                       // move the metadata over
+                       foreach ($metadata_names as $name) {
+                               $this->set($name, $orig_entity->$name);
+                       }
+               }
+       }
+       
        /**
         * Return the value of a given key.
         * If $name is a key field (as defined in $this->attributes) that value is returned, otherwise it will
@@ -265,10 +306,10 @@ abstract class ElggEntity implements
        /**
         * Set a piece of metadata.
         *
-        * @param string $name
-        * @param mixed $value
-        * @param string $value_type
-        * @param bool $multiple
+        * @param string $name Name of the metadata
+        * @param mixed $value Value of the metadata
+        * @param string $value_type Types supported: integer and string. Will auto-identify if not set
+        * @param bool $multiple 
         * @return bool
         */
        public function setMetaData($name, $value, $value_type = "", $multiple = false) {
index 589e664d13692e7d8caa1e4022c3508678b052ce..cbc34b71be0dc301b4d21d25443df05b5b345d56 100644 (file)
@@ -48,6 +48,8 @@ class ElggGroup extends ElggEntity
                        }
                        // Is $guid is an ElggGroup? Use a copy constructor
                        else if ($guid instanceof ElggGroup) {
+                               elgg_log('This type of usage of the ElggGroup constructor was deprecated in 1.7. Please use the clone method.', 'WARNING');
+                               
                                foreach ($guid->attributes as $key => $value) {
                                        $this->attributes[$key] = $value;
                                }
index dbe021414631cd7aa71dc5eb412608fa6e68cc20..955939e42f395da7ca99cbbc863832a29bbb5f2f 100644 (file)
@@ -183,9 +183,9 @@ function remove_metadata($entity_guid, $name, $value = "") {
 /**
  * Create a new metadata object, or update an existing one.
  *
- * @param int $entity_guid
- * @param string $name
- * @param string $value
+ * @param int $entity_guid The entity to attach the metadata to
+ * @param string $name Name of the metadata
+ * @param string $value Value of the metadata (cannot be associative array)
  * @param string $value_type
  * @param int $owner_guid
  * @param int $access_id
index e4c3128b937a27d19ff7027b3797bf9d12620c6c..f2faf0d10154a8601aa92e36b720f254c3406b30 100644 (file)
@@ -54,6 +54,8 @@ class ElggObject extends ElggEntity {
 
                        // Is $guid is an ElggObject? Use a copy constructor
                        else if ($guid instanceof ElggObject) {
+                               elgg_log('This type of usage of the ElggObject constructor was deprecated in 1.7. Please use the clone method.', 'WARNING');
+                               
                                foreach ($guid->attributes as $key => $value) {
                                        $this->attributes[$key] = $value;
                                }
index d38113d2741288c358fb4e69079bbc2e80e07e1a..0892f296353343c48b51b2a664ba9a5aa3c514b7 100644 (file)
@@ -54,6 +54,8 @@ class ElggSite extends ElggEntity {
 
                        // Is $guid is an ElggSite? Use a copy constructor
                        else if ($guid instanceof ElggSite) {
+                               elgg_log('This type of usage of the ElggSite constructor was deprecated in 1.7. Please use the clone method.', 'WARNING');
+                               
                                foreach ($guid->attributes as $key => $value) {
                                        $this->attributes[$key] = $value;
                                }
index 7ef9e2fda61c93b2f3e3c47638eb88b09c0adbf9..512b8a6e6a13086152f55cfad2fa2b14391e1cba 100644 (file)
@@ -75,6 +75,8 @@ class ElggUser extends ElggEntity
 
                        // Is $guid is an ElggUser? Use a copy constructor
                        else if ($guid instanceof ElggUser) {
+                               elgg_log('This type of usage of the ElggUser constructor was deprecated in 1.7. Please use the clone method.', 'WARNING');
+                               
                                foreach ($guid->attributes as $key => $value) {
                                        $this->attributes[$key] = $value;
                                }
index d30e4fae4d19e25de244753994fb42e35c390a35..8209917bf50eb2fd600814dab7a635f32897ee85 100644 (file)
@@ -103,30 +103,40 @@ class ElggCoreObjectTest extends ElggCoreUnitTest {
                // clean up
                $this->entity->delete();
        }
-       
-       public function testElggObjectConstructorByObject() {
-               $guid = $this->entity->save();
                
-               // stdClass: use guid
-               $object_row = $this->get_object_row($guid);
-               $entity_row = $this->get_entity_row($guid);
-               $this->assertIdentical($this->entity, new ElggObjectTest($object_row));
-               $this->assertIdentical($this->entity, new ElggObjectTest($entity_row));
+       public function testElggObjectClone() {
+               $this->entity->title = 'testing';
+               $this->entity->description = 'ElggObject';
+               $this->entity->var1 = "test";
+               $this->entity->var2 = 1;
+               $this->entity->var3 = true;
+               $this->entity->save();
                
-               // copy attributes of ElggObject
-               $this->assertIdentical($this->entity, new ElggObjectTest($this->entity));
+               // add tag array
+               $tag_string = 'tag1, tag2, tag3';
+               $tagarray = string_to_tag_array($tag_string);
+               $this->entity->tags = $tagarray;
                
-               // error on ElggEntity
-               $entity = new ElggEntityTest($guid);
-               try {
-                       $error = new ElggObjectTest($entity);
-                       $this->assertTrue(FALSE);
-               } catch (Exception $e) {
-                       $this->assertIsA($e, 'InvalidParameterException');
-                       $this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:NonElggObject'));
-               }
+               // a cloned ElggEntity has the guid reset
+               $object = clone $this->entity;
+               $this->assertIdentical(0, (int)$object->guid);
+               
+               // make sure attributes were copied over
+               $this->assertIdentical($object->title, 'testing');
+               $this->assertIdentical($object->description, 'ElggObject');
+               
+               $guid = $object->save();
+               $this->assertTrue($guid !== 0);
+               $this->assertTrue($guid !== $this->entity->guid);
+               
+               // test that metadata was transfered
+               $this->assertIdentical($this->entity->var1, $object->var1);
+               $this->assertIdentical($this->entity->var2, $object->var2);
+               $this->assertIdentical($this->entity->var3, $object->var3);
+               $this->assertIdentical($this->entity->tags, $object->tags);
                
                // clean up
+               $object->delete();
                $this->entity->delete();
        }
        
index c1403af1a1df2857504fa2512a6352484cbeed33..c03091a919225b9c53d26a7227e6a0b9a7c8d299 100644 (file)
@@ -119,29 +119,7 @@ class ElggCoreUserTest extends ElggCoreUnitTest {
                $user = new ElggUser($row->username);
                $this->assertIdentical($user, $_SESSION['user']);
        }
-       
-       public function testElggUserConstructorByObject() {
-               $obj = new ElggUser(get_loggedin_userid());
-               $user = new ElggUser($obj);
-               $this->assertIdentical($obj, $user);
-               $this->assertIdentical($user, $_SESSION['user']);
-               
-               // fail on non-user object
-               $object = new ElggObject();
-               $object->save();
-               
-               try {
-                       $error = new ElggUserTest($object);
-                       $this->assertTrue(FALSE);
-               } catch (Exception $e) {
-                       $this->assertIsA($e, 'InvalidParameterException');
-                       $message = sprintf(elgg_echo('InvalidParameterException:NonElggUser'));
-                       $this->assertIdentical($e->getMessage(), $message);
-               }
                
-               $object->delete();
-       }
-       
        public function testElggUserSave() {
                // new object
                $this->AssertEqual($this->user->getGUID(), 0);