/** duplicate_entity( $id, $dup_relationships = true, $maintain_dates = false, $overrides = array() ) {{{ * Duplicates entity with id = $id. * * Specifically, copies all fields of an entity to a new id. If dup_relationships * is true, also copies all relationships, replacing the old id with the new inserted one. * * @param $id ID of entity to duplicate * OR an entity object to duplicate * @param $dup_relationships Bool that determines whether to duplicate relationships or not * @param $maintain_dates Bool that determines whether to * @param $overrides array of field => value pairs to override any values for the new entity * @return the new, duplicated entity id */ function duplicate_entity( $id, $dup_relationships = true, $maintain_dates = false, $overrides = array() ) { // get all values and structure from existing object if( is_object( $id ) AND get_class( $id ) == 'entity' ) $e = $id; else $e = new entity( $id ); // get the site that owns this entity $site = $e->get_owner(); // get the tables used by this type/entity $tables = get_entity_tables_by_id( $e->id() ); //! start of new code (see commented note below) $ignored_fields = array( 'id', 'name', 'type', 'last_edited_by' ); if( !$maintain_dates ) { $ignored_fields[] = 'last_modified'; $ignored_fields[] = 'creation_date'; } // Don't ignore values set as overrides foreach ($ignored_fields as $key => $val) if (isset($overrides[$val])) unset ($ignored_fields[$key]); // convert values of entity to tabled-array structure, make sure to ignore proper fields $values = values_to_tables( $tables, array_merge( $e->get_values(), $overrides ), $ignored_fields ); // create new entity record $new_entity_id = create_entity( $site->id(), $e->get_value('type'), $e->get_value('last_edited_by'), $e->get_value('name'), $values ); // copy relationships if( $dup_relationships ) { // make new left relationships $left_rels = $e->get_left_relationships(); foreach( $left_rels AS $rel_type => $rel_obj ) { if( is_int( $rel_type ) ) { foreach( $rel_obj AS $r ) create_relationship( $new_entity_id, $r->id(), $rel_type ); } } // make new right relationships $right_rels = $e->get_right_relationships(); foreach( $right_rels AS $rel_type => $rel_obj ) { if( is_int( $rel_type ) ) { foreach( $rel_obj AS $r ) create_relationship( $r->id(), $new_entity_id, $rel_type ); } } } // return the new entity return $new_entity_id; } // }}}